VirtualBox

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

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

two gcc warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 303.8 KB
Line 
1/* $Id: DevVGA-SVGA3d-ogl.cpp 55297 2015-04-16 07:50:18Z vboxsync $ */
2/** @file
3 * DevVMWare - VMWare SVGA device
4 */
5
6/*
7 * Copyright (C) 2013-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22/* Enable to disassemble defined shaders. (Windows host only) */
23#if defined(RT_OS_WINDOWS) && defined(DEBUG) && 0 /* Disabled as we don't have the DirectX SDK avaible atm. */
24# define DUMP_SHADER_DISASSEMBLY
25#endif
26#ifdef DEBUG_bird
27# define RTMEM_WRAP_TO_EF_APIS
28#endif
29#define LOG_GROUP LOG_GROUP_DEV_VMSVGA
30#include <VBox/vmm/pdmdev.h>
31#include <VBox/version.h>
32#include <VBox/err.h>
33#include <VBox/log.h>
34#include <VBox/vmm/pgm.h>
35
36#include <iprt/assert.h>
37#include <iprt/semaphore.h>
38#include <iprt/uuid.h>
39#include <iprt/mem.h>
40#include <iprt/avl.h>
41
42#include <VBox/VMMDev.h>
43#include <VBox/VBoxVideo.h>
44#include <VBox/bioslogo.h>
45
46/* should go BEFORE any other DevVGA include to make all DevVGA.h config defines be visible */
47#include "DevVGA.h"
48
49#include "DevVGA-SVGA.h"
50#include "DevVGA-SVGA3d.h"
51#include "vmsvga/svga_reg.h"
52#include "vmsvga/svga3d_reg.h"
53#include "vmsvga/svga3d_shaderdefs.h"
54
55#ifdef RT_OS_WINDOWS
56# include <GL/gl.h>
57# include "vmsvga_glext/wglext.h"
58
59#elif defined(RT_OS_DARWIN)
60# include <OpenGL/OpenGL.h>
61# include <OpenGL/gl3.h>
62# include <OpenGL/gl3ext.h>
63# define GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED
64# include <OpenGL/gl.h>
65# include <OpenGL/glext.h>
66# include "DevVGA-SVGA3d-cocoa.h"
67/* work around conflicting definition of GLhandleARB in VMware's glext.h */
68//#define GL_ARB_shader_objects
69// HACK
70typedef void (APIENTRYP PFNGLFOGCOORDPOINTERPROC) (GLenum type, GLsizei stride, const GLvoid *pointer);
71typedef void (APIENTRYP PFNGLCLIENTACTIVETEXTUREPROC) (GLenum texture);
72typedef void (APIENTRYP PFNGLGETPROGRAMIVARBPROC) (GLenum target, GLenum pname, GLint *params);
73# define GL_RGBA_S3TC 0x83A2
74# define GL_ALPHA8_EXT 0x803c
75# define GL_LUMINANCE8_EXT 0x8040
76# define GL_LUMINANCE16_EXT 0x8042
77# define GL_LUMINANCE4_ALPHA4_EXT 0x8043
78# define GL_LUMINANCE8_ALPHA8_EXT 0x8045
79# define GL_INT_2_10_10_10_REV 0x8D9F
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#ifdef DUMP_SHADER_DISASSEMBLY
89# include <d3dx9shader.h>
90#endif
91#include "vmsvga_glext/glext.h"
92
93#include "shaderlib/shaderlib.h"
94
95#include <stdlib.h>
96#include <math.h>
97#include <float.h>
98
99
100/*******************************************************************************
101* Defined Constants And Macros *
102*******************************************************************************/
103/** Experimental: Create a dedicated context for handling surfaces in, thus
104 * avoiding orphaned surfaces after context destruction.
105 *
106 * This cures, for instance, an assertion on fedora 21 that happens in
107 * vmsvga3dSurfaceStretchBlt if the login screen and the desktop has different
108 * sizes. The context of the login screen seems to have just been destroyed
109 * earlier and I believe the driver/X/whoever is attemting to strech the old
110 * screen content onto the new sized screen.
111 *
112 * @remarks This probably comes at a slight preformance expense, as we currently
113 * switches context when setting up the surface the first time. Not sure
114 * if we really need to, but as this is an experiment, I'm playing it safe.
115 */
116#define VMSVGA3D_OGL_WITH_SHARED_CTX
117#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
118/** Fake surface ID for the shared context. */
119# define VMSVGA3D_SHARED_CTX_ID UINT32_C(0xffffeeee)
120#endif
121
122/** @def VBOX_VMSVGA3D_GL_HACK_LEVEL
123 * Turns out that on Linux gl.h may often define the first 2-4 OpenGL versions
124 * worth of extensions, but missing out on a function pointer of fifteen. This
125 * causes headache for us when we use the function pointers below. This hack
126 * changes the code to call the known problematic functions directly.
127 * The value is ((x)<<16 | (y)) where x and y are taken from the GL_VERSION_x_y.
128 */
129#ifndef VBOX_VMSVGA3D_GL_HACK_LEVEL
130# define VBOX_VMSVGA3D_GL_HACK_LEVEL 0
131#endif
132
133#ifndef VBOX_VMSVGA3D_DEFAULT_OGL_PROFILE
134# define VBOX_VMSVGA3D_DEFAULT_OGL_PROFILE 1.0
135#endif
136
137#ifdef RT_OS_WINDOWS
138# define OGLGETPROCADDRESS wglGetProcAddress
139
140#elif defined(RT_OS_DARWIN)
141# include <dlfcn.h>
142# define OGLGETPROCADDRESS MyNSGLGetProcAddress
143/** Resolves an OpenGL symbol. */
144static void *MyNSGLGetProcAddress(const char *pszSymbol)
145{
146 /* Another copy in shaderapi.c. */
147 static void *s_pvImage = NULL;
148 if (s_pvImage == NULL)
149 s_pvImage = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
150 return s_pvImage ? dlsym(s_pvImage, pszSymbol) : NULL;
151}
152
153#else
154# define OGLGETPROCADDRESS(x) glXGetProcAddress((const GLubyte *)x)
155#endif
156
157/* Invert y-coordinate for OpenGL's bottom left origin. */
158#define D3D_TO_OGL_Y_COORD(ptrSurface, y_coordinate) (ptrSurface->pMipmapLevels[0].size.height - (y_coordinate))
159#define D3D_TO_OGL_Y_COORD_MIPLEVEL(ptrMipLevel, y_coordinate) (ptrMipLevel->size.height - (y_coordinate))
160
161#define OPENGL_INVALID_ID 0
162
163//#define MANUAL_FLIP_SURFACE_DATA
164/* Enable to render the result of DrawPrimitive in a seperate window. */
165//#define DEBUG_GFX_WINDOW
166
167
168/** @name VMSVGA3D_DEF_CTX_F_XXX - vmsvga3dContextDefineOgl flags.
169 * @{ */
170/** When clear, the context is created using the default OpenGL profile.
171 * When set, it's created using the alternative profile. The latter is only
172 * allowed if the VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE is set. */
173#define VMSVGA3D_DEF_CTX_F_OTHER_PROFILE RT_BIT_32(0)
174/** Defining the shared context. */
175#define VMSVGA3D_DEF_CTX_F_SHARED_CTX RT_BIT_32(1)
176/** Defining the init time context (EMT). */
177#define VMSVGA3D_DEF_CTX_F_INIT RT_BIT_32(2)
178/** @} */
179
180
181#define VMSVGA3D_CLEAR_CURRENT_CONTEXT(pState) \
182 do { (pState)->idActiveContext = OPENGL_INVALID_ID; } while (0)
183
184/** @def VMSVGA3D_SET_CURRENT_CONTEXT
185 * Makes sure the @a pContext is the active OpenGL context.
186 * @parm pState The VMSVGA3d state.
187 * @parm pContext The new context.
188 */
189#ifdef RT_OS_WINDOWS
190# define VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext) \
191 if ((pState)->idActiveContext != (pContext)->id) \
192 { \
193 BOOL fMakeCurrentRc = wglMakeCurrent((pContext)->hdc, (pContext)->hglrc); \
194 Assert(fMakeCurrentRc == TRUE); \
195 LogFlowFunc(("Changing context: %#x -> %#x\n", (pState)->idActiveContext, (pContext)->id)); \
196 (pState)->idActiveContext = (pContext)->id; \
197 } else do { } while (0)
198
199#elif defined(RT_OS_DARWIN)
200# define VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext) \
201 if ((pState)->idActiveContext != (pContext)->id) \
202 { \
203 vmsvga3dCocoaViewMakeCurrentContext((pContext)->cocoaView, (pContext)->cocoaContext); \
204 LogFlowFunc(("Changing context: %#x -> %#x\n", (pState)->idActiveContext, (pContext)->id)); \
205 (pState)->idActiveContext = (pContext)->id; \
206 } else do { } while (0)
207#else
208# define VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext) \
209 if ((pState)->idActiveContext != (pContext)->id) \
210 { \
211 Bool fMakeCurrentRc = glXMakeCurrent((pState)->display, \
212 (pContext)->window, \
213 (pContext)->glxContext); \
214 Assert(fMakeCurrentRc == True); \
215 LogFlowFunc(("Changing context: %#x -> %#x\n", (pState)->idActiveContext, (pContext)->id)); \
216 (pState)->idActiveContext = (pContext)->id; \
217 } else do { } while (0)
218#endif
219
220/** @def VMSVGA3D_CLEAR_GL_ERRORS
221 * Clears all pending OpenGL errors.
222 *
223 * If I understood this correctly, OpenGL maintains a bitmask internally and
224 * glGetError gets the next bit (clearing it) from the bitmap and translates it
225 * into a GL_XXX constant value which it then returns. A single OpenGL call can
226 * set more than one bit, and they stick around across calls, from what I
227 * understand.
228 *
229 * So in order to be able to use glGetError to check whether a function
230 * succeeded, we need to call glGetError until all error bits have been cleared.
231 * This macro does that (in all types of builds).
232 *
233 * @sa VMSVGA3D_GET_GL_ERROR, VMSVGA3D_GL_IS_SUCCESS
234 */
235#define VMSVGA3D_CLEAR_GL_ERRORS() \
236 do { \
237 if (RT_UNLIKELY(glGetError() != GL_NO_ERROR)) /* predict no errors pending */ \
238 { \
239 uint32_t iErrorClearingLoopsLeft = 64; \
240 while (glGetError() != GL_NO_ERROR && iErrorClearingLoopsLeft > 0) \
241 iErrorClearingLoopsLeft--; \
242 } \
243 } while (0)
244
245/** @def VMSVGA3D_GET_LAST_GL_ERROR
246 * Gets the last OpenGL error, stores it in a_pContext->lastError and returns
247 * it.
248 *
249 * @returns Same as glGetError.
250 * @param a_pContext The context to store the error in.
251 *
252 * @sa VMSVGA3D_GL_IS_SUCCESS, VMSVGA3D_GL_COMPLAIN
253 */
254#define VMSVGA3D_GET_GL_ERROR(a_pContext) ((a_pContext)->lastError = glGetError())
255
256/** @def VMSVGA3D_GL_SUCCESS
257 * Checks whether VMSVGA3D_GET_LAST_GL_ERROR() return GL_NO_ERROR.
258 *
259 * Will call glGetError() and store the result in a_pContext->lastError.
260 * Will predict GL_NO_ERROR outcome.
261 *
262 * @returns True on success, false on error.
263 * @parm a_pContext The context to store the error in.
264 *
265 * @sa VMSVGA3D_GET_GL_ERROR, VMSVGA3D_GL_COMPLAIN
266 */
267#define VMSVGA3D_GL_IS_SUCCESS(a_pContext) RT_LIKELY((((a_pContext)->lastError = glGetError()) == GL_NO_ERROR))
268
269/** @def VMSVGA3D_GL_COMPLAIN
270 * Complains about one or more OpenGL errors (first in a_pContext->lastError).
271 *
272 * Strict builds will trigger an assertion, while other builds will put the
273 * first few occurences in the release log.
274 *
275 * All GL errors will be cleared after invocation. Assumes lastError
276 * is an error, will not check for GL_NO_ERROR.
277 *
278 * @param a_pState The 3D state structure.
279 * @param a_pContext The context that holds the first error.
280 * @param a_LogRelDetails Argument list for LogRel or similar that describes
281 * the operation in greater detail.
282 *
283 * @sa VMSVGA3D_GET_GL_ERROR, VMSVGA3D_GL_IS_SUCCESS
284 */
285#ifdef VBOX_STRICT
286# define VMSVGA3D_GL_COMPLAIN(a_pState, a_pContext, a_LogRelDetails) \
287 do { \
288 AssertMsg((a_pState)->idActiveContext == (a_pContext)->id, \
289 ("idActiveContext=%#x id=%x\n", (a_pState)->idActiveContext, (a_pContext)->id)); \
290 RTAssertMsg2Weak a_LogRelDetails; \
291 GLenum iNextError; \
292 while ((iNextError = glGetError()) != GL_NO_ERROR) \
293 RTAssertMsg2Weak("next error: %#x\n", iNextError); \
294 AssertMsgFailed(("first error: %#x (idActiveContext=%#x)\n", (a_pContext)->lastError, (a_pContext)->id)); \
295 } while (0)
296#else
297# define VMSVGA3D_GL_COMPLAIN(a_pState, a_pContext, a_LogRelDetails) \
298 do { \
299 LogRelMax(32, ("VMSVGA3d: OpenGL error %#x (idActiveContext=%#x) on line %u ", (a_pContext)->lastError, (a_pContext)->id)); \
300 GLenum iNextError; \
301 while ((iNextError = glGetError()) != GL_NO_ERROR) \
302 LogRelMax(32, (" - also error %#x ", iNextError)); \
303 LogRelMax(32, a_LogRelDetails); \
304 } while (0)
305#endif
306
307/** @def VMSVGA3D_GL_GET_AND_COMPLAIN
308 * Combination of VMSVGA3D_GET_GL_ERROR and VMSVGA3D_GL_COMPLAIN, assuming that
309 * there is a pending error.
310 *
311 * @param a_pState The 3D state structure.
312 * @param a_pContext The context that holds the first error.
313 * @param a_LogRelDetails Argument list for LogRel or similar that describes
314 * the operation in greater detail.
315 *
316 * @sa VMSVGA3D_GET_GL_ERROR, VMSVGA3D_GL_IS_SUCCESS, VMSVGA3D_GL_COMPLAIN
317 */
318#define VMSVGA3D_GL_GET_AND_COMPLAIN(a_pState, a_pContext, a_LogRelDetails) \
319 do { \
320 VMSVGA3D_GET_GL_ERROR(a_pContext); \
321 VMSVGA3D_GL_COMPLAIN(a_pState, a_pContext, a_LogRelDetails); \
322 } while (0)
323
324/** @def VMSVGA3D_GL_ASSERT_SUCCESS
325 * Asserts that VMSVGA3D_GL_IS_SUCCESS is true, complains if not.
326 *
327 * Uses VMSVGA3D_GL_COMPLAIN for complaining, so check it out wrt to release
328 * logging in non-strict builds.
329 *
330 * @param a_pState The 3D state structure.
331 * @param a_pContext The context that holds the first error.
332 * @param a_LogRelDetails Argument list for LogRel or similar that describes
333 * the operation in greater detail.
334 *
335 * @sa VMSVGA3D_GET_GL_ERROR, VMSVGA3D_GL_IS_SUCCESS, VMSVGA3D_GL_COMPLAIN
336 */
337#define VMSVGA3D_GL_ASSERT_SUCCESS(a_pState, a_pContext, a_LogRelDetails) \
338 if (VMSVGA3D_GL_IS_SUCCESS(a_pContext)) \
339 { /* likely */ } \
340 else do { \
341 VMSVGA3D_GET_GL_ERROR(a_pContext); \
342 VMSVGA3D_GL_COMPLAIN(a_pState, a_pContext, a_LogRelDetails); \
343 } while (0)
344
345/** @def VMSVGA3D_ASSERT_GL_CALL_EX
346 * Executes the specified OpenGL API call and asserts that it succeeded, variant
347 * with extra logging flexibility.
348 *
349 * ASSUMES no GL errors pending prior to invocation - caller should use
350 * VMSVGA3D_CLEAR_GL_ERRORS if uncertain.
351 *
352 * Uses VMSVGA3D_GL_COMPLAIN for complaining, so check it out wrt to release
353 * logging in non-strict builds.
354 *
355 * @param a_GlCall Expression making an OpenGL call.
356 * @param a_pState The 3D state structure.
357 * @param a_pContext The context that holds the first error.
358 * @param a_LogRelDetails Argument list for LogRel or similar that describes
359 * the operation in greater detail.
360 *
361 * @sa VMSVGA3D_ASSERT_GL_CALL, VMSVGA3D_GL_ASSERT_SUCCESS,
362 * VMSVGA3D_GET_GL_ERROR, VMSVGA3D_GL_IS_SUCCESS, VMSVGA3D_GL_COMPLAIN
363 */
364#define VMSVGA3D_ASSERT_GL_CALL_EX(a_GlCall, a_pState, a_pContext, a_LogRelDetails) \
365 do { \
366 (a_GlCall); \
367 VMSVGA3D_GL_ASSERT_SUCCESS(a_pState, a_pContext, a_LogRelDetails); \
368 } while (0)
369
370/** @def VMSVGA3D_ASSERT_GL_CALL
371 * Executes the specified OpenGL API call and asserts that it succeeded.
372 *
373 * ASSUMES no GL errors pending prior to invocation - caller should use
374 * VMSVGA3D_CLEAR_GL_ERRORS if uncertain.
375 *
376 * Uses VMSVGA3D_GL_COMPLAIN for complaining, so check it out wrt to release
377 * logging in non-strict builds.
378 *
379 * @param a_GlCall Expression making an OpenGL call.
380 * @param a_pState The 3D state structure.
381 * @param a_pContext The context that holds the first error.
382 *
383 * @sa VMSVGA3D_ASSERT_GL_CALL_EX, VMSVGA3D_GL_ASSERT_SUCCESS,
384 * VMSVGA3D_GET_GL_ERROR, VMSVGA3D_GL_IS_SUCCESS, VMSVGA3D_GL_COMPLAIN
385 */
386#define VMSVGA3D_ASSERT_GL_CALL(a_GlCall, a_pState, a_pContext) \
387 VMSVGA3D_ASSERT_GL_CALL_EX(a_GlCall, a_pState, a_pContext, ("%s\n", #a_GlCall))
388
389
390/** @def VMSVGA3D_CHECK_LAST_ERROR
391 * Checks that the last OpenGL error code indicates success.
392 *
393 * Will assert and return VERR_INTERNAL_ERROR in strict builds, in other
394 * builds it will do nothing and is a NOOP.
395 *
396 * @parm pState The VMSVGA3d state.
397 * @parm pContext The context.
398 *
399 * @todo Replace with proper error handling, it's crazy to return
400 * VERR_INTERNAL_ERROR in strict builds and just barge on ahead in
401 * release builds.
402 */
403#ifdef VBOX_STRICT
404# define VMSVGA3D_CHECK_LAST_ERROR(pState, pContext) do { \
405 Assert((pState)->idActiveContext == (pContext)->id); \
406 (pContext)->lastError = glGetError(); \
407 AssertMsgReturn((pContext)->lastError == GL_NO_ERROR, \
408 ("%s (%d): last error 0x%x\n", __FUNCTION__, __LINE__, (pContext)->lastError), \
409 VERR_INTERNAL_ERROR); \
410 } while (0)
411#else
412# define VMSVGA3D_CHECK_LAST_ERROR(pState, pContext) do { } while (0)
413#endif
414
415/** @def VMSVGA3D_CHECK_LAST_ERROR_WARN
416 * Checks that the last OpenGL error code indicates success.
417 *
418 * Will assert in strict builds, otherwise it's a NOOP.
419 *
420 * @parm pState The VMSVGA3d state.
421 * @parm pContext The new context.
422 */
423#ifdef VBOX_STRICT
424# define VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext) do { \
425 Assert((pState)->idActiveContext == (pContext)->id); \
426 (pContext)->lastError = glGetError(); \
427 AssertMsg((pContext)->lastError == GL_NO_ERROR, ("%s (%d): last error 0x%x\n", __FUNCTION__, __LINE__, (pContext)->lastError)); \
428 } while (0)
429#else
430# define VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext) do { } while (0)
431#endif
432
433
434/**
435 * Macro for doing something and then checking for errors during initialization.
436 * Uses AssertLogRelMsg.
437 */
438#define VMSVGA3D_INIT_CHECKED(a_Expr) \
439 do \
440 { \
441 a_Expr; \
442 GLenum iGlError = glGetError(); \
443 AssertLogRelMsg(iGlError == GL_NO_ERROR, ("VMSVGA3d: %s -> %#x\n", #a_Expr, iGlError)); \
444 } while (0)
445
446/**
447 * Macro for doing something and then checking for errors during initialization,
448 * doing the same in the other context when enabled.
449 *
450 * This will try both profiles in dual profile builds. Caller must be in the
451 * default context.
452 *
453 * Uses AssertLogRelMsg to indicate trouble.
454 */
455#ifdef VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE
456# define VMSVGA3D_INIT_CHECKED_BOTH(a_pState, a_pContext, a_pOtherCtx, a_Expr) \
457 do \
458 { \
459 for (uint32_t i = 0; i < 64; i++) if (glGetError() == GL_NO_ERROR) break; Assert(glGetError() == GL_NO_ERROR); \
460 a_Expr; \
461 GLenum iGlError = glGetError(); \
462 if (iGlError != GL_NO_ERROR) \
463 { \
464 VMSVGA3D_SET_CURRENT_CONTEXT(a_pState, a_pOtherCtx); \
465 for (uint32_t i = 0; i < 64; i++) if (glGetError() == GL_NO_ERROR) break; Assert(glGetError() == GL_NO_ERROR); \
466 a_Expr; \
467 GLenum iGlError2 = glGetError(); \
468 AssertLogRelMsg(iGlError2 == GL_NO_ERROR, ("VMSVGA3d: %s -> %#x / %#x\n", #a_Expr, iGlError, iGlError2)); \
469 VMSVGA3D_SET_CURRENT_CONTEXT(a_pState, a_pContext); \
470 } \
471 } while (0)
472#else
473# define VMSVGA3D_INIT_CHECKED_BOTH(a_pState, a_pContext, a_pOtherCtx, a_Expr) VMSVGA3D_INIT_CHECKED(a_Expr)
474#endif
475
476
477/*******************************************************************************
478* Structures, Typedefs and Globals. *
479*******************************************************************************/
480typedef struct
481{
482 SVGA3dSize size;
483 uint32_t cbSurface;
484 uint32_t cbSurfacePitch;
485 void *pSurfaceData;
486 bool fDirty;
487} VMSVGA3DMIPMAPLEVEL, *PVMSVGA3DMIPMAPLEVEL;
488
489/**
490 * SSM descriptor table for the VMSVGA3DMIPMAPLEVEL structure.
491 */
492static SSMFIELD const g_aVMSVGA3DMIPMAPLEVELFields[] =
493{
494 SSMFIELD_ENTRY( VMSVGA3DMIPMAPLEVEL, size),
495 SSMFIELD_ENTRY( VMSVGA3DMIPMAPLEVEL, cbSurface),
496 SSMFIELD_ENTRY( VMSVGA3DMIPMAPLEVEL, cbSurfacePitch),
497 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DMIPMAPLEVEL, pSurfaceData),
498 SSMFIELD_ENTRY_IGNORE( VMSVGA3DMIPMAPLEVEL, fDirty),
499 SSMFIELD_ENTRY_TERM()
500};
501
502typedef struct
503{
504 uint32_t id;
505#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
506 uint32_t idAssociatedContextUnused;
507#else
508 uint32_t idAssociatedContext;
509#endif
510 uint32_t flags;
511 SVGA3dSurfaceFormat format;
512 GLint internalFormatGL;
513 GLint formatGL;
514 GLint typeGL;
515 union
516 {
517 GLuint texture;
518 GLuint buffer;
519 GLuint renderbuffer;
520 } oglId;
521 SVGA3dSurfaceFace faces[SVGA3D_MAX_SURFACE_FACES];
522 uint32_t cFaces;
523 PVMSVGA3DMIPMAPLEVEL pMipmapLevels;
524 uint32_t multiSampleCount;
525 SVGA3dTextureFilter autogenFilter;
526 uint32_t cbBlock; /* block/pixel size in bytes */
527 /* Dirty state; surface was manually updated. */
528 bool fDirty;
529} VMSVGA3DSURFACE, *PVMSVGA3DSURFACE;
530
531/**
532 * SSM descriptor table for the VMSVGA3DSURFACE structure.
533 */
534static SSMFIELD const g_aVMSVGA3DSURFACEFields[] =
535{
536 SSMFIELD_ENTRY( VMSVGA3DSURFACE, id),
537#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
538 SSMFIELD_ENTRY( VMSVGA3DSURFACE, idAssociatedContextUnused),
539#else
540 SSMFIELD_ENTRY( VMSVGA3DSURFACE, idAssociatedContext),
541#endif
542 SSMFIELD_ENTRY( VMSVGA3DSURFACE, flags),
543 SSMFIELD_ENTRY( VMSVGA3DSURFACE, format),
544 SSMFIELD_ENTRY( VMSVGA3DSURFACE, internalFormatGL),
545 SSMFIELD_ENTRY( VMSVGA3DSURFACE, formatGL),
546 SSMFIELD_ENTRY( VMSVGA3DSURFACE, typeGL),
547 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSURFACE, id),
548 SSMFIELD_ENTRY( VMSVGA3DSURFACE, faces),
549 SSMFIELD_ENTRY( VMSVGA3DSURFACE, cFaces),
550 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSURFACE, pMipmapLevels),
551 SSMFIELD_ENTRY( VMSVGA3DSURFACE, multiSampleCount),
552 SSMFIELD_ENTRY( VMSVGA3DSURFACE, autogenFilter),
553 SSMFIELD_ENTRY( VMSVGA3DSURFACE, cbBlock),
554 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSURFACE, fDirty),
555 SSMFIELD_ENTRY_TERM()
556};
557
558typedef struct
559{
560 uint32_t id;
561 uint32_t cid;
562 SVGA3dShaderType type;
563 uint32_t cbData;
564 void *pShaderProgram;
565 union
566 {
567 void *pVertexShader;
568 void *pPixelShader;
569 } u;
570} VMSVGA3DSHADER, *PVMSVGA3DSHADER;
571
572/**
573 * SSM descriptor table for the VMSVGA3DSHADER structure.
574 */
575static SSMFIELD const g_aVMSVGA3DSHADERFields[] =
576{
577 SSMFIELD_ENTRY( VMSVGA3DSHADER, id),
578 SSMFIELD_ENTRY( VMSVGA3DSHADER, cid),
579 SSMFIELD_ENTRY( VMSVGA3DSHADER, type),
580 SSMFIELD_ENTRY( VMSVGA3DSHADER, cbData),
581 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSHADER, pShaderProgram),
582 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSHADER, u.pVertexShader),
583 SSMFIELD_ENTRY_TERM()
584};
585
586typedef struct
587{
588 bool fValid;
589 float matrix[16];
590} VMSVGATRANSFORMSTATE, *PVMSVGATRANSFORMSTATE;
591
592typedef struct
593{
594 bool fValid;
595 SVGA3dMaterial material;
596} VMSVGAMATERIALSTATE, *PVMSVGAMATERIALSTATE;
597
598typedef struct
599{
600 bool fValid;
601 float plane[4];
602} VMSVGACLIPPLANESTATE, *PVMSVGACLIPPLANESTATE;
603
604typedef struct
605{
606 bool fEnabled;
607 bool fValidData;
608 SVGA3dLightData data;
609} VMSVGALIGHTSTATE, *PVMSVGALIGHTSTATE;
610
611typedef struct
612{
613 bool fValid;
614 SVGA3dShaderConstType ctype;
615 uint32_t value[4];
616} VMSVGASHADERCONST, *PVMSVGASHADERCONST;
617
618/**
619 * SSM descriptor table for the VMSVGASHADERCONST structure.
620 */
621static SSMFIELD const g_aVMSVGASHADERCONSTFields[] =
622{
623 SSMFIELD_ENTRY( VMSVGASHADERCONST, fValid),
624 SSMFIELD_ENTRY( VMSVGASHADERCONST, ctype),
625 SSMFIELD_ENTRY( VMSVGASHADERCONST, value),
626 SSMFIELD_ENTRY_TERM()
627};
628
629#define VMSVGA3D_UPDATE_SCISSORRECT RT_BIT(0)
630#define VMSVGA3D_UPDATE_ZRANGE RT_BIT(1)
631#define VMSVGA3D_UPDATE_VIEWPORT RT_BIT(2)
632#define VMSVGA3D_UPDATE_VERTEXSHADER RT_BIT(3)
633#define VMSVGA3D_UPDATE_PIXELSHADER RT_BIT(4)
634#define VMSVGA3D_UPDATE_TRANSFORM RT_BIT(5)
635#define VMSVGA3D_UPDATE_MATERIAL RT_BIT(6)
636
637typedef struct
638{
639 uint32_t id;
640#ifdef RT_OS_WINDOWS
641 /* Device context of the context window. */
642 HDC hdc;
643 /* OpenGL rendering context handle. */
644 HGLRC hglrc;
645 /* Device context window handle. */
646 HWND hwnd;
647#elif defined(RT_OS_DARWIN)
648 /* OpenGL rendering context */
649 NativeNSOpenGLContextRef cocoaContext;
650 NativeNSViewRef cocoaView;
651 bool fOtherProfile;
652#else
653 /** XGL rendering context handle */
654 GLXContext glxContext;
655 /** Device context window handle */
656 Window window;
657 /** flag whether the window is mapped (=visible) */
658 bool fMapped;
659#endif
660 /* Framebuffer object associated with this context. */
661 GLuint idFramebuffer;
662 /* Read and draw framebuffer objects for various operations. */
663 GLuint idReadFramebuffer;
664 GLuint idDrawFramebuffer;
665 /* Last GL error recorded. */
666 GLenum lastError;
667
668 /* Current active render target (if any) */
669 uint32_t sidRenderTarget;
670 /* Current selected texture surfaces (if any) */
671 uint32_t aSidActiveTexture[SVGA3D_MAX_TEXTURE_STAGE];
672 /* Per context pixel and vertex shaders. */
673 uint32_t cPixelShaders;
674 PVMSVGA3DSHADER paPixelShader;
675 uint32_t cVertexShaders;
676 PVMSVGA3DSHADER paVertexShader;
677 void *pShaderContext;
678 /* Keep track of the internal state to be able to recreate the context properly (save/restore, window resize). */
679 struct
680 {
681 uint32_t u32UpdateFlags;
682
683 SVGA3dRenderState aRenderState[SVGA3D_RS_MAX];
684 SVGA3dTextureState aTextureState[SVGA3D_MAX_TEXTURE_STAGE][SVGA3D_TS_MAX];
685 VMSVGATRANSFORMSTATE aTransformState[SVGA3D_TRANSFORM_MAX];
686 VMSVGAMATERIALSTATE aMaterial[SVGA3D_FACE_MAX];
687 VMSVGACLIPPLANESTATE aClipPlane[SVGA3D_CLIPPLANE_MAX];
688 VMSVGALIGHTSTATE aLightData[SVGA3D_MAX_LIGHTS];
689
690 uint32_t aRenderTargets[SVGA3D_RT_MAX];
691 SVGA3dRect RectScissor;
692 SVGA3dRect RectViewPort;
693 SVGA3dZRange zRange;
694 uint32_t shidPixel;
695 uint32_t shidVertex;
696
697 uint32_t cPixelShaderConst;
698 PVMSVGASHADERCONST paPixelShaderConst;
699 uint32_t cVertexShaderConst;
700 PVMSVGASHADERCONST paVertexShaderConst;
701 } state;
702} VMSVGA3DCONTEXT, *PVMSVGA3DCONTEXT;
703
704/**
705 * SSM descriptor table for the VMSVGA3DCONTEXT structure.
706 */
707static SSMFIELD const g_aVMSVGA3DCONTEXTFields[] =
708{
709 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, id),
710#ifdef RT_OS_WINDOWS
711 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, hdc),
712 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, hglrc),
713 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, hwnd),
714#endif
715
716 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, idFramebuffer),
717 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, idReadFramebuffer),
718 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, idDrawFramebuffer),
719 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, lastError),
720
721 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, sidRenderTarget),
722 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, aSidActiveTexture),
723 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, cPixelShaders),
724 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DCONTEXT, paPixelShader),
725 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, cVertexShaders),
726 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DCONTEXT, paVertexShader),
727 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DCONTEXT, pShaderContext),
728 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.u32UpdateFlags),
729
730 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.aRenderState),
731 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.aTextureState),
732 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.aTransformState),
733 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.aMaterial),
734 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.aClipPlane),
735 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.aLightData),
736
737 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.aRenderTargets),
738 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.RectScissor),
739 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.RectViewPort),
740 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.zRange),
741 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.shidPixel),
742 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.shidVertex),
743 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.cPixelShaderConst),
744 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DCONTEXT, state.paPixelShaderConst),
745 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.cVertexShaderConst),
746 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DCONTEXT, state.paVertexShaderConst),
747 SSMFIELD_ENTRY_TERM()
748};
749
750/**
751 * VMSVGA3d state data.
752 *
753 * Allocated on the heap and pointed to by VMSVGAState::p3dState.
754 */
755typedef struct VMSVGA3DSTATE
756{
757#ifdef RT_OS_WINDOWS
758 /** Window Thread. */
759 R3PTRTYPE(RTTHREAD) pWindowThread;
760 DWORD idWindowThread;
761 HMODULE hInstance;
762 /** Window request semaphore. */
763 RTSEMEVENT WndRequestSem;
764#elif defined(RT_OS_LINUX)
765 /* The X display */
766 Display *display;
767 R3PTRTYPE(RTTHREAD) pWindowThread;
768 bool bTerminate;
769#endif
770
771 float fGLVersion;
772 /* Current active context. */
773 uint32_t idActiveContext;
774
775 struct
776 {
777 PFNGLISRENDERBUFFERPROC glIsRenderbuffer;
778 PFNGLBINDRENDERBUFFERPROC glBindRenderbuffer;
779 PFNGLDELETERENDERBUFFERSPROC glDeleteRenderbuffers;
780 PFNGLGENRENDERBUFFERSPROC glGenRenderbuffers;
781 PFNGLRENDERBUFFERSTORAGEPROC glRenderbufferStorage;
782 PFNGLGETRENDERBUFFERPARAMETERIVPROC glGetRenderbufferParameteriv;
783 PFNGLISFRAMEBUFFERPROC glIsFramebuffer;
784 PFNGLBINDFRAMEBUFFERPROC glBindFramebuffer;
785 PFNGLDELETEFRAMEBUFFERSPROC glDeleteFramebuffers;
786 PFNGLGENFRAMEBUFFERSPROC glGenFramebuffers;
787 PFNGLCHECKFRAMEBUFFERSTATUSPROC glCheckFramebufferStatus;
788 PFNGLFRAMEBUFFERTEXTURE1DPROC glFramebufferTexture1D;
789 PFNGLFRAMEBUFFERTEXTURE2DPROC glFramebufferTexture2D;
790 PFNGLFRAMEBUFFERTEXTURE3DPROC glFramebufferTexture3D;
791 PFNGLFRAMEBUFFERRENDERBUFFERPROC glFramebufferRenderbuffer;
792 PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC glGetFramebufferAttachmentParameteriv;
793 PFNGLGENERATEMIPMAPPROC glGenerateMipmap;
794 PFNGLBLITFRAMEBUFFERPROC glBlitFramebuffer;
795 PFNGLRENDERBUFFERSTORAGEMULTISAMPLEPROC glRenderbufferStorageMultisample;
796 PFNGLFRAMEBUFFERTEXTURELAYERPROC glFramebufferTextureLayer;
797 PFNGLPOINTPARAMETERFPROC glPointParameterf;
798#if VBOX_VMSVGA3D_GL_HACK_LEVEL < 0x102
799 PFNGLBLENDCOLORPROC glBlendColor;
800 PFNGLBLENDEQUATIONPROC glBlendEquation;
801#endif
802 PFNGLBLENDEQUATIONSEPARATEPROC glBlendEquationSeparate;
803 PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate;
804 PFNGLSTENCILOPSEPARATEPROC glStencilOpSeparate;
805 PFNGLSTENCILFUNCSEPARATEPROC glStencilFuncSeparate;
806 PFNGLBINDBUFFERPROC glBindBuffer;
807 PFNGLDELETEBUFFERSPROC glDeleteBuffers;
808 PFNGLGENBUFFERSPROC glGenBuffers;
809 PFNGLBUFFERDATAPROC glBufferData;
810 PFNGLMAPBUFFERPROC glMapBuffer;
811 PFNGLUNMAPBUFFERPROC glUnmapBuffer;
812 PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray;
813 PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray;
814 PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer;
815 PFNGLFOGCOORDPOINTERPROC glFogCoordPointer;
816 PFNGLDRAWELEMENTSINSTANCEDBASEVERTEXPROC glDrawElementsInstancedBaseVertex;
817 PFNGLDRAWELEMENTSBASEVERTEXPROC glDrawElementsBaseVertex;
818 PFNGLACTIVETEXTUREPROC glActiveTexture;
819#if VBOX_VMSVGA3D_GL_HACK_LEVEL < 0x103
820 PFNGLCLIENTACTIVETEXTUREPROC glClientActiveTexture;
821#endif
822 PFNGLGETPROGRAMIVARBPROC glGetProgramivARB;
823 PFNGLPROVOKINGVERTEXPROC glProvokingVertex;
824 bool fEXT_stencil_two_side;
825 } ext;
826
827 struct
828 {
829 GLint maxActiveLights;
830 GLint maxTextureBufferSize;
831 GLint maxTextures;
832 GLint maxClipDistances;
833 GLint maxColorAttachments;
834 GLint maxRectangleTextureSize;
835 GLint maxTextureAnisotropy;
836 GLint maxVertexShaderInstructions;
837 GLint maxFragmentShaderInstructions;
838 GLint maxVertexShaderTemps;
839 GLint maxFragmentShaderTemps;
840 GLfloat flPointSize[2];
841 SVGA3dPixelShaderVersion fragmentShaderVersion;
842 SVGA3dVertexShaderVersion vertexShaderVersion;
843 bool fS3TCSupported;
844 } caps;
845
846 uint32_t cContexts;
847 PVMSVGA3DCONTEXT *papContexts;
848 uint32_t cSurfaces;
849 PVMSVGA3DSURFACE *papSurfaces;
850#ifdef DEBUG_GFX_WINDOW_TEST_CONTEXT
851 uint32_t idTestContext;
852#endif
853 /** The GL_EXTENSIONS value (space padded) for the default OpenGL profile.
854 * Free with RTStrFree. */
855 R3PTRTYPE(char *) pszExtensions;
856
857 /** The GL_EXTENSIONS value (space padded) for the other OpenGL profile.
858 * Free with RTStrFree.
859 *
860 * This is used to detect shader model version since some implementations
861 * (darwin) hides extensions that have made it into core and probably a
862 * bunch of others when using a OpenGL core profile instead of a legacy one */
863 R3PTRTYPE(char *) pszOtherExtensions;
864 /** The version of the other GL profile. */
865 float fOtherGLVersion;
866
867 /** Shader talk back interface. */
868 VBOXVMSVGASHADERIF ShaderIf;
869
870#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
871 /** The shared context. */
872 VMSVGA3DCONTEXT SharedCtx;
873#endif
874} VMSVGA3DSTATE;
875/** Pointer to the VMSVGA3d state. */
876typedef VMSVGA3DSTATE *PVMSVGA3DSTATE;
877
878/**
879 * SSM descriptor table for the VMSVGA3DSTATE structure.
880 */
881static SSMFIELD const g_aVMSVGA3DSTATEFields[] =
882{
883#ifdef RT_OS_WINDOWS
884 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSTATE, pWindowThread),
885 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSTATE, idWindowThread),
886 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSTATE, hInstance),
887 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSTATE, WndRequestSem),
888#elif defined(RT_OS_LINUX)
889 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSTATE, display),
890 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSTATE, pWindowThread),
891 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSTATE, bTerminate),
892#endif
893 SSMFIELD_ENTRY( VMSVGA3DSTATE, fGLVersion),
894 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSTATE, idActiveContext),
895
896 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSTATE, ext),
897 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSTATE, caps),
898
899 SSMFIELD_ENTRY( VMSVGA3DSTATE, cContexts),
900 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSTATE, papContexts),
901 SSMFIELD_ENTRY( VMSVGA3DSTATE, cSurfaces),
902 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSTATE, papSurfaces),
903 SSMFIELD_ENTRY_TERM()
904};
905
906
907/** Save and setup everything. */
908#define VMSVGA3D_PARANOID_TEXTURE_PACKING
909
910/**
911 * Saved texture packing parameters (shared by both pack and unpack).
912 */
913typedef struct VMSVGAPACKPARAMS
914{
915 GLint iAlignment;
916 GLint cxRow;
917#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
918 GLint cyImage;
919 GLboolean fSwapBytes;
920 GLboolean fLsbFirst;
921 GLint cSkipRows;
922 GLint cSkipPixels;
923 GLint cSkipImages;
924#endif
925} VMSVGAPACKPARAMS;
926/** Pointer to saved texture packing parameters. */
927typedef VMSVGAPACKPARAMS *PVMSVGAPACKPARAMS;
928/** Pointer to const saved texture packing parameters. */
929typedef VMSVGAPACKPARAMS const *PCVMSVGAPACKPARAMS;
930
931
932/*******************************************************************************
933* Global Variables *
934*******************************************************************************/
935/* Define the default light parameters as specified by MSDN. */
936/* @todo move out; fetched from Wine */
937const SVGA3dLightData vmsvga3d_default_light =
938{
939 SVGA3D_LIGHTTYPE_DIRECTIONAL, /* type */
940 false, /* inWorldSpace */
941 { 1.0f, 1.0f, 1.0f, 0.0f }, /* diffuse r,g,b,a */
942 { 0.0f, 0.0f, 0.0f, 0.0f }, /* specular r,g,b,a */
943 { 0.0f, 0.0f, 0.0f, 0.0f }, /* ambient r,g,b,a, */
944 { 0.0f, 0.0f, 0.0f }, /* position x,y,z */
945 { 0.0f, 0.0f, 1.0f }, /* direction x,y,z */
946 0.0f, /* range */
947 0.0f, /* falloff */
948 0.0f, 0.0f, 0.0f, /* attenuation 0,1,2 */
949 0.0f, /* theta */
950 0.0f /* phi */
951};
952
953
954/*******************************************************************************
955* Internal Functions *
956*******************************************************************************/
957static int vmsvga3dCreateTexture(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t idAssociatedContext, PVMSVGA3DSURFACE pSurface);
958static int vmsvga3dContextDefineOgl(PVGASTATE pThis, uint32_t cid, uint32_t fFlags);
959static void vmsvgaColor2GLFloatArray(uint32_t color, GLfloat *pRed, GLfloat *pGreen, GLfloat *pBlue, GLfloat *pAlpha);
960static void vmsvga3dSetPackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
961 PVMSVGAPACKPARAMS pSave);
962static void vmsvga3dRestorePackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
963 PCVMSVGAPACKPARAMS pSave);
964
965/* Generated by VBoxDef2LazyLoad from the VBoxSVGA3D.def and VBoxSVGA3DObjC.def files. */
966extern "C" int ExplicitlyLoadVBoxSVGA3D(bool fResolveAllImports, PRTERRINFO pErrInfo);
967#ifdef RT_OS_DARWIN
968extern "C" int ExplicitlyLoadVBoxSVGA3DObjC(bool fResolveAllImports, PRTERRINFO pErrInfo);
969#endif
970
971
972/**
973 * Checks if the given OpenGL extension is supported.
974 *
975 * @returns true if supported, false if not.
976 * @param pState The VMSVGA3d state.
977 * @param fActualGLVersion The actual OpenGL version we're working against.
978 * @param fMinGLVersion The OpenGL version that introduced this feature
979 * into the core.
980 * @param pszWantedExtension The name of the OpenGL extension we want padded
981 * with one space at each end.
982 * @remarks Init time only.
983 */
984static bool vmsvga3dCheckGLExtension(PVMSVGA3DSTATE pState, float fMinGLVersion, const char *pszWantedExtension)
985{
986 /* check padding. */
987 Assert(pszWantedExtension[0] == ' ');
988 Assert(pszWantedExtension[1] != ' ');
989 Assert(strchr(&pszWantedExtension[1], ' ') + 1 == strchr(pszWantedExtension, '\0'));
990
991 /* Look it up. */
992 bool fRet = false;
993 if (strstr(pState->pszExtensions, pszWantedExtension))
994 fRet = true;
995
996 /* Temporarily. Later start if (fMinGLVersion != 0.0 && fActualGLVersion >= fMinGLVersion) return true; */
997#ifdef RT_OS_DARWIN
998 AssertMsg( fMinGLVersion == 0.0
999 || fRet == (pState->fGLVersion >= fMinGLVersion)
1000 || VBOX_VMSVGA3D_DEFAULT_OGL_PROFILE == 2.1,
1001 ("%s actual:%d min:%d fRet=%d\n",
1002 pszWantedExtension, (int)(pState->fGLVersion * 10), (int)(fMinGLVersion * 10), fRet));
1003#else
1004 AssertMsg(fMinGLVersion == 0.0 || fRet == (pState->fGLVersion >= fMinGLVersion),
1005 ("%s actual:%d min:%d fRet=%d\n",
1006 pszWantedExtension, (int)(pState->fGLVersion * 10), (int)(fMinGLVersion * 10), fRet));
1007#endif
1008 return fRet;
1009}
1010
1011
1012/**
1013 * Outputs GL_EXTENSIONS list to the release log.
1014 */
1015static void vmsvga3dLogRelExtensions(const char *pszPrefix, const char *pszExtensions)
1016{
1017 /* OpenGL 3.0 interface (glGetString(GL_EXTENSIONS) return NULL). */
1018 bool fBuffered = RTLogRelSetBuffering(true);
1019
1020 /*
1021 * Determin the column widths first.
1022 */
1023 size_t acchWidths[4] = { 1, 1, 1, 1 };
1024 uint32_t i;
1025 const char *psz = pszExtensions;
1026 for (i = 0; ; i++)
1027 {
1028 while (*psz == ' ')
1029 psz++;
1030 if (!*psz)
1031 break;
1032
1033 const char *pszEnd = strchr(psz, ' ');
1034 AssertBreak(pszEnd);
1035 size_t cch = pszEnd - psz;
1036
1037 uint32_t iColumn = i % RT_ELEMENTS(acchWidths);
1038 if (acchWidths[iColumn] < cch)
1039 acchWidths[iColumn] = cch;
1040
1041 psz = pszEnd;
1042 }
1043
1044 /*
1045 * Output it.
1046 */
1047 LogRel(("VMSVGA3d: %sOpenGL extensions (%d):", pszPrefix, i));
1048 psz = pszExtensions;
1049 for (i = 0; ; i++)
1050 {
1051 while (*psz == ' ')
1052 psz++;
1053 if (!*psz)
1054 break;
1055
1056 const char *pszEnd = strchr(psz, ' ');
1057 AssertBreak(pszEnd);
1058 size_t cch = pszEnd - psz;
1059
1060 uint32_t iColumn = i % RT_ELEMENTS(acchWidths);
1061 if (iColumn == 0)
1062 LogRel(("\nVMSVGA3d: %-*.*s", acchWidths[iColumn], cch, psz));
1063 else if (iColumn != RT_ELEMENTS(acchWidths) - 1)
1064 LogRel((" %-*.*s", acchWidths[iColumn], cch, psz));
1065 else
1066 LogRel((" %.*s", cch, psz));
1067
1068 psz = pszEnd;
1069 }
1070
1071 RTLogRelSetBuffering(fBuffered);
1072 LogRel(("\n"));
1073}
1074
1075/**
1076 * Gathers the GL_EXTENSIONS list, storing it as a space padded list at
1077 * @a ppszExtensions.
1078 *
1079 * @returns VINF_SUCCESS or VERR_NO_STR_MEMORY
1080 * @param ppszExtensions Pointer to the string pointer. Free with RTStrFree.
1081 * @param fGLProfileVersion The OpenGL profile version.
1082 */
1083static int vmsvga3dGatherExtensions(char **ppszExtensions, float fGLProfileVersion)
1084{
1085 int rc;
1086 *ppszExtensions = NULL;
1087
1088 /*
1089 * Try the old glGetString interface first.
1090 */
1091 const char *pszExtensions = (const char *)glGetString(GL_EXTENSIONS);
1092 if (pszExtensions)
1093 {
1094 rc = RTStrAAppendExN(ppszExtensions, 3, " ", (size_t)1, pszExtensions, RTSTR_MAX, " ", (size_t)1);
1095 AssertLogRelRCReturn(rc, rc);
1096 }
1097 else
1098 {
1099 /*
1100 * The new interface where each extension string is retrieved separately.
1101 * Note! Cannot use VMSVGA3D_INIT_CHECKED_GL_GET_INTEGER_VALUE here because
1102 * the above GL_EXTENSIONS error lingers on darwin. sucks.
1103 */
1104#ifndef GL_NUM_EXTENSIONS
1105# define GL_NUM_EXTENSIONS 0x821D
1106#endif
1107 GLint cExtensions = 1024;
1108 glGetIntegerv(GL_NUM_EXTENSIONS, &cExtensions);
1109 Assert(cExtensions != 1024);
1110
1111 PFNGLGETSTRINGIPROC pfnGlGetStringi = (PFNGLGETSTRINGIPROC)OGLGETPROCADDRESS("glGetStringi");
1112 AssertLogRelReturn(pfnGlGetStringi, VERR_NOT_SUPPORTED);
1113
1114 rc = RTStrAAppend(ppszExtensions, " ");
1115 for (GLint i = 0; RT_SUCCESS(rc) && i < cExtensions; i++)
1116 {
1117 const char *pszExt = (const char *)pfnGlGetStringi(GL_EXTENSIONS, i);
1118 if (pszExt)
1119 rc = RTStrAAppendExN(ppszExtensions, 2, pfnGlGetStringi(GL_EXTENSIONS, i), RTSTR_MAX, " ", (size_t)1);
1120 }
1121 AssertRCReturn(rc, rc);
1122 }
1123
1124#if 1
1125 /*
1126 * Add extensions promoted into the core OpenGL profile.
1127 */
1128 static const struct
1129 {
1130 float fGLVersion;
1131 const char *pszzExtensions;
1132 } s_aPromotedExtensions[] =
1133 {
1134 {
1135 1.1f,
1136 " GL_EXT_vertex_array \0"
1137 " GL_EXT_polygon_offset \0"
1138 " GL_EXT_blend_logic_op \0"
1139 " GL_EXT_texture \0"
1140 " GL_EXT_copy_texture \0"
1141 " GL_EXT_subtexture \0"
1142 " GL_EXT_texture_object \0"
1143 " GL_ARB_framebuffer_object \0"
1144 " GL_ARB_map_buffer_range \0"
1145 " GL_ARB_vertex_array_object \0"
1146 "\0"
1147 },
1148 {
1149 1.2f,
1150 " EXT_texture3D \0"
1151 " EXT_bgra \0"
1152 " EXT_packed_pixels \0"
1153 " EXT_rescale_normal \0"
1154 " EXT_separate_specular_color \0"
1155 " SGIS_texture_edge_clamp \0"
1156 " SGIS_texture_lod \0"
1157 " EXT_draw_range_elements \0"
1158 "\0"
1159 },
1160 {
1161 1.3f,
1162 " GL_ARB_texture_compression \0"
1163 " GL_ARB_texture_cube_map \0"
1164 " GL_ARB_multisample \0"
1165 " GL_ARB_multitexture \0"
1166 " GL_ARB_texture_env_add \0"
1167 " GL_ARB_texture_env_combine \0"
1168 " GL_ARB_texture_env_dot3 \0"
1169 " GL_ARB_texture_border_clamp \0"
1170 " GL_ARB_transpose_matrix \0"
1171 "\0"
1172 },
1173 {
1174 1.5f,
1175 " GL_SGIS_generate_mipmap \0"
1176 /*" GL_NV_blend_equare \0"*/
1177 " GL_ARB_depth_texture \0"
1178 " GL_ARB_shadow \0"
1179 " GL_EXT_fog_coord \0"
1180 " GL_EXT_multi_draw_arrays \0"
1181 " GL_ARB_point_parameters \0"
1182 " GL_EXT_secondary_color \0"
1183 " GL_EXT_blend_func_separate \0"
1184 " GL_EXT_stencil_wrap \0"
1185 " GL_ARB_texture_env_crossbar \0"
1186 " GL_EXT_texture_lod_bias \0"
1187 " GL_ARB_texture_mirrored_repeat \0"
1188 " GL_ARB_window_pos \0"
1189 "\0"
1190 },
1191 {
1192 1.6f,
1193 " GL_ARB_vertex_buffer_object \0"
1194 " GL_ARB_occlusion_query \0"
1195 " GL_EXT_shadow_funcs \0"
1196 },
1197 {
1198 2.0f,
1199 " GL_ARB_shader_objects \0" /*??*/
1200 " GL_ARB_vertex_shader \0" /*??*/
1201 " GL_ARB_fragment_shader \0" /*??*/
1202 " GL_ARB_shading_language_100 \0" /*??*/
1203 " GL_ARB_draw_buffers \0"
1204 " GL_ARB_texture_non_power_of_two \0"
1205 " GL_ARB_point_sprite \0"
1206 " GL_ATI_separate_stencil \0"
1207 " GL_EXT_stencil_two_side \0"
1208 "\0"
1209 },
1210 {
1211 2.1f,
1212 " GL_ARB_pixel_buffer_object \0"
1213 " GL_EXT_texture_sRGB \0"
1214 "\0"
1215 },
1216 {
1217 3.0f,
1218 " GL_ARB_framebuffer_object \0"
1219 " GL_ARB_map_buffer_range \0"
1220 " GL_ARB_vertex_array_object \0"
1221 "\0"
1222 },
1223 {
1224 3.1f,
1225 " GL_ARB_copy_buffer \0"
1226 " GL_ARB_uniform_buffer_object \0"
1227 "\0"
1228 },
1229 {
1230 3.2f,
1231 " GL_ARB_vertex_array_bgra \0"
1232 " GL_ARB_draw_elements_base_vertex \0"
1233 " GL_ARB_fragment_coord_conventions \0"
1234 " GL_ARB_provoking_vertex \0"
1235 " GL_ARB_seamless_cube_map \0"
1236 " GL_ARB_texture_multisample \0"
1237 " GL_ARB_depth_clamp \0"
1238 " GL_ARB_sync \0"
1239 " GL_ARB_geometry_shader4 \0" /*??*/
1240 "\0"
1241 },
1242 {
1243 3.3f,
1244 " GL_ARB_blend_func_extended \0"
1245 " GL_ARB_sampler_objects \0"
1246 " GL_ARB_explicit_attrib_location \0"
1247 " GL_ARB_occlusion_query2 \0"
1248 " GL_ARB_shader_bit_encoding \0"
1249 " GL_ARB_texture_rgb10_a2ui \0"
1250 " GL_ARB_texture_swizzle \0"
1251 " GL_ARB_timer_query \0"
1252 " GL_ARB_vertex_type_2_10_10_10_rev \0"
1253 "\0"
1254 },
1255 {
1256 4.0f,
1257 " GL_ARB_texture_query_lod \0"
1258 " GL_ARB_draw_indirect \0"
1259 " GL_ARB_gpu_shader5 \0"
1260 " GL_ARB_gpu_shader_fp64 \0"
1261 " GL_ARB_shader_subroutine \0"
1262 " GL_ARB_tessellation_shader \0"
1263 " GL_ARB_texture_buffer_object_rgb32 \0"
1264 " GL_ARB_texture_cube_map_array \0"
1265 " GL_ARB_texture_gather \0"
1266 " GL_ARB_transform_feedback2 \0"
1267 " GL_ARB_transform_feedback3 \0"
1268 "\0"
1269 },
1270 {
1271 4.1f,
1272 " GL_ARB_ES2_compatibility \0"
1273 " GL_ARB_get_program_binary \0"
1274 " GL_ARB_separate_shader_objects \0"
1275 " GL_ARB_shader_precision \0"
1276 " GL_ARB_vertex_attrib_64bit \0"
1277 " GL_ARB_viewport_array \0"
1278 "\0"
1279 }
1280 };
1281
1282 uint32_t cPromoted = 0;
1283 for (uint32_t i = 0; i < RT_ELEMENTS(s_aPromotedExtensions) && s_aPromotedExtensions[i].fGLVersion <= fGLProfileVersion; i++)
1284 {
1285 const char *pszExt = s_aPromotedExtensions[i].pszzExtensions;
1286 while (*pszExt)
1287 {
1288 size_t cchExt = strlen(pszExt);
1289 Assert(cchExt > 3);
1290 Assert(pszExt[0] == ' ');
1291 Assert(pszExt[1] != ' ');
1292 Assert(pszExt[cchExt - 2] != ' ');
1293 Assert(pszExt[cchExt - 1] == ' ');
1294
1295 if (strstr(*ppszExtensions, pszExt) == NULL)
1296 {
1297 if (cPromoted++ == 0)
1298 {
1299 rc = RTStrAAppend(ppszExtensions, " <promoted-extensions:> <promoted-extensions:> <promoted-extensions:> ");
1300 AssertRCReturn(rc, rc);
1301 }
1302
1303 rc = RTStrAAppend(ppszExtensions, pszExt);
1304 AssertRCReturn(rc, rc);
1305 }
1306
1307 pszExt = strchr(pszExt, '\0') + 1;
1308 }
1309 }
1310#endif
1311
1312 return VINF_SUCCESS;
1313}
1314
1315/**
1316 * @interface_method_impl{VBOXVMSVGASHADERIF, pfnSwitchInitProfile}
1317 */
1318static DECLCALLBACK(void) vmsvga3dShaderIfSwitchInitProfile(PVBOXVMSVGASHADERIF pThis, bool fOtherProfile)
1319{
1320#ifdef VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE
1321 PVMSVGA3DSTATE pState = RT_FROM_MEMBER(pThis, VMSVGA3DSTATE, ShaderIf);
1322 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pState->papContexts[fOtherProfile ? 2 : 1]);
1323#else
1324 NOREF(pThis);
1325 NOREF(fOtherProfile);
1326#endif
1327}
1328
1329
1330/**
1331 * @interface_method_impl{VBOXVMSVGASHADERIF, pfnGetNextExtension}
1332 */
1333static DECLCALLBACK(bool) vmsvga3dShaderIfGetNextExtension(PVBOXVMSVGASHADERIF pThis, void **ppvEnumCtx,
1334 char *pszBuf, size_t cbBuf, bool fOtherProfile)
1335{
1336 PVMSVGA3DSTATE pState = RT_FROM_MEMBER(pThis, VMSVGA3DSTATE, ShaderIf);
1337 const char *pszCur = *ppvEnumCtx ? (const char *)*ppvEnumCtx
1338 : fOtherProfile ? pState->pszOtherExtensions : pState->pszExtensions;
1339 while (*pszCur == ' ')
1340 pszCur++;
1341 if (!*pszCur)
1342 return false;
1343
1344 const char *pszEnd = strchr(pszCur, ' ');
1345 AssertReturn(pszEnd, false);
1346 size_t cch = pszEnd - pszCur;
1347 if (cch < cbBuf)
1348 {
1349 memcpy(pszBuf, pszCur, cch);
1350 pszBuf[cch] = '\0';
1351 }
1352 else if (cbBuf > 0)
1353 {
1354 memcpy(pszBuf, "<overflow>", RT_MIN(sizeof("<overflow>"), cbBuf));
1355 pszBuf[cbBuf - 1] = '\0';
1356 }
1357
1358 *ppvEnumCtx = (void *)pszEnd;
1359 return true;
1360}
1361
1362
1363/**
1364 * Initializes the VMSVGA3D state during VGA device construction.
1365 *
1366 * Failure are generally not fatal, 3D support will just be disabled.
1367 *
1368 * @returns VBox status code.
1369 * @param pThis The VGA device state where svga.p3dState will be modified.
1370 */
1371int vmsvga3dInit(PVGASTATE pThis)
1372{
1373 AssertCompile(GL_TRUE == 1);
1374 AssertCompile(GL_FALSE == 0);
1375
1376 /*
1377 * Load and resolve imports from the external shared libraries.
1378 */
1379 RTERRINFOSTATIC ErrInfo;
1380 int rc = ExplicitlyLoadVBoxSVGA3D(true /*fResolveAllImports*/, RTErrInfoInitStatic(&ErrInfo));
1381 if (RT_FAILURE(rc))
1382 {
1383 LogRel(("VMSVGA3d: Error loading VBoxSVGA3D and resolving necessary functions: %Rrc - %s\n", rc, ErrInfo.Core.pszMsg));
1384 return rc;
1385 }
1386#ifdef RT_OS_DARWIN
1387 rc = ExplicitlyLoadVBoxSVGA3DObjC(true /*fResolveAllImports*/, RTErrInfoInitStatic(&ErrInfo));
1388 if (RT_FAILURE(rc))
1389 {
1390 LogRel(("VMSVGA3d: Error loading VBoxSVGA3DObjC and resolving necessary functions: %Rrc - %s\n", rc, ErrInfo.Core.pszMsg));
1391 return rc;
1392 }
1393#endif
1394
1395 /*
1396 * Allocate the state.
1397 */
1398 pThis->svga.p3dState = RTMemAllocZ(sizeof(VMSVGA3DSTATE));
1399 AssertReturn(pThis->svga.p3dState, VERR_NO_MEMORY);
1400
1401#ifdef RT_OS_WINDOWS
1402 /* Create event semaphore and async IO thread. */
1403 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
1404 rc = RTSemEventCreate(&pState->WndRequestSem);
1405 if (RT_SUCCESS(rc))
1406 {
1407 rc = RTThreadCreate(&pState->pWindowThread, vmsvga3dWindowThread, pState->WndRequestSem, 0, RTTHREADTYPE_GUI, 0,
1408 "VMSVGA3DWND");
1409 if (RT_SUCCESS(rc))
1410 return VINF_SUCCESS;
1411
1412 /* bail out. */
1413 LogRel(("VMSVGA3d: RTThreadCreate failed: %Rrc\n", rc));
1414 RTSemEventDestroy(pState->WndRequestSem);
1415 }
1416 else
1417 LogRel(("VMSVGA3d: RTSemEventCreate failed: %Rrc\n", rc));
1418 RTMemFree(pThis->svga.p3dState);
1419 pThis->svga.p3dState = NULL;
1420 return rc;
1421#else
1422 return VINF_SUCCESS;
1423#endif
1424}
1425
1426/* We must delay window creation until the PowerOn phase. Init is too early and will cause failures. */
1427int vmsvga3dPowerOn(PVGASTATE pThis)
1428{
1429 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
1430 AssertReturn(pThis->svga.p3dState, VERR_NO_MEMORY);
1431 PVMSVGA3DCONTEXT pContext;
1432#ifdef VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE
1433 PVMSVGA3DCONTEXT pOtherCtx;
1434#endif
1435 int rc;
1436
1437 if (pState->fGLVersion != 0.0)
1438 return VINF_SUCCESS; /* already initialized (load state) */
1439
1440 /*
1441 * OpenGL function calls aren't possible without a valid current context, so create a fake one here.
1442 */
1443 rc = vmsvga3dContextDefineOgl(pThis, 1, VMSVGA3D_DEF_CTX_F_INIT);
1444 AssertRCReturn(rc, rc);
1445
1446 pContext = pState->papContexts[1];
1447 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
1448
1449 LogRel(("VMSVGA3d: OpenGL version: %s\n"
1450 "VMSVGA3d: OpenGL Vendor: %s\n"
1451 "VMSVGA3d: OpenGL Renderer: %s\n"
1452 "VMSVGA3d: OpenGL shader language version: %s\n",
1453 glGetString(GL_VERSION), glGetString(GL_VENDOR), glGetString(GL_RENDERER),
1454 glGetString(GL_SHADING_LANGUAGE_VERSION)));
1455
1456 rc = vmsvga3dGatherExtensions(&pState->pszExtensions, VBOX_VMSVGA3D_DEFAULT_OGL_PROFILE);
1457 AssertRCReturn(rc, rc);
1458 vmsvga3dLogRelExtensions("", pState->pszExtensions);
1459
1460 pState->fGLVersion = atof((const char *)glGetString(GL_VERSION));
1461
1462
1463#ifdef VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE
1464 /*
1465 * Get the extension list for the alternative profile so we can better
1466 * figure out the shader model and stuff.
1467 */
1468 rc = vmsvga3dContextDefineOgl(pThis, 2, VMSVGA3D_DEF_CTX_F_INIT | VMSVGA3D_DEF_CTX_F_OTHER_PROFILE);
1469 AssertLogRelRCReturn(rc, rc);
1470 pContext = pState->papContexts[1]; /* Array may have been reallocated. */
1471
1472 pOtherCtx = pState->papContexts[2];
1473 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pOtherCtx);
1474
1475 LogRel(("VMSVGA3d: Alternative OpenGL version: %s\n"
1476 "VMSVGA3d: Alternative OpenGL Vendor: %s\n"
1477 "VMSVGA3d: Alternative OpenGL Renderer: %s\n"
1478 "VMSVGA3d: Alternative OpenGL shader language version: %s\n",
1479 glGetString(GL_VERSION), glGetString(GL_VENDOR), glGetString(GL_RENDERER),
1480 glGetString(GL_SHADING_LANGUAGE_VERSION)));
1481
1482 rc = vmsvga3dGatherExtensions(&pState->pszOtherExtensions, VBOX_VMSVGA3D_OTHER_OGL_PROFILE);
1483 AssertRCReturn(rc, rc);
1484 vmsvga3dLogRelExtensions("Alternative ", pState->pszOtherExtensions);
1485
1486 pState->fOtherGLVersion = atof((const char *)glGetString(GL_VERSION));
1487
1488 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
1489#else
1490 pState->pszOtherExtensions = (char *)"";
1491 pState->fOtherGLVersion = pState->fGLVersion;
1492#endif
1493
1494
1495 if (vmsvga3dCheckGLExtension(pState, 3.0f, " GL_ARB_framebuffer_object "))
1496 {
1497 pState->ext.glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC)OGLGETPROCADDRESS("glIsRenderbuffer");
1498 pState->ext.glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC)OGLGETPROCADDRESS("glBindRenderbuffer");
1499 pState->ext.glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC)OGLGETPROCADDRESS("glDeleteRenderbuffers");
1500 pState->ext.glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC)OGLGETPROCADDRESS("glGenRenderbuffers");
1501 pState->ext.glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC)OGLGETPROCADDRESS("glRenderbufferStorage");
1502 pState->ext.glGetRenderbufferParameteriv = (PFNGLGETRENDERBUFFERPARAMETERIVPROC)OGLGETPROCADDRESS("glGetRenderbufferParameteriv");
1503 pState->ext.glIsFramebuffer = (PFNGLISFRAMEBUFFERPROC)OGLGETPROCADDRESS("glIsFramebuffer");
1504 pState->ext.glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC)OGLGETPROCADDRESS("glBindFramebuffer");
1505 pState->ext.glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC)OGLGETPROCADDRESS("glDeleteFramebuffers");
1506 pState->ext.glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC)OGLGETPROCADDRESS("glGenFramebuffers");
1507 pState->ext.glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC)OGLGETPROCADDRESS("glCheckFramebufferStatus");
1508 pState->ext.glFramebufferTexture1D = (PFNGLFRAMEBUFFERTEXTURE1DPROC)OGLGETPROCADDRESS("glFramebufferTexture1D");
1509 pState->ext.glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC)OGLGETPROCADDRESS("glFramebufferTexture2D");
1510 pState->ext.glFramebufferTexture3D = (PFNGLFRAMEBUFFERTEXTURE3DPROC)OGLGETPROCADDRESS("glFramebufferTexture3D");
1511 pState->ext.glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC)OGLGETPROCADDRESS("glFramebufferRenderbuffer");
1512 pState->ext.glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC)OGLGETPROCADDRESS("glGetFramebufferAttachmentParameteriv");
1513 pState->ext.glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC)OGLGETPROCADDRESS("glGenerateMipmap");
1514 pState->ext.glBlitFramebuffer = (PFNGLBLITFRAMEBUFFERPROC)OGLGETPROCADDRESS("glBlitFramebuffer");
1515 pState->ext.glRenderbufferStorageMultisample = (PFNGLRENDERBUFFERSTORAGEMULTISAMPLEPROC)OGLGETPROCADDRESS("glRenderbufferStorageMultisample");
1516 pState->ext.glFramebufferTextureLayer = (PFNGLFRAMEBUFFERTEXTURELAYERPROC)OGLGETPROCADDRESS("glFramebufferTextureLayer");
1517 }
1518 pState->ext.glPointParameterf = (PFNGLPOINTPARAMETERFPROC)OGLGETPROCADDRESS("glPointParameterf");
1519 AssertMsgReturn(pState->ext.glPointParameterf, ("glPointParameterf missing"), VERR_NOT_IMPLEMENTED);
1520#if VBOX_VMSVGA3D_GL_HACK_LEVEL < 0x102
1521 pState->ext.glBlendColor = (PFNGLBLENDCOLORPROC)OGLGETPROCADDRESS("glBlendColor");
1522 AssertMsgReturn(pState->ext.glBlendColor, ("glBlendColor missing"), VERR_NOT_IMPLEMENTED);
1523 pState->ext.glBlendEquation = (PFNGLBLENDEQUATIONPROC)OGLGETPROCADDRESS("glBlendEquation");
1524 AssertMsgReturn(pState->ext.glBlendEquation, ("glBlendEquation missing"), VERR_NOT_IMPLEMENTED);
1525#endif
1526 pState->ext.glBlendEquationSeparate = (PFNGLBLENDEQUATIONSEPARATEPROC)OGLGETPROCADDRESS("glBlendEquationSeparate");
1527 AssertMsgReturn(pState->ext.glBlendEquationSeparate, ("glBlendEquationSeparate missing"), VERR_NOT_IMPLEMENTED);
1528 pState->ext.glBlendFuncSeparate = (PFNGLBLENDFUNCSEPARATEPROC)OGLGETPROCADDRESS("glBlendFuncSeparate");
1529 AssertMsgReturn(pState->ext.glBlendFuncSeparate, ("glBlendFuncSeparate missing"), VERR_NOT_IMPLEMENTED);
1530 pState->ext.glStencilOpSeparate = (PFNGLSTENCILOPSEPARATEPROC)OGLGETPROCADDRESS("glStencilOpSeparate");
1531 AssertMsgReturn(pState->ext.glStencilOpSeparate, ("glStencilOpSeparate missing"), VERR_NOT_IMPLEMENTED);
1532 pState->ext.glStencilFuncSeparate = (PFNGLSTENCILFUNCSEPARATEPROC)OGLGETPROCADDRESS("glStencilFuncSeparate");
1533 AssertMsgReturn(pState->ext.glStencilFuncSeparate, ("glStencilFuncSeparate missing"), VERR_NOT_IMPLEMENTED);
1534 pState->ext.glBindBuffer = (PFNGLBINDBUFFERPROC)OGLGETPROCADDRESS("glBindBuffer");
1535 AssertMsgReturn(pState->ext.glBindBuffer, ("glBindBuffer missing"), VERR_NOT_IMPLEMENTED);
1536 pState->ext.glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)OGLGETPROCADDRESS("glDeleteBuffers");
1537 AssertMsgReturn(pState->ext.glDeleteBuffers, ("glDeleteBuffers missing"), VERR_NOT_IMPLEMENTED);
1538 pState->ext.glGenBuffers = (PFNGLGENBUFFERSPROC)OGLGETPROCADDRESS("glGenBuffers");
1539 AssertMsgReturn(pState->ext.glGenBuffers, ("glGenBuffers missing"), VERR_NOT_IMPLEMENTED);
1540 pState->ext.glBufferData = (PFNGLBUFFERDATAPROC)OGLGETPROCADDRESS("glBufferData");
1541 AssertMsgReturn(pState->ext.glBufferData, ("glBufferData missing"), VERR_NOT_IMPLEMENTED);
1542 pState->ext.glMapBuffer = (PFNGLMAPBUFFERPROC)OGLGETPROCADDRESS("glMapBuffer");
1543 AssertMsgReturn(pState->ext.glMapBuffer, ("glMapBuffer missing"), VERR_NOT_IMPLEMENTED);
1544 pState->ext.glUnmapBuffer = (PFNGLUNMAPBUFFERPROC)OGLGETPROCADDRESS("glUnmapBuffer");
1545 AssertMsgReturn(pState->ext.glUnmapBuffer, ("glUnmapBuffer missing"), VERR_NOT_IMPLEMENTED);
1546 pState->ext.glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)OGLGETPROCADDRESS("glEnableVertexAttribArray");
1547 AssertMsgReturn(pState->ext.glEnableVertexAttribArray, ("glEnableVertexAttribArray missing"), VERR_NOT_IMPLEMENTED);
1548 pState->ext.glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYPROC)OGLGETPROCADDRESS("glDisableVertexAttribArray");
1549 AssertMsgReturn(pState->ext.glDisableVertexAttribArray, ("glDisableVertexAttribArray missing"), VERR_NOT_IMPLEMENTED);
1550 pState->ext.glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)OGLGETPROCADDRESS("glVertexAttribPointer");
1551 AssertMsgReturn(pState->ext.glVertexAttribPointer, ("glVertexAttribPointer missing"), VERR_NOT_IMPLEMENTED);
1552 pState->ext.glFogCoordPointer = (PFNGLFOGCOORDPOINTERPROC)OGLGETPROCADDRESS("glFogCoordPointer");
1553 AssertMsgReturn(pState->ext.glFogCoordPointer, ("glFogCoordPointer missing"), VERR_NOT_IMPLEMENTED);
1554 pState->ext.glActiveTexture = (PFNGLACTIVETEXTUREPROC)OGLGETPROCADDRESS("glActiveTexture");
1555 AssertMsgReturn(pState->ext.glActiveTexture, ("glActiveTexture missing"), VERR_NOT_IMPLEMENTED);
1556#if VBOX_VMSVGA3D_GL_HACK_LEVEL < 0x103
1557 pState->ext.glClientActiveTexture = (PFNGLCLIENTACTIVETEXTUREPROC)OGLGETPROCADDRESS("glClientActiveTexture");
1558 AssertMsgReturn(pState->ext.glClientActiveTexture, ("glClientActiveTexture missing"), VERR_NOT_IMPLEMENTED);
1559#endif
1560 pState->ext.glGetProgramivARB = (PFNGLGETPROGRAMIVARBPROC)OGLGETPROCADDRESS("glGetProgramivARB");
1561 AssertMsgReturn(pState->ext.glGetProgramivARB, ("glGetProgramivARB missing"), VERR_NOT_IMPLEMENTED);
1562
1563 /* OpenGL 3.2 core */
1564 if (vmsvga3dCheckGLExtension(pState, 3.2f, " GL_ARB_draw_elements_base_vertex "))
1565 {
1566 pState->ext.glDrawElementsBaseVertex = (PFNGLDRAWELEMENTSBASEVERTEXPROC)OGLGETPROCADDRESS("glDrawElementsBaseVertex");
1567 pState->ext.glDrawElementsInstancedBaseVertex = (PFNGLDRAWELEMENTSINSTANCEDBASEVERTEXPROC)OGLGETPROCADDRESS("glDrawElementsInstancedBaseVertex");
1568 }
1569 else
1570 LogRel(("VMSVGA3d: missing extension GL_ARB_draw_elements_base_vertex\n"));
1571
1572 /* OpenGL 3.2 core */
1573 if (vmsvga3dCheckGLExtension(pState, 3.2f, " GL_ARB_provoking_vertex "))
1574 {
1575 pState->ext.glProvokingVertex = (PFNGLPROVOKINGVERTEXPROC)OGLGETPROCADDRESS("glProvokingVertex");
1576 }
1577 else
1578 LogRel(("VMSVGA3d: missing extension GL_ARB_provoking_vertex\n"));
1579
1580 /* Extension support */
1581#if defined(RT_OS_DARWIN)
1582 /** @todo OpenGL version history suggest this, verify... */
1583 pState->ext.fEXT_stencil_two_side = vmsvga3dCheckGLExtension(pState, 2.0f, " GL_EXT_stencil_two_side ");
1584#else
1585 pState->ext.fEXT_stencil_two_side = vmsvga3dCheckGLExtension(pState, 0.0f, " GL_EXT_stencil_two_side ");
1586#endif
1587
1588 /*
1589 * Initialize the capabilities with sensible defaults.
1590 */
1591 pState->caps.maxActiveLights = 1;
1592 pState->caps.maxTextureBufferSize = 65536;
1593 pState->caps.maxTextures = 1;
1594 pState->caps.maxClipDistances = 4;
1595 pState->caps.maxColorAttachments = 1;
1596 pState->caps.maxRectangleTextureSize = 2048;
1597 pState->caps.maxTextureAnisotropy = 2;
1598 pState->caps.maxVertexShaderInstructions = 1024;
1599 pState->caps.maxFragmentShaderInstructions = 1024;
1600 pState->caps.vertexShaderVersion = SVGA3DVSVERSION_NONE;
1601 pState->caps.fragmentShaderVersion = SVGA3DPSVERSION_NONE;
1602 pState->caps.flPointSize[0] = 1;
1603 pState->caps.flPointSize[1] = 1;
1604
1605 /*
1606 * Query capabilities
1607 */
1608 VMSVGA3D_INIT_CHECKED_BOTH(pState, pContext, pOtherCtx, glGetIntegerv(GL_MAX_LIGHTS, &pState->caps.maxActiveLights));
1609 VMSVGA3D_INIT_CHECKED_BOTH(pState, pContext, pOtherCtx, glGetIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE, &pState->caps.maxTextureBufferSize));
1610 VMSVGA3D_INIT_CHECKED_BOTH(pState, pContext, pOtherCtx, glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &pState->caps.maxTextures));
1611#ifdef VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE /* The alternative profile has a higher number here (ati/darwin). */
1612 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pOtherCtx);
1613 VMSVGA3D_INIT_CHECKED_BOTH(pState, pOtherCtx, pContext, glGetIntegerv(GL_MAX_CLIP_DISTANCES, &pState->caps.maxClipDistances));
1614 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
1615#else
1616 VMSVGA3D_INIT_CHECKED(glGetIntegerv(GL_MAX_CLIP_DISTANCES, &pState->caps.maxClipDistances));
1617#endif
1618 VMSVGA3D_INIT_CHECKED(glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &pState->caps.maxColorAttachments));
1619 VMSVGA3D_INIT_CHECKED(glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE, &pState->caps.maxRectangleTextureSize));
1620 VMSVGA3D_INIT_CHECKED(glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &pState->caps.maxTextureAnisotropy));
1621 VMSVGA3D_INIT_CHECKED_BOTH(pState, pContext, pOtherCtx, glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, pState->caps.flPointSize));
1622
1623 if (pState->ext.glGetProgramivARB)
1624 {
1625 VMSVGA3D_INIT_CHECKED_BOTH(pState, pContext, pOtherCtx,
1626 pState->ext.glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB,
1627 &pState->caps.maxFragmentShaderTemps));
1628 VMSVGA3D_INIT_CHECKED_BOTH(pState, pContext, pOtherCtx,
1629 pState->ext.glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB,
1630 &pState->caps.maxFragmentShaderInstructions));
1631 VMSVGA3D_INIT_CHECKED_BOTH(pState, pContext, pOtherCtx,
1632 pState->ext.glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB,
1633 &pState->caps.maxVertexShaderTemps));
1634 VMSVGA3D_INIT_CHECKED_BOTH(pState, pContext, pOtherCtx,
1635 pState->ext.glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB,
1636 &pState->caps.maxVertexShaderInstructions));
1637 }
1638 pState->caps.fS3TCSupported = vmsvga3dCheckGLExtension(pState, 0.0f, " GL_EXT_texture_compression_s3tc ");
1639
1640 /* http://http://www.opengl.org/wiki/Detecting_the_Shader_Model
1641 * ARB Assembly Language
1642 * These are done through testing the presence of extensions. You should test them in this order:
1643 * GL_NV_gpu_program4: SM 4.0 or better.
1644 * GL_NV_vertex_program3: SM 3.0 or better.
1645 * GL_ARB_fragment_program: SM 2.0 or better.
1646 * ATI does not support higher than SM 2.0 functionality in assembly shaders.
1647 *
1648 */
1649 /** @todo: distinguish between vertex and pixel shaders??? */
1650 if ( vmsvga3dCheckGLExtension(pState, 0.0f, " GL_NV_gpu_program4 ")
1651 || strstr(pState->pszOtherExtensions, " GL_NV_gpu_program4 "))
1652 {
1653 pState->caps.vertexShaderVersion = SVGA3DVSVERSION_40;
1654 pState->caps.fragmentShaderVersion = SVGA3DPSVERSION_40;
1655 }
1656 else
1657 if ( vmsvga3dCheckGLExtension(pState, 0.0f, " GL_NV_vertex_program3 ")
1658 || strstr(pState->pszOtherExtensions, " GL_NV_vertex_program3 ")
1659 || vmsvga3dCheckGLExtension(pState, 0.0f, " GL_ARB_shader_texture_lod ") /* Wine claims this suggests SM 3.0 support */
1660 || strstr(pState->pszOtherExtensions, " GL_ARB_shader_texture_lod ")
1661 )
1662 {
1663 pState->caps.vertexShaderVersion = SVGA3DVSVERSION_30;
1664 pState->caps.fragmentShaderVersion = SVGA3DPSVERSION_30;
1665 }
1666 else
1667 if ( vmsvga3dCheckGLExtension(pState, 0.0f, " GL_ARB_fragment_program ")
1668 || strstr(pState->pszOtherExtensions, " GL_ARB_fragment_program "))
1669 {
1670 pState->caps.vertexShaderVersion = SVGA3DVSVERSION_20;
1671 pState->caps.fragmentShaderVersion = SVGA3DPSVERSION_20;
1672 }
1673 else
1674 {
1675 LogRel(("VMSVGA3D: WARNING: unknown support for assembly shaders!!\n"));
1676 pState->caps.vertexShaderVersion = SVGA3DVSVERSION_11;
1677 pState->caps.fragmentShaderVersion = SVGA3DPSVERSION_11;
1678 }
1679
1680 if (!vmsvga3dCheckGLExtension(pState, 3.2f, " GL_ARB_vertex_array_bgra "))
1681 {
1682 /** @todo Intel drivers don't support this extension! */
1683 LogRel(("VMSVGA3D: WARNING: Missing required extension GL_ARB_vertex_array_bgra (d3dcolor)!!!\n"));
1684 }
1685#if 0
1686 SVGA3D_DEVCAP_MAX_FIXED_VERTEXBLEND = 11,
1687 SVGA3D_DEVCAP_QUERY_TYPES = 15,
1688 SVGA3D_DEVCAP_TEXTURE_GRADIENT_SAMPLING = 16,
1689 SVGA3D_DEVCAP_MAX_POINT_SIZE = 17,
1690 SVGA3D_DEVCAP_MAX_SHADER_TEXTURES = 18,
1691 SVGA3D_DEVCAP_MAX_VOLUME_EXTENT = 21,
1692 SVGA3D_DEVCAP_MAX_TEXTURE_REPEAT = 22,
1693 SVGA3D_DEVCAP_MAX_TEXTURE_ASPECT_RATIO = 23,
1694 SVGA3D_DEVCAP_MAX_TEXTURE_ANISOTROPY = 24,
1695 SVGA3D_DEVCAP_MAX_PRIMITIVE_COUNT = 25,
1696 SVGA3D_DEVCAP_MAX_VERTEX_INDEX = 26,
1697 SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_INSTRUCTIONS = 28,
1698 SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEMPS = 29,
1699 SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_TEMPS = 30,
1700 SVGA3D_DEVCAP_TEXTURE_OPS = 31,
1701 SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8 = 32,
1702 SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8 = 33,
1703 SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10 = 34,
1704 SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5 = 35,
1705 SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5 = 36,
1706 SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4 = 37,
1707 SVGA3D_DEVCAP_SURFACEFMT_R5G6B5 = 38,
1708 SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE16 = 39,
1709 SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8_ALPHA8 = 40,
1710 SVGA3D_DEVCAP_SURFACEFMT_ALPHA8 = 41,
1711 SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8 = 42,
1712 SVGA3D_DEVCAP_SURFACEFMT_Z_D16 = 43,
1713 SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8 = 44,
1714 SVGA3D_DEVCAP_SURFACEFMT_Z_D24X8 = 45,
1715 SVGA3D_DEVCAP_SURFACEFMT_DXT1 = 46,
1716 SVGA3D_DEVCAP_SURFACEFMT_DXT2 = 47,
1717 SVGA3D_DEVCAP_SURFACEFMT_DXT3 = 48,
1718 SVGA3D_DEVCAP_SURFACEFMT_DXT4 = 49,
1719 SVGA3D_DEVCAP_SURFACEFMT_DXT5 = 50,
1720 SVGA3D_DEVCAP_SURFACEFMT_BUMPX8L8V8U8 = 51,
1721 SVGA3D_DEVCAP_SURFACEFMT_A2W10V10U10 = 52,
1722 SVGA3D_DEVCAP_SURFACEFMT_BUMPU8V8 = 53,
1723 SVGA3D_DEVCAP_SURFACEFMT_Q8W8V8U8 = 54,
1724 SVGA3D_DEVCAP_SURFACEFMT_CxV8U8 = 55,
1725 SVGA3D_DEVCAP_SURFACEFMT_R_S10E5 = 56,
1726 SVGA3D_DEVCAP_SURFACEFMT_R_S23E8 = 57,
1727 SVGA3D_DEVCAP_SURFACEFMT_RG_S10E5 = 58,
1728 SVGA3D_DEVCAP_SURFACEFMT_RG_S23E8 = 59,
1729 SVGA3D_DEVCAP_SURFACEFMT_ARGB_S10E5 = 60,
1730 SVGA3D_DEVCAP_SURFACEFMT_ARGB_S23E8 = 61,
1731 SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEXTURES = 63,
1732 SVGA3D_DEVCAP_SURFACEFMT_V16U16 = 65,
1733 SVGA3D_DEVCAP_SURFACEFMT_G16R16 = 66,
1734 SVGA3D_DEVCAP_SURFACEFMT_A16B16G16R16 = 67,
1735 SVGA3D_DEVCAP_SURFACEFMT_UYVY = 68,
1736 SVGA3D_DEVCAP_SURFACEFMT_YUY2 = 69,
1737 SVGA3D_DEVCAP_MULTISAMPLE_NONMASKABLESAMPLES = 70,
1738 SVGA3D_DEVCAP_MULTISAMPLE_MASKABLESAMPLES = 71,
1739 SVGA3D_DEVCAP_ALPHATOCOVERAGE = 72,
1740 SVGA3D_DEVCAP_SUPERSAMPLE = 73,
1741 SVGA3D_DEVCAP_AUTOGENMIPMAPS = 74,
1742 SVGA3D_DEVCAP_SURFACEFMT_NV12 = 75,
1743 SVGA3D_DEVCAP_SURFACEFMT_AYUV = 76,
1744 SVGA3D_DEVCAP_SURFACEFMT_Z_DF16 = 79,
1745 SVGA3D_DEVCAP_SURFACEFMT_Z_DF24 = 80,
1746 SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8_INT = 81,
1747 SVGA3D_DEVCAP_SURFACEFMT_BC4_UNORM = 82,
1748 SVGA3D_DEVCAP_SURFACEFMT_BC5_UNORM = 83,
1749#endif
1750
1751 LogRel(("VMSVGA3d: Capabilities:\n"));
1752 LogRel(("VMSVGA3d: maxActiveLights=%-2d maxTextures=%-2d maxTextureBufferSize=%d\n",
1753 pState->caps.maxActiveLights, pState->caps.maxTextures, pState->caps.maxTextureBufferSize));
1754 LogRel(("VMSVGA3d: maxClipDistances=%-2d maxColorAttachments=%-2d maxClipDistances=%d\n",
1755 pState->caps.maxClipDistances, pState->caps.maxColorAttachments, pState->caps.maxClipDistances));
1756 LogRel(("VMSVGA3d: maxColorAttachments=%-2d maxTextureAnisotropy=%-2d maxRectangleTextureSize=%d\n",
1757 pState->caps.maxColorAttachments, pState->caps.maxTextureAnisotropy, pState->caps.maxRectangleTextureSize));
1758 LogRel(("VMSVGA3d: maxVertexShaderTemps=%-2d maxVertexShaderInstructions=%d maxFragmentShaderInstructions=%d\n",
1759 pState->caps.maxVertexShaderTemps, pState->caps.maxVertexShaderInstructions, pState->caps.maxFragmentShaderInstructions));
1760 LogRel(("VMSVGA3d: maxFragmentShaderTemps=%d flPointSize={%d.%02u, %d.%02u}\n",
1761 pState->caps.maxFragmentShaderTemps,
1762 (int)pState->caps.flPointSize[0], (int)(pState->caps.flPointSize[0] * 100) % 100,
1763 (int)pState->caps.flPointSize[1], (int)(pState->caps.flPointSize[1] * 100) % 100));
1764 LogRel(("VMSVGA3d: fragmentShaderVersion=%-2d vertexShaderVersion=%-2d fS3TCSupported=%d\n",
1765 pState->caps.fragmentShaderVersion, pState->caps.vertexShaderVersion, pState->caps.fS3TCSupported));
1766
1767
1768 /* Initialize the shader library. */
1769 pState->ShaderIf.pfnSwitchInitProfile = vmsvga3dShaderIfSwitchInitProfile;
1770 pState->ShaderIf.pfnGetNextExtension = vmsvga3dShaderIfGetNextExtension;
1771 rc = ShaderInitLib(&pState->ShaderIf);
1772 AssertRC(rc);
1773
1774 /* Cleanup */
1775 rc = vmsvga3dContextDestroy(pThis, 1);
1776 AssertRC(rc);
1777#ifdef VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE
1778 rc = vmsvga3dContextDestroy(pThis, 2);
1779 AssertRC(rc);
1780#endif
1781
1782 if ( pState->fGLVersion < 3.0
1783 && pState->fOtherGLVersion < 3.0 /* darwin: legacy profile hack */)
1784 {
1785 LogRel(("VMSVGA3d: unsupported OpenGL version; minimum is 3.0\n"));
1786 return VERR_NOT_IMPLEMENTED;
1787 }
1788 if ( !pState->ext.glIsRenderbuffer
1789 || !pState->ext.glBindRenderbuffer
1790 || !pState->ext.glDeleteRenderbuffers
1791 || !pState->ext.glGenRenderbuffers
1792 || !pState->ext.glRenderbufferStorage
1793 || !pState->ext.glGetRenderbufferParameteriv
1794 || !pState->ext.glIsFramebuffer
1795 || !pState->ext.glBindFramebuffer
1796 || !pState->ext.glDeleteFramebuffers
1797 || !pState->ext.glGenFramebuffers
1798 || !pState->ext.glCheckFramebufferStatus
1799 || !pState->ext.glFramebufferTexture1D
1800 || !pState->ext.glFramebufferTexture2D
1801 || !pState->ext.glFramebufferTexture3D
1802 || !pState->ext.glFramebufferRenderbuffer
1803 || !pState->ext.glGetFramebufferAttachmentParameteriv
1804 || !pState->ext.glGenerateMipmap
1805 || !pState->ext.glBlitFramebuffer
1806 || !pState->ext.glRenderbufferStorageMultisample
1807 || !pState->ext.glFramebufferTextureLayer)
1808 {
1809 LogRel(("VMSVGA3d: missing required OpenGL extension; aborting\n"));
1810 return VERR_NOT_IMPLEMENTED;
1811 }
1812
1813#ifdef DEBUG_DEBUG_GFX_WINDOW_TEST_CONTEXT
1814 pState->idTestContext = SVGA_ID_INVALID;
1815#endif
1816 return VINF_SUCCESS;
1817}
1818
1819int vmsvga3dReset(PVGASTATE pThis)
1820{
1821 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
1822 AssertReturn(pThis->svga.p3dState, VERR_NO_MEMORY);
1823
1824 /* Destroy all leftover surfaces. */
1825 for (uint32_t i = 0; i < pState->cSurfaces; i++)
1826 {
1827 if (pState->papSurfaces[i]->id != SVGA3D_INVALID_ID)
1828 vmsvga3dSurfaceDestroy(pThis, pState->papSurfaces[i]->id);
1829 }
1830
1831 /* Destroy all leftover contexts. */
1832 for (uint32_t i = 0; i < pState->cContexts; i++)
1833 {
1834 if (pState->papContexts[i]->id != SVGA3D_INVALID_ID)
1835 vmsvga3dContextDestroy(pThis, pState->papContexts[i]->id);
1836 }
1837 return VINF_SUCCESS;
1838}
1839
1840int vmsvga3dTerminate(PVGASTATE pThis)
1841{
1842 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
1843 AssertReturn(pState, VERR_WRONG_ORDER);
1844 int rc;
1845
1846 rc = vmsvga3dReset(pThis);
1847 AssertRCReturn(rc, rc);
1848
1849 /* Terminate the shader library. */
1850 rc = ShaderDestroyLib();
1851 AssertRC(rc);
1852
1853#ifdef RT_OS_WINDOWS
1854 /* Terminate the window creation thread. */
1855 rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_EXIT, 0, 0);
1856 AssertRCReturn(rc, rc);
1857
1858 RTSemEventDestroy(pState->WndRequestSem);
1859#elif defined(RT_OS_DARWIN)
1860
1861#elif defined(RT_OS_LINUX)
1862 /* signal to the thread that it is supposed to exit */
1863 pState->bTerminate = true;
1864 /* wait for it to terminate */
1865 rc = RTThreadWait(pState->pWindowThread, 10000, NULL);
1866 AssertRC(rc);
1867 XCloseDisplay(pState->display);
1868#endif
1869
1870 RTStrFree(pState->pszExtensions);
1871 pState->pszExtensions = NULL;
1872#ifdef VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE
1873 RTStrFree(pState->pszOtherExtensions);
1874#endif
1875 pState->pszOtherExtensions = NULL;
1876
1877 return VINF_SUCCESS;
1878}
1879
1880/* Shared functions that depend on private structure definitions. */
1881#define VMSVGA3D_OPENGL
1882#include "DevVGA-SVGA3d-shared.h"
1883
1884/**
1885 * Worker for vmsvga3dQueryCaps that figures out supported operations for a
1886 * given surface format capability.
1887 *
1888 * @returns Supported/indented operations (SVGA3DFORMAT_OP_XXX).
1889 * @param pState3D The 3D state.
1890 * @param idx3dCaps The SVGA3D_CAPS_XXX value of the surface format.
1891 *
1892 * @remarks See fromat_cap_table in svga_format.c (mesa/gallium) for a reference
1893 * of implicit guest expectations:
1894 * http://cgit.freedesktop.org/mesa/mesa/tree/src/gallium/drivers/svga/svga_format.c
1895 */
1896static uint32_t vmsvga3dGetSurfaceFormatSupport(PVMSVGA3DSTATE pState3D, uint32_t idx3dCaps)
1897{
1898 uint32_t result = 0;
1899
1900 /* @todo missing:
1901 *
1902 * SVGA3DFORMAT_OP_PIXELSIZE
1903 */
1904
1905 switch (idx3dCaps)
1906 {
1907 case SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8:
1908 case SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5:
1909 case SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
1910 result |= SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB
1911 | SVGA3DFORMAT_OP_CONVERT_TO_ARGB
1912 | SVGA3DFORMAT_OP_DISPLAYMODE /* Should not be set for alpha formats. */
1913 | SVGA3DFORMAT_OP_3DACCELERATION; /* implies OP_DISPLAYMODE */
1914 break;
1915
1916 case SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8:
1917 case SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10:
1918 case SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5:
1919 case SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4:
1920 result |= SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB
1921 | SVGA3DFORMAT_OP_CONVERT_TO_ARGB
1922 | SVGA3DFORMAT_OP_SAME_FORMAT_UP_TO_ALPHA_RENDERTARGET;
1923 break;
1924 }
1925
1926 /* @todo check hardware caps! */
1927 switch (idx3dCaps)
1928 {
1929 case SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8:
1930 case SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8:
1931 case SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10:
1932 case SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5:
1933 case SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5:
1934 case SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4:
1935 case SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
1936 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE16:
1937 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8_ALPHA8:
1938 case SVGA3D_DEVCAP_SURFACEFMT_ALPHA8:
1939 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8:
1940 result |= SVGA3DFORMAT_OP_TEXTURE
1941 | SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
1942 | SVGA3DFORMAT_OP_OFFSCREENPLAIN
1943 | SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET
1944 | SVGA3DFORMAT_OP_VOLUMETEXTURE
1945 | SVGA3DFORMAT_OP_CUBETEXTURE
1946 | SVGA3DFORMAT_OP_SRGBREAD
1947 | SVGA3DFORMAT_OP_SRGBWRITE;
1948 break;
1949
1950 case SVGA3D_DEVCAP_SURFACEFMT_Z_D16:
1951 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8:
1952 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24X8:
1953 case SVGA3D_DEVCAP_SURFACEFMT_Z_DF16:
1954 case SVGA3D_DEVCAP_SURFACEFMT_Z_DF24:
1955 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8_INT:
1956 result |= SVGA3DFORMAT_OP_ZSTENCIL
1957 | SVGA3DFORMAT_OP_ZSTENCIL_WITH_ARBITRARY_COLOR_DEPTH
1958 | SVGA3DFORMAT_OP_TEXTURE /* Necessary for Ubuntu Unity */;
1959 break;
1960
1961 case SVGA3D_DEVCAP_SURFACEFMT_DXT1:
1962 case SVGA3D_DEVCAP_SURFACEFMT_DXT3:
1963 case SVGA3D_DEVCAP_SURFACEFMT_DXT5:
1964 result |= SVGA3DFORMAT_OP_TEXTURE
1965 | SVGA3DFORMAT_OP_VOLUMETEXTURE
1966 | SVGA3DFORMAT_OP_CUBETEXTURE
1967 | SVGA3DFORMAT_OP_SRGBREAD;
1968 break;
1969
1970 case SVGA3D_DEVCAP_SURFACEFMT_BUMPX8L8V8U8:
1971 case SVGA3D_DEVCAP_SURFACEFMT_A2W10V10U10:
1972 case SVGA3D_DEVCAP_SURFACEFMT_BUMPU8V8:
1973 case SVGA3D_DEVCAP_SURFACEFMT_Q8W8V8U8:
1974 case SVGA3D_DEVCAP_SURFACEFMT_CxV8U8:
1975 break;
1976
1977 case SVGA3D_DEVCAP_SURFACEFMT_R_S10E5:
1978 case SVGA3D_DEVCAP_SURFACEFMT_R_S23E8:
1979 case SVGA3D_DEVCAP_SURFACEFMT_RG_S10E5:
1980 case SVGA3D_DEVCAP_SURFACEFMT_RG_S23E8:
1981 case SVGA3D_DEVCAP_SURFACEFMT_ARGB_S10E5:
1982 case SVGA3D_DEVCAP_SURFACEFMT_ARGB_S23E8:
1983 break;
1984
1985 case SVGA3D_DEVCAP_SURFACEFMT_V16U16:
1986 case SVGA3D_DEVCAP_SURFACEFMT_G16R16:
1987 case SVGA3D_DEVCAP_SURFACEFMT_A16B16G16R16:
1988
1989 case SVGA3D_DEVCAP_SURFACEFMT_UYVY:
1990 case SVGA3D_DEVCAP_SURFACEFMT_YUY2:
1991 case SVGA3D_DEVCAP_SURFACEFMT_NV12:
1992 case SVGA3D_DEVCAP_SURFACEFMT_AYUV:
1993 break;
1994 }
1995 Log(("CAPS: %s =\n%s\n", vmsvga3dGetCapString(idx3dCaps), vmsvga3dGet3dFormatString(result)));
1996
1997 return result;
1998}
1999
2000static uint32_t vmsvga3dGetDepthFormatSupport(PVMSVGA3DSTATE pState3D, uint32_t idx3dCaps)
2001{
2002 uint32_t result = 0;
2003
2004 /* @todo test this somehow */
2005 result = SVGA3DFORMAT_OP_ZSTENCIL | SVGA3DFORMAT_OP_ZSTENCIL_WITH_ARBITRARY_COLOR_DEPTH;
2006
2007 Log(("CAPS: %s =\n%s\n", vmsvga3dGetCapString(idx3dCaps), vmsvga3dGet3dFormatString(result)));
2008 return result;
2009}
2010
2011
2012int vmsvga3dQueryCaps(PVGASTATE pThis, uint32_t idx3dCaps, uint32_t *pu32Val)
2013{
2014 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
2015 AssertReturn(pState, VERR_NO_MEMORY);
2016 int rc = VINF_SUCCESS;
2017
2018 *pu32Val = 0;
2019
2020 /*
2021 * The capabilities access by current (2015-03-01) linux sources (gallium,
2022 * vmwgfx, xorg-video-vmware) are annotated, caps without xref annotations
2023 * aren't access.
2024 */
2025
2026 switch (idx3dCaps)
2027 {
2028 /* Linux: vmwgfx_fifo.c in kmod; only used with SVGA_CAP_GBOBJECTS. */
2029 case SVGA3D_DEVCAP_3D:
2030 *pu32Val = 1; /* boolean? */
2031 break;
2032
2033 case SVGA3D_DEVCAP_MAX_LIGHTS:
2034 *pu32Val = pState->caps.maxActiveLights;
2035 break;
2036
2037 case SVGA3D_DEVCAP_MAX_TEXTURES:
2038 *pu32Val = pState->caps.maxTextures;
2039 break;
2040
2041 case SVGA3D_DEVCAP_MAX_CLIP_PLANES:
2042 *pu32Val = pState->caps.maxClipDistances;
2043 break;
2044
2045 /* Linux: svga_screen.c in gallium; 3.0 or later required. */
2046 case SVGA3D_DEVCAP_VERTEX_SHADER_VERSION:
2047 *pu32Val = pState->caps.vertexShaderVersion;
2048 break;
2049
2050 case SVGA3D_DEVCAP_VERTEX_SHADER:
2051 /* boolean? */
2052 *pu32Val = (pState->caps.vertexShaderVersion != SVGA3DVSVERSION_NONE);
2053 break;
2054
2055 /* Linux: svga_screen.c in gallium; 3.0 or later required. */
2056 case SVGA3D_DEVCAP_FRAGMENT_SHADER_VERSION:
2057 *pu32Val = pState->caps.fragmentShaderVersion;
2058 break;
2059
2060 case SVGA3D_DEVCAP_FRAGMENT_SHADER:
2061 /* boolean? */
2062 *pu32Val = (pState->caps.fragmentShaderVersion != SVGA3DPSVERSION_NONE);
2063 break;
2064
2065 case SVGA3D_DEVCAP_S23E8_TEXTURES:
2066 case SVGA3D_DEVCAP_S10E5_TEXTURES:
2067 /* Must be obsolete by now; surface format caps specify the same thing. */
2068 rc = VERR_INVALID_PARAMETER;
2069 break;
2070
2071 case SVGA3D_DEVCAP_MAX_FIXED_VERTEXBLEND:
2072 break;
2073
2074 /*
2075 * 2. The BUFFER_FORMAT capabilities are deprecated, and they always
2076 * return TRUE. Even on physical hardware that does not support
2077 * these formats natively, the SVGA3D device will provide an emulation
2078 * which should be invisible to the guest OS.
2079 */
2080 case SVGA3D_DEVCAP_D16_BUFFER_FORMAT:
2081 case SVGA3D_DEVCAP_D24S8_BUFFER_FORMAT:
2082 case SVGA3D_DEVCAP_D24X8_BUFFER_FORMAT:
2083 *pu32Val = 1;
2084 break;
2085
2086 case SVGA3D_DEVCAP_QUERY_TYPES:
2087 break;
2088
2089 case SVGA3D_DEVCAP_TEXTURE_GRADIENT_SAMPLING:
2090 break;
2091
2092 /* Linux: svga_screen.c in gallium; capped at 80.0, default 1.0. */
2093 case SVGA3D_DEVCAP_MAX_POINT_SIZE:
2094 AssertCompile(sizeof(uint32_t) == sizeof(float));
2095 *(float *)pu32Val = pState->caps.flPointSize[1];
2096 break;
2097
2098 case SVGA3D_DEVCAP_MAX_SHADER_TEXTURES:
2099 /* @todo ?? */
2100 rc = VERR_INVALID_PARAMETER;
2101 break;
2102
2103 /* Linux: svga_screen.c in gallium (for PIPE_CAP_MAX_TEXTURE_2D_LEVELS); have default if missing. */
2104 case SVGA3D_DEVCAP_MAX_TEXTURE_WIDTH:
2105 case SVGA3D_DEVCAP_MAX_TEXTURE_HEIGHT:
2106 *pu32Val = pState->caps.maxRectangleTextureSize;
2107 break;
2108
2109 /* Linux: svga_screen.c in gallium (for PIPE_CAP_MAX_TEXTURE_3D_LEVELS); have default if missing. */
2110 case SVGA3D_DEVCAP_MAX_VOLUME_EXTENT:
2111 //*pu32Val = pCaps->MaxVolumeExtent;
2112 break;
2113
2114 case SVGA3D_DEVCAP_MAX_TEXTURE_REPEAT:
2115 *pu32Val = 32768; /* hardcoded in Wine */
2116 break;
2117
2118 case SVGA3D_DEVCAP_MAX_TEXTURE_ASPECT_RATIO:
2119 //*pu32Val = pCaps->MaxTextureAspectRatio;
2120 break;
2121
2122 /* Linux: svga_screen.c in gallium (for PIPE_CAPF_MAX_TEXTURE_ANISOTROPY); defaults to 4.0. */
2123 case SVGA3D_DEVCAP_MAX_TEXTURE_ANISOTROPY:
2124 *pu32Val = pState->caps.maxTextureAnisotropy;
2125 break;
2126
2127 case SVGA3D_DEVCAP_MAX_PRIMITIVE_COUNT:
2128 case SVGA3D_DEVCAP_MAX_VERTEX_INDEX:
2129 *pu32Val = 0xFFFFF; /* hardcoded in Wine */
2130 break;
2131
2132 /* Linux: svga_screen.c in gallium (for PIPE_SHADER_VERTEX/PIPE_SHADER_CAP_MAX_INSTRUCTIONS); defaults to 512. */
2133 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_INSTRUCTIONS:
2134 *pu32Val = pState->caps.maxVertexShaderInstructions;
2135 break;
2136
2137 case SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_INSTRUCTIONS:
2138 *pu32Val = pState->caps.maxFragmentShaderInstructions;
2139 break;
2140
2141 /* Linux: svga_screen.c in gallium (for PIPE_SHADER_VERTEX/PIPE_SHADER_CAP_MAX_TEMPS); defaults to 32. */
2142 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEMPS:
2143 *pu32Val = pState->caps.maxVertexShaderTemps;
2144 break;
2145
2146 /* Linux: svga_screen.c in gallium (for PIPE_SHADER_FRAGMENT/PIPE_SHADER_CAP_MAX_TEMPS); defaults to 32. */
2147 case SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_TEMPS:
2148 *pu32Val = pState->caps.maxFragmentShaderTemps;
2149 break;
2150
2151 case SVGA3D_DEVCAP_TEXTURE_OPS:
2152 break;
2153
2154 case SVGA3D_DEVCAP_MULTISAMPLE_NONMASKABLESAMPLES:
2155 break;
2156
2157 case SVGA3D_DEVCAP_MULTISAMPLE_MASKABLESAMPLES:
2158 break;
2159
2160 case SVGA3D_DEVCAP_ALPHATOCOVERAGE:
2161 break;
2162
2163 case SVGA3D_DEVCAP_SUPERSAMPLE:
2164 break;
2165
2166 case SVGA3D_DEVCAP_AUTOGENMIPMAPS:
2167 //*pu32Val = !!(pCaps->Caps2 & D3DCAPS2_CANAUTOGENMIPMAP);
2168 break;
2169
2170 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEXTURES:
2171 break;
2172
2173 case SVGA3D_DEVCAP_MAX_RENDER_TARGETS: /* @todo same thing? */
2174 case SVGA3D_DEVCAP_MAX_SIMULTANEOUS_RENDER_TARGETS:
2175 *pu32Val = pState->caps.maxColorAttachments;
2176 break;
2177
2178 /*
2179 * This is the maximum number of SVGA context IDs that the guest
2180 * can define using SVGA_3D_CMD_CONTEXT_DEFINE.
2181 */
2182 case SVGA3D_DEVCAP_MAX_CONTEXT_IDS:
2183 *pu32Val = SVGA3D_MAX_CONTEXT_IDS;
2184 break;
2185
2186 /*
2187 * This is the maximum number of SVGA surface IDs that the guest
2188 * can define using SVGA_3D_CMD_SURFACE_DEFINE*.
2189 */
2190 case SVGA3D_DEVCAP_MAX_SURFACE_IDS:
2191 *pu32Val = SVGA3D_MAX_SURFACE_IDS;
2192 break;
2193
2194#if 0 /* Appeared more recently, not yet implemented. */
2195 /* Linux: svga_screen.c in gallium; defaults to FALSE. */
2196 case SVGA3D_DEVCAP_LINE_AA:
2197 break;
2198 /* Linux: svga_screen.c in gallium; defaults to FALSE. */
2199 case SVGA3D_DEVCAP_LINE_STIPPLE:
2200 break;
2201 /* Linux: svga_screen.c in gallium; defaults to 1.0. */
2202 case SVGA3D_DEVCAP_MAX_LINE_WIDTH:
2203 break;
2204 /* Linux: svga_screen.c in gallium; defaults to 1.0. */
2205 case SVGA3D_DEVCAP_MAX_AA_LINE_WIDTH:
2206 break;
2207#endif
2208
2209 /*
2210 * Supported surface formats.
2211 * Linux: svga_format.c in gallium, format_cap_table defines implicit expectations.
2212 */
2213 case SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8:
2214 case SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8:
2215 case SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10:
2216 case SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5:
2217 case SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5:
2218 case SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4:
2219 case SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
2220 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE16:
2221 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8_ALPHA8:
2222 case SVGA3D_DEVCAP_SURFACEFMT_ALPHA8:
2223 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8:
2224 case SVGA3D_DEVCAP_SURFACEFMT_Z_D16:
2225 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8:
2226 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24X8:
2227 case SVGA3D_DEVCAP_SURFACEFMT_Z_DF16:
2228 case SVGA3D_DEVCAP_SURFACEFMT_Z_DF24:
2229 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8_INT:
2230 case SVGA3D_DEVCAP_SURFACEFMT_DXT1:
2231 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps);
2232 break;
2233
2234 case SVGA3D_DEVCAP_SURFACEFMT_DXT2:
2235 case SVGA3D_DEVCAP_SURFACEFMT_DXT4:
2236 *pu32Val = 0; /* apparently not supported in OpenGL */
2237 break;
2238
2239 case SVGA3D_DEVCAP_SURFACEFMT_DXT3:
2240 case SVGA3D_DEVCAP_SURFACEFMT_DXT5:
2241 case SVGA3D_DEVCAP_SURFACEFMT_BUMPX8L8V8U8:
2242 case SVGA3D_DEVCAP_SURFACEFMT_A2W10V10U10:
2243 case SVGA3D_DEVCAP_SURFACEFMT_BUMPU8V8:
2244 case SVGA3D_DEVCAP_SURFACEFMT_Q8W8V8U8:
2245 case SVGA3D_DEVCAP_SURFACEFMT_CxV8U8:
2246 case SVGA3D_DEVCAP_SURFACEFMT_R_S10E5:
2247 case SVGA3D_DEVCAP_SURFACEFMT_R_S23E8:
2248 case SVGA3D_DEVCAP_SURFACEFMT_RG_S10E5:
2249 case SVGA3D_DEVCAP_SURFACEFMT_RG_S23E8:
2250 case SVGA3D_DEVCAP_SURFACEFMT_ARGB_S10E5:
2251 case SVGA3D_DEVCAP_SURFACEFMT_ARGB_S23E8:
2252 case SVGA3D_DEVCAP_SURFACEFMT_V16U16:
2253 case SVGA3D_DEVCAP_SURFACEFMT_G16R16:
2254 case SVGA3D_DEVCAP_SURFACEFMT_A16B16G16R16:
2255 case SVGA3D_DEVCAP_SURFACEFMT_UYVY:
2256 case SVGA3D_DEVCAP_SURFACEFMT_YUY2:
2257 case SVGA3D_DEVCAP_SURFACEFMT_NV12:
2258 case SVGA3D_DEVCAP_SURFACEFMT_AYUV:
2259 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps);
2260 break;
2261
2262 /* Linux: Not referenced in current sources. */
2263 case SVGA3D_DEVCAP_SURFACEFMT_BC4_UNORM:
2264 case SVGA3D_DEVCAP_SURFACEFMT_BC5_UNORM:
2265 Log(("CAPS: Unknown CAP %s\n", vmsvga3dGetCapString(idx3dCaps)));
2266 rc = VERR_INVALID_PARAMETER;
2267 *pu32Val = 0;
2268 break;
2269
2270 default:
2271 Log(("CAPS: Unexpected CAP %d\n", idx3dCaps));
2272 rc = VERR_INVALID_PARAMETER;
2273 break;
2274 }
2275
2276 Log(("CAPS: %s - %x\n", vmsvga3dGetCapString(idx3dCaps), *pu32Val));
2277 return rc;
2278}
2279
2280/**
2281 * Convert SVGA format value to its OpenGL equivalent
2282 *
2283 * @remarks Clues to be had in format_texture_info table (wined3d/utils.c) with
2284 * help from wined3dformat_from_d3dformat().
2285 */
2286static void vmsvga3dSurfaceFormat2OGL(PVMSVGA3DSURFACE pSurface, SVGA3dSurfaceFormat format)
2287{
2288 switch (format)
2289 {
2290 case SVGA3D_X8R8G8B8: /* D3DFMT_X8R8G8B8 - WINED3DFMT_B8G8R8X8_UNORM */
2291 pSurface->internalFormatGL = GL_RGB8;
2292 pSurface->formatGL = GL_BGRA;
2293 pSurface->typeGL = GL_UNSIGNED_INT_8_8_8_8_REV;
2294 break;
2295 case SVGA3D_A8R8G8B8: /* D3DFMT_A8R8G8B8 - WINED3DFMT_B8G8R8A8_UNORM */
2296 pSurface->internalFormatGL = GL_RGBA8;
2297 pSurface->formatGL = GL_BGRA;
2298 pSurface->typeGL = GL_UNSIGNED_INT_8_8_8_8_REV;
2299 break;
2300 case SVGA3D_R5G6B5: /* D3DFMT_R5G6B5 - WINED3DFMT_B5G6R5_UNORM */
2301 pSurface->internalFormatGL = GL_RGB5;
2302 pSurface->formatGL = GL_RGB;
2303 pSurface->typeGL = GL_UNSIGNED_SHORT_5_6_5;
2304 AssertMsgFailed(("Test me - SVGA3D_R5G6B5\n"));
2305 break;
2306 case SVGA3D_X1R5G5B5: /* D3DFMT_X1R5G5B5 - WINED3DFMT_B5G5R5X1_UNORM */
2307 pSurface->internalFormatGL = GL_RGB5;
2308 pSurface->formatGL = GL_BGRA;
2309 pSurface->typeGL = GL_UNSIGNED_SHORT_1_5_5_5_REV;
2310 AssertMsgFailed(("Test me - SVGA3D_X1R5G5B5\n"));
2311 break;
2312 case SVGA3D_A1R5G5B5: /* D3DFMT_A1R5G5B5 - WINED3DFMT_B5G5R5A1_UNORM */
2313 pSurface->internalFormatGL = GL_RGB5_A1;
2314 pSurface->formatGL = GL_BGRA;
2315 pSurface->typeGL = GL_UNSIGNED_SHORT_1_5_5_5_REV;
2316 AssertMsgFailed(("Test me - SVGA3D_A1R5G5B5\n"));
2317 break;
2318 case SVGA3D_A4R4G4B4: /* D3DFMT_A4R4G4B4 - WINED3DFMT_B4G4R4A4_UNORM */
2319 pSurface->internalFormatGL = GL_RGBA4;
2320 pSurface->formatGL = GL_BGRA;
2321 pSurface->typeGL = GL_UNSIGNED_SHORT_4_4_4_4_REV;
2322 AssertMsgFailed(("Test me - SVGA3D_A4R4G4B4\n"));
2323 break;
2324
2325 case SVGA3D_Z_D32: /* D3DFMT_D32 - WINED3DFMT_D32_UNORM */
2326 pSurface->internalFormatGL = GL_DEPTH_COMPONENT32;
2327 pSurface->formatGL = GL_DEPTH_COMPONENT;
2328 pSurface->typeGL = GL_UNSIGNED_INT;
2329 break;
2330 case SVGA3D_Z_D16: /* D3DFMT_D16 - WINED3DFMT_D16_UNORM */
2331 pSurface->internalFormatGL = GL_DEPTH_COMPONENT16; /** @todo Wine suggests GL_DEPTH_COMPONENT24. */
2332 pSurface->formatGL = GL_DEPTH_COMPONENT;
2333 pSurface->typeGL = GL_UNSIGNED_SHORT;
2334 AssertMsgFailed(("Test me - SVGA3D_Z_D16\n"));
2335 break;
2336 case SVGA3D_Z_D24S8: /* D3DFMT_D24S8 - WINED3DFMT_D24_UNORM_S8_UINT */
2337 pSurface->internalFormatGL = GL_DEPTH24_STENCIL8;
2338 pSurface->formatGL = GL_DEPTH_STENCIL;
2339 pSurface->typeGL = GL_UNSIGNED_INT;
2340 break;
2341 case SVGA3D_Z_D15S1: /* D3DFMT_D15S1 - WINED3DFMT_S1_UINT_D15_UNORM */
2342 pSurface->internalFormatGL = GL_DEPTH_COMPONENT16; /* @todo ??? */
2343 pSurface->formatGL = GL_DEPTH_STENCIL;
2344 pSurface->typeGL = GL_UNSIGNED_SHORT;
2345 /** @todo Wine sources hints at no hw support for this, so test this one! */
2346 AssertMsgFailed(("Test me - SVGA3D_Z_D15S1\n"));
2347 break;
2348 case SVGA3D_Z_D24X8: /* D3DFMT_D24X8 - WINED3DFMT_X8D24_UNORM */
2349 pSurface->internalFormatGL = GL_DEPTH_COMPONENT24;
2350 pSurface->formatGL = GL_DEPTH_COMPONENT;
2351 pSurface->typeGL = GL_UNSIGNED_INT;
2352 break;
2353
2354 /* Advanced D3D9 depth formats. */
2355 case SVGA3D_Z_DF16: /* D3DFMT_DF16? - not supported */
2356 pSurface->internalFormatGL = GL_DEPTH_COMPONENT16;
2357 pSurface->formatGL = GL_DEPTH_COMPONENT;
2358 pSurface->typeGL = GL_FLOAT;
2359 break;
2360
2361 case SVGA3D_Z_DF24: /* D3DFMT_DF24? - not supported */
2362 pSurface->internalFormatGL = GL_DEPTH_COMPONENT24;
2363 pSurface->formatGL = GL_DEPTH_COMPONENT;
2364 pSurface->typeGL = GL_FLOAT; /* ??? */
2365 break;
2366
2367 case SVGA3D_Z_D24S8_INT: /* D3DFMT_??? - not supported */
2368 pSurface->internalFormatGL = GL_DEPTH24_STENCIL8;
2369 pSurface->formatGL = GL_DEPTH_STENCIL;
2370 pSurface->typeGL = GL_INT; /* ??? */
2371 break;
2372
2373 case SVGA3D_DXT1: /* D3DFMT_DXT1 - WINED3DFMT_DXT1 */
2374 pSurface->internalFormatGL = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
2375#if 0
2376 pSurface->formatGL = GL_RGBA_S3TC; /* ??? */
2377 pSurface->typeGL = GL_UNSIGNED_INT; /* ??? */
2378#else /* wine suggests: */
2379 pSurface->formatGL = GL_RGBA;
2380 pSurface->typeGL = GL_UNSIGNED_BYTE;
2381 AssertMsgFailed(("Test me - SVGA3D_DXT1\n"));
2382#endif
2383 break;
2384
2385 case SVGA3D_DXT3: /* D3DFMT_DXT3 - WINED3DFMT_DXT3 */
2386 pSurface->internalFormatGL = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
2387#if 0 /** @todo this needs testing... */
2388 pSurface->formatGL = GL_RGBA_S3TC; /* ??? */
2389 pSurface->typeGL = GL_UNSIGNED_INT; /* ??? */
2390#else /* wine suggests: */
2391 pSurface->formatGL = GL_RGBA;
2392 pSurface->typeGL = GL_UNSIGNED_BYTE;
2393 AssertMsgFailed(("Test me - SVGA3D_DXT3\n"));
2394#endif
2395 break;
2396
2397 case SVGA3D_DXT5: /* D3DFMT_DXT5 - WINED3DFMT_DXT5 */
2398 pSurface->internalFormatGL = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
2399#if 0 /** @todo this needs testing... */
2400 pSurface->formatGL = GL_RGBA_S3TC;
2401 pSurface->typeGL = GL_UNSIGNED_INT;
2402#else /* wine suggests: */
2403 pSurface->formatGL = GL_RGBA;
2404 pSurface->typeGL = GL_UNSIGNED_BYTE;
2405 AssertMsgFailed(("Test me - SVGA3D_DXT5\n"));
2406#endif
2407 break;
2408
2409 case SVGA3D_LUMINANCE8: /* D3DFMT_? - ? */
2410 pSurface->internalFormatGL = GL_LUMINANCE8_EXT;
2411 pSurface->formatGL = GL_LUMINANCE;
2412 pSurface->typeGL = GL_UNSIGNED_BYTE;
2413 break;
2414
2415 case SVGA3D_LUMINANCE16: /* D3DFMT_? - ? */
2416 pSurface->internalFormatGL = GL_LUMINANCE16_EXT;
2417 pSurface->formatGL = GL_LUMINANCE;
2418 pSurface->typeGL = GL_UNSIGNED_SHORT;
2419 break;
2420
2421 case SVGA3D_LUMINANCE4_ALPHA4: /* D3DFMT_? - ? */
2422 pSurface->internalFormatGL = GL_LUMINANCE4_ALPHA4_EXT;
2423 pSurface->formatGL = GL_LUMINANCE_ALPHA;
2424 pSurface->typeGL = GL_UNSIGNED_BYTE;
2425 break;
2426
2427 case SVGA3D_LUMINANCE8_ALPHA8: /* D3DFMT_? - ? */
2428 pSurface->internalFormatGL = GL_LUMINANCE8_ALPHA8_EXT;
2429 pSurface->formatGL = GL_LUMINANCE_ALPHA;
2430 pSurface->typeGL = GL_UNSIGNED_BYTE; /* unsigned_short causes issues even though this type should be 16-bit */
2431 break;
2432
2433 case SVGA3D_ALPHA8: /* D3DFMT_A8? - WINED3DFMT_A8_UNORM? */
2434 pSurface->internalFormatGL = GL_ALPHA8_EXT;
2435 pSurface->formatGL = GL_ALPHA;
2436 pSurface->typeGL = GL_UNSIGNED_BYTE;
2437 break;
2438
2439#if 0
2440
2441 /* Bump-map formats */
2442 case SVGA3D_BUMPU8V8:
2443 return D3DFMT_V8U8;
2444 case SVGA3D_BUMPL6V5U5:
2445 return D3DFMT_L6V5U5;
2446 case SVGA3D_BUMPX8L8V8U8:
2447 return D3DFMT_X8L8V8U8;
2448 case SVGA3D_BUMPL8V8U8:
2449 /* No corresponding D3D9 equivalent. */
2450 AssertFailedReturn(D3DFMT_UNKNOWN);
2451 /* signed bump-map formats */
2452 case SVGA3D_V8U8:
2453 return D3DFMT_V8U8;
2454 case SVGA3D_Q8W8V8U8:
2455 return D3DFMT_Q8W8V8U8;
2456 case SVGA3D_CxV8U8:
2457 return D3DFMT_CxV8U8;
2458 /* mixed bump-map formats */
2459 case SVGA3D_X8L8V8U8:
2460 return D3DFMT_X8L8V8U8;
2461 case SVGA3D_A2W10V10U10:
2462 return D3DFMT_A2W10V10U10;
2463#endif
2464
2465 case SVGA3D_ARGB_S10E5: /* 16-bit floating-point ARGB */ /* D3DFMT_A16B16G16R16F - WINED3DFMT_R16G16B16A16_FLOAT */
2466 pSurface->internalFormatGL = GL_RGBA16F;
2467 pSurface->formatGL = GL_RGBA;
2468#if 0 /* bird: wine uses half float, sounds correct to me... */
2469 pSurface->typeGL = GL_FLOAT;
2470#else
2471 pSurface->typeGL = GL_HALF_FLOAT;
2472 AssertMsgFailed(("Test me - SVGA3D_ARGB_S10E5\n"));
2473#endif
2474 break;
2475
2476 case SVGA3D_ARGB_S23E8: /* 32-bit floating-point ARGB */ /* D3DFMT_A32B32G32R32F - WINED3DFMT_R32G32B32A32_FLOAT */
2477 pSurface->internalFormatGL = GL_RGBA32F;
2478 pSurface->formatGL = GL_RGBA;
2479 pSurface->typeGL = GL_FLOAT; /* ?? - same as wine, so probably correct */
2480 break;
2481
2482 case SVGA3D_A2R10G10B10: /* D3DFMT_A2R10G10B10 - WINED3DFMT_B10G10R10A2_UNORM */
2483 pSurface->internalFormatGL = GL_RGB10_A2; /* ?? - same as wine, so probably correct */
2484#if 0 /* bird: Wine uses GL_BGRA instead of GL_RGBA. */
2485 pSurface->formatGL = GL_RGBA;
2486#else
2487 pSurface->formatGL = GL_BGRA;
2488#endif
2489 pSurface->typeGL = GL_UNSIGNED_INT;
2490 AssertMsgFailed(("Test me - SVGA3D_A2R10G10B10\n"));
2491 break;
2492
2493
2494 /* Single- and dual-component floating point formats */
2495 case SVGA3D_R_S10E5: /* D3DFMT_R16F - WINED3DFMT_R16_FLOAT */
2496 pSurface->internalFormatGL = GL_R16F;
2497 pSurface->formatGL = GL_RED;
2498#if 0 /* bird: wine uses half float, sounds correct to me... */
2499 pSurface->typeGL = GL_FLOAT;
2500#else
2501 pSurface->typeGL = GL_HALF_FLOAT;
2502 AssertMsgFailed(("Test me - SVGA3D_R_S10E5\n"));
2503#endif
2504 break;
2505 case SVGA3D_R_S23E8: /* D3DFMT_R32F - WINED3DFMT_R32_FLOAT */
2506 pSurface->internalFormatGL = GL_R32F;
2507 pSurface->formatGL = GL_RG;
2508 pSurface->typeGL = GL_FLOAT;
2509 break;
2510 case SVGA3D_RG_S10E5: /* D3DFMT_G16R16F - WINED3DFMT_R16G16_FLOAT */
2511 pSurface->internalFormatGL = GL_RG16F;
2512 pSurface->formatGL = GL_RG;
2513#if 0 /* bird: wine uses half float, sounds correct to me... */
2514 pSurface->typeGL = GL_FLOAT;
2515#else
2516 pSurface->typeGL = GL_HALF_FLOAT;
2517 AssertMsgFailed(("Test me - SVGA3D_RG_S10E5\n"));
2518#endif
2519 break;
2520 case SVGA3D_RG_S23E8: /* D3DFMT_G32R32F - WINED3DFMT_R32G32_FLOAT */
2521 pSurface->internalFormatGL = GL_RG32F;
2522 pSurface->formatGL = GL_RG;
2523 pSurface->typeGL = GL_FLOAT;
2524 break;
2525
2526 /*
2527 * Any surface can be used as a buffer object, but SVGA3D_BUFFER is
2528 * the most efficient format to use when creating new surfaces
2529 * expressly for index or vertex data.
2530 */
2531 case SVGA3D_BUFFER:
2532 pSurface->internalFormatGL = -1;
2533 pSurface->formatGL = -1;
2534 pSurface->typeGL = -1;
2535 break;
2536
2537#if 0
2538 return D3DFMT_UNKNOWN;
2539
2540 case SVGA3D_V16U16:
2541 return D3DFMT_V16U16;
2542#endif
2543
2544 case SVGA3D_G16R16: /* D3DFMT_G16R16 - WINED3DFMT_R16G16_UNORM */
2545 pSurface->internalFormatGL = GL_RG16;
2546 pSurface->formatGL = GL_RG;
2547#if 0 /* bird: Wine uses GL_UNSIGNED_SHORT here. */
2548 pSurface->typeGL = GL_UNSIGNED_INT;
2549#else
2550 pSurface->typeGL = GL_UNSIGNED_SHORT;
2551 AssertMsgFailed(("test me - SVGA3D_G16R16\n"));
2552#endif
2553 break;
2554
2555 case SVGA3D_A16B16G16R16: /* D3DFMT_A16B16G16R16 - WINED3DFMT_R16G16B16A16_UNORM */
2556 pSurface->internalFormatGL = GL_RGBA16;
2557 pSurface->formatGL = GL_RGBA;
2558#if 0 /* bird: Wine uses GL_UNSIGNED_SHORT here. */
2559 pSurface->typeGL = GL_UNSIGNED_INT; /* ??? */
2560#else
2561 pSurface->typeGL = GL_UNSIGNED_SHORT;
2562 AssertMsgFailed(("Test me - SVGA3D_A16B16G16R16\n"));
2563#endif
2564 break;
2565
2566#if 0
2567 /* Packed Video formats */
2568 case SVGA3D_UYVY:
2569 return D3DFMT_UYVY;
2570 case SVGA3D_YUY2:
2571 return D3DFMT_YUY2;
2572
2573 /* Planar video formats */
2574 case SVGA3D_NV12:
2575 return (D3DFORMAT)MAKEFOURCC('N', 'V', '1', '2');
2576
2577 /* Video format with alpha */
2578 case SVGA3D_AYUV:
2579 return (D3DFORMAT)MAKEFOURCC('A', 'Y', 'U', 'V');
2580
2581 case SVGA3D_BC4_UNORM:
2582 case SVGA3D_BC5_UNORM:
2583 /* Unknown; only in DX10 & 11 */
2584 break;
2585#endif
2586 default:
2587 AssertMsgFailed(("Unsupported format %d\n", format));
2588 break;
2589 }
2590}
2591
2592
2593#if 0
2594/**
2595 * Convert SVGA multi sample count value to its D3D equivalent
2596 */
2597D3DMULTISAMPLE_TYPE vmsvga3dMultipeSampleCount2D3D(uint32_t multisampleCount)
2598{
2599 AssertCompile(D3DMULTISAMPLE_2_SAMPLES == 2);
2600 AssertCompile(D3DMULTISAMPLE_16_SAMPLES == 16);
2601
2602 if (multisampleCount > 16)
2603 return D3DMULTISAMPLE_NONE;
2604
2605 /* @todo exact same mapping as d3d? */
2606 return (D3DMULTISAMPLE_TYPE)multisampleCount;
2607}
2608#endif
2609
2610int vmsvga3dSurfaceDefine(PVGASTATE pThis, uint32_t sid, uint32_t surfaceFlags, SVGA3dSurfaceFormat format,
2611 SVGA3dSurfaceFace face[SVGA3D_MAX_SURFACE_FACES], uint32_t multisampleCount,
2612 SVGA3dTextureFilter autogenFilter, uint32_t cMipLevels, SVGA3dSize *pMipLevelSize)
2613{
2614 PVMSVGA3DSURFACE pSurface;
2615 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
2616 AssertReturn(pState, VERR_NO_MEMORY);
2617
2618 Log(("vmsvga3dSurfaceDefine: sid=%x surfaceFlags=%x format=%s (%x) multiSampleCount=%d autogenFilter=%d, cMipLevels=%d size=(%d,%d,%d)\n",
2619 sid, surfaceFlags, vmsvgaSurfaceType2String(format), format, multisampleCount, autogenFilter, cMipLevels, pMipLevelSize->width, pMipLevelSize->height, pMipLevelSize->depth));
2620
2621 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
2622 AssertReturn(cMipLevels >= 1, VERR_INVALID_PARAMETER);
2623 /* Assuming all faces have the same nr of mipmaps. */
2624 AssertReturn(!(surfaceFlags & SVGA3D_SURFACE_CUBEMAP) || cMipLevels == face[0].numMipLevels * 6, VERR_INVALID_PARAMETER);
2625 AssertReturn((surfaceFlags & SVGA3D_SURFACE_CUBEMAP) || cMipLevels == face[0].numMipLevels, VERR_INVALID_PARAMETER);
2626
2627 if (sid >= pState->cSurfaces)
2628 {
2629 /* Grow the array. */
2630 uint32_t cNew = RT_ALIGN(sid + 15, 16);
2631 void *pvNew = RTMemRealloc(pState->papSurfaces, sizeof(pState->papSurfaces[0]) * cNew);
2632 AssertReturn(pvNew, VERR_NO_MEMORY);
2633 pState->papSurfaces = (PVMSVGA3DSURFACE *)pvNew;
2634 while (pState->cSurfaces < cNew)
2635 {
2636 pSurface = (PVMSVGA3DSURFACE)RTMemAllocZ(sizeof(*pSurface));
2637 AssertReturn(pSurface, VERR_NO_MEMORY);
2638 pSurface->id = SVGA3D_INVALID_ID;
2639 pState->papSurfaces[pState->cSurfaces++] = pSurface;
2640 }
2641 }
2642 pSurface = pState->papSurfaces[sid];
2643
2644 /* If one already exists with this id, then destroy it now. */
2645 if (pSurface->id != SVGA3D_INVALID_ID)
2646 vmsvga3dSurfaceDestroy(pThis, sid);
2647
2648 memset(pSurface, 0, sizeof(*pSurface));
2649 pSurface->id = sid;
2650#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
2651 pSurface->idAssociatedContextUnused = SVGA3D_INVALID_ID;
2652#else
2653 pSurface->idAssociatedContext = SVGA3D_INVALID_ID;
2654#endif
2655 vmsvga3dSurfaceFormat2OGL(pSurface, format);
2656
2657 pSurface->oglId.buffer = OPENGL_INVALID_ID;
2658
2659 /* The surface type is sort of undefined now, even though the hints and format can help to clear that up.
2660 * In some case we'll have to wait until the surface is used to create the D3D object.
2661 */
2662 switch (format)
2663 {
2664 case SVGA3D_Z_D32:
2665 case SVGA3D_Z_D16:
2666 case SVGA3D_Z_D24S8:
2667 case SVGA3D_Z_D15S1:
2668 case SVGA3D_Z_D24X8:
2669 case SVGA3D_Z_DF16:
2670 case SVGA3D_Z_DF24:
2671 case SVGA3D_Z_D24S8_INT:
2672 surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
2673 break;
2674
2675 /* Texture compression formats */
2676 case SVGA3D_DXT1:
2677 case SVGA3D_DXT2:
2678 case SVGA3D_DXT3:
2679 case SVGA3D_DXT4:
2680 case SVGA3D_DXT5:
2681 /* Bump-map formats */
2682 case SVGA3D_BUMPU8V8:
2683 case SVGA3D_BUMPL6V5U5:
2684 case SVGA3D_BUMPX8L8V8U8:
2685 case SVGA3D_BUMPL8V8U8:
2686 case SVGA3D_V8U8:
2687 case SVGA3D_Q8W8V8U8:
2688 case SVGA3D_CxV8U8:
2689 case SVGA3D_X8L8V8U8:
2690 case SVGA3D_A2W10V10U10:
2691 case SVGA3D_V16U16:
2692 /* Typical render target formats; we should allow render target buffers to be used as textures. */
2693 case SVGA3D_X8R8G8B8:
2694 case SVGA3D_A8R8G8B8:
2695 case SVGA3D_R5G6B5:
2696 case SVGA3D_X1R5G5B5:
2697 case SVGA3D_A1R5G5B5:
2698 case SVGA3D_A4R4G4B4:
2699 surfaceFlags |= SVGA3D_SURFACE_HINT_TEXTURE;
2700 break;
2701
2702 case SVGA3D_LUMINANCE8:
2703 case SVGA3D_LUMINANCE4_ALPHA4:
2704 case SVGA3D_LUMINANCE16:
2705 case SVGA3D_LUMINANCE8_ALPHA8:
2706 case SVGA3D_ARGB_S10E5: /* 16-bit floating-point ARGB */
2707 case SVGA3D_ARGB_S23E8: /* 32-bit floating-point ARGB */
2708 case SVGA3D_A2R10G10B10:
2709 case SVGA3D_ALPHA8:
2710 case SVGA3D_R_S10E5:
2711 case SVGA3D_R_S23E8:
2712 case SVGA3D_RG_S10E5:
2713 case SVGA3D_RG_S23E8:
2714 case SVGA3D_G16R16:
2715 case SVGA3D_A16B16G16R16:
2716 case SVGA3D_UYVY:
2717 case SVGA3D_YUY2:
2718 case SVGA3D_NV12:
2719 case SVGA3D_AYUV:
2720 case SVGA3D_BC4_UNORM:
2721 case SVGA3D_BC5_UNORM:
2722 break;
2723
2724 /*
2725 * Any surface can be used as a buffer object, but SVGA3D_BUFFER is
2726 * the most efficient format to use when creating new surfaces
2727 * expressly for index or vertex data.
2728 */
2729 case SVGA3D_BUFFER:
2730 break;
2731
2732 default:
2733 break;
2734 }
2735
2736 pSurface->flags = surfaceFlags;
2737 pSurface->format = format;
2738 memcpy(pSurface->faces, face, sizeof(pSurface->faces));
2739 pSurface->cFaces = 1; /* check for cube maps later */
2740 pSurface->multiSampleCount = multisampleCount;
2741 pSurface->autogenFilter = autogenFilter;
2742 Assert(autogenFilter != SVGA3D_TEX_FILTER_FLATCUBIC);
2743 Assert(autogenFilter != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
2744 pSurface->pMipmapLevels = (PVMSVGA3DMIPMAPLEVEL)RTMemAllocZ(cMipLevels * sizeof(VMSVGA3DMIPMAPLEVEL));
2745 AssertReturn(pSurface->pMipmapLevels, VERR_NO_MEMORY);
2746
2747 for (uint32_t i=0; i < cMipLevels; i++)
2748 pSurface->pMipmapLevels[i].size = pMipLevelSize[i];
2749
2750 pSurface->cbBlock = vmsvga3dSurfaceFormatSize(format);
2751
2752 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))
2753 {
2754 case SVGA3D_SURFACE_CUBEMAP:
2755 Log(("SVGA3D_SURFACE_CUBEMAP\n"));
2756 pSurface->cFaces = 6;
2757 break;
2758
2759 case SVGA3D_SURFACE_HINT_INDEXBUFFER:
2760 Log(("SVGA3D_SURFACE_HINT_INDEXBUFFER\n"));
2761 /* else type unknown at this time; postpone buffer creation */
2762 break;
2763
2764 case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2765 Log(("SVGA3D_SURFACE_HINT_VERTEXBUFFER\n"));
2766 /* Type unknown at this time; postpone buffer creation */
2767 break;
2768
2769 case SVGA3D_SURFACE_HINT_TEXTURE:
2770 Log(("SVGA3D_SURFACE_HINT_TEXTURE\n"));
2771 break;
2772
2773 case SVGA3D_SURFACE_HINT_RENDERTARGET:
2774 Log(("SVGA3D_SURFACE_HINT_RENDERTARGET\n"));
2775 break;
2776
2777 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
2778 Log(("SVGA3D_SURFACE_HINT_DEPTHSTENCIL\n"));
2779 break;
2780
2781 default:
2782 /* Unknown; decide later. */
2783 break;
2784 }
2785
2786 /* Allocate buffer to hold the surface data until we can move it into a D3D object */
2787 for (uint32_t iFace=0; iFace < pSurface->cFaces; iFace++)
2788 {
2789 for (uint32_t i=0; i < pSurface->faces[iFace].numMipLevels; i++)
2790 {
2791 uint32_t idx = i + iFace * pSurface->faces[0].numMipLevels;
2792
2793 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));
2794 Log(("vmsvga3dSurfaceDefine: cbPitch=%x cbBlock=%x \n", pSurface->cbBlock * pSurface->pMipmapLevels[idx].size.width, pSurface->cbBlock));
2795
2796 pSurface->pMipmapLevels[idx].cbSurfacePitch = pSurface->cbBlock * pSurface->pMipmapLevels[idx].size.width;
2797 pSurface->pMipmapLevels[idx].cbSurface = pSurface->pMipmapLevels[idx].cbSurfacePitch * pSurface->pMipmapLevels[idx].size.height * pSurface->pMipmapLevels[idx].size.depth;
2798 pSurface->pMipmapLevels[idx].pSurfaceData = RTMemAllocZ(pSurface->pMipmapLevels[idx].cbSurface);
2799 AssertReturn(pSurface->pMipmapLevels[idx].pSurfaceData, VERR_NO_MEMORY);
2800 }
2801 }
2802 return VINF_SUCCESS;
2803}
2804
2805int vmsvga3dSurfaceDestroy(PVGASTATE pThis, uint32_t sid)
2806{
2807 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
2808 AssertReturn(pState, VERR_NO_MEMORY);
2809
2810 if ( sid < pState->cSurfaces
2811 && pState->papSurfaces[sid]->id == sid)
2812 {
2813 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
2814 PVMSVGA3DCONTEXT pContext;
2815
2816 Log(("vmsvga3dSurfaceDestroy id %x\n", sid));
2817
2818#if 1 /* Windows is doing this, guess it makes sense here as well... */
2819 /* Check all contexts if this surface is used as a render target or active texture. */
2820 for (uint32_t cid = 0; cid < pState->cContexts; cid++)
2821 {
2822 pContext = pState->papContexts[cid];
2823 if (pContext->id == cid)
2824 {
2825 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTexture); i++)
2826 if (pContext->aSidActiveTexture[i] == sid)
2827 pContext->aSidActiveTexture[i] = SVGA3D_INVALID_ID;
2828 if (pContext->sidRenderTarget == sid)
2829 pContext->sidRenderTarget = SVGA3D_INVALID_ID;
2830 }
2831 }
2832#endif
2833
2834#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
2835 pContext = &pState->SharedCtx;
2836 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2837#else
2838 /* @todo stricter checks for associated context */
2839 uint32_t cid = pSurface->idAssociatedContext;
2840 if ( cid <= pState->cContexts
2841 && pState->papContexts[cid]->id == cid)
2842 {
2843 pContext = pState->papContexts[cid];
2844 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2845 }
2846 /* If there is a GL buffer or something associated with the surface, we
2847 really need something here, so pick any active context. */
2848 else if (pSurface->oglId.buffer != OPENGL_INVALID_ID)
2849 {
2850 for (cid = 0; cid < pState->cContexts; cid++)
2851 {
2852 if (pState->papContexts[cid]->id == cid)
2853 {
2854 pContext = pState->papContexts[cid];
2855 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2856 break;
2857 }
2858 }
2859 AssertReturn(pContext, VERR_INTERNAL_ERROR); /* otherwise crashes/fails; create temp context if this ever triggers! */
2860 }
2861#endif
2862
2863 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))
2864 {
2865 case SVGA3D_SURFACE_CUBEMAP:
2866 AssertFailed(); /* @todo */
2867 break;
2868
2869 case SVGA3D_SURFACE_HINT_INDEXBUFFER:
2870 case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2871 if (pSurface->oglId.buffer != OPENGL_INVALID_ID)
2872 {
2873 pState->ext.glDeleteBuffers(1, &pSurface->oglId.buffer);
2874 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2875 }
2876 break;
2877
2878 case SVGA3D_SURFACE_HINT_TEXTURE:
2879 case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2880 if (pSurface->oglId.texture != OPENGL_INVALID_ID)
2881 {
2882 glDeleteTextures(1, &pSurface->oglId.texture);
2883 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2884 }
2885 break;
2886
2887 case SVGA3D_SURFACE_HINT_RENDERTARGET:
2888 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
2889 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL | SVGA3D_SURFACE_HINT_TEXTURE: /* @todo actual texture surface not supported */
2890 if (pSurface->oglId.renderbuffer != OPENGL_INVALID_ID)
2891 {
2892 pState->ext.glDeleteRenderbuffers(1, &pSurface->oglId.renderbuffer);
2893 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2894 }
2895 break;
2896
2897 default:
2898 break;
2899 }
2900
2901 if (pSurface->pMipmapLevels)
2902 {
2903 for (uint32_t face=0; face < pSurface->cFaces; face++)
2904 {
2905 for (uint32_t i=0; i < pSurface->faces[face].numMipLevels; i++)
2906 {
2907 uint32_t idx = i + face * pSurface->faces[0].numMipLevels;
2908 if (pSurface->pMipmapLevels[idx].pSurfaceData)
2909 RTMemFree(pSurface->pMipmapLevels[idx].pSurfaceData);
2910 }
2911 }
2912 RTMemFree(pSurface->pMipmapLevels);
2913 }
2914
2915 memset(pSurface, 0, sizeof(*pSurface));
2916 pSurface->id = SVGA3D_INVALID_ID;
2917 }
2918 else
2919 AssertFailedReturn(VERR_INVALID_PARAMETER);
2920
2921 return VINF_SUCCESS;
2922}
2923
2924int vmsvga3dSurfaceCopy(PVGASTATE pThis, SVGA3dSurfaceImageId dest, SVGA3dSurfaceImageId src, uint32_t cCopyBoxes, SVGA3dCopyBox *pBox)
2925{
2926 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
2927 uint32_t sidSrc = src.sid;
2928 uint32_t sidDest = dest.sid;
2929 int rc = VINF_SUCCESS;
2930
2931 AssertReturn(pState, VERR_NO_MEMORY);
2932 AssertReturn(sidSrc < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
2933 AssertReturn(sidSrc < pState->cSurfaces && pState->papSurfaces[sidSrc]->id == sidSrc, VERR_INVALID_PARAMETER);
2934 AssertReturn(sidDest < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
2935 AssertReturn(sidDest < pState->cSurfaces && pState->papSurfaces[sidDest]->id == sidDest, VERR_INVALID_PARAMETER);
2936
2937 for (uint32_t i = 0; i < cCopyBoxes; i++)
2938 {
2939 SVGA3dBox destBox, srcBox;
2940
2941 srcBox.x = pBox[i].srcx;
2942 srcBox.y = pBox[i].srcy;
2943 srcBox.z = pBox[i].srcz;
2944 srcBox.w = pBox[i].w;
2945 srcBox.h = pBox[i].h;
2946 srcBox.d = pBox[i].z;
2947
2948 destBox.x = pBox[i].x;
2949 destBox.y = pBox[i].y;
2950 destBox.z = pBox[i].z;
2951 destBox.w = pBox[i].w;
2952 destBox.h = pBox[i].h;
2953 destBox.z = pBox[i].z;
2954
2955 rc = vmsvga3dSurfaceStretchBlt(pThis, dest, destBox, src, srcBox, SVGA3D_STRETCH_BLT_LINEAR);
2956 AssertRCReturn(rc, rc);
2957 }
2958 return VINF_SUCCESS;
2959}
2960
2961
2962/**
2963 * Save texture unpacking parameters and loads those appropriate for the given
2964 * surface.
2965 *
2966 * @param pState The VMSVGA3D state structure.
2967 * @param pContext The active context.
2968 * @param pSurface The surface.
2969 * @param pSave Where to save stuff.
2970 */
2971static void vmsvga3dSetUnpackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
2972 PVMSVGAPACKPARAMS pSave)
2973{
2974 /*
2975 * Save (ignore errors, setting the defaults we want and avoids restore).
2976 */
2977 pSave->iAlignment = 1;
2978 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_UNPACK_ALIGNMENT, &pSave->iAlignment), pState, pContext);
2979 pSave->cxRow = 0;
2980 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_UNPACK_ROW_LENGTH, &pSave->cxRow), pState, pContext);
2981
2982#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
2983 pSave->cyImage = 0;
2984 glGetIntegerv(GL_UNPACK_IMAGE_HEIGHT, &pSave->cyImage);
2985 Assert(pSave->cyImage == 0);
2986
2987 pSave->fSwapBytes = GL_FALSE;
2988 glGetBooleanv(GL_UNPACK_SWAP_BYTES, &pSave->fSwapBytes);
2989 Assert(pSave->fSwapBytes == GL_FALSE);
2990
2991 pSave->fLsbFirst = GL_FALSE;
2992 glGetBooleanv(GL_UNPACK_LSB_FIRST, &pSave->fLsbFirst);
2993 Assert(pSave->fLsbFirst == GL_FALSE);
2994
2995 pSave->cSkipRows = 0;
2996 glGetIntegerv(GL_UNPACK_SKIP_ROWS, &pSave->cSkipRows);
2997 Assert(pSave->cSkipRows == 0);
2998
2999 pSave->cSkipPixels = 0;
3000 glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &pSave->cSkipPixels);
3001 Assert(pSave->cSkipPixels == 0);
3002
3003 pSave->cSkipImages = 0;
3004 glGetIntegerv(GL_UNPACK_SKIP_IMAGES, &pSave->cSkipImages);
3005 Assert(pSave->cSkipImages == 0);
3006
3007 VMSVGA3D_CLEAR_GL_ERRORS();
3008#endif
3009
3010 /*
3011 * Setup unpack.
3012 *
3013 * Note! We use 1 as alignment here because we currently don't do any
3014 * aligning of line pitches anywhere.
3015 */
3016 NOREF(pSurface);
3017 if (pSave->iAlignment != 1)
3018 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_ALIGNMENT, 1), pState, pContext);
3019 if (pSave->cxRow != 0)
3020 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0), pState, pContext);
3021#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
3022 if (pSave->cyImage != 0)
3023 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0), pState, pContext);
3024 if (pSave->fSwapBytes != 0)
3025 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE), pState, pContext);
3026 if (pSave->fLsbFirst != 0)
3027 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE), pState, pContext);
3028 if (pSave->cSkipRows != 0)
3029 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_ROWS, 0), pState, pContext);
3030 if (pSave->cSkipPixels != 0)
3031 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0), pState, pContext);
3032 if (pSave->cSkipImages != 0)
3033 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0), pState, pContext);
3034#endif
3035}
3036
3037
3038/**
3039 * Restores texture unpacking parameters.
3040 *
3041 * @param pState The VMSVGA3D state structure.
3042 * @param pContext The active context.
3043 * @param pSurface The surface.
3044 * @param pSave Where stuff was saved.
3045 */
3046static void vmsvga3dRestoreUnpackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
3047 PCVMSVGAPACKPARAMS pSave)
3048{
3049 NOREF(pSurface);
3050 if (pSave->iAlignment != 1)
3051 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_ALIGNMENT, pSave->iAlignment), pState, pContext);
3052 if (pSave->cxRow != 0)
3053 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, pSave->cxRow), pState, pContext);
3054#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
3055 if (pSave->cyImage != 0)
3056 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, pSave->cyImage), pState, pContext);
3057 if (pSave->fSwapBytes != 0)
3058 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SWAP_BYTES, pSave->fSwapBytes), pState, pContext);
3059 if (pSave->fLsbFirst != 0)
3060 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_LSB_FIRST, pSave->fLsbFirst), pState, pContext);
3061 if (pSave->cSkipRows != 0)
3062 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_ROWS, pSave->cSkipRows), pState, pContext);
3063 if (pSave->cSkipPixels != 0)
3064 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS, pSave->cSkipPixels), pState, pContext);
3065 if (pSave->cSkipImages != 0)
3066 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_IMAGES, pSave->cSkipImages), pState, pContext);
3067#endif
3068}
3069
3070
3071/* Create D3D texture object for the specified surface. */
3072static int vmsvga3dCreateTexture(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t idAssociatedContext,
3073 PVMSVGA3DSURFACE pSurface)
3074{
3075 GLint activeTexture = 0;
3076#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
3077 uint32_t idPrevCtx = pState->idActiveContext;
3078 pContext = &pState->SharedCtx;
3079 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3080#endif
3081
3082 glGenTextures(1, &pSurface->oglId.texture);
3083 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3084 /* @todo Set the mip map generation filter settings. */
3085
3086 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
3087 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3088
3089 /* Must bind texture to the current context in order to change it. */
3090 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
3091 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3092
3093 /* Set the unpacking parameters. */
3094 VMSVGAPACKPARAMS SavedParams;
3095 vmsvga3dSetUnpackParams(pState, pContext, pSurface, &SavedParams);
3096
3097 if (pSurface->fDirty)
3098 {
3099 Log(("vmsvga3dCreateTexture: sync dirty texture\n"));
3100 for (uint32_t i = 0; i < pSurface->faces[0].numMipLevels; i++)
3101 {
3102 /* Paranoia: Always do level 0 here to mirror the non-dirty case. */
3103 if (pSurface->pMipmapLevels[i].fDirty || i == 0)
3104 {
3105 if (pSurface->pMipmapLevels[i].fDirty)
3106 Log(("vmsvga3dCreateTexture: sync dirty texture mipmap level %d (pitch %x)\n", i, pSurface->pMipmapLevels[i].cbSurfacePitch));
3107
3108 glTexImage2D(GL_TEXTURE_2D,
3109 i,
3110 pSurface->internalFormatGL,
3111 pSurface->pMipmapLevels[i].size.width,
3112 pSurface->pMipmapLevels[i].size.height,
3113 0,
3114 pSurface->formatGL,
3115 pSurface->typeGL,
3116 pSurface->pMipmapLevels[i].pSurfaceData);
3117
3118 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3119
3120 pSurface->pMipmapLevels[i].fDirty = false;
3121 }
3122 }
3123 pSurface->fDirty = false;
3124 }
3125 else
3126 {
3127 /* Allocate and initialize texture memory. Passing the zero filled pSurfaceData avoids
3128 exposing random host memory to the guest and helps a with the fedora 21 surface
3129 corruption issues (launchpad, background, search field, login). */
3130 glTexImage2D(GL_TEXTURE_2D,
3131 0,
3132 pSurface->internalFormatGL,
3133 pSurface->pMipmapLevels[0].size.width,
3134 pSurface->pMipmapLevels[0].size.height,
3135 0,
3136 pSurface->formatGL,
3137 pSurface->typeGL,
3138 pSurface->pMipmapLevels[0].pSurfaceData);
3139 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3140 }
3141
3142 /* Restore unpacking parameters. */
3143 vmsvga3dRestoreUnpackParams(pState, pContext, pSurface, &SavedParams);
3144
3145 /* Restore the old active texture. */
3146 glBindTexture(GL_TEXTURE_2D, activeTexture);
3147 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3148
3149 pSurface->flags |= SVGA3D_SURFACE_HINT_TEXTURE;
3150#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
3151 LogFlow(("vmsvga3dCreateTexture: sid=%x idAssociatedContext %#x -> %#x; oglId.texture=%#x\n",
3152 pSurface->id, pSurface->idAssociatedContext, idAssociatedContext, pSurface->oglId.texture));
3153 pSurface->idAssociatedContext = idAssociatedContext;
3154#endif
3155
3156#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
3157 if (idPrevCtx < pState->cContexts && pState->papContexts[idPrevCtx]->id == idPrevCtx)
3158 {
3159 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pState->papContexts[idPrevCtx]);
3160 }
3161#endif
3162 return VINF_SUCCESS;
3163}
3164
3165int vmsvga3dSurfaceStretchBlt(PVGASTATE pThis, SVGA3dSurfaceImageId dest, SVGA3dBox destBox,
3166 SVGA3dSurfaceImageId src, SVGA3dBox srcBox, SVGA3dStretchBltMode mode)
3167{
3168 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
3169 PVMSVGA3DSURFACE pSurfaceSrc;
3170 uint32_t sidSrc = src.sid;
3171 PVMSVGA3DSURFACE pSurfaceDest;
3172 uint32_t sidDest = dest.sid;
3173 int rc = VINF_SUCCESS;
3174 uint32_t cid;
3175 PVMSVGA3DCONTEXT pContext;
3176
3177 AssertReturn(pState, VERR_NO_MEMORY);
3178 AssertReturn(sidSrc < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
3179 AssertReturn(sidSrc < pState->cSurfaces && pState->papSurfaces[sidSrc]->id == sidSrc, VERR_INVALID_PARAMETER);
3180 AssertReturn(sidDest < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
3181 AssertReturn(sidDest < pState->cSurfaces && pState->papSurfaces[sidDest]->id == sidDest, VERR_INVALID_PARAMETER);
3182
3183 pSurfaceSrc = pState->papSurfaces[sidSrc];
3184 pSurfaceDest = pState->papSurfaces[sidDest];
3185 AssertReturn(pSurfaceSrc->faces[0].numMipLevels > src.mipmap, VERR_INVALID_PARAMETER);
3186 AssertReturn(pSurfaceDest->faces[0].numMipLevels > dest.mipmap, VERR_INVALID_PARAMETER);
3187
3188#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
3189 Log(("vmsvga3dSurfaceStretchBlt: src sid=%x (%d,%d)(%d,%d) dest sid=%x (%d,%d)(%d,%d) mode=%x\n",
3190 src.sid, srcBox.x, srcBox.y, srcBox.x + srcBox.w, srcBox.y + srcBox.h,
3191 dest.sid, destBox.x, destBox.y, destBox.x + destBox.w, destBox.y + destBox.h, mode));
3192 cid = SVGA3D_INVALID_ID;
3193 pContext = &pState->SharedCtx;
3194 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3195#else
3196 Log(("vmsvga3dSurfaceStretchBlt: src sid=%x cid=%x (%d,%d)(%d,%d) dest sid=%x cid=%x (%d,%d)(%d,%d) mode=%x\n",
3197 src.sid, pSurfaceSrc->idAssociatedContext, srcBox.x, srcBox.y, srcBox.x + srcBox.w, srcBox.y + srcBox.h,
3198 dest.sid, pSurfaceDest->idAssociatedContext, destBox.x, destBox.y, destBox.x + destBox.w, destBox.y + destBox.h, mode));
3199
3200 /* @todo stricter checks for associated context */
3201 cid = pSurfaceDest->idAssociatedContext;
3202 if (cid == SVGA3D_INVALID_ID)
3203 cid = pSurfaceSrc->idAssociatedContext;
3204
3205 if ( cid >= pState->cContexts
3206 || pState->papContexts[cid]->id != cid)
3207 {
3208 Log(("vmsvga3dSurfaceStretchBlt invalid context id!\n"));
3209 AssertFailedReturn(VERR_INVALID_PARAMETER);
3210 }
3211 pContext = pState->papContexts[cid];
3212 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3213#endif
3214
3215 if (pSurfaceSrc->oglId.texture == OPENGL_INVALID_ID)
3216 {
3217 /* Unknown surface type; turn it into a texture, which can be used for other purposes too. */
3218 Log(("vmsvga3dSurfaceStretchBlt: unknown src surface id=%x type=%d format=%d -> create texture\n", sidSrc, pSurfaceSrc->flags, pSurfaceSrc->format));
3219 rc = vmsvga3dCreateTexture(pState, pContext, cid, pSurfaceSrc);
3220 AssertRCReturn(rc, rc);
3221 }
3222
3223 if (pSurfaceDest->oglId.texture == OPENGL_INVALID_ID)
3224 {
3225 /* Unknown surface type; turn it into a texture, which can be used for other purposes too. */
3226 Log(("vmsvga3dSurfaceStretchBlt: unknown dest surface id=%x type=%d format=%d -> create texture\n", sidDest, pSurfaceDest->flags, pSurfaceDest->format));
3227 rc = vmsvga3dCreateTexture(pState, pContext, cid, pSurfaceDest);
3228 AssertRCReturn(rc, rc);
3229 }
3230
3231 /* Activate the read and draw framebuffer objects. */
3232 pState->ext.glBindFramebuffer(GL_READ_FRAMEBUFFER, pContext->idReadFramebuffer);
3233 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3234 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, pContext->idDrawFramebuffer);
3235 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3236
3237 /* Bind the source and destination objects to the right place. */
3238 pState->ext.glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
3239 pSurfaceSrc->oglId.texture, src.mipmap);
3240 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3241 pState->ext.glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
3242 pSurfaceDest->oglId.texture, dest.mipmap);
3243 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3244
3245 Log(("src conv. (%d,%d)(%d,%d); dest conv (%d,%d)(%d,%d)\n", srcBox.x, D3D_TO_OGL_Y_COORD(pSurfaceSrc, srcBox.y + srcBox.h),
3246 srcBox.x + srcBox.w, D3D_TO_OGL_Y_COORD(pSurfaceSrc, srcBox.y), destBox.x, D3D_TO_OGL_Y_COORD(pSurfaceDest, destBox.y + destBox.h),
3247 destBox.x + destBox.w, D3D_TO_OGL_Y_COORD(pSurfaceDest, destBox.y)));
3248
3249 pState->ext.glBlitFramebuffer(srcBox.x,
3250#ifdef MANUAL_FLIP_SURFACE_DATA
3251 D3D_TO_OGL_Y_COORD(pSurfaceSrc, srcBox.y + srcBox.h), /* inclusive */
3252#else
3253 srcBox.y,
3254#endif
3255 srcBox.x + srcBox.w, /* exclusive. */
3256#ifdef MANUAL_FLIP_SURFACE_DATA
3257 D3D_TO_OGL_Y_COORD(pSurfaceSrc, srcBox.y), /* exclusive */
3258#else
3259 srcBox.y + srcBox.h,
3260#endif
3261 destBox.x,
3262#ifdef MANUAL_FLIP_SURFACE_DATA
3263 D3D_TO_OGL_Y_COORD(pSurfaceDest, destBox.y + destBox.h), /* inclusive. */
3264#else
3265 destBox.y,
3266#endif
3267 destBox.x + destBox.w, /* exclusive. */
3268#ifdef MANUAL_FLIP_SURFACE_DATA
3269 D3D_TO_OGL_Y_COORD(pSurfaceDest, destBox.y), /* exclusive */
3270#else
3271 destBox.y + destBox.h,
3272#endif
3273 GL_COLOR_BUFFER_BIT,
3274 (mode == SVGA3D_STRETCH_BLT_POINT) ? GL_NEAREST : GL_LINEAR);
3275 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3276
3277 /* Reset the frame buffer association */
3278 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
3279 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3280
3281 return VINF_SUCCESS;
3282}
3283
3284/**
3285 * Save texture packing parameters and loads those appropriate for the given
3286 * surface.
3287 *
3288 * @param pState The VMSVGA3D state structure.
3289 * @param pContext The active context.
3290 * @param pSurface The surface.
3291 * @param pSave Where to save stuff.
3292 */
3293static void vmsvga3dSetPackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
3294 PVMSVGAPACKPARAMS pSave)
3295{
3296 /*
3297 * Save (ignore errors, setting the defaults we want and avoids restore).
3298 */
3299 pSave->iAlignment = 1;
3300 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_UNPACK_ALIGNMENT, &pSave->iAlignment), pState, pContext);
3301 pSave->cxRow = 0;
3302 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_UNPACK_ROW_LENGTH, &pSave->cxRow), pState, pContext);
3303
3304#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
3305 pSave->cyImage = 0;
3306 glGetIntegerv(GL_PACK_IMAGE_HEIGHT, &pSave->cyImage);
3307 Assert(pSave->cyImage == 0);
3308
3309 pSave->fSwapBytes = GL_FALSE;
3310 glGetBooleanv(GL_PACK_SWAP_BYTES, &pSave->fSwapBytes);
3311 Assert(pSave->fSwapBytes == GL_FALSE);
3312
3313 pSave->fLsbFirst = GL_FALSE;
3314 glGetBooleanv(GL_PACK_LSB_FIRST, &pSave->fLsbFirst);
3315 Assert(pSave->fLsbFirst == GL_FALSE);
3316
3317 pSave->cSkipRows = 0;
3318 glGetIntegerv(GL_PACK_SKIP_ROWS, &pSave->cSkipRows);
3319 Assert(pSave->cSkipRows == 0);
3320
3321 pSave->cSkipPixels = 0;
3322 glGetIntegerv(GL_PACK_SKIP_PIXELS, &pSave->cSkipPixels);
3323 Assert(pSave->cSkipPixels == 0);
3324
3325 pSave->cSkipImages = 0;
3326 glGetIntegerv(GL_PACK_SKIP_IMAGES, &pSave->cSkipImages);
3327 Assert(pSave->cSkipImages == 0);
3328
3329 VMSVGA3D_CLEAR_GL_ERRORS();
3330#endif
3331
3332 /*
3333 * Setup unpack.
3334 *
3335 * Note! We use 1 as alignment here because we currently don't do any
3336 * aligning of line pitches anywhere.
3337 */
3338 NOREF(pSurface);
3339 if (pSave->iAlignment != 1)
3340 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ALIGNMENT, 1), pState, pContext);
3341 if (pSave->cxRow != 0)
3342 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ROW_LENGTH, 0), pState, pContext);
3343#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
3344 if (pSave->cyImage != 0)
3345 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0), pState, pContext);
3346 if (pSave->fSwapBytes != 0)
3347 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE), pState, pContext);
3348 if (pSave->fLsbFirst != 0)
3349 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE), pState, pContext);
3350 if (pSave->cSkipRows != 0)
3351 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_ROWS, 0), pState, pContext);
3352 if (pSave->cSkipPixels != 0)
3353 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_PIXELS, 0), pState, pContext);
3354 if (pSave->cSkipImages != 0)
3355 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_IMAGES, 0), pState, pContext);
3356#endif
3357}
3358
3359
3360/**
3361 * Restores texture packing parameters.
3362 *
3363 * @param pState The VMSVGA3D state structure.
3364 * @param pContext The active context.
3365 * @param pSurface The surface.
3366 * @param pSave Where stuff was saved.
3367 */
3368static void vmsvga3dRestorePackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
3369 PCVMSVGAPACKPARAMS pSave)
3370{
3371 NOREF(pSurface);
3372 if (pSave->iAlignment != 1)
3373 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ALIGNMENT, pSave->iAlignment), pState, pContext);
3374 if (pSave->cxRow != 0)
3375 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ROW_LENGTH, pSave->cxRow), pState, pContext);
3376#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
3377 if (pSave->cyImage != 0)
3378 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_IMAGE_HEIGHT, pSave->cyImage), pState, pContext);
3379 if (pSave->fSwapBytes != 0)
3380 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SWAP_BYTES, pSave->fSwapBytes), pState, pContext);
3381 if (pSave->fLsbFirst != 0)
3382 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_LSB_FIRST, pSave->fLsbFirst), pState, pContext);
3383 if (pSave->cSkipRows != 0)
3384 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_ROWS, pSave->cSkipRows), pState, pContext);
3385 if (pSave->cSkipPixels != 0)
3386 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_PIXELS, pSave->cSkipPixels), pState, pContext);
3387 if (pSave->cSkipImages != 0)
3388 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_IMAGES, pSave->cSkipImages), pState, pContext);
3389#endif
3390}
3391
3392
3393int vmsvga3dSurfaceDMA(PVGASTATE pThis, SVGA3dGuestImage guest, SVGA3dSurfaceImageId host, SVGA3dTransferType transfer,
3394 uint32_t cCopyBoxes, SVGA3dCopyBox *pBoxes)
3395{
3396 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
3397 PVMSVGA3DSURFACE pSurface;
3398 PVMSVGA3DMIPMAPLEVEL pMipLevel;
3399 uint32_t sid = host.sid;
3400 int rc = VINF_SUCCESS;
3401
3402 AssertReturn(pState, VERR_NO_MEMORY);
3403 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
3404 AssertReturn(sid < pState->cSurfaces && pState->papSurfaces[sid]->id == sid, VERR_INVALID_PARAMETER);
3405
3406 pSurface = pState->papSurfaces[sid];
3407 AssertReturn(pSurface->faces[0].numMipLevels > host.mipmap, VERR_INVALID_PARAMETER);
3408 pMipLevel = &pSurface->pMipmapLevels[host.mipmap];
3409
3410 if (pSurface->flags & SVGA3D_SURFACE_HINT_TEXTURE)
3411 Log(("vmsvga3dSurfaceDMA TEXTURE guestptr gmr=%x offset=%x pitch=%x host sid=%x face=%d mipmap=%d transfer=%s cCopyBoxes=%d\n", guest.ptr.gmrId, guest.ptr.offset, guest.pitch, host.sid, host.face, host.mipmap, (transfer == SVGA3D_WRITE_HOST_VRAM) ? "READ" : "WRITE", cCopyBoxes));
3412 else
3413 Log(("vmsvga3dSurfaceDMA guestptr gmr=%x offset=%x pitch=%x host sid=%x face=%d mipmap=%d transfer=%s cCopyBoxes=%d\n", guest.ptr.gmrId, guest.ptr.offset, guest.pitch, host.sid, host.face, host.mipmap, (transfer == SVGA3D_WRITE_HOST_VRAM) ? "READ" : "WRITE", cCopyBoxes));
3414
3415 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
3416 {
3417 AssertReturn(pSurface->pMipmapLevels[host.mipmap].pSurfaceData, VERR_INTERNAL_ERROR);
3418
3419 for (unsigned i = 0; i < cCopyBoxes; i++)
3420 {
3421 unsigned uDestOffset;
3422 unsigned cbSrcPitch;
3423 uint8_t *pBufferStart;
3424
3425 Log(("Copy box %d (%d,%d,%d)(%d,%d,%d) dest (%d,%d)\n", i, pBoxes[i].srcx, pBoxes[i].srcy, pBoxes[i].srcz, pBoxes[i].w, pBoxes[i].h, pBoxes[i].d, pBoxes[i].x, pBoxes[i].y));
3426 /* Apparently we're supposed to clip it (gmr test sample) */
3427 if (pBoxes[i].x + pBoxes[i].w > pMipLevel->size.width)
3428 pBoxes[i].w = pMipLevel->size.width - pBoxes[i].x;
3429 if (pBoxes[i].y + pBoxes[i].h > pMipLevel->size.height)
3430 pBoxes[i].h = pMipLevel->size.height - pBoxes[i].y;
3431 if (pBoxes[i].z + pBoxes[i].d > pMipLevel->size.depth)
3432 pBoxes[i].d = pMipLevel->size.depth - pBoxes[i].z;
3433
3434 if ( !pBoxes[i].w
3435 || !pBoxes[i].h
3436 || !pBoxes[i].d
3437 || pBoxes[i].x > pMipLevel->size.width
3438 || pBoxes[i].y > pMipLevel->size.height
3439 || pBoxes[i].z > pMipLevel->size.depth)
3440 {
3441 Log(("Empty box; skip\n"));
3442 continue;
3443 }
3444
3445 uDestOffset = pBoxes[i].x * pSurface->cbBlock + pBoxes[i].y * pMipLevel->cbSurfacePitch + pBoxes[i].z * pMipLevel->size.height * pMipLevel->cbSurfacePitch;
3446 AssertReturn(uDestOffset + pBoxes[i].w * pSurface->cbBlock * pBoxes[i].h * pBoxes[i].d <= pMipLevel->cbSurface, VERR_INTERNAL_ERROR);
3447
3448 cbSrcPitch = (guest.pitch == 0) ? pBoxes[i].w * pSurface->cbBlock : guest.pitch;
3449#ifdef MANUAL_FLIP_SURFACE_DATA
3450 pBufferStart = (uint8_t *)pMipLevel->pSurfaceData
3451 + pBoxes[i].x * pSurface->cbBlock
3452 + pMipLevel->cbSurface - pBoxes[i].y * pMipLevel->cbSurfacePitch
3453 - pMipLevel->cbSurfacePitch; /* flip image during copy */
3454#else
3455 pBufferStart = (uint8_t *)pMipLevel->pSurfaceData + uDestOffset;
3456#endif
3457 rc = vmsvgaGMRTransfer(pThis,
3458 transfer,
3459 pBufferStart,
3460#ifdef MANUAL_FLIP_SURFACE_DATA
3461 -(int32_t)pMipLevel->cbSurfacePitch,
3462#else
3463 (int32_t)pMipLevel->cbSurfacePitch,
3464#endif
3465 guest.ptr,
3466 pBoxes[i].srcx * pSurface->cbBlock + (pBoxes[i].srcy + pBoxes[i].srcz * pBoxes[i].h) * cbSrcPitch,
3467 cbSrcPitch,
3468 pBoxes[i].w * pSurface->cbBlock,
3469 pBoxes[i].d * pBoxes[i].h);
3470
3471 Log4(("first line:\n%.*Rhxd\n", pMipLevel->cbSurface, pMipLevel->pSurfaceData));
3472
3473 AssertRC(rc);
3474 }
3475 pSurface->pMipmapLevels[host.mipmap].fDirty = true;
3476 pSurface->fDirty = true;
3477 }
3478 else
3479 {
3480#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
3481 PVMSVGA3DCONTEXT pContext = &pState->SharedCtx;
3482#else
3483 /* @todo stricter checks for associated context */
3484 uint32_t cid = pSurface->idAssociatedContext;
3485 if ( cid >= pState->cContexts
3486 || pState->papContexts[cid]->id != cid)
3487 {
3488 Log(("vmsvga3dSurfaceDMA invalid context id (%x - %x)!\n", cid, (cid >= pState->cContexts) ? -1 : pState->papContexts[cid]->id));
3489 AssertFailedReturn(VERR_INVALID_PARAMETER);
3490 }
3491 PVMSVGA3DCONTEXT pContext = pState->papContexts[cid];
3492#endif
3493 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3494
3495 for (unsigned i = 0; i < cCopyBoxes; i++)
3496 {
3497 bool fVertex = false;
3498 unsigned cbSrcPitch;
3499
3500 /* Apparently we're supposed to clip it (gmr test sample) */
3501 if (pBoxes[i].x + pBoxes[i].w > pMipLevel->size.width)
3502 pBoxes[i].w = pMipLevel->size.width - pBoxes[i].x;
3503 if (pBoxes[i].y + pBoxes[i].h > pMipLevel->size.height)
3504 pBoxes[i].h = pMipLevel->size.height - pBoxes[i].y;
3505 if (pBoxes[i].z + pBoxes[i].d > pMipLevel->size.depth)
3506 pBoxes[i].d = pMipLevel->size.depth - pBoxes[i].z;
3507
3508 Assert((pBoxes[i].d == 1 || pBoxes[i].d == 0) && pBoxes[i].z == 0);
3509
3510 if ( !pBoxes[i].w
3511 || !pBoxes[i].h
3512 || pBoxes[i].x > pMipLevel->size.width
3513 || pBoxes[i].y > pMipLevel->size.height)
3514 {
3515 Log(("Empty box; skip\n"));
3516 continue;
3517 }
3518
3519 Log(("Copy box %d (%d,%d,%d)(%d,%d,%d) dest (%d,%d)\n", i, pBoxes[i].srcx, pBoxes[i].srcy, pBoxes[i].srcz, pBoxes[i].w, pBoxes[i].h, pBoxes[i].d, pBoxes[i].x, pBoxes[i].y));
3520
3521 cbSrcPitch = (guest.pitch == 0) ? pBoxes[i].w * pSurface->cbBlock : guest.pitch;
3522
3523 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))
3524 {
3525 case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
3526 case SVGA3D_SURFACE_HINT_TEXTURE:
3527 case SVGA3D_SURFACE_HINT_RENDERTARGET:
3528 {
3529 uint32_t cbSurfacePitch;
3530 uint8_t *pDoubleBuffer, *pBufferStart;
3531 unsigned uDestOffset = 0;
3532
3533 pDoubleBuffer = (uint8_t *)RTMemAlloc(pMipLevel->cbSurface);
3534 AssertReturn(pDoubleBuffer, VERR_NO_MEMORY);
3535
3536 if (transfer == SVGA3D_READ_HOST_VRAM)
3537 {
3538 GLint activeTexture;
3539
3540 /* Must bind texture to the current context in order to read it. */
3541 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
3542 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3543
3544 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
3545 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3546
3547 /* Set row length and alignment of the input data. */
3548 VMSVGAPACKPARAMS SavedParams;
3549 vmsvga3dSetPackParams(pState, pContext, pSurface, &SavedParams);
3550
3551 glGetTexImage(GL_TEXTURE_2D,
3552 host.mipmap,
3553 pSurface->formatGL,
3554 pSurface->typeGL,
3555 pDoubleBuffer);
3556 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3557
3558 vmsvga3dRestorePackParams(pState, pContext, pSurface, &SavedParams);
3559
3560 /* Restore the old active texture. */
3561 glBindTexture(GL_TEXTURE_2D, activeTexture);
3562 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3563
3564 uDestOffset = pBoxes[i].x * pSurface->cbBlock + pBoxes[i].y * pMipLevel->cbSurfacePitch;
3565 AssertReturnStmt( uDestOffset + pBoxes[i].w * pSurface->cbBlock + (pBoxes[i].h - 1) * pMipLevel->cbSurfacePitch
3566 <= pMipLevel->cbSurface,
3567 RTMemFree(pDoubleBuffer),
3568 VERR_INTERNAL_ERROR);
3569
3570 cbSurfacePitch = pMipLevel->cbSurfacePitch;
3571
3572#ifdef MANUAL_FLIP_SURFACE_DATA
3573 pBufferStart = pDoubleBuffer
3574 + pBoxes[i].x * pSurface->cbBlock
3575 + pMipLevel->cbSurface - pBoxes[i].y * cbSurfacePitch
3576 - cbSurfacePitch; /* flip image during copy */
3577#else
3578 pBufferStart = pDoubleBuffer + uDestOffset;
3579#endif
3580 }
3581 else
3582 {
3583 cbSurfacePitch = pBoxes[i].w * pSurface->cbBlock;
3584#ifdef MANUAL_FLIP_SURFACE_DATA
3585 pBufferStart = pDoubleBuffer + cbSurfacePitch * pBoxes[i].h - cbSurfacePitch; /* flip image during copy */
3586#else
3587 pBufferStart = pDoubleBuffer;
3588#endif
3589 }
3590
3591 rc = vmsvgaGMRTransfer(pThis,
3592 transfer,
3593 pBufferStart,
3594#ifdef MANUAL_FLIP_SURFACE_DATA
3595 -(int32_t)cbSurfacePitch,
3596#else
3597 (int32_t)cbSurfacePitch,
3598#endif
3599 guest.ptr,
3600 pBoxes[i].srcx * pSurface->cbBlock + pBoxes[i].srcy * cbSrcPitch,
3601 cbSrcPitch,
3602 pBoxes[i].w * pSurface->cbBlock,
3603 pBoxes[i].h);
3604 AssertRC(rc);
3605
3606 /* Update the opengl surface data. */
3607 if (transfer == SVGA3D_WRITE_HOST_VRAM)
3608 {
3609 GLint activeTexture = 0;
3610
3611 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
3612 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3613
3614 /* Must bind texture to the current context in order to change it. */
3615 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
3616 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3617
3618 Log(("vmsvga3dSurfaceDMA: copy texture mipmap level %d (pitch %x)\n", host.mipmap, pMipLevel->cbSurfacePitch));
3619
3620 /* Set row length and alignment of the input data. */
3621 VMSVGAPACKPARAMS SavedParams;
3622 vmsvga3dSetUnpackParams(pState, pContext, pSurface, &SavedParams); /** @todo do we need to set ROW_LENGTH to w here? */
3623
3624 glTexSubImage2D(GL_TEXTURE_2D,
3625 host.mipmap,
3626 pBoxes[i].x,
3627 pBoxes[i].y,
3628 pBoxes[i].w,
3629 pBoxes[i].h,
3630 pSurface->formatGL,
3631 pSurface->typeGL,
3632 pDoubleBuffer);
3633
3634 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3635
3636 /* Restore old values. */
3637 vmsvga3dRestoreUnpackParams(pState, pContext, pSurface, &SavedParams);
3638
3639 /* Restore the old active texture. */
3640 glBindTexture(GL_TEXTURE_2D, activeTexture);
3641 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3642 }
3643
3644 Log4(("first line:\n%.*Rhxd\n", pBoxes[i].w * pSurface->cbBlock, pDoubleBuffer));
3645
3646 /* Free the double buffer. */
3647 RTMemFree(pDoubleBuffer);
3648 break;
3649 }
3650
3651 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
3652 AssertFailed(); /* @todo */
3653 break;
3654
3655 case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
3656 case SVGA3D_SURFACE_HINT_INDEXBUFFER:
3657 {
3658 Assert(pBoxes[i].h == 1);
3659
3660 VMSVGA3D_CLEAR_GL_ERRORS();
3661 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pSurface->oglId.buffer);
3662 if (VMSVGA3D_GL_IS_SUCCESS(pContext))
3663 {
3664 GLenum enmGlTransfer = (transfer == SVGA3D_READ_HOST_VRAM) ? GL_READ_ONLY : GL_WRITE_ONLY;
3665 uint8_t *pbData = (uint8_t *)pState->ext.glMapBuffer(GL_ARRAY_BUFFER, enmGlTransfer);
3666 if (RT_LIKELY(pbData != NULL))
3667 {
3668#if defined(VBOX_STRICT) && defined(RT_OS_DARWIN)
3669 GLint cbStrictBufSize;
3670 glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &cbStrictBufSize);
3671 Assert(VMSVGA3D_GL_IS_SUCCESS(pContext));
3672# ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
3673 AssertMsg(cbStrictBufSize >= (int32_t)pMipLevel->cbSurface,
3674 ("cbStrictBufSize=%#x cbSurface=%#x pContext->id=%#x\n", (uint32_t)cbStrictBufSize, pMipLevel->cbSurface, pContext->id));
3675# else
3676 AssertMsg(cbStrictBufSize >= (int32_t)pMipLevel->cbSurface,
3677 ("cbStrictBufSize=%#x cbSurface=%#x isAssociatedContext=%#x pContext->id=%#x\n", (uint32_t)cbStrictBufSize, pMipLevel->cbSurface, pSurface->idAssociatedContext, pContext->id));
3678# endif
3679#endif
3680
3681 unsigned offDst = pBoxes[i].x * pSurface->cbBlock + pBoxes[i].y * pMipLevel->cbSurfacePitch;
3682 if (RT_LIKELY( offDst + pBoxes[i].w * pSurface->cbBlock + (pBoxes[i].h - 1) * pMipLevel->cbSurfacePitch
3683 <= pMipLevel->cbSurface))
3684 {
3685 Log(("Lock %s memory for rectangle (%d,%d)(%d,%d)\n", (fVertex) ? "vertex" : "index",
3686 pBoxes[i].x, pBoxes[i].y, pBoxes[i].x + pBoxes[i].w, pBoxes[i].y + pBoxes[i].h));
3687
3688 rc = vmsvgaGMRTransfer(pThis,
3689 transfer,
3690 pbData + offDst,
3691 pMipLevel->cbSurfacePitch,
3692 guest.ptr,
3693 pBoxes[i].srcx * pSurface->cbBlock + pBoxes[i].srcy * cbSrcPitch,
3694 cbSrcPitch,
3695 pBoxes[i].w * pSurface->cbBlock,
3696 pBoxes[i].h);
3697 AssertRC(rc);
3698
3699 Log4(("first line:\n%.*Rhxd\n", cbSrcPitch, pbData));
3700 }
3701 else
3702 {
3703 AssertFailed();
3704 rc = VERR_INTERNAL_ERROR;
3705 }
3706
3707 pState->ext.glUnmapBuffer(GL_ARRAY_BUFFER);
3708 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3709 }
3710 else
3711 VMSVGA3D_GL_GET_AND_COMPLAIN(pState, pContext, ("glMapBuffer(GL_ARRAY_BUFFER, %#x) -> NULL\n", enmGlTransfer));
3712 }
3713 else
3714 VMSVGA3D_GL_COMPLAIN(pState, pContext, ("glBindBuffer(GL_ARRAY_BUFFER, %#x)\n", pSurface->oglId.buffer));
3715 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0);
3716 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3717 break;
3718 }
3719
3720 default:
3721 AssertFailed();
3722 break;
3723 }
3724 }
3725 }
3726 return rc;
3727}
3728
3729int vmsvga3dSurfaceBlitToScreen(PVGASTATE pThis, uint32_t dest, SVGASignedRect destRect, SVGA3dSurfaceImageId src, SVGASignedRect srcRect, uint32_t cRects, SVGASignedRect *pRect)
3730{
3731 /* Requires SVGA_FIFO_CAP_SCREEN_OBJECT support */
3732 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));
3733 for (uint32_t i = 0; i < cRects; i++)
3734 {
3735 Log(("vmsvga3dSurfaceBlitToScreen: clipping rect %d (%d,%d)(%d,%d)\n", i, pRect[i].left, pRect[i].top, pRect[i].right, pRect[i].bottom));
3736 }
3737
3738 /* @todo Only screen 0 for now. */
3739 AssertReturn(dest == 0, VERR_INTERNAL_ERROR);
3740 AssertReturn(src.mipmap == 0 && src.face == 0, VERR_INVALID_PARAMETER);
3741 /* @todo scaling */
3742 AssertReturn(destRect.right - destRect.left == srcRect.right - srcRect.left && destRect.bottom - destRect.top == srcRect.bottom - srcRect.top, VERR_INVALID_PARAMETER);
3743
3744 if (cRects == 0)
3745 {
3746 /* easy case; no clipping */
3747 SVGA3dCopyBox box;
3748 SVGA3dGuestImage dst;
3749
3750 box.x = destRect.left;
3751 box.y = destRect.top;
3752 box.z = 0;
3753 box.w = destRect.right - destRect.left;
3754 box.h = destRect.bottom - destRect.top;
3755 box.d = 1;
3756 box.srcx = srcRect.left;
3757 box.srcy = srcRect.top;
3758 box.srcz = 0;
3759
3760 dst.ptr.gmrId = SVGA_GMR_FRAMEBUFFER;
3761 dst.ptr.offset = 0;
3762 dst.pitch = pThis->svga.cbScanline;
3763
3764 int rc = vmsvga3dSurfaceDMA(pThis, dst, src, SVGA3D_READ_HOST_VRAM, 1, &box);
3765 AssertRCReturn(rc, rc);
3766
3767 vgaR3UpdateDisplay(pThis, box.x, box.y, box.w, box.h);
3768 return VINF_SUCCESS;
3769 }
3770 else
3771 {
3772 SVGA3dGuestImage dst;
3773 SVGA3dCopyBox box;
3774
3775 box.srcz = 0;
3776 box.z = 0;
3777 box.d = 1;
3778
3779 dst.ptr.gmrId = SVGA_GMR_FRAMEBUFFER;
3780 dst.ptr.offset = 0;
3781 dst.pitch = pThis->svga.cbScanline;
3782
3783 /* @todo merge into one SurfaceDMA call */
3784 for (uint32_t i = 0; i < cRects; i++)
3785 {
3786 /* The clipping rectangle is relative to the top-left corner of srcRect & destRect. Adjust here. */
3787 box.srcx = srcRect.left + pRect[i].left;
3788 box.srcy = srcRect.top + pRect[i].top;
3789
3790 box.x = pRect[i].left + destRect.left;
3791 box.y = pRect[i].top + destRect.top;
3792 box.z = 0;
3793 box.w = pRect[i].right - pRect[i].left;
3794 box.h = pRect[i].bottom - pRect[i].top;
3795
3796 int rc = vmsvga3dSurfaceDMA(pThis, dst, src, SVGA3D_READ_HOST_VRAM, 1, &box);
3797 AssertRCReturn(rc, rc);
3798
3799 vgaR3UpdateDisplay(pThis, box.x, box.y, box.w, box.h);
3800 }
3801
3802 return VINF_SUCCESS;
3803 }
3804}
3805
3806int vmsvga3dGenerateMipmaps(PVGASTATE pThis, uint32_t sid, SVGA3dTextureFilter filter)
3807{
3808 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
3809 PVMSVGA3DSURFACE pSurface;
3810 int rc = VINF_SUCCESS;
3811 PVMSVGA3DCONTEXT pContext;
3812 uint32_t cid;
3813 GLint activeTexture = 0;
3814
3815 AssertReturn(pState, VERR_NO_MEMORY);
3816 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
3817 AssertReturn(sid < pState->cSurfaces && pState->papSurfaces[sid]->id == sid, VERR_INVALID_PARAMETER);
3818
3819 pSurface = pState->papSurfaces[sid];
3820#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
3821 AssertReturn(pSurface->idAssociatedContext != SVGA3D_INVALID_ID, VERR_INTERNAL_ERROR);
3822#endif
3823
3824 Assert(filter != SVGA3D_TEX_FILTER_FLATCUBIC);
3825 Assert(filter != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
3826 pSurface->autogenFilter = filter;
3827
3828 Log(("vmsvga3dGenerateMipmaps: sid=%x filter=%d\n", sid, filter));
3829
3830#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
3831 cid = SVGA3D_INVALID_ID;
3832 pContext = &pState->SharedCtx;
3833 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3834#else
3835 /* @todo stricter checks for associated context */
3836 cid = pSurface->idAssociatedContext;
3837
3838 if ( cid >= pState->cContexts
3839 || pState->papContexts[cid]->id != cid)
3840 {
3841 Log(("vmsvga3dGenerateMipmaps invalid context id!\n"));
3842 return VERR_INVALID_PARAMETER;
3843 }
3844 pContext = pState->papContexts[cid];
3845 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3846#endif
3847
3848 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
3849 {
3850 /* Unknown surface type; turn it into a texture. */
3851 Log(("vmsvga3dGenerateMipmaps: unknown src surface id=%x type=%d format=%d -> create texture\n", sid, pSurface->flags, pSurface->format));
3852 rc = vmsvga3dCreateTexture(pState, pContext, cid, pSurface);
3853 AssertRCReturn(rc, rc);
3854 }
3855 else
3856 {
3857 /* @todo new filter */
3858 AssertFailed();
3859 }
3860
3861 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
3862 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3863
3864 /* Must bind texture to the current context in order to change it. */
3865 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
3866 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3867
3868 /* Generate the mip maps. */
3869 pState->ext.glGenerateMipmap(GL_TEXTURE_2D);
3870 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3871
3872 /* Restore the old texture. */
3873 glBindTexture(GL_TEXTURE_2D, activeTexture);
3874 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3875
3876 return VINF_SUCCESS;
3877}
3878
3879int vmsvga3dCommandPresent(PVGASTATE pThis, uint32_t sid, uint32_t cRects, SVGA3dCopyRect *pRect)
3880{
3881 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
3882 PVMSVGA3DSURFACE pSurface;
3883 int rc = VINF_SUCCESS;
3884 PVMSVGA3DCONTEXT pContext;
3885 uint32_t cid;
3886 struct
3887 {
3888 uint32_t x;
3889 uint32_t y;
3890 uint32_t cx;
3891 uint32_t cy;
3892 } srcViewPort;
3893
3894 AssertReturn(pState, VERR_NO_MEMORY);
3895 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
3896 AssertReturn(sid < pState->cSurfaces && pState->papSurfaces[sid]->id == sid, VERR_INVALID_PARAMETER);
3897
3898 pSurface = pState->papSurfaces[sid];
3899#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
3900 AssertReturn(pSurface->idAssociatedContext != SVGA3D_INVALID_ID, VERR_INTERNAL_ERROR);
3901#endif
3902
3903#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
3904 /* @todo stricter checks for associated context */
3905 Log(("vmsvga3dCommandPresent: sid=%x cRects=%d\n", sid, cRects));
3906 for (uint32_t i=0; i < cRects; i++)
3907 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));
3908
3909 cid = SVGA3D_INVALID_ID;
3910 pContext = &pState->SharedCtx;
3911 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3912#else
3913 /* @todo stricter checks for associated context */
3914 cid = pSurface->idAssociatedContext;
3915 Log(("vmsvga3dCommandPresent: sid=%x cRects=%d cid=%x\n", sid, cRects, cid));
3916 for (uint32_t i=0; i < cRects; i++)
3917 {
3918 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));
3919 }
3920
3921 if ( cid >= pState->cContexts
3922 || pState->papContexts[cid]->id != cid)
3923 {
3924 Log(("vmsvga3dCommandPresent invalid context id!\n"));
3925 return VERR_INVALID_PARAMETER;
3926 }
3927 pContext = pState->papContexts[cid];
3928 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3929#endif
3930
3931 /* Source surface different size? */
3932 if (pSurface->pMipmapLevels[0].size.width != pThis->svga.uWidth ||
3933 pSurface->pMipmapLevels[0].size.height != pThis->svga.uHeight)
3934 {
3935 float xMultiplier = (float)pSurface->pMipmapLevels[0].size.width / (float)pThis->svga.uWidth;
3936 float yMultiplier = (float)pSurface->pMipmapLevels[0].size.height / (float)pThis->svga.uHeight;
3937
3938 LogFlow(("size (%d vs %d) (%d vs %d) multiplier %d\n", pSurface->pMipmapLevels[0].size.width, pThis->svga.uWidth, pSurface->pMipmapLevels[0].size.height, pThis->svga.uHeight, (int)(xMultiplier * 100.0), (int)(yMultiplier * 100.0)));
3939
3940 srcViewPort.x = (uint32_t)((float)pThis->svga.viewport.x * xMultiplier);
3941 srcViewPort.y = (uint32_t)((float)pThis->svga.viewport.y * yMultiplier);
3942 srcViewPort.cx = (uint32_t)((float)pThis->svga.viewport.cx * xMultiplier);
3943 srcViewPort.cy = (uint32_t)((float)pThis->svga.viewport.cy * yMultiplier);
3944 }
3945 else
3946 {
3947 srcViewPort.x = pThis->svga.viewport.x;
3948 srcViewPort.y = pThis->svga.viewport.y;
3949 srcViewPort.cx = pThis->svga.viewport.cx;
3950 srcViewPort.cy = pThis->svga.viewport.cy;
3951 }
3952
3953#if 1
3954 /* @note this path is slightly faster than the glBlitFrameBuffer path below. */
3955 SVGA3dCopyRect rect;
3956 uint32_t oldVShader, oldPShader;
3957 GLint oldTextureId;
3958
3959 if (cRects == 0)
3960 {
3961 rect.x = rect.y = rect.srcx = rect.srcy = 0;
3962 rect.w = pSurface->pMipmapLevels[0].size.width;
3963 rect.h = pSurface->pMipmapLevels[0].size.height;
3964 pRect = &rect;
3965 cRects = 1;
3966 }
3967
3968 //glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
3969
3970#if 0
3971 glDisable(GL_CULL_FACE);
3972 glDisable(GL_BLEND);
3973 glDisable(GL_ALPHA_TEST);
3974 glDisable(GL_SCISSOR_TEST);
3975 glDisable(GL_STENCIL_TEST);
3976 glEnable(GL_DEPTH_TEST);
3977 glDepthFunc(GL_ALWAYS);
3978 glDepthMask(GL_TRUE);
3979 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
3980 glViewport(0, 0, pSurface->pMipmapLevels[0].size.width, pSurface->pMipmapLevels[0].size.height);
3981#endif
3982
3983 glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTextureId);
3984
3985 oldVShader = pContext->state.shidVertex;
3986 oldPShader = pContext->state.shidPixel;
3987 vmsvga3dShaderSet(pThis, cid, SVGA3D_SHADERTYPE_VS, SVGA_ID_INVALID);
3988 vmsvga3dShaderSet(pThis, cid, SVGA3D_SHADERTYPE_PS, SVGA_ID_INVALID);
3989
3990 /* Flush shader changes. */
3991 if (pContext->pShaderContext)
3992 ShaderUpdateState(pContext->pShaderContext, 0);
3993
3994 /* Activate the read and draw framebuffer objects. */
3995 pState->ext.glBindFramebuffer(GL_READ_FRAMEBUFFER, pContext->idReadFramebuffer);
3996 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3997 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0 /* back buffer */);
3998 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3999
4000 pState->ext.glActiveTexture(GL_TEXTURE0);
4001 glEnable(GL_TEXTURE_2D);
4002 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
4003 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4004
4005 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
4006 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
4007
4008// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
4009// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
4010
4011 /* Reset the transformation matrices. */
4012 glMatrixMode(GL_MODELVIEW);
4013 glPushMatrix();
4014 glLoadIdentity();
4015 glMatrixMode(GL_PROJECTION);
4016 glPushMatrix();
4017 glLoadIdentity();
4018 glScalef(1.0f, -1.0f, 1.0f);
4019 glOrtho(0, pThis->svga.uWidth, pThis->svga.uHeight, 0, 0.0, -1.0);
4020
4021 for (uint32_t i = 0; i < cRects; i++)
4022 {
4023 float left, right, top, bottom; /* Texture coordinates */
4024 int vertexLeft, vertexRight, vertexTop, vertexBottom;
4025
4026 pRect[i].srcx = RT_MAX(pRect[i].srcx, srcViewPort.x);
4027 pRect[i].srcy = RT_MAX(pRect[i].srcy, srcViewPort.y);
4028 pRect[i].x = RT_MAX(pRect[i].x, pThis->svga.viewport.x) - pThis->svga.viewport.x;
4029 pRect[i].y = RT_MAX(pRect[i].y, pThis->svga.viewport.y) - pThis->svga.viewport.y;
4030 pRect[i].w = pThis->svga.viewport.cx;
4031 pRect[i].h = pThis->svga.viewport.cy;
4032
4033 if ( pRect[i].x + pRect[i].w <= pThis->svga.viewport.x
4034 || pThis->svga.viewport.x + pThis->svga.viewport.cx <= pRect[i].x
4035 || pRect[i].y + pRect[i].h <= pThis->svga.viewport.y
4036 || pThis->svga.viewport.y + pThis->svga.viewport.cy <= pRect[i].y)
4037 {
4038 /* Intersection is empty; skip */
4039 continue;
4040 }
4041
4042 left = pRect[i].srcx;
4043 right = pRect[i].srcx + pRect[i].w;
4044 top = pRect[i].srcy + pRect[i].h;
4045 bottom = pRect[i].srcy;
4046
4047 left /= pSurface->pMipmapLevels[0].size.width;
4048 right /= pSurface->pMipmapLevels[0].size.width;
4049 top /= pSurface->pMipmapLevels[0].size.height;
4050 bottom /= pSurface->pMipmapLevels[0].size.height;
4051
4052 vertexLeft = pRect[i].x;
4053 vertexRight = pRect[i].x + pRect[i].w;
4054 vertexTop = ((uint32_t)pThis->svga.uHeight >= pRect[i].y + pRect[i].h) ? pThis->svga.uHeight - pRect[i].y - pRect[i].h : 0;
4055 vertexBottom = pThis->svga.uHeight - pRect[i].y;
4056
4057 Log(("view port (%d,%d)(%d,%d)\n", srcViewPort.x, srcViewPort.y, srcViewPort.cx, srcViewPort.cy));
4058 Log(("vertex (%d,%d) (%d,%d) (%d,%d) (%d,%d)\n", vertexLeft, vertexBottom, vertexLeft, vertexTop, vertexRight, vertexTop, vertexRight, vertexBottom));
4059 Log(("texture (%d,%d) (%d,%d) (%d,%d) (%d,%d)\n", pRect[i].srcx, pSurface->pMipmapLevels[0].size.height - (pRect[i].srcy + pRect[i].h), pRect[i].srcx, pSurface->pMipmapLevels[0].size.height - pRect[i].srcy, pRect[i].srcx + pRect[i].w, pSurface->pMipmapLevels[0].size.height - pRect[i].srcy, pRect[i].srcx + pRect[i].w, pSurface->pMipmapLevels[0].size.height - (pRect[i].srcy + pRect[i].h)));
4060
4061 glBegin(GL_QUADS);
4062 /* bottom left */
4063 glTexCoord2f(left, bottom);
4064 glVertex2i(vertexLeft, vertexBottom);
4065
4066 /* top left */
4067 glTexCoord2f(left, top);
4068 glVertex2i(vertexLeft, vertexTop);
4069
4070 /* top right */
4071 glTexCoord2f(right, top);
4072 glVertex2i(vertexRight, vertexTop);
4073
4074 /* bottom right */
4075 glTexCoord2f(right, bottom);
4076 glVertex2i(vertexRight, vertexBottom);
4077
4078 glEnd();
4079 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4080 }
4081
4082 /* Restore old settings. */
4083 glMatrixMode(GL_PROJECTION);
4084 glPopMatrix();
4085 glMatrixMode(GL_MODELVIEW);
4086 glPopMatrix();
4087
4088 //glPopAttrib();
4089
4090 glBindTexture(GL_TEXTURE_2D, oldTextureId);
4091 vmsvga3dShaderSet(pThis, cid, SVGA3D_SHADERTYPE_VS, oldVShader);
4092 vmsvga3dShaderSet(pThis, cid, SVGA3D_SHADERTYPE_PS, oldPShader);
4093
4094 /* Reset the frame buffer association */
4095 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
4096 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4097
4098#else
4099 /* Activate the read and draw framebuffer objects. */
4100 pState->ext.glBindFramebuffer(GL_READ_FRAMEBUFFER, pContext->idReadFramebuffer);
4101 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
4102 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0 /* back buffer */);
4103 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
4104
4105 /* Bind the source objects to the right place. */
4106 pState->ext.glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pSurface->oglId.texture, 0 /* level 0 */);
4107 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
4108
4109 /* Blit the surface rectangle(s) to the back buffer. */
4110 if (cRects == 0)
4111 {
4112 Log(("view port (%d,%d)(%d,%d)\n", srcViewPort.x, srcViewPort.y, srcViewPort.cx, srcViewPort.cy));
4113 pState->ext.glBlitFramebuffer(srcViewPort.x,
4114 srcViewPort.y,
4115 srcViewPort.x + srcViewPort.cx, /* exclusive. */
4116 srcViewPort.y + srcViewPort.cy, /* exclusive. (reverse to flip the image) */
4117 0,
4118 pThis->svga.viewport.cy, /* exclusive. */
4119 pThis->svga.viewport.cx, /* exclusive. */
4120 0,
4121 GL_COLOR_BUFFER_BIT,
4122 GL_LINEAR);
4123 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
4124 }
4125 else
4126 {
4127 for (uint32_t i = 0; i < cRects; i++)
4128 {
4129 if ( pRect[i].x + pRect[i].w <= pThis->svga.viewport.x
4130 || pThis->svga.viewport.x + pThis->svga.viewport.cx <= pRect[i].x
4131 || pRect[i].y + pRect[i].h <= pThis->svga.viewport.y
4132 || pThis->svga.viewport.y + pThis->svga.viewport.cy <= pRect[i].y)
4133 {
4134 /* Intersection is empty; skip */
4135 continue;
4136 }
4137
4138 pState->ext.glBlitFramebuffer(RT_MAX(pRect[i].srcx, srcViewPort.x),
4139 pSurface->pMipmapLevels[0].size.width - RT_MAX(pRect[i].srcy, srcViewPort.y), /* exclusive. (reverse to flip the image) */
4140 RT_MIN(pRect[i].srcx + pRect[i].w, srcViewPort.x + srcViewPort.cx), /* exclusive. */
4141 pSurface->pMipmapLevels[0].size.width - RT_MIN(pRect[i].srcy + pRect[i].h, srcViewPort.y + srcViewPort.cy),
4142 RT_MAX(pRect[i].x, pThis->svga.viewport.x) - pThis->svga.viewport.x,
4143 pThis->svga.uHeight - (RT_MIN(pRect[i].y + pRect[i].h, pThis->svga.viewport.y + pThis->svga.viewport.cy) - pThis->svga.viewport.y), /* exclusive. */
4144 RT_MIN(pRect[i].x + pRect[i].w, pThis->svga.viewport.x + pThis->svga.viewport.cx) - pThis->svga.viewport.x, /* exclusive. */
4145 pThis->svga.uHeight - (RT_MAX(pRect[i].y, pThis->svga.viewport.y) - pThis->svga.viewport.y),
4146 GL_COLOR_BUFFER_BIT,
4147 GL_LINEAR);
4148 }
4149 }
4150 /* Reset the frame buffer association */
4151 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
4152 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
4153#endif
4154
4155 /* Flip the front and back buffers. */
4156#ifdef RT_OS_WINDOWS
4157 BOOL ret = SwapBuffers(pContext->hdc);
4158 AssertMsg(ret, ("SwapBuffers failed with %d\n", GetLastError()));
4159#elif defined(RT_OS_DARWIN)
4160 vmsvga3dCocoaSwapBuffers(pContext->cocoaView, pContext->cocoaContext);
4161#else
4162 /* show the window if not already done */
4163 if (!pContext->fMapped)
4164 {
4165 XMapWindow(pState->display, pContext->window);
4166 pContext->fMapped = true;
4167 }
4168 /* now swap the buffers, i.e. display the rendering result */
4169 glXSwapBuffers(pState->display, pContext->window);
4170#endif
4171 return VINF_SUCCESS;
4172}
4173
4174#ifdef RT_OS_LINUX
4175/**
4176 * X11 event handling thread
4177 * @param ThreadSelf thread handle
4178 * @param pvUser pointer to pState structure
4179 * @returns VBox status code
4180 */
4181DECLCALLBACK(int) vmsvga3dXEventThread(RTTHREAD ThreadSelf, void *pvUser)
4182{
4183 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pvUser;
4184 while (!pState->bTerminate)
4185 {
4186 while (XPending(pState->display) > 0)
4187 {
4188 XEvent event;
4189 XNextEvent(pState->display, &event);
4190
4191 switch (event.type)
4192 {
4193 default:
4194 break;
4195 }
4196 }
4197 /* sleep for 16ms to not burn too many cycles */
4198 RTThreadSleep(16);
4199 }
4200 return VINF_SUCCESS;
4201}
4202#endif // RT_OS_LINUX
4203
4204
4205/**
4206 * Create a new 3d context
4207 *
4208 * @returns VBox status code.
4209 * @param pThis VGA device instance data.
4210 * @param cid Context id
4211 * @param fFlags VMSVGA3D_DEF_CTX_F_XXX.
4212 */
4213static int vmsvga3dContextDefineOgl(PVGASTATE pThis, uint32_t cid, uint32_t fFlags)
4214{
4215 int rc;
4216 PVMSVGA3DCONTEXT pContext;
4217 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4218
4219 AssertReturn(pState, VERR_NO_MEMORY);
4220#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
4221 AssertReturn( cid < SVGA3D_MAX_CONTEXT_IDS
4222 || (cid == VMSVGA3D_SHARED_CTX_ID && (fFlags & VMSVGA3D_DEF_CTX_F_SHARED_CTX)), VERR_INVALID_PARAMETER);
4223#else
4224 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
4225#endif
4226#if !defined(VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE) || !(defined(RT_OS_DARWIN))
4227 AssertReturn(!(fFlags & VMSVGA3D_DEF_CTX_F_OTHER_PROFILE), VERR_INTERNAL_ERROR_3);
4228#endif
4229
4230 Log(("vmsvga3dContextDefine id %x\n", cid));
4231#ifdef DEBUG_DEBUG_GFX_WINDOW_TEST_CONTEXT
4232 if (pState->idTestContext == SVGA_ID_INVALID)
4233 {
4234 pState->idTestContext = 207;
4235 rc = vmsvga3dContextDefine(pThis, pState->idTestContext);
4236 AssertRCReturn(rc, rc);
4237 }
4238#endif
4239
4240#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
4241 if (cid == VMSVGA3D_SHARED_CTX_ID)
4242 pContext = &pState->SharedCtx;
4243 else
4244#endif
4245 {
4246 if (cid >= pState->cContexts)
4247 {
4248 /* Grow the array. */
4249 uint32_t cNew = RT_ALIGN(cid + 15, 16);
4250 void *pvNew = RTMemRealloc(pState->papContexts, sizeof(pState->papContexts[0]) * cNew);
4251 AssertReturn(pvNew, VERR_NO_MEMORY);
4252 pState->papContexts = (PVMSVGA3DCONTEXT *)pvNew;
4253 while (pState->cContexts < cNew)
4254 {
4255 pContext = (PVMSVGA3DCONTEXT)RTMemAllocZ(sizeof(*pContext));
4256 AssertReturn(pContext, VERR_NO_MEMORY);
4257 pContext->id = SVGA3D_INVALID_ID;
4258 pState->papContexts[pState->cContexts++] = pContext;
4259 }
4260 }
4261 /* If one already exists with this id, then destroy it now. */
4262 if (pState->papContexts[cid]->id != SVGA3D_INVALID_ID)
4263 vmsvga3dContextDestroy(pThis, cid);
4264
4265 pContext = pState->papContexts[cid];
4266 }
4267
4268 /*
4269 * Find the shared context (necessary for sharing e.g. textures between contexts).
4270 */
4271#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
4272 PVMSVGA3DCONTEXT pSharedCtx = NULL;
4273 if (!(fFlags & (VMSVGA3D_DEF_CTX_F_INIT | VMSVGA3D_DEF_CTX_F_SHARED_CTX)))
4274 {
4275 pSharedCtx = &pState->SharedCtx;
4276 if (pSharedCtx->id != VMSVGA3D_SHARED_CTX_ID)
4277 {
4278 rc = vmsvga3dContextDefineOgl(pThis, VMSVGA3D_SHARED_CTX_ID, VMSVGA3D_DEF_CTX_F_SHARED_CTX);
4279 AssertLogRelRCReturn(rc, rc);
4280 }
4281 }
4282#else
4283 // TODO isn't this default on Linux since OpenGL 1.1?
4284 /* Find the first active context to share the display list with (necessary for sharing e.g. textures between contexts). */
4285 PVMSVGA3DCONTEXT pSharedCtx = NULL;
4286 for (uint32_t i = 0; i < pState->cContexts; i++)
4287 if ( pState->papContexts[i]->id != SVGA3D_INVALID_ID
4288 && i != cid
4289# ifdef VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE
4290 && pState->papContexts[i]->fOtherProfile == RT_BOOL(fFlags & VMSVGA3D_DEF_CTX_F_OTHER_PROFILE)
4291# endif
4292 )
4293 {
4294 Log(("Sharing display lists between cid=%d and cid=%d\n", pContext->id, i));
4295 pSharedCtx = pState->papContexts[i];
4296 break;
4297 }
4298#endif
4299
4300 /*
4301 * Initialize the context.
4302 */
4303 memset(pContext, 0, sizeof(*pContext));
4304 pContext->id = cid;
4305 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTexture); i++)
4306 pContext->aSidActiveTexture[i] = SVGA3D_INVALID_ID;
4307
4308 pContext->sidRenderTarget = SVGA3D_INVALID_ID;
4309 pContext->state.shidVertex = SVGA3D_INVALID_ID;
4310 pContext->state.shidPixel = SVGA3D_INVALID_ID;
4311 pContext->idFramebuffer = OPENGL_INVALID_ID;
4312 pContext->idReadFramebuffer = OPENGL_INVALID_ID;
4313 pContext->idDrawFramebuffer = OPENGL_INVALID_ID;
4314
4315 rc = ShaderContextCreate(&pContext->pShaderContext);
4316 AssertRCReturn(rc, rc);
4317
4318 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); i++)
4319 pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
4320
4321 AssertReturn(pThis->svga.u64HostWindowId, VERR_INTERNAL_ERROR);
4322
4323#ifdef RT_OS_WINDOWS
4324 /* Create a context window. */
4325 CREATESTRUCT cs;
4326 cs.lpCreateParams = NULL;
4327 cs.dwExStyle = WS_EX_NOACTIVATE | WS_EX_NOPARENTNOTIFY | WS_EX_TRANSPARENT;
4328# ifdef DEBUG_GFX_WINDOW
4329 cs.lpszName = (char *)RTMemAllocZ(256);
4330 RTStrPrintf((char *)cs.lpszName, 256, "Context %d OpenGL Window", cid);
4331# else
4332 cs.lpszName = NULL;
4333# endif
4334 cs.lpszClass = 0;
4335# ifdef DEBUG_GFX_WINDOW
4336 cs.style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE | WS_CAPTION;
4337# else
4338 cs.style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED | WS_CHILD | WS_VISIBLE;
4339# endif
4340 cs.x = 0;
4341 cs.y = 0;
4342 cs.cx = pThis->svga.uWidth;
4343 cs.cy = pThis->svga.uHeight;
4344 cs.hwndParent = (HWND)pThis->svga.u64HostWindowId;
4345 cs.hMenu = NULL;
4346 cs.hInstance = pState->hInstance;
4347
4348 rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_CREATEWINDOW, (WPARAM)&pContext->hwnd, (LPARAM)&cs);
4349 AssertRCReturn(rc, rc);
4350
4351 pContext->hdc = GetDC(pContext->hwnd);
4352 AssertMsgReturn(pContext->hdc, ("GetDC %x failed with %d\n", pContext->hwnd, GetLastError()), VERR_INTERNAL_ERROR);
4353
4354 PIXELFORMATDESCRIPTOR pfd = {
4355 sizeof(PIXELFORMATDESCRIPTOR), /* size of this pfd */
4356 1, /* version number */
4357 PFD_DRAW_TO_WINDOW | /* support window */
4358 PFD_DOUBLEBUFFER | /* support double buffering */
4359 PFD_SUPPORT_OPENGL, /* support OpenGL */
4360 PFD_TYPE_RGBA, /* RGBA type */
4361 24, /* 24-bit color depth */
4362 0, 0, 0, 0, 0, 0, /* color bits ignored */
4363 8, /* alpha buffer */
4364 0, /* shift bit ignored */
4365 0, /* no accumulation buffer */
4366 0, 0, 0, 0, /* accum bits ignored */
4367 16, /* set depth buffer */
4368 16, /* set stencil buffer */
4369 0, /* no auxiliary buffer */
4370 PFD_MAIN_PLANE, /* main layer */
4371 0, /* reserved */
4372 0, 0, 0 /* layer masks ignored */
4373 };
4374 int pixelFormat;
4375 BOOL ret;
4376
4377 pixelFormat = ChoosePixelFormat(pContext->hdc, &pfd);
4378 /* @todo is this really necessary?? */
4379 pixelFormat = ChoosePixelFormat(pContext->hdc, &pfd);
4380 AssertMsgReturn(pixelFormat != 0, ("ChoosePixelFormat failed with %d\n", GetLastError()), VERR_INTERNAL_ERROR);
4381
4382 ret = SetPixelFormat(pContext->hdc, pixelFormat, &pfd);
4383 AssertMsgReturn(ret == TRUE, ("SetPixelFormat failed with %d\n", GetLastError()), VERR_INTERNAL_ERROR);
4384
4385 pContext->hglrc = wglCreateContext(pContext->hdc);
4386 AssertMsgReturn(pContext->hglrc, ("wglCreateContext %x failed with %d\n", pContext->hdc, GetLastError()), VERR_INTERNAL_ERROR);
4387
4388 if (pSharedCtx)
4389 {
4390 ret = wglShareLists(pSharedCtx->hglrc, pContext->hglrc);
4391 AssertMsg(ret == TRUE, ("wglShareLists(%p, %p) failed with %d\n", pSharedCtx->hglrc, pContext->hglrc, GetLastError()));
4392 }
4393
4394#elif defined(RT_OS_DARWIN)
4395 pContext->fOtherProfile = RT_BOOL(fFlags & VMSVGA3D_DEF_CTX_F_OTHER_PROFILE);
4396
4397 NativeNSOpenGLContextRef shareContext = pSharedCtx ? pSharedCtx->cocoaContext : NULL;
4398 vmsvga3dCocoaCreateContext(&pContext->cocoaContext, shareContext, pContext->fOtherProfile);
4399 NativeNSViewRef pHostView = (NativeNSViewRef)pThis->svga.u64HostWindowId;
4400 vmsvga3dCocoaCreateView(&pContext->cocoaView, pHostView);
4401
4402#else
4403 Window hostWindow = (Window)pThis->svga.u64HostWindowId;
4404
4405 if (pState->display == NULL)
4406 {
4407 /* get an X display and make sure we have glX 1.3 */
4408 pState->display = XOpenDisplay(0);
4409 Assert(pState->display);
4410 int glxMajor, glxMinor;
4411 Bool ret = glXQueryVersion(pState->display, &glxMajor, &glxMinor);
4412 AssertMsgReturn(ret && glxMajor == 1 && glxMinor >= 3, ("glX >=1.3 not present"), VERR_INTERNAL_ERROR);
4413 /* start our X event handling thread */
4414 rc = RTThreadCreate(&pState->pWindowThread, vmsvga3dXEventThread, pState, 0, RTTHREADTYPE_GUI, RTTHREADFLAGS_WAITABLE, "VMSVGA3DXEVENT");
4415 if (RT_FAILURE(rc))
4416 {
4417 AssertMsgFailed(("%s: Async IO Thread creation for 3d window handling failed rc=%d\n", __FUNCTION__, rc));
4418 return rc;
4419 }
4420 }
4421 int attrib[] =
4422 {
4423 GLX_RGBA,
4424 GLX_RED_SIZE, 1,
4425 GLX_GREEN_SIZE, 1,
4426 GLX_BLUE_SIZE, 1,
4427 //GLX_ALPHA_SIZE, 1, this flips the bbos screen
4428 GLX_DOUBLEBUFFER,
4429 None
4430 };
4431 XVisualInfo *vi = glXChooseVisual(pState->display, DefaultScreen(pState->display), attrib);
4432 XSetWindowAttributes swa;
4433 swa.colormap = XCreateColormap(pState->display, XDefaultRootWindow(pState->display), vi->visual, AllocNone);
4434 swa.border_pixel = 0;
4435 swa.background_pixel = 0;
4436 swa.event_mask = StructureNotifyMask | ExposureMask;
4437 unsigned long flags = CWBorderPixel | CWBackPixel | CWColormap | CWEventMask;
4438 pContext->window = XCreateWindow(pState->display, hostWindow,//XDefaultRootWindow(pState->display),//hostWindow,
4439 0, 0, pThis->svga.uWidth, pThis->svga.uHeight,
4440 0, vi->depth, InputOutput,
4441 vi->visual, flags, &swa);
4442 AssertMsgReturn(pContext->window, ("XCreateWindow failed"), VERR_INTERNAL_ERROR);
4443 uint32_t cardinal_alpha = (uint32_t) (0.5 * (uint32_t)-1) ;
4444
4445 /* the window is hidden by default and only mapped when CommandPresent is executed on it */
4446
4447 GLXContext shareContext = pSharedCtx ? pSharedCtx->glxContext : NULL;
4448 pContext->glxContext = glXCreateContext(pState->display, vi, shareContext, GL_TRUE);
4449 AssertMsgReturn(pContext->glxContext, ("glXCreateContext failed"), VERR_INTERNAL_ERROR);
4450#endif
4451
4452 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4453
4454 /* NULL during the first PowerOn call. */
4455 if (pState->ext.glGenFramebuffers)
4456 {
4457 /* Create a framebuffer object for this context. */
4458 pState->ext.glGenFramebuffers(1, &pContext->idFramebuffer);
4459 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4460
4461 /* Bind the object to the framebuffer target. */
4462 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
4463 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4464
4465 /* Create read and draw framebuffer objects for this context. */
4466 pState->ext.glGenFramebuffers(1, &pContext->idReadFramebuffer);
4467 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4468
4469 pState->ext.glGenFramebuffers(1, &pContext->idDrawFramebuffer);
4470 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4471
4472 }
4473#if 0
4474 /* @todo move to shader lib!!! */
4475 /* Clear the screen */
4476 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4477
4478 glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
4479 glClearIndex(0);
4480 glClearDepth(1);
4481 glClearStencil(0xffff);
4482 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
4483 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
4484 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
4485 if (pState->ext.glProvokingVertex)
4486 pState->ext.glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);
4487 /* @todo move to shader lib!!! */
4488#endif
4489 return VINF_SUCCESS;
4490}
4491
4492
4493/**
4494 * Create a new 3d context
4495 *
4496 * @returns VBox status code.
4497 * @param pThis VGA device instance data.
4498 * @param cid Context id
4499 */
4500int vmsvga3dContextDefine(PVGASTATE pThis, uint32_t cid)
4501{
4502 return vmsvga3dContextDefineOgl(pThis, cid, 0/*fFlags*/);
4503}
4504
4505
4506/**
4507 * Destroy an existing 3d context
4508 *
4509 * @returns VBox status code.
4510 * @param pThis VGA device instance data.
4511 * @param cid Context id
4512 */
4513int vmsvga3dContextDestroy(PVGASTATE pThis, uint32_t cid)
4514{
4515 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4516 AssertReturn(pState, VERR_NO_MEMORY);
4517
4518 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
4519
4520 if ( cid < pState->cContexts
4521 && pState->papContexts[cid]->id == cid)
4522 {
4523 PVMSVGA3DCONTEXT pContext = pState->papContexts[cid];
4524
4525 Log(("vmsvga3dContextDestroy id %x\n", cid));
4526
4527 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4528
4529 /* Destroy all leftover pixel shaders. */
4530 for (uint32_t i = 0; i < pContext->cPixelShaders; i++)
4531 {
4532 if (pContext->paPixelShader[i].id != SVGA3D_INVALID_ID)
4533 vmsvga3dShaderDestroy(pThis, pContext->paPixelShader[i].cid, pContext->paPixelShader[i].id, pContext->paPixelShader[i].type);
4534 }
4535 if (pContext->paPixelShader)
4536 RTMemFree(pContext->paPixelShader);
4537
4538 /* Destroy all leftover vertex shaders. */
4539 for (uint32_t i = 0; i < pContext->cVertexShaders; i++)
4540 {
4541 if (pContext->paVertexShader[i].id != SVGA3D_INVALID_ID)
4542 vmsvga3dShaderDestroy(pThis, pContext->paVertexShader[i].cid, pContext->paVertexShader[i].id, pContext->paVertexShader[i].type);
4543 }
4544 if (pContext->paVertexShader)
4545 RTMemFree(pContext->paVertexShader);
4546
4547 if (pContext->state.paVertexShaderConst)
4548 RTMemFree(pContext->state.paVertexShaderConst);
4549 if (pContext->state.paPixelShaderConst)
4550 RTMemFree(pContext->state.paPixelShaderConst);
4551
4552 if (pContext->pShaderContext)
4553 {
4554 int rc = ShaderContextDestroy(pContext->pShaderContext);
4555 AssertRC(rc);
4556 }
4557
4558#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX /* This is done on windows - prevents various assertions at runtime, as well as shutdown & reset assertions when destroying surfaces. */
4559 /* Check for all surfaces that are associated with this context to remove all dependencies */
4560 for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
4561 {
4562 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
4563 if ( pSurface->idAssociatedContext == cid
4564 && pSurface->id == sid)
4565 {
4566 int rc;
4567
4568 Log(("vmsvga3dContextDestroy: remove all dependencies for surface %x\n", sid));
4569
4570 uint32_t surfaceFlags = pSurface->flags;
4571 SVGA3dSurfaceFormat format = pSurface->format;
4572 SVGA3dSurfaceFace face[SVGA3D_MAX_SURFACE_FACES];
4573 uint32_t multisampleCount = pSurface->multiSampleCount;
4574 SVGA3dTextureFilter autogenFilter = pSurface->autogenFilter;
4575 SVGA3dSize *pMipLevelSize;
4576 uint32_t cFaces = pSurface->cFaces;
4577
4578 pMipLevelSize = (SVGA3dSize *)RTMemAllocZ(pSurface->faces[0].numMipLevels * pSurface->cFaces * sizeof(SVGA3dSize));
4579 AssertReturn(pMipLevelSize, VERR_NO_MEMORY);
4580
4581 for (uint32_t iFace = 0; iFace < pSurface->cFaces; iFace++)
4582 {
4583 for (uint32_t i = 0; i < pSurface->faces[0].numMipLevels; i++)
4584 {
4585 uint32_t idx = i + iFace * pSurface->faces[0].numMipLevels;
4586 memcpy(&pMipLevelSize[idx], &pSurface->pMipmapLevels[idx].size, sizeof(SVGA3dSize));
4587 }
4588 }
4589 memcpy(face, pSurface->faces, sizeof(pSurface->faces));
4590
4591 /* Recreate the surface with the original settings; destroys the contents, but that seems fairly safe since the context is also destroyed. */
4592 rc = vmsvga3dSurfaceDestroy(pThis, sid);
4593 AssertRC(rc);
4594
4595 rc = vmsvga3dSurfaceDefine(pThis, sid, surfaceFlags, format, face, multisampleCount, autogenFilter, face[0].numMipLevels * cFaces, pMipLevelSize);
4596 AssertRC(rc);
4597 }
4598 }
4599#endif
4600
4601 if (pContext->idFramebuffer != OPENGL_INVALID_ID)
4602 {
4603 /* Unbind the object from the framebuffer target. */
4604 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, 0 /* back buffer */);
4605 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4606 pState->ext.glDeleteFramebuffers(1, &pContext->idFramebuffer);
4607 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4608
4609 if (pContext->idReadFramebuffer != OPENGL_INVALID_ID)
4610 {
4611 pState->ext.glDeleteFramebuffers(1, &pContext->idReadFramebuffer);
4612 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4613 }
4614 if (pContext->idDrawFramebuffer != OPENGL_INVALID_ID)
4615 {
4616 pState->ext.glDeleteFramebuffers(1, &pContext->idDrawFramebuffer);
4617 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4618 }
4619 }
4620#ifdef RT_OS_WINDOWS
4621 wglMakeCurrent(pContext->hdc, NULL);
4622 wglDeleteContext(pContext->hglrc);
4623 ReleaseDC(pContext->hwnd, pContext->hdc);
4624
4625 /* Destroy the window we've created. */
4626 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_DESTROYWINDOW, (WPARAM)pContext->hwnd, 0);
4627 AssertRC(rc);
4628#elif defined(RT_OS_DARWIN)
4629 vmsvga3dCocoaDestroyView(pContext->cocoaView);
4630 vmsvga3dCocoaDestroyContext(pContext->cocoaContext);
4631#elif defined(RT_OS_LINUX)
4632 glXMakeCurrent(pState->display, None, NULL);
4633 glXDestroyContext(pState->display, pContext->glxContext);
4634 XDestroyWindow(pState->display, pContext->window);
4635#endif
4636
4637 memset(pContext, 0, sizeof(*pContext));
4638 pContext->id = SVGA3D_INVALID_ID;
4639
4640 VMSVGA3D_CLEAR_CURRENT_CONTEXT(pState);
4641 }
4642 else
4643 AssertFailed();
4644
4645 return VINF_SUCCESS;
4646}
4647
4648/* Handle resize */
4649int vmsvga3dChangeMode(PVGASTATE pThis)
4650{
4651 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4652 AssertReturn(pState, VERR_NO_MEMORY);
4653
4654 /* Resize all active contexts. */
4655 for (uint32_t i = 0; i < pState->cContexts; i++)
4656 {
4657 PVMSVGA3DCONTEXT pContext = pState->papContexts[i];
4658 uint32_t cid = pContext->id;
4659
4660 if (cid != SVGA3D_INVALID_ID)
4661 {
4662#ifdef RT_OS_WINDOWS
4663 CREATESTRUCT cs;
4664
4665 memset(&cs, 0, sizeof(cs));
4666 cs.cx = pThis->svga.uWidth;
4667 cs.cy = pThis->svga.uHeight;
4668
4669 /* Resize the window. */
4670 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_RESIZEWINDOW, (WPARAM)pContext->hwnd, (LPARAM)&cs);
4671 AssertRC(rc);
4672#elif defined(RT_OS_DARWIN)
4673 vmsvga3dCocoaViewSetSize(pContext->cocoaView, pThis->svga.uWidth, pThis->svga.uHeight);
4674#elif defined(RT_OS_LINUX)
4675 XWindowChanges wc;
4676 wc.width = pThis->svga.uWidth;
4677 wc.height = pThis->svga.uHeight;
4678 XConfigureWindow(pState->display, pContext->window, CWWidth | CWHeight, &wc);
4679#endif
4680 }
4681 }
4682 return VINF_SUCCESS;
4683}
4684
4685
4686int vmsvga3dSetTransform(PVGASTATE pThis, uint32_t cid, SVGA3dTransformType type, float matrix[16])
4687{
4688 PVMSVGA3DCONTEXT pContext;
4689 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4690 AssertReturn(pState, VERR_NO_MEMORY);
4691 bool fModelViewChanged = false;
4692
4693 Log(("vmsvga3dSetTransform cid=%x %s\n", cid, vmsvgaTransformToString(type)));
4694
4695 if ( cid >= pState->cContexts
4696 || pState->papContexts[cid]->id != cid)
4697 {
4698 Log(("vmsvga3dSetTransform invalid context id!\n"));
4699 return VERR_INVALID_PARAMETER;
4700 }
4701 pContext = pState->papContexts[cid];
4702 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4703
4704 /* Save this matrix for vm state save/restore. */
4705 pContext->state.aTransformState[type].fValid = true;
4706 memcpy(pContext->state.aTransformState[type].matrix, matrix, sizeof(pContext->state.aTransformState[type].matrix));
4707 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_TRANSFORM;
4708
4709 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)));
4710 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)));
4711 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)));
4712 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)));
4713
4714 switch (type)
4715 {
4716 case SVGA3D_TRANSFORM_VIEW:
4717 /* View * World = Model View */
4718 glMatrixMode(GL_MODELVIEW);
4719 glLoadMatrixf(matrix);
4720 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_WORLD].fValid)
4721 glMultMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_WORLD].matrix);
4722 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4723 fModelViewChanged = true;
4724 break;
4725
4726 case SVGA3D_TRANSFORM_PROJECTION:
4727 {
4728 int rc = ShaderTransformProjection(pContext->state.RectViewPort.w, pContext->state.RectViewPort.h, matrix);
4729 AssertRCReturn(rc, rc);
4730 break;
4731 }
4732
4733 case SVGA3D_TRANSFORM_TEXTURE0:
4734 glMatrixMode(GL_TEXTURE);
4735 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4736 glLoadMatrixf(matrix);
4737 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4738 break;
4739
4740 case SVGA3D_TRANSFORM_TEXTURE1:
4741 case SVGA3D_TRANSFORM_TEXTURE2:
4742 case SVGA3D_TRANSFORM_TEXTURE3:
4743 case SVGA3D_TRANSFORM_TEXTURE4:
4744 case SVGA3D_TRANSFORM_TEXTURE5:
4745 case SVGA3D_TRANSFORM_TEXTURE6:
4746 case SVGA3D_TRANSFORM_TEXTURE7:
4747 Log(("vmsvga3dSetTransform: unsupported SVGA3D_TRANSFORM_TEXTUREx transform!!\n"));
4748 return VERR_INVALID_PARAMETER;
4749
4750 case SVGA3D_TRANSFORM_WORLD:
4751 /* View * World = Model View */
4752 glMatrixMode(GL_MODELVIEW);
4753 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].fValid)
4754 glLoadMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
4755 else
4756 glLoadIdentity();
4757 glMultMatrixf(matrix);
4758 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4759 fModelViewChanged = true;
4760 break;
4761
4762 case SVGA3D_TRANSFORM_WORLD1:
4763 case SVGA3D_TRANSFORM_WORLD2:
4764 case SVGA3D_TRANSFORM_WORLD3:
4765 Log(("vmsvga3dSetTransform: unsupported SVGA3D_TRANSFORM_WORLDx transform!!\n"));
4766 return VERR_INVALID_PARAMETER;
4767
4768 default:
4769 Log(("vmsvga3dSetTransform: unknown type!!\n"));
4770 return VERR_INVALID_PARAMETER;
4771 }
4772
4773 /* Apparently we need to reset the light and clip data after modifying the modelview matrix. */
4774 if (fModelViewChanged)
4775 {
4776 /* Reprogram the clip planes. */
4777 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aClipPlane); j++)
4778 {
4779 if (pContext->state.aClipPlane[j].fValid == true)
4780 vmsvga3dSetClipPlane(pThis, cid, j, pContext->state.aClipPlane[j].plane);
4781 }
4782
4783 /* Reprogram the light data. */
4784 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aLightData); j++)
4785 {
4786 if (pContext->state.aLightData[j].fValidData == true)
4787 vmsvga3dSetLightData(pThis, cid, j, &pContext->state.aLightData[j].data);
4788 }
4789 }
4790
4791 return VINF_SUCCESS;
4792}
4793
4794int vmsvga3dSetZRange(PVGASTATE pThis, uint32_t cid, SVGA3dZRange zRange)
4795{
4796 PVMSVGA3DCONTEXT pContext;
4797 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4798 AssertReturn(pState, VERR_NO_MEMORY);
4799
4800 Log(("vmsvga3dSetZRange cid=%x min=%d max=%d\n", cid, (uint32_t)(zRange.min * 100.0), (uint32_t)(zRange.max * 100.0)));
4801
4802 if ( cid >= pState->cContexts
4803 || pState->papContexts[cid]->id != cid)
4804 {
4805 Log(("vmsvga3dSetZRange invalid context id!\n"));
4806 return VERR_INVALID_PARAMETER;
4807 }
4808 pContext = pState->papContexts[cid];
4809 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4810
4811 pContext->state.zRange = zRange;
4812 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_ZRANGE;
4813
4814 if (zRange.min < -1.0)
4815 zRange.min = -1.0;
4816 if (zRange.max > 1.0)
4817 zRange.max = 1.0;
4818
4819 glDepthRange((GLdouble)zRange.min, (GLdouble)zRange.max);
4820 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4821 return VINF_SUCCESS;
4822}
4823
4824/**
4825 * Convert SVGA blend op value to its OpenGL equivalent
4826 */
4827static GLenum vmsvga3dBlendOp2GL(uint32_t blendOp)
4828{
4829 switch (blendOp)
4830 {
4831 case SVGA3D_BLENDOP_ZERO:
4832 return GL_ZERO;
4833 case SVGA3D_BLENDOP_ONE:
4834 return GL_ONE;
4835 case SVGA3D_BLENDOP_SRCCOLOR:
4836 return GL_SRC_COLOR;
4837 case SVGA3D_BLENDOP_INVSRCCOLOR:
4838 return GL_ONE_MINUS_SRC_COLOR;
4839 case SVGA3D_BLENDOP_SRCALPHA:
4840 return GL_SRC_ALPHA;
4841 case SVGA3D_BLENDOP_INVSRCALPHA:
4842 return GL_ONE_MINUS_SRC_ALPHA;
4843 case SVGA3D_BLENDOP_DESTALPHA:
4844 return GL_DST_ALPHA;
4845 case SVGA3D_BLENDOP_INVDESTALPHA:
4846 return GL_ONE_MINUS_DST_ALPHA;
4847 case SVGA3D_BLENDOP_DESTCOLOR:
4848 return GL_DST_COLOR;
4849 case SVGA3D_BLENDOP_INVDESTCOLOR:
4850 return GL_ONE_MINUS_DST_COLOR;
4851 case SVGA3D_BLENDOP_SRCALPHASAT:
4852 return GL_SRC_ALPHA_SATURATE;
4853 case SVGA3D_BLENDOP_BLENDFACTOR:
4854 return GL_CONSTANT_ALPHA; /* @todo correct?? */
4855 case SVGA3D_BLENDOP_INVBLENDFACTOR:
4856 return GL_ONE_MINUS_CONSTANT_ALPHA; /* @todo correct?? */
4857 default:
4858 AssertFailed();
4859 return GL_ONE;
4860 }
4861}
4862
4863static GLenum vmsvga3dBlendEquation2GL(uint32_t blendEq)
4864{
4865 switch (blendEq)
4866 {
4867 case SVGA3D_BLENDEQ_ADD:
4868 return GL_FUNC_ADD;
4869 case SVGA3D_BLENDEQ_SUBTRACT:
4870 return GL_FUNC_SUBTRACT;
4871 case SVGA3D_BLENDEQ_REVSUBTRACT:
4872 return GL_FUNC_REVERSE_SUBTRACT;
4873 case SVGA3D_BLENDEQ_MINIMUM:
4874 return GL_MIN;
4875 case SVGA3D_BLENDEQ_MAXIMUM:
4876 return GL_MAX;
4877 default:
4878 AssertFailed();
4879 return GL_FUNC_ADD;
4880 }
4881}
4882
4883static GLenum vmsvgaCmpFunc2GL(uint32_t cmpFunc)
4884{
4885 switch (cmpFunc)
4886 {
4887 case SVGA3D_CMP_NEVER:
4888 return GL_NEVER;
4889 case SVGA3D_CMP_LESS:
4890 return GL_LESS;
4891 case SVGA3D_CMP_EQUAL:
4892 return GL_EQUAL;
4893 case SVGA3D_CMP_LESSEQUAL:
4894 return GL_LEQUAL;
4895 case SVGA3D_CMP_GREATER:
4896 return GL_GREATER;
4897 case SVGA3D_CMP_NOTEQUAL:
4898 return GL_NOTEQUAL;
4899 case SVGA3D_CMP_GREATEREQUAL:
4900 return GL_GEQUAL;
4901 case SVGA3D_CMP_ALWAYS:
4902 return GL_ALWAYS;
4903 default:
4904 AssertFailed();
4905 return GL_LESS;
4906 }
4907}
4908
4909static GLenum vmsvgaStencipOp2GL(uint32_t stencilOp)
4910{
4911 switch (stencilOp)
4912 {
4913 case SVGA3D_STENCILOP_KEEP:
4914 return GL_KEEP;
4915 case SVGA3D_STENCILOP_ZERO:
4916 return GL_ZERO;
4917 case SVGA3D_STENCILOP_REPLACE:
4918 return GL_REPLACE;
4919 case SVGA3D_STENCILOP_INCRSAT:
4920 return GL_INCR_WRAP;
4921 case SVGA3D_STENCILOP_DECRSAT:
4922 return GL_DECR_WRAP;
4923 case SVGA3D_STENCILOP_INVERT:
4924 return GL_INVERT;
4925 case SVGA3D_STENCILOP_INCR:
4926 return GL_INCR;
4927 case SVGA3D_STENCILOP_DECR:
4928 return GL_DECR;
4929 default:
4930 AssertFailed();
4931 return GL_KEEP;
4932 }
4933}
4934
4935int vmsvga3dSetRenderState(PVGASTATE pThis, uint32_t cid, uint32_t cRenderStates, SVGA3dRenderState *pRenderState)
4936{
4937 uint32_t val;
4938 PVMSVGA3DCONTEXT pContext;
4939 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4940 AssertReturn(pState, VERR_NO_MEMORY);
4941
4942 Log(("vmsvga3dSetRenderState cid=%x cRenderStates=%d\n", cid, cRenderStates));
4943
4944 if ( cid >= pState->cContexts
4945 || pState->papContexts[cid]->id != cid)
4946 {
4947 Log(("vmsvga3dSetRenderState invalid context id!\n"));
4948 return VERR_INVALID_PARAMETER;
4949 }
4950 pContext = pState->papContexts[cid];
4951 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4952
4953 for (unsigned i = 0; i < cRenderStates; i++)
4954 {
4955 GLenum enableCap = ~0U;
4956 Log(("vmsvga3dSetRenderState: cid=%x state=%s (%d) val=%x\n", cid, vmsvga3dGetRenderStateName(pRenderState[i].state), pRenderState[i].state, pRenderState[i].uintValue));
4957 /* Save the render state for vm state saving. */
4958 if (pRenderState[i].state < SVGA3D_RS_MAX)
4959 pContext->state.aRenderState[pRenderState[i].state] = pRenderState[i];
4960
4961 switch (pRenderState[i].state)
4962 {
4963 case SVGA3D_RS_ZENABLE: /* SVGA3dBool */
4964 enableCap = GL_DEPTH_TEST;
4965 val = pRenderState[i].uintValue;
4966 break;
4967
4968 case SVGA3D_RS_ZWRITEENABLE: /* SVGA3dBool */
4969 glDepthMask(!!pRenderState[i].uintValue);
4970 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4971 break;
4972
4973 case SVGA3D_RS_ALPHATESTENABLE: /* SVGA3dBool */
4974 enableCap = GL_ALPHA_TEST;
4975 val = pRenderState[i].uintValue;
4976 break;
4977
4978 case SVGA3D_RS_DITHERENABLE: /* SVGA3dBool */
4979 enableCap = GL_DITHER;
4980 val = pRenderState[i].uintValue;
4981 break;
4982
4983 case SVGA3D_RS_FOGENABLE: /* SVGA3dBool */
4984 enableCap = GL_FOG;
4985 val = pRenderState[i].uintValue;
4986 break;
4987
4988 case SVGA3D_RS_SPECULARENABLE: /* SVGA3dBool */
4989 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
4990 break;
4991
4992 case SVGA3D_RS_LIGHTINGENABLE: /* SVGA3dBool */
4993 enableCap = GL_LIGHTING;
4994 val = pRenderState[i].uintValue;
4995 break;
4996
4997 case SVGA3D_RS_NORMALIZENORMALS: /* SVGA3dBool */
4998 /* not applicable */
4999 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
5000 break;
5001
5002 case SVGA3D_RS_POINTSPRITEENABLE: /* SVGA3dBool */
5003 enableCap = GL_POINT_SPRITE_ARB;
5004 val = pRenderState[i].uintValue;
5005 break;
5006
5007 case SVGA3D_RS_POINTSIZE: /* float */
5008 /* @todo we need to apply scaling for point sizes below the min or above the max; see Wine) */
5009 if (pRenderState[i].floatValue < pState->caps.flPointSize[0])
5010 pRenderState[i].floatValue = pState->caps.flPointSize[0];
5011 if (pRenderState[i].floatValue > pState->caps.flPointSize[1])
5012 pRenderState[i].floatValue = pState->caps.flPointSize[1];
5013
5014 glPointSize(pRenderState[i].floatValue);
5015 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5016 Log(("SVGA3D_RS_POINTSIZE: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
5017 break;
5018
5019 case SVGA3D_RS_POINTSIZEMIN: /* float */
5020 pState->ext.glPointParameterf(GL_POINT_SIZE_MIN, pRenderState[i].floatValue);
5021 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5022 Log(("SVGA3D_RS_POINTSIZEMIN: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
5023 break;
5024
5025 case SVGA3D_RS_POINTSIZEMAX: /* float */
5026 pState->ext.glPointParameterf(GL_POINT_SIZE_MAX, pRenderState[i].floatValue);
5027 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5028 Log(("SVGA3D_RS_POINTSIZEMAX: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
5029 break;
5030
5031 case SVGA3D_RS_POINTSCALEENABLE: /* SVGA3dBool */
5032 case SVGA3D_RS_POINTSCALE_A: /* float */
5033 case SVGA3D_RS_POINTSCALE_B: /* float */
5034 case SVGA3D_RS_POINTSCALE_C: /* float */
5035 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
5036 break;
5037
5038 case SVGA3D_RS_AMBIENT: /* SVGA3dColor */
5039 {
5040 GLfloat color[4]; /* red, green, blue, alpha */
5041
5042 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &color[0], &color[1], &color[2], &color[3]);
5043
5044 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, color);
5045 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5046 break;
5047 }
5048
5049 case SVGA3D_RS_CLIPPLANEENABLE: /* SVGA3dClipPlanes */
5050 {
5051 AssertCompile(SVGA3D_CLIPPLANE_MAX == (1 << 5));
5052 for (uint32_t j = 0; j <= 5; j++)
5053 {
5054 if (pRenderState[i].uintValue & RT_BIT(j))
5055 glEnable(GL_CLIP_PLANE0 + j);
5056 else
5057 glDisable(GL_CLIP_PLANE0 + j);
5058 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5059 }
5060 break;
5061 }
5062
5063 case SVGA3D_RS_FOGCOLOR: /* SVGA3dColor */
5064 {
5065 GLfloat color[4]; /* red, green, blue, alpha */
5066
5067 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &color[0], &color[1], &color[2], &color[3]);
5068
5069 glFogfv(GL_FOG_COLOR, color);
5070 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5071 break;
5072 }
5073
5074 case SVGA3D_RS_FOGSTART: /* float */
5075 glFogf(GL_FOG_START, pRenderState[i].floatValue);
5076 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5077 break;
5078
5079 case SVGA3D_RS_FOGEND: /* float */
5080 glFogf(GL_FOG_END, pRenderState[i].floatValue);
5081 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5082 break;
5083
5084 case SVGA3D_RS_FOGDENSITY: /* float */
5085 glFogf(GL_FOG_DENSITY, pRenderState[i].floatValue);
5086 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5087 break;
5088
5089 case SVGA3D_RS_RANGEFOGENABLE: /* SVGA3dBool */
5090 glFogi(GL_FOG_COORD_SRC, (pRenderState[i].uintValue) ? GL_FOG_COORD : GL_FRAGMENT_DEPTH);
5091 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5092 break;
5093
5094 case SVGA3D_RS_FOGMODE: /* SVGA3dFogMode */
5095 {
5096 SVGA3dFogMode mode;
5097 mode.uintValue = pRenderState[i].uintValue;
5098
5099 enableCap = GL_FOG_MODE;
5100 switch (mode.s.function)
5101 {
5102 case SVGA3D_FOGFUNC_EXP:
5103 val = GL_EXP;
5104 break;
5105 case SVGA3D_FOGFUNC_EXP2:
5106 val = GL_EXP2;
5107 break;
5108 case SVGA3D_FOGFUNC_LINEAR:
5109 val = GL_LINEAR;
5110 break;
5111 default:
5112 AssertMsgFailedReturn(("Unexpected fog function %d\n", mode.s.function), VERR_INTERNAL_ERROR);
5113 break;
5114 }
5115
5116 /* @todo how to switch between vertex and pixel fog modes??? */
5117 Assert(mode.s.type == SVGA3D_FOGTYPE_PIXEL);
5118#if 0
5119 /* The fog type determines the render state. */
5120 switch (mode.s.type)
5121 {
5122 case SVGA3D_FOGTYPE_VERTEX:
5123 renderState = D3DRS_FOGVERTEXMODE;
5124 break;
5125 case SVGA3D_FOGTYPE_PIXEL:
5126 renderState = D3DRS_FOGTABLEMODE;
5127 break;
5128 default:
5129 AssertMsgFailedReturn(("Unexpected fog type %d\n", mode.s.type), VERR_INTERNAL_ERROR);
5130 break;
5131 }
5132#endif
5133
5134 /* Set the fog base to depth or range. */
5135 switch (mode.s.base)
5136 {
5137 case SVGA3D_FOGBASE_DEPTHBASED:
5138 glFogi(GL_FOG_COORD_SRC, GL_FRAGMENT_DEPTH);
5139 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5140 break;
5141 case SVGA3D_FOGBASE_RANGEBASED:
5142 glFogi(GL_FOG_COORD_SRC, GL_FOG_COORD);
5143 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5144 break;
5145 default:
5146 /* ignore */
5147 AssertMsgFailed(("Unexpected fog base %d\n", mode.s.base));
5148 break;
5149 }
5150 break;
5151 }
5152
5153 case SVGA3D_RS_FILLMODE: /* SVGA3dFillMode */
5154 {
5155 SVGA3dFillMode mode;
5156
5157 mode.uintValue = pRenderState[i].uintValue;
5158
5159 switch (mode.s.mode)
5160 {
5161 case SVGA3D_FILLMODE_POINT:
5162 val = GL_POINT;
5163 break;
5164 case SVGA3D_FILLMODE_LINE:
5165 val = GL_LINE;
5166 break;
5167 case SVGA3D_FILLMODE_FILL:
5168 val = GL_FILL;
5169 break;
5170 default:
5171 AssertMsgFailedReturn(("Unexpected fill mode %d\n", mode.s.mode), VERR_INTERNAL_ERROR);
5172 break;
5173 }
5174 /* @note only front and back faces */
5175 Assert(mode.s.face == SVGA3D_FACE_FRONT_BACK);
5176 glPolygonMode(GL_FRONT_AND_BACK, val);
5177 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5178 break;
5179 }
5180
5181 case SVGA3D_RS_SHADEMODE: /* SVGA3dShadeMode */
5182 switch (pRenderState[i].uintValue)
5183 {
5184 case SVGA3D_SHADEMODE_FLAT:
5185 val = GL_FLAT;
5186 break;
5187
5188 case SVGA3D_SHADEMODE_SMOOTH:
5189 val = GL_SMOOTH;
5190 break;
5191
5192 default:
5193 AssertMsgFailedReturn(("Unexpected shade mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
5194 break;
5195 }
5196
5197 glShadeModel(val);
5198 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5199 break;
5200
5201 case SVGA3D_RS_LINEPATTERN: /* SVGA3dLinePattern */
5202 /* No longer supported by d3d; mesagl comments suggest not all backends support it */
5203 /* @todo */
5204 Log(("WARNING: SVGA3D_RS_LINEPATTERN %x not supported!!\n", pRenderState[i].uintValue));
5205 /*
5206 renderState = D3DRS_LINEPATTERN;
5207 val = pRenderState[i].uintValue;
5208 */
5209 break;
5210
5211 case SVGA3D_RS_LINEAA: /* SVGA3dBool */
5212 enableCap = GL_LINE_SMOOTH;
5213 val = pRenderState[i].uintValue;
5214 break;
5215
5216 case SVGA3D_RS_LINEWIDTH: /* float */
5217 glLineWidth(pRenderState[i].floatValue);
5218 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5219 break;
5220
5221 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
5222 {
5223 /* Refresh the blending state based on the new enable setting. */
5224 SVGA3dRenderState renderstate[2];
5225
5226 renderstate[0].state = SVGA3D_RS_SRCBLEND;
5227 renderstate[0].uintValue = pContext->state.aRenderState[SVGA3D_RS_SRCBLEND].uintValue;
5228 renderstate[1].state = SVGA3D_RS_BLENDEQUATION;
5229 renderstate[1].uintValue = pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATION].uintValue;
5230
5231 int rc = vmsvga3dSetRenderState(pThis, cid, 2, renderstate);
5232 AssertRCReturn(rc, rc);
5233
5234 if (pContext->state.aRenderState[SVGA3D_RS_BLENDENABLE].uintValue != 0)
5235 continue; /* ignore if blend is already enabled */
5236 /* no break */
5237 }
5238
5239 case SVGA3D_RS_BLENDENABLE: /* SVGA3dBool */
5240 enableCap = GL_BLEND;
5241 val = pRenderState[i].uintValue;
5242 break;
5243
5244 case SVGA3D_RS_SRCBLENDALPHA: /* SVGA3dBlendOp */
5245 case SVGA3D_RS_DSTBLENDALPHA: /* SVGA3dBlendOp */
5246 case SVGA3D_RS_SRCBLEND: /* SVGA3dBlendOp */
5247 case SVGA3D_RS_DSTBLEND: /* SVGA3dBlendOp */
5248 {
5249 GLint srcRGB, srcAlpha, dstRGB, dstAlpha;
5250 GLint blendop = vmsvga3dBlendOp2GL(pRenderState[i].uintValue);
5251
5252 glGetIntegerv(GL_BLEND_SRC_RGB, &srcRGB);
5253 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5254 glGetIntegerv(GL_BLEND_DST_RGB, &dstRGB);
5255 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5256 glGetIntegerv(GL_BLEND_DST_ALPHA, &dstAlpha);
5257 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5258 glGetIntegerv(GL_BLEND_SRC_ALPHA, &srcAlpha);
5259 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5260
5261 switch (pRenderState[i].state)
5262 {
5263 case SVGA3D_RS_SRCBLEND:
5264 srcRGB = blendop;
5265 break;
5266 case SVGA3D_RS_DSTBLEND:
5267 dstRGB = blendop;
5268 break;
5269 case SVGA3D_RS_SRCBLENDALPHA:
5270 srcAlpha = blendop;
5271 break;
5272 case SVGA3D_RS_DSTBLENDALPHA:
5273 dstAlpha = blendop;
5274 break;
5275 default:
5276 /* not possible; shut up gcc */
5277 AssertFailed();
5278 break;
5279 }
5280
5281 if (pContext->state.aRenderState[SVGA3D_RS_SEPARATEALPHABLENDENABLE].uintValue != 0)
5282 pState->ext.glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
5283 else
5284 glBlendFunc(srcRGB, dstRGB);
5285 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5286 break;
5287 }
5288
5289 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation */
5290 case SVGA3D_RS_BLENDEQUATION: /* SVGA3dBlendEquation */
5291 if (pContext->state.aRenderState[SVGA3D_RS_SEPARATEALPHABLENDENABLE].uintValue != 0)
5292 pState->ext.glBlendEquationSeparate(vmsvga3dBlendEquation2GL(pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATION].uintValue),
5293 vmsvga3dBlendEquation2GL(pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATIONALPHA].uintValue));
5294 else
5295 {
5296#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x102
5297 glBlendEquation(vmsvga3dBlendEquation2GL(pRenderState[i].uintValue));
5298#else
5299 pState->ext.glBlendEquation(vmsvga3dBlendEquation2GL(pRenderState[i].uintValue));
5300#endif
5301 }
5302 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5303 break;
5304
5305 case SVGA3D_RS_BLENDCOLOR: /* SVGA3dColor */
5306 {
5307 GLfloat red, green, blue, alpha;
5308
5309 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &red, &green, &blue, &alpha);
5310
5311#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x102
5312 glBlendColor(red, green, blue, alpha);
5313#else
5314 pState->ext.glBlendColor(red, green, blue, alpha);
5315#endif
5316 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5317 break;
5318 }
5319
5320 case SVGA3D_RS_CULLMODE: /* SVGA3dFace */
5321 {
5322 GLenum mode = GL_BACK; /* default for OpenGL */
5323
5324 switch (pRenderState[i].uintValue)
5325 {
5326 case SVGA3D_FACE_NONE:
5327 break;
5328 case SVGA3D_FACE_FRONT:
5329 mode = GL_FRONT;
5330 break;
5331 case SVGA3D_FACE_BACK:
5332 mode = GL_BACK;
5333 break;
5334 case SVGA3D_FACE_FRONT_BACK:
5335 mode = GL_FRONT_AND_BACK;
5336 break;
5337 default:
5338 AssertMsgFailedReturn(("Unexpected cull mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
5339 break;
5340 }
5341 enableCap = GL_CULL_FACE;
5342 if (pRenderState[i].uintValue != SVGA3D_FACE_NONE)
5343 {
5344 glCullFace(mode);
5345 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5346 val = 1;
5347 }
5348 else
5349 val = 0;
5350 break;
5351 }
5352
5353 case SVGA3D_RS_ZFUNC: /* SVGA3dCmpFunc */
5354 glDepthFunc(vmsvgaCmpFunc2GL(pRenderState[i].uintValue));
5355 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5356 break;
5357
5358 case SVGA3D_RS_ALPHAFUNC: /* SVGA3dCmpFunc */
5359 {
5360 GLclampf ref;
5361
5362 glGetFloatv(GL_ALPHA_TEST_REF, &ref);
5363 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5364 glAlphaFunc(vmsvgaCmpFunc2GL(pRenderState[i].uintValue), ref);
5365 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5366 break;
5367 }
5368
5369 case SVGA3D_RS_ALPHAREF: /* float (0.0 .. 1.0) */
5370 {
5371 GLint func;
5372
5373 glGetIntegerv(GL_ALPHA_TEST_FUNC, &func);
5374 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5375 glAlphaFunc(func, pRenderState[i].floatValue);
5376 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5377 break;
5378 }
5379
5380 case SVGA3D_RS_STENCILENABLE: /* SVGA3dBool */
5381 enableCap = GL_STENCIL_TEST;
5382 val = pRenderState[i].uintValue;
5383 break;
5384
5385 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc */
5386 case SVGA3D_RS_STENCILREF: /* uint32_t */
5387 case SVGA3D_RS_STENCILMASK: /* uint32_t */
5388 {
5389 GLint func, ref;
5390 GLuint mask;
5391
5392 glGetIntegerv(GL_STENCIL_FUNC, &func);
5393 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5394 glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)&mask);
5395 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5396 glGetIntegerv(GL_STENCIL_REF, &ref);
5397 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5398
5399 switch (pRenderState[i].state)
5400 {
5401 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc */
5402 func = vmsvgaCmpFunc2GL(pRenderState[i].uintValue);
5403 break;
5404
5405 case SVGA3D_RS_STENCILREF: /* uint32_t */
5406 ref = pRenderState[i].uintValue;
5407 break;
5408
5409 case SVGA3D_RS_STENCILMASK: /* uint32_t */
5410 mask = pRenderState[i].uintValue;
5411 break;
5412
5413 default:
5414 /* not possible; shut up gcc */
5415 AssertFailed();
5416 break;
5417 }
5418
5419 glStencilFunc(func, ref, mask);
5420 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5421 break;
5422 }
5423
5424 case SVGA3D_RS_STENCILWRITEMASK: /* uint32_t */
5425 glStencilMask(pRenderState[i].uintValue);
5426 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5427 break;
5428
5429 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp */
5430 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp */
5431 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp */
5432 {
5433 GLint sfail, dpfail, dppass;
5434 GLenum stencilop = vmsvgaStencipOp2GL(pRenderState[i].uintValue);
5435
5436 glGetIntegerv(GL_STENCIL_FAIL, &sfail);
5437 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5438 glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, &dpfail);
5439 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5440 glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, &dppass);
5441 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5442
5443 switch (pRenderState[i].state)
5444 {
5445 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp */
5446 sfail = stencilop;
5447 break;
5448 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp */
5449 dpfail = stencilop;
5450 break;
5451 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp */
5452 dppass = stencilop;
5453 break;
5454 default:
5455 /* not possible; shut up gcc */
5456 AssertFailed();
5457 break;
5458 }
5459 glStencilOp(sfail, dpfail, dppass);
5460 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5461 break;
5462 }
5463
5464 case SVGA3D_RS_STENCILENABLE2SIDED: /* SVGA3dBool */
5465 /* @note GL_EXT_stencil_two_side required! */
5466 if (pState->ext.fEXT_stencil_two_side)
5467 {
5468 enableCap = GL_STENCIL_TEST_TWO_SIDE_EXT;
5469 val = pRenderState[i].uintValue;
5470 }
5471 else
5472 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_STENCILENABLE2SIDED\n"));
5473 break;
5474
5475 case SVGA3D_RS_CCWSTENCILFUNC: /* SVGA3dCmpFunc */
5476 {
5477 /* @todo SVGA3D_RS_STENCILFAIL/ZFAIL/PASS for front & back faces
5478 * SVGA3D_RS_CCWSTENCILFAIL/ZFAIL/PASS for back faces ??
5479 */
5480 GLint ref;
5481 GLuint mask;
5482
5483 glGetIntegerv(GL_STENCIL_BACK_VALUE_MASK, (GLint *)&mask);
5484 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5485 glGetIntegerv(GL_STENCIL_BACK_REF, &ref);
5486 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5487
5488 pState->ext.glStencilFuncSeparate(GL_BACK, vmsvgaCmpFunc2GL(pRenderState[i].uintValue), ref, mask);
5489 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5490 break;
5491 }
5492
5493 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp */
5494 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp */
5495 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp */
5496 {
5497 /* @todo SVGA3D_RS_STENCILFAIL/ZFAIL/PASS for front & back faces
5498 * SVGA3D_RS_CCWSTENCILFAIL/ZFAIL/PASS for back faces ??
5499 */
5500 GLint sfail, dpfail, dppass;
5501 GLenum stencilop = vmsvgaStencipOp2GL(pRenderState[i].uintValue);
5502
5503 glGetIntegerv(GL_STENCIL_BACK_FAIL, &sfail);
5504 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5505 glGetIntegerv(GL_STENCIL_BACK_PASS_DEPTH_FAIL, &dpfail);
5506 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5507 glGetIntegerv(GL_STENCIL_BACK_PASS_DEPTH_PASS, &dppass);
5508 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5509
5510 switch (pRenderState[i].state)
5511 {
5512 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp */
5513 sfail = stencilop;
5514 break;
5515 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp */
5516 dpfail = stencilop;
5517 break;
5518 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp */
5519 dppass = stencilop;
5520 break;
5521 default:
5522 /* not possible; shut up gcc */
5523 AssertFailed();
5524 break;
5525 }
5526 pState->ext.glStencilOpSeparate(GL_BACK, sfail, dpfail, dppass);
5527 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5528 break;
5529 }
5530
5531 case SVGA3D_RS_ZBIAS: /* float */
5532 /* @todo unknown meaning; depth bias is not identical
5533 renderState = D3DRS_DEPTHBIAS;
5534 val = pRenderState[i].uintValue;
5535 */
5536 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_ZBIAS\n"));
5537 break;
5538
5539 case SVGA3D_RS_DEPTHBIAS: /* float */
5540 {
5541 GLfloat factor;
5542
5543 /* @todo not sure if the d3d & ogl definitions are identical. */
5544
5545 /* Do not change the factor part. */
5546 glGetFloatv(GL_POLYGON_OFFSET_FACTOR, &factor);
5547 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5548
5549 glPolygonOffset(factor, pRenderState[i].floatValue);
5550 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5551 break;
5552 }
5553
5554 case SVGA3D_RS_SLOPESCALEDEPTHBIAS: /* float */
5555 {
5556 GLfloat units;
5557
5558 /* @todo not sure if the d3d & ogl definitions are identical. */
5559
5560 /* Do not change the factor part. */
5561 glGetFloatv(GL_POLYGON_OFFSET_UNITS, &units);
5562 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5563
5564 glPolygonOffset(pRenderState[i].floatValue, units);
5565 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5566 break;
5567 }
5568
5569 case SVGA3D_RS_COLORWRITEENABLE: /* SVGA3dColorMask */
5570 {
5571 GLboolean red, green, blue, alpha;
5572 SVGA3dColorMask mask;
5573
5574 mask.uintValue = pRenderState[i].uintValue;
5575
5576 red = mask.s.red;
5577 green = mask.s.green;
5578 blue = mask.s.blue;
5579 alpha = mask.s.alpha;
5580
5581 glColorMask(red, green, blue, alpha);
5582 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5583 break;
5584 }
5585
5586 case SVGA3D_RS_COLORWRITEENABLE1: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5587 case SVGA3D_RS_COLORWRITEENABLE2: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5588 case SVGA3D_RS_COLORWRITEENABLE3: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5589 Log(("vmsvga3dSetRenderState: WARNING SVGA3D_RS_COLORWRITEENABLEx not supported!!\n"));
5590 break;
5591
5592 case SVGA3D_RS_SCISSORTESTENABLE: /* SVGA3dBool */
5593 enableCap = GL_SCISSOR_TEST;
5594 val = pRenderState[i].uintValue;
5595 break;
5596
5597#if 0
5598 case SVGA3D_RS_DIFFUSEMATERIALSOURCE: /* SVGA3dVertexMaterial */
5599 AssertCompile(D3DMCS_COLOR2 == SVGA3D_VERTEXMATERIAL_SPECULAR);
5600 renderState = D3DRS_DIFFUSEMATERIALSOURCE;
5601 val = pRenderState[i].uintValue;
5602 break;
5603
5604 case SVGA3D_RS_SPECULARMATERIALSOURCE: /* SVGA3dVertexMaterial */
5605 renderState = D3DRS_SPECULARMATERIALSOURCE;
5606 val = pRenderState[i].uintValue;
5607 break;
5608
5609 case SVGA3D_RS_AMBIENTMATERIALSOURCE: /* SVGA3dVertexMaterial */
5610 renderState = D3DRS_AMBIENTMATERIALSOURCE;
5611 val = pRenderState[i].uintValue;
5612 break;
5613
5614 case SVGA3D_RS_EMISSIVEMATERIALSOURCE: /* SVGA3dVertexMaterial */
5615 renderState = D3DRS_EMISSIVEMATERIALSOURCE;
5616 val = pRenderState[i].uintValue;
5617 break;
5618#endif
5619
5620 case SVGA3D_RS_WRAP3: /* SVGA3dWrapFlags */
5621 case SVGA3D_RS_WRAP4: /* SVGA3dWrapFlags */
5622 case SVGA3D_RS_WRAP5: /* SVGA3dWrapFlags */
5623 case SVGA3D_RS_WRAP6: /* SVGA3dWrapFlags */
5624 case SVGA3D_RS_WRAP7: /* SVGA3dWrapFlags */
5625 case SVGA3D_RS_WRAP8: /* SVGA3dWrapFlags */
5626 case SVGA3D_RS_WRAP9: /* SVGA3dWrapFlags */
5627 case SVGA3D_RS_WRAP10: /* SVGA3dWrapFlags */
5628 case SVGA3D_RS_WRAP11: /* SVGA3dWrapFlags */
5629 case SVGA3D_RS_WRAP12: /* SVGA3dWrapFlags */
5630 case SVGA3D_RS_WRAP13: /* SVGA3dWrapFlags */
5631 case SVGA3D_RS_WRAP14: /* SVGA3dWrapFlags */
5632 case SVGA3D_RS_WRAP15: /* SVGA3dWrapFlags */
5633 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_WRAPx (x >= 3)\n"));
5634 break;
5635
5636 case SVGA3D_RS_LASTPIXEL: /* SVGA3dBool */
5637 case SVGA3D_RS_TWEENFACTOR: /* float */
5638 case SVGA3D_RS_INDEXEDVERTEXBLENDENABLE: /* SVGA3dBool */
5639 case SVGA3D_RS_VERTEXBLEND: /* SVGA3dVertexBlendFlags */
5640 Log(("vmsvga3dSetRenderState: WARNING not applicable!!\n"));
5641 break;
5642
5643 case SVGA3D_RS_MULTISAMPLEANTIALIAS: /* SVGA3dBool */
5644 enableCap = GL_MULTISAMPLE;
5645 val = pRenderState[i].uintValue;
5646 break;
5647
5648 case SVGA3D_RS_MULTISAMPLEMASK: /* uint32_t */
5649 case SVGA3D_RS_ANTIALIASEDLINEENABLE: /* SVGA3dBool */
5650 Log(("vmsvga3dSetRenderState: WARNING not applicable??!!\n"));
5651 break;
5652
5653 case SVGA3D_RS_COORDINATETYPE: /* SVGA3dCoordinateType */
5654 Assert(pRenderState[i].uintValue == SVGA3D_COORDINATE_LEFTHANDED);
5655 /* @todo setup a view matrix to scale the world space by -1 in the z-direction for right handed coordinates. */
5656 /*
5657 renderState = D3DRS_COORDINATETYPE;
5658 val = pRenderState[i].uintValue;
5659 */
5660 break;
5661
5662 case SVGA3D_RS_FRONTWINDING: /* SVGA3dFrontWinding */
5663 Assert(pRenderState[i].uintValue == SVGA3D_FRONTWINDING_CW);
5664 /* Invert the selected mode because of y-inversion (?) */
5665 glFrontFace((pRenderState[i].uintValue != SVGA3D_FRONTWINDING_CW) ? GL_CW : GL_CCW);
5666 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5667 break;
5668
5669 case SVGA3D_RS_OUTPUTGAMMA: /* float */
5670 //AssertFailed();
5671 /*
5672 D3DRS_SRGBWRITEENABLE ??
5673 renderState = D3DRS_OUTPUTGAMMA;
5674 val = pRenderState[i].uintValue;
5675 */
5676 break;
5677
5678#if 0
5679
5680 case SVGA3D_RS_VERTEXMATERIALENABLE: /* SVGA3dBool */
5681 //AssertFailed();
5682 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE; /* correct?? */
5683 val = pRenderState[i].uintValue;
5684 break;
5685
5686 case SVGA3D_RS_TEXTUREFACTOR: /* SVGA3dColor */
5687 renderState = D3DRS_TEXTUREFACTOR;
5688 val = pRenderState[i].uintValue;
5689 break;
5690
5691 case SVGA3D_RS_LOCALVIEWER: /* SVGA3dBool */
5692 renderState = D3DRS_LOCALVIEWER;
5693 val = pRenderState[i].uintValue;
5694 break;
5695
5696 case SVGA3D_RS_ZVISIBLE: /* SVGA3dBool */
5697 AssertFailed();
5698 /*
5699 renderState = D3DRS_ZVISIBLE;
5700 val = pRenderState[i].uintValue;
5701 */
5702 break;
5703
5704 case SVGA3D_RS_CLIPPING: /* SVGA3dBool */
5705 renderState = D3DRS_CLIPPING;
5706 val = pRenderState[i].uintValue;
5707 break;
5708
5709 case SVGA3D_RS_WRAP0: /* SVGA3dWrapFlags */
5710 glTexParameter GL_TEXTURE_WRAP_S
5711 Assert(SVGA3D_WRAPCOORD_3 == D3DWRAPCOORD_3);
5712 renderState = D3DRS_WRAP0;
5713 val = pRenderState[i].uintValue;
5714 break;
5715
5716 case SVGA3D_RS_WRAP1: /* SVGA3dWrapFlags */
5717 glTexParameter GL_TEXTURE_WRAP_T
5718 renderState = D3DRS_WRAP1;
5719 val = pRenderState[i].uintValue;
5720 break;
5721
5722 case SVGA3D_RS_WRAP2: /* SVGA3dWrapFlags */
5723 glTexParameter GL_TEXTURE_WRAP_R
5724 renderState = D3DRS_WRAP2;
5725 val = pRenderState[i].uintValue;
5726 break;
5727
5728
5729 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
5730 renderState = D3DRS_SEPARATEALPHABLENDENABLE;
5731 val = pRenderState[i].uintValue;
5732 break;
5733
5734
5735 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation */
5736 renderState = D3DRS_BLENDOPALPHA;
5737 val = pRenderState[i].uintValue;
5738 break;
5739
5740 case SVGA3D_RS_TRANSPARENCYANTIALIAS: /* SVGA3dTransparencyAntialiasType */
5741 AssertFailed();
5742 /*
5743 renderState = D3DRS_TRANSPARENCYANTIALIAS;
5744 val = pRenderState[i].uintValue;
5745 */
5746 break;
5747
5748#endif
5749 default:
5750 AssertFailed();
5751 break;
5752 }
5753
5754 if (enableCap != ~0U)
5755 {
5756 if (val)
5757 glEnable(enableCap);
5758 else
5759 glDisable(enableCap);
5760 }
5761 }
5762
5763 return VINF_SUCCESS;
5764}
5765
5766int vmsvga3dSetRenderTarget(PVGASTATE pThis, uint32_t cid, SVGA3dRenderTargetType type, SVGA3dSurfaceImageId target)
5767{
5768 PVMSVGA3DCONTEXT pContext;
5769 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
5770 PVMSVGA3DSURFACE pRenderTarget;
5771
5772 AssertReturn(pState, VERR_NO_MEMORY);
5773 AssertReturn(type < SVGA3D_RT_MAX, VERR_INVALID_PARAMETER);
5774 AssertReturn(target.face == 0, VERR_INVALID_PARAMETER);
5775 AssertReturn(target.mipmap == 0, VERR_INVALID_PARAMETER);
5776
5777 Log(("vmsvga3dSetRenderTarget cid=%x type=%x surface id=%x\n", cid, type, target.sid));
5778
5779 if ( cid >= pState->cContexts
5780 || pState->papContexts[cid]->id != cid)
5781 {
5782 Log(("vmsvga3dSetRenderTarget invalid context id!\n"));
5783 return VERR_INVALID_PARAMETER;
5784 }
5785 pContext = pState->papContexts[cid];
5786 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5787
5788 /* Save for vm state save/restore. */
5789 pContext->state.aRenderTargets[type] = target.sid;
5790
5791 if (target.sid == SVGA3D_INVALID_ID)
5792 {
5793 /* Disable render target. */
5794 switch (type)
5795 {
5796 case SVGA3D_RT_DEPTH:
5797 case SVGA3D_RT_STENCIL:
5798 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER, (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
5799 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5800 break;
5801
5802 case SVGA3D_RT_COLOR0:
5803 case SVGA3D_RT_COLOR1:
5804 case SVGA3D_RT_COLOR2:
5805 case SVGA3D_RT_COLOR3:
5806 case SVGA3D_RT_COLOR4:
5807 case SVGA3D_RT_COLOR5:
5808 case SVGA3D_RT_COLOR6:
5809 case SVGA3D_RT_COLOR7:
5810 pContext->sidRenderTarget = SVGA3D_INVALID_ID;
5811 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0, 0, 0, 0);
5812 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5813 break;
5814
5815 default:
5816 AssertFailedReturn(VERR_INVALID_PARAMETER);
5817 }
5818 return VINF_SUCCESS;
5819 }
5820
5821 AssertReturn(target.sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
5822 AssertReturn(target.sid < pState->cSurfaces && pState->papSurfaces[target.sid]->id == target.sid, VERR_INVALID_PARAMETER);
5823 pRenderTarget = pState->papSurfaces[target.sid];
5824
5825 switch (type)
5826 {
5827 case SVGA3D_RT_DEPTH:
5828 case SVGA3D_RT_STENCIL:
5829 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
5830 {
5831 Log(("vmsvga3dSetRenderTarget: create renderbuffer to be used as render target; surface id=%x type=%d format=%d\n", target.sid, pRenderTarget->flags, pRenderTarget->internalFormatGL));
5832#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
5833 pContext = &pState->SharedCtx;
5834 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5835#endif
5836 pState->ext.glGenRenderbuffers(1, &pRenderTarget->oglId.renderbuffer);
5837 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5838
5839 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5840 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5841
5842 pState->ext.glRenderbufferStorage(GL_RENDERBUFFER,
5843 pRenderTarget->internalFormatGL,
5844 pRenderTarget->pMipmapLevels[0].size.width,
5845 pRenderTarget->pMipmapLevels[0].size.height);
5846 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5847
5848#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
5849 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, OPENGL_INVALID_ID);
5850 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5851
5852 pContext = pState->papContexts[cid];
5853 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5854#else
5855 LogFlow(("vmsvga3dSetRenderTarget: sid=%x idAssociatedContext %#x -> %#x\n", pRenderTarget->id, pRenderTarget->idAssociatedContext, cid));
5856 pRenderTarget->idAssociatedContext = cid;
5857#endif
5858 }
5859#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
5860 else
5861#endif
5862 {
5863 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5864 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5865 }
5866#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
5867 Assert(pRenderTarget->idAssociatedContext == cid);
5868#endif
5869 Assert(!pRenderTarget->fDirty);
5870 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
5871
5872 pRenderTarget->flags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
5873
5874 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
5875 (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT,
5876 GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5877 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5878 break;
5879
5880 case SVGA3D_RT_COLOR0:
5881 case SVGA3D_RT_COLOR1:
5882 case SVGA3D_RT_COLOR2:
5883 case SVGA3D_RT_COLOR3:
5884 case SVGA3D_RT_COLOR4:
5885 case SVGA3D_RT_COLOR5:
5886 case SVGA3D_RT_COLOR6:
5887 case SVGA3D_RT_COLOR7:
5888 {
5889 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
5890 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
5891 {
5892 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));
5893 int rc = vmsvga3dCreateTexture(pState, pContext, cid, pRenderTarget);
5894 AssertRCReturn(rc, rc);
5895 }
5896
5897 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
5898 Assert(!pRenderTarget->fDirty);
5899
5900 pRenderTarget->flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
5901
5902 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0, GL_TEXTURE_2D, pRenderTarget->oglId.texture, target.mipmap);
5903 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5904
5905 pContext->sidRenderTarget = target.sid;
5906
5907#ifdef DEBUG
5908 GLenum status = pState->ext.glCheckFramebufferStatus(GL_FRAMEBUFFER);
5909 if (status != GL_FRAMEBUFFER_COMPLETE)
5910 Log(("vmsvga3dSetRenderTarget: WARNING: glCheckFramebufferStatus returned %x\n", status));
5911#endif
5912 /* @todo use glDrawBuffers too? */
5913 break;
5914 }
5915
5916 default:
5917 AssertFailedReturn(VERR_INVALID_PARAMETER);
5918 }
5919
5920 return VINF_SUCCESS;
5921}
5922
5923#if 0
5924/**
5925 * Convert SVGA texture combiner value to its D3D equivalent
5926 */
5927static DWORD vmsvga3dTextureCombiner2D3D(uint32_t value)
5928{
5929 switch (value)
5930 {
5931 case SVGA3D_TC_DISABLE:
5932 return D3DTOP_DISABLE;
5933 case SVGA3D_TC_SELECTARG1:
5934 return D3DTOP_SELECTARG1;
5935 case SVGA3D_TC_SELECTARG2:
5936 return D3DTOP_SELECTARG2;
5937 case SVGA3D_TC_MODULATE:
5938 return D3DTOP_MODULATE;
5939 case SVGA3D_TC_ADD:
5940 return D3DTOP_ADD;
5941 case SVGA3D_TC_ADDSIGNED:
5942 return D3DTOP_ADDSIGNED;
5943 case SVGA3D_TC_SUBTRACT:
5944 return D3DTOP_SUBTRACT;
5945 case SVGA3D_TC_BLENDTEXTUREALPHA:
5946 return D3DTOP_BLENDTEXTUREALPHA;
5947 case SVGA3D_TC_BLENDDIFFUSEALPHA:
5948 return D3DTOP_BLENDDIFFUSEALPHA;
5949 case SVGA3D_TC_BLENDCURRENTALPHA:
5950 return D3DTOP_BLENDCURRENTALPHA;
5951 case SVGA3D_TC_BLENDFACTORALPHA:
5952 return D3DTOP_BLENDFACTORALPHA;
5953 case SVGA3D_TC_MODULATE2X:
5954 return D3DTOP_MODULATE2X;
5955 case SVGA3D_TC_MODULATE4X:
5956 return D3DTOP_MODULATE4X;
5957 case SVGA3D_TC_DSDT:
5958 AssertFailed(); /* @todo ??? */
5959 return D3DTOP_DISABLE;
5960 case SVGA3D_TC_DOTPRODUCT3:
5961 return D3DTOP_DOTPRODUCT3;
5962 case SVGA3D_TC_BLENDTEXTUREALPHAPM:
5963 return D3DTOP_BLENDTEXTUREALPHAPM;
5964 case SVGA3D_TC_ADDSIGNED2X:
5965 return D3DTOP_ADDSIGNED2X;
5966 case SVGA3D_TC_ADDSMOOTH:
5967 return D3DTOP_ADDSMOOTH;
5968 case SVGA3D_TC_PREMODULATE:
5969 return D3DTOP_PREMODULATE;
5970 case SVGA3D_TC_MODULATEALPHA_ADDCOLOR:
5971 return D3DTOP_MODULATEALPHA_ADDCOLOR;
5972 case SVGA3D_TC_MODULATECOLOR_ADDALPHA:
5973 return D3DTOP_MODULATECOLOR_ADDALPHA;
5974 case SVGA3D_TC_MODULATEINVALPHA_ADDCOLOR:
5975 return D3DTOP_MODULATEINVALPHA_ADDCOLOR;
5976 case SVGA3D_TC_MODULATEINVCOLOR_ADDALPHA:
5977 return D3DTOP_MODULATEINVCOLOR_ADDALPHA;
5978 case SVGA3D_TC_BUMPENVMAPLUMINANCE:
5979 return D3DTOP_BUMPENVMAPLUMINANCE;
5980 case SVGA3D_TC_MULTIPLYADD:
5981 return D3DTOP_MULTIPLYADD;
5982 case SVGA3D_TC_LERP:
5983 return D3DTOP_LERP;
5984 default:
5985 AssertFailed();
5986 return D3DTOP_DISABLE;
5987 }
5988}
5989
5990/**
5991 * Convert SVGA texture arg data value to its D3D equivalent
5992 */
5993static DWORD vmsvga3dTextureArgData2D3D(uint32_t value)
5994{
5995 switch (value)
5996 {
5997 case SVGA3D_TA_CONSTANT:
5998 return D3DTA_CONSTANT;
5999 case SVGA3D_TA_PREVIOUS:
6000 return D3DTA_CURRENT; /* current = previous */
6001 case SVGA3D_TA_DIFFUSE:
6002 return D3DTA_DIFFUSE;
6003 case SVGA3D_TA_TEXTURE:
6004 return D3DTA_TEXTURE;
6005 case SVGA3D_TA_SPECULAR:
6006 return D3DTA_SPECULAR;
6007 default:
6008 AssertFailed();
6009 return 0;
6010 }
6011}
6012
6013/**
6014 * Convert SVGA texture transform flag value to its D3D equivalent
6015 */
6016static DWORD vmsvga3dTextTransformFlags2D3D(uint32_t value)
6017{
6018 switch (value)
6019 {
6020 case SVGA3D_TEX_TRANSFORM_OFF:
6021 return D3DTTFF_DISABLE;
6022 case SVGA3D_TEX_TRANSFORM_S:
6023 return D3DTTFF_COUNT1; /* @todo correct? */
6024 case SVGA3D_TEX_TRANSFORM_T:
6025 return D3DTTFF_COUNT2; /* @todo correct? */
6026 case SVGA3D_TEX_TRANSFORM_R:
6027 return D3DTTFF_COUNT3; /* @todo correct? */
6028 case SVGA3D_TEX_TRANSFORM_Q:
6029 return D3DTTFF_COUNT4; /* @todo correct? */
6030 case SVGA3D_TEX_PROJECTED:
6031 return D3DTTFF_PROJECTED;
6032 default:
6033 AssertFailed();
6034 return 0;
6035 }
6036}
6037#endif
6038
6039static GLenum vmsvga3dTextureAddress2OGL(SVGA3dTextureAddress value)
6040{
6041 switch (value)
6042 {
6043 case SVGA3D_TEX_ADDRESS_WRAP:
6044 return GL_REPEAT;
6045 case SVGA3D_TEX_ADDRESS_MIRROR:
6046 return GL_MIRRORED_REPEAT;
6047 case SVGA3D_TEX_ADDRESS_CLAMP:
6048 return GL_CLAMP_TO_EDGE;
6049 case SVGA3D_TEX_ADDRESS_BORDER:
6050 return GL_CLAMP_TO_BORDER;
6051 case SVGA3D_TEX_ADDRESS_MIRRORONCE:
6052 AssertFailed();
6053 return GL_CLAMP_TO_EDGE_SGIS; /* @todo correct? */
6054
6055 case SVGA3D_TEX_ADDRESS_EDGE:
6056 case SVGA3D_TEX_ADDRESS_INVALID:
6057 default:
6058 AssertFailed();
6059 return GL_REPEAT; /* default */
6060 }
6061}
6062
6063static GLenum vmsvga3dTextureFilter2OGL(SVGA3dTextureFilter value)
6064{
6065 switch (value)
6066 {
6067 case SVGA3D_TEX_FILTER_NONE:
6068 case SVGA3D_TEX_FILTER_LINEAR:
6069 return GL_LINEAR;
6070 case SVGA3D_TEX_FILTER_NEAREST:
6071 return GL_NEAREST;
6072 case SVGA3D_TEX_FILTER_ANISOTROPIC:
6073 /* @todo */
6074 case SVGA3D_TEX_FILTER_FLATCUBIC: // Deprecated, not implemented
6075 case SVGA3D_TEX_FILTER_GAUSSIANCUBIC: // Deprecated, not implemented
6076 case SVGA3D_TEX_FILTER_PYRAMIDALQUAD: // Not currently implemented
6077 case SVGA3D_TEX_FILTER_GAUSSIANQUAD: // Not currently implemented
6078 default:
6079 AssertFailed();
6080 return GL_LINEAR; /* default */
6081 }
6082}
6083
6084uint32_t vmsvga3dSVGA3dColor2RGBA(SVGA3dColor value)
6085{
6086 /* flip the red and blue bytes */
6087 uint8_t blue = value & 0xff;
6088 uint8_t red = (value >> 16) & 0xff;
6089 return (value & 0xff00ff00) | red | (blue << 16);
6090}
6091
6092int vmsvga3dSetTextureState(PVGASTATE pThis, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
6093{
6094 GLenum val;
6095 GLenum currentStage = ~0L;
6096 PVMSVGA3DCONTEXT pContext;
6097 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6098 AssertReturn(pState, VERR_NO_MEMORY);
6099
6100 Log(("vmsvga3dSetTextureState %x cTextureState=%d\n", cid, cTextureStates));
6101
6102 if ( cid >= pState->cContexts
6103 || pState->papContexts[cid]->id != cid)
6104 {
6105 Log(("vmsvga3dSetTextureState invalid context id!\n"));
6106 return VERR_INVALID_PARAMETER;
6107 }
6108 pContext = pState->papContexts[cid];
6109 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6110
6111 for (unsigned i = 0; i < cTextureStates; i++)
6112 {
6113 GLenum textureType = ~0U;
6114 GLenum samplerType = ~0U;
6115
6116 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));
6117 /* Record the texture state for vm state saving. */
6118 if ( pTextureState[i].stage < SVGA3D_MAX_TEXTURE_STAGE
6119 && pTextureState[i].name < SVGA3D_TS_MAX)
6120 {
6121 pContext->state.aTextureState[pTextureState[i].stage][pTextureState[i].name] = pTextureState[i];
6122 }
6123
6124 /* Active the right texture unit for subsequent texture state changes. */
6125 if (pTextureState[i].stage != currentStage || i == 0)
6126 {
6127 /** @todo Is this the appropriate limit for all kinds of textures? It is the
6128 * size of aSidActiveTexture and for binding/unbinding we cannot exceed it. */
6129 if (pTextureState[i].stage < SVGA3D_MAX_TEXTURE_STAGE)
6130 {
6131 pState->ext.glActiveTexture(GL_TEXTURE0 + pTextureState[i].stage);
6132 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6133 currentStage = pTextureState[i].stage;
6134 }
6135 else
6136 {
6137 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x\n", i, pTextureState[i].stage, pTextureState[i].name));
6138 continue;
6139 }
6140 }
6141
6142 switch (pTextureState[i].name)
6143 {
6144 case SVGA3D_TS_BUMPENVMAT00: /* float */
6145 case SVGA3D_TS_BUMPENVMAT01: /* float */
6146 case SVGA3D_TS_BUMPENVMAT10: /* float */
6147 case SVGA3D_TS_BUMPENVMAT11: /* float */
6148 case SVGA3D_TS_BUMPENVLSCALE: /* float */
6149 case SVGA3D_TS_BUMPENVLOFFSET: /* float */
6150 Log(("vmsvga3dSetTextureState: bump mapping texture options not supported!!\n"));
6151 break;
6152
6153 case SVGA3D_TS_COLOROP: /* SVGA3dTextureCombiner */
6154 case SVGA3D_TS_COLORARG0: /* SVGA3dTextureArgData */
6155 case SVGA3D_TS_COLORARG1: /* SVGA3dTextureArgData */
6156 case SVGA3D_TS_COLORARG2: /* SVGA3dTextureArgData */
6157 case SVGA3D_TS_ALPHAOP: /* SVGA3dTextureCombiner */
6158 case SVGA3D_TS_ALPHAARG0: /* SVGA3dTextureArgData */
6159 case SVGA3D_TS_ALPHAARG1: /* SVGA3dTextureArgData */
6160 case SVGA3D_TS_ALPHAARG2: /* SVGA3dTextureArgData */
6161 /* @todo; not used by MesaGL */
6162 Log(("vmsvga3dSetTextureState: colorop/alphaop not yet supported!!\n"));
6163 break;
6164#if 0
6165
6166 case SVGA3D_TS_TEXCOORDINDEX: /* uint32_t */
6167 textureType = D3DTSS_TEXCOORDINDEX;
6168 val = pTextureState[i].value;
6169 break;
6170
6171 case SVGA3D_TS_TEXTURETRANSFORMFLAGS: /* SVGA3dTexTransformFlags */
6172 textureType = D3DTSS_TEXTURETRANSFORMFLAGS;
6173 val = vmsvga3dTextTransformFlags2D3D(pTextureState[i].value);
6174 break;
6175#endif
6176
6177 case SVGA3D_TS_BIND_TEXTURE: /* SVGA3dSurfaceId */
6178 if (pTextureState[i].value == SVGA3D_INVALID_ID)
6179 {
6180 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture surface id=%x replacing=%x\n",
6181 currentStage, pTextureState[i].value, pContext->aSidActiveTexture[currentStage]));
6182
6183 pContext->aSidActiveTexture[currentStage] = SVGA3D_INVALID_ID;
6184 /* Unselect the currently associated texture. */
6185 glBindTexture(GL_TEXTURE_2D, 0);
6186 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6187 /* Necessary for the fixed pipeline. */
6188 glDisable(GL_TEXTURE_2D);
6189 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6190 }
6191 else
6192 {
6193 uint32_t sid = pTextureState[i].value;
6194
6195 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
6196 AssertReturn(sid < pState->cSurfaces && pState->papSurfaces[sid]->id == sid, VERR_INVALID_PARAMETER);
6197
6198 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
6199
6200 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture surface id=%x (%d,%d) replacing=%x\n",
6201 currentStage, pTextureState[i].value, pSurface->pMipmapLevels[0].size.width,
6202 pSurface->pMipmapLevels[0].size.height, pContext->aSidActiveTexture[currentStage]));
6203
6204 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
6205 {
6206#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
6207 Assert(pSurface->idAssociatedContext == SVGA3D_INVALID_ID);
6208#endif
6209 Log(("CreateTexture (%d,%d) level=%d\n", pSurface->pMipmapLevels[0].size.width, pSurface->pMipmapLevels[0].size.height, pSurface->faces[0].numMipLevels));
6210 int rc = vmsvga3dCreateTexture(pState, pContext, cid, pSurface);
6211 AssertRCReturn(rc, rc);
6212 }
6213
6214 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
6215 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6216
6217 /* Necessary for the fixed pipeline. */
6218 glEnable(GL_TEXTURE_2D);
6219 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6220
6221 if (pContext->aSidActiveTexture[currentStage] != sid)
6222 {
6223 /* Recreate the texture state as glBindTexture resets them all (sigh). */
6224 for (uint32_t iStage = 0; iStage < SVGA3D_MAX_TEXTURE_STAGE; iStage++)
6225 {
6226 for (uint32_t j = 0; j < SVGA3D_TS_MAX; j++)
6227 {
6228 SVGA3dTextureState *pTextureStateIter = &pContext->state.aTextureState[iStage][j];
6229
6230 if ( pTextureStateIter->name != SVGA3D_TS_INVALID
6231 && pTextureStateIter->name != SVGA3D_TS_BIND_TEXTURE)
6232 vmsvga3dSetTextureState(pThis, pContext->id, 1, pTextureStateIter);
6233 }
6234 }
6235 }
6236 pContext->aSidActiveTexture[currentStage] = sid;
6237 }
6238 /* Finished; continue with the next one. */
6239 continue;
6240
6241 case SVGA3D_TS_ADDRESSW: /* SVGA3dTextureAddress */
6242 textureType = GL_TEXTURE_WRAP_R; /* R = W */
6243 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
6244 break;
6245
6246 case SVGA3D_TS_ADDRESSU: /* SVGA3dTextureAddress */
6247 textureType = GL_TEXTURE_WRAP_S; /* S = U */
6248 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
6249 break;
6250
6251 case SVGA3D_TS_ADDRESSV: /* SVGA3dTextureAddress */
6252 textureType = GL_TEXTURE_WRAP_T; /* T = V */
6253 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
6254 break;
6255
6256 case SVGA3D_TS_MIPFILTER: /* SVGA3dTextureFilter */
6257 case SVGA3D_TS_MINFILTER: /* SVGA3dTextureFilter */
6258 {
6259 uint32_t mipFilter = pContext->state.aTextureState[currentStage][SVGA3D_TS_MIPFILTER].value;
6260 uint32_t minFilter = pContext->state.aTextureState[currentStage][SVGA3D_TS_MINFILTER].value;
6261
6262 /* If SVGA3D_TS_MIPFILTER is set to NONE, then use SVGA3D_TS_MIPFILTER, otherwise SVGA3D_TS_MIPFILTER enables mipmap minification. */
6263 textureType = GL_TEXTURE_MIN_FILTER;
6264 if (mipFilter != SVGA3D_TEX_FILTER_NONE)
6265 {
6266 if (minFilter == SVGA3D_TEX_FILTER_NEAREST)
6267 {
6268 if (mipFilter == SVGA3D_TEX_FILTER_LINEAR)
6269 val = GL_NEAREST_MIPMAP_LINEAR;
6270 else
6271 val = GL_NEAREST_MIPMAP_NEAREST;
6272 }
6273 else
6274 {
6275 if (mipFilter == SVGA3D_TEX_FILTER_LINEAR)
6276 val = GL_LINEAR_MIPMAP_LINEAR;
6277 else
6278 val = GL_LINEAR_MIPMAP_NEAREST;
6279 }
6280 }
6281 else
6282 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)minFilter);
6283 break;
6284 }
6285
6286 case SVGA3D_TS_MAGFILTER: /* SVGA3dTextureFilter */
6287 textureType = GL_TEXTURE_MAG_FILTER;
6288 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)pTextureState[i].value);
6289 Assert(val == GL_NEAREST || val == GL_LINEAR);
6290 break;
6291
6292 case SVGA3D_TS_BORDERCOLOR: /* SVGA3dColor */
6293 {
6294 GLfloat color[4]; /* red, green, blue, alpha */
6295
6296 vmsvgaColor2GLFloatArray(pTextureState[i].value, &color[0], &color[1], &color[2], &color[3]);
6297
6298 glTexParameterfv(GL_TEXTURE_2D /* @todo flexible type */, GL_TEXTURE_BORDER_COLOR, color); /* Identical; default 0.0 identical too */
6299 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6300 break;
6301 }
6302
6303 case SVGA3D_TS_TEXTURE_LOD_BIAS: /* float */
6304 glTexParameterf(GL_TEXTURE_2D /* @todo flexible type */, GL_TEXTURE_LOD_BIAS, pTextureState[i].value); /* Identical; default 0.0 identical too */
6305 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6306 break;
6307
6308 case SVGA3D_TS_TEXTURE_MIPMAP_LEVEL: /* uint32_t */
6309 textureType = GL_TEXTURE_BASE_LEVEL;
6310 val = pTextureState[i].value;
6311 break;
6312
6313#if 0
6314 case SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL: /* uint32_t */
6315 samplerType = D3DSAMP_MAXANISOTROPY;
6316 val = pTextureState[i].value; /* Identical?? */
6317 break;
6318
6319 case SVGA3D_TS_GAMMA: /* float */
6320 samplerType = D3DSAMP_SRGBTEXTURE;
6321 /* Boolean in D3D */
6322 if (pTextureState[i].floatValue == 1.0f)
6323 val = FALSE;
6324 else
6325 val = TRUE;
6326 break;
6327#endif
6328 /* Internal commands, that don't map directly to the SetTextureStageState API. */
6329 case SVGA3D_TS_TEXCOORDGEN: /* SVGA3dTextureCoordGen */
6330 AssertFailed();
6331 break;
6332
6333 default:
6334 //AssertFailed();
6335 break;
6336 }
6337
6338 if (textureType != ~0U)
6339 {
6340 glTexParameteri(GL_TEXTURE_2D /* @todo flexible type */, textureType, val);
6341 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6342 }
6343 }
6344
6345 return VINF_SUCCESS;
6346}
6347
6348int vmsvga3dSetMaterial(PVGASTATE pThis, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
6349{
6350 PVMSVGA3DCONTEXT pContext;
6351 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6352 AssertReturn(pState, VERR_NO_MEMORY);
6353 GLenum oglFace;
6354
6355 Log(("vmsvga3dSetMaterial cid=%x face %d\n", cid, face));
6356
6357 if ( cid >= pState->cContexts
6358 || pState->papContexts[cid]->id != cid)
6359 {
6360 Log(("vmsvga3dSetMaterial invalid context id!\n"));
6361 return VERR_INVALID_PARAMETER;
6362 }
6363 pContext = pState->papContexts[cid];
6364 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6365
6366 switch (face)
6367 {
6368 case SVGA3D_FACE_NONE:
6369 case SVGA3D_FACE_FRONT:
6370 oglFace = GL_FRONT;
6371 break;
6372
6373 case SVGA3D_FACE_BACK:
6374 oglFace = GL_BACK;
6375 break;
6376
6377 case SVGA3D_FACE_FRONT_BACK:
6378 oglFace = GL_FRONT_AND_BACK;
6379 break;
6380
6381 default:
6382 AssertFailedReturn(VERR_INVALID_PARAMETER);
6383 }
6384
6385 /* Save for vm state save/restore. */
6386 pContext->state.aMaterial[face].fValid = true;
6387 pContext->state.aMaterial[face].material = *pMaterial;
6388 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_MATERIAL;
6389
6390 glMaterialfv(oglFace, GL_DIFFUSE, pMaterial->diffuse);
6391 glMaterialfv(oglFace, GL_AMBIENT, pMaterial->ambient);
6392 glMaterialfv(oglFace, GL_SPECULAR, pMaterial->specular);
6393 glMaterialfv(oglFace, GL_EMISSION, pMaterial->emissive);
6394 glMaterialfv(oglFace, GL_SHININESS, &pMaterial->shininess);
6395 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6396
6397 return VINF_SUCCESS;
6398}
6399
6400/* @todo Move into separate library as we are using logic from Wine here. */
6401int vmsvga3dSetLightData(PVGASTATE pThis, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
6402{
6403 PVMSVGA3DCONTEXT pContext;
6404 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6405 AssertReturn(pState, VERR_NO_MEMORY);
6406 float QuadAttenuation;
6407
6408 Log(("vmsvga3dSetLightData cid=%x index=%d type=%d\n", cid, index, pData->type));
6409
6410 if ( cid >= pState->cContexts
6411 || pState->papContexts[cid]->id != cid)
6412 {
6413 Log(("vmsvga3dSetLightData invalid context id!\n"));
6414 return VERR_INVALID_PARAMETER;
6415 }
6416 pContext = pState->papContexts[cid];
6417 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6418
6419 /* Store for vm state save/restore */
6420 if (index < SVGA3D_MAX_LIGHTS)
6421 {
6422 pContext->state.aLightData[index].fValidData = true;
6423 pContext->state.aLightData[index].data = *pData;
6424 }
6425 else
6426 AssertFailed();
6427
6428 if ( pData->attenuation0 < 0.0f
6429 || pData->attenuation1 < 0.0f
6430 || pData->attenuation2 < 0.0f)
6431 {
6432 Log(("vmsvga3dSetLightData: invalid negative attenuation values!!\n"));
6433 return VINF_SUCCESS; /* ignore; could crash the GL driver */
6434 }
6435
6436 /* Light settings are affected by the model view in OpenGL, the View transform in direct3d */
6437 glMatrixMode(GL_MODELVIEW);
6438 glPushMatrix();
6439 glLoadMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
6440
6441 glLightfv(GL_LIGHT0 + index, GL_DIFFUSE, pData->diffuse);
6442 glLightfv(GL_LIGHT0 + index, GL_SPECULAR, pData->specular);
6443 glLightfv(GL_LIGHT0 + index, GL_AMBIENT, pData->ambient);
6444
6445 if (pData->range * pData->range >= FLT_MIN)
6446 QuadAttenuation = 1.4f / (pData->range * pData->range);
6447 else
6448 QuadAttenuation = 0.0f;
6449
6450 switch (pData->type)
6451 {
6452 case SVGA3D_LIGHTTYPE_POINT:
6453 {
6454 GLfloat position[4];
6455
6456 position[0] = pData->position[0];
6457 position[1] = pData->position[1];
6458 position[2] = pData->position[2];
6459 position[3] = 1.0f;
6460
6461 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
6462 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6463
6464 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
6465 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6466
6467 /* Attenuation - Are these right? guessing... */
6468 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
6469 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6470
6471 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
6472 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6473
6474 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
6475 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6476
6477 /* @todo range */
6478 break;
6479 }
6480
6481 case SVGA3D_LIGHTTYPE_SPOT1:
6482 {
6483 GLfloat exponent;
6484 GLfloat position[4];
6485 const GLfloat pi = 4.0f * atanf(1.0f);
6486
6487 position[0] = pData->position[0];
6488 position[1] = pData->position[1];
6489 position[2] = pData->position[2];
6490 position[3] = 1.0f;
6491
6492 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
6493 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6494
6495 position[0] = pData->direction[0];
6496 position[1] = pData->direction[1];
6497 position[2] = pData->direction[2];
6498 position[3] = 1.0f;
6499
6500 glLightfv(GL_LIGHT0 + index, GL_SPOT_DIRECTION, position);
6501 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6502
6503 /*
6504 * opengl-ish and d3d-ish spot lights use too different models for the
6505 * light "intensity" as a function of the angle towards the main light direction,
6506 * so we only can approximate very roughly.
6507 * however spot lights are rather rarely used in games (if ever used at all).
6508 * furthermore if still used, probably nobody pays attention to such details.
6509 */
6510 if (pData->falloff == 0)
6511 {
6512 /* Falloff = 0 is easy, because d3d's and opengl's spot light equations have the
6513 * falloff resp. exponent parameter as an exponent, so the spot light lighting
6514 * will always be 1.0 for both of them, and we don't have to care for the
6515 * rest of the rather complex calculation
6516 */
6517 exponent = 0.0f;
6518 }
6519 else
6520 {
6521 float rho = pData->theta + (pData->phi - pData->theta) / (2 * pData->falloff);
6522 if (rho < 0.0001f)
6523 rho = 0.0001f;
6524 exponent = -0.3f/log(cos(rho/2));
6525 }
6526 if (exponent > 128.0f)
6527 exponent = 128.0f;
6528
6529 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, exponent);
6530 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6531
6532 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, pData->phi * 90.0 / pi);
6533 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6534
6535 /* Attenuation - Are these right? guessing... */
6536 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
6537 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6538
6539 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
6540 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6541
6542 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
6543 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6544
6545 /* @todo range */
6546 break;
6547 }
6548
6549 case SVGA3D_LIGHTTYPE_DIRECTIONAL:
6550 {
6551 GLfloat position[4];
6552
6553 position[0] = -pData->direction[0];
6554 position[1] = -pData->direction[1];
6555 position[2] = -pData->direction[2];
6556 position[3] = 0.0f;
6557
6558 glLightfv(GL_LIGHT0 + index, GL_POSITION, position); /* Note gl uses w position of 0 for direction! */
6559 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6560
6561 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
6562 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6563
6564 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, 0.0f);
6565 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6566 break;
6567 }
6568
6569 case SVGA3D_LIGHTTYPE_SPOT2:
6570 default:
6571 Log(("Unsupported light type!!\n"));
6572 return VERR_INVALID_PARAMETER;
6573 }
6574
6575 /* Restore the modelview matrix */
6576 glPopMatrix();
6577
6578 return VINF_SUCCESS;
6579}
6580
6581int vmsvga3dSetLightEnabled(PVGASTATE pThis, uint32_t cid, uint32_t index, uint32_t enabled)
6582{
6583 PVMSVGA3DCONTEXT pContext;
6584 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6585 AssertReturn(pState, VERR_NO_MEMORY);
6586
6587 Log(("vmsvga3dSetLightEnabled cid=%x %d -> %d\n", cid, index, enabled));
6588
6589 if ( cid >= pState->cContexts
6590 || pState->papContexts[cid]->id != cid)
6591 {
6592 Log(("vmsvga3dSetLightEnabled invalid context id!\n"));
6593 return VERR_INVALID_PARAMETER;
6594 }
6595 pContext = pState->papContexts[cid];
6596 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6597
6598 /* Store for vm state save/restore */
6599 if (index < SVGA3D_MAX_LIGHTS)
6600 pContext->state.aLightData[index].fEnabled = !!enabled;
6601 else
6602 AssertFailed();
6603
6604 if (enabled)
6605 {
6606 /* Load the default settings if none have been set yet. */
6607 if (!pContext->state.aLightData[index].fValidData)
6608 vmsvga3dSetLightData(pThis, cid, index, (SVGA3dLightData *)&vmsvga3d_default_light);
6609 glEnable(GL_LIGHT0 + index);
6610 }
6611 else
6612 glDisable(GL_LIGHT0 + index);
6613
6614 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6615 return VINF_SUCCESS;
6616}
6617
6618int vmsvga3dSetViewPort(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
6619{
6620 PVMSVGA3DCONTEXT pContext;
6621 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6622 AssertReturn(pState, VERR_NO_MEMORY);
6623
6624 Log(("vmsvga3dSetViewPort cid=%x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
6625
6626 if ( cid >= pState->cContexts
6627 || pState->papContexts[cid]->id != cid)
6628 {
6629 Log(("vmsvga3dSetViewPort invalid context id!\n"));
6630 return VERR_INVALID_PARAMETER;
6631 }
6632 pContext = pState->papContexts[cid];
6633 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6634
6635 /* Save for vm state save/restore. */
6636 pContext->state.RectViewPort = *pRect;
6637 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VIEWPORT;
6638
6639 /* @todo y-inversion for partial viewport coordinates? */
6640 glViewport(pRect->x, pRect->y, pRect->w, pRect->h);
6641 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6642
6643 /* Reset the projection matrix as that relies on the viewport setting. */
6644 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].fValid == true)
6645 {
6646 vmsvga3dSetTransform(pThis, cid, SVGA3D_TRANSFORM_PROJECTION, pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].matrix);
6647 }
6648 else
6649 {
6650 float matrix[16];
6651
6652 /* identity matrix if no matrix set. */
6653 memset(matrix, 0, sizeof(matrix));
6654 matrix[0] = 1.0;
6655 matrix[5] = 1.0;
6656 matrix[10] = 1.0;
6657 matrix[15] = 1.0;
6658 vmsvga3dSetTransform(pThis, cid, SVGA3D_TRANSFORM_PROJECTION, matrix);
6659 }
6660
6661 return VINF_SUCCESS;
6662}
6663
6664int vmsvga3dSetClipPlane(PVGASTATE pThis, uint32_t cid, uint32_t index, float plane[4])
6665{
6666 PVMSVGA3DCONTEXT pContext;
6667 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6668 AssertReturn(pState, VERR_NO_MEMORY);
6669 double oglPlane[4];
6670
6671 Log(("vmsvga3dSetClipPlane cid=%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)));
6672 AssertReturn(index < SVGA3D_CLIPPLANE_MAX, VERR_INVALID_PARAMETER);
6673
6674 if ( cid >= pState->cContexts
6675 || pState->papContexts[cid]->id != cid)
6676 {
6677 Log(("vmsvga3dSetClipPlane invalid context id!\n"));
6678 return VERR_INVALID_PARAMETER;
6679 }
6680 pContext = pState->papContexts[cid];
6681 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6682
6683 /* Store for vm state save/restore. */
6684 pContext->state.aClipPlane[index].fValid = true;
6685 memcpy(pContext->state.aClipPlane[index].plane, plane, sizeof(pContext->state.aClipPlane[index].plane));
6686
6687 /** @todo clip plane affected by model view in OpenGL & view in D3D + vertex shader -> not transformed (see Wine; state.c clipplane) */
6688 oglPlane[0] = (double)plane[0];
6689 oglPlane[1] = (double)plane[1];
6690 oglPlane[2] = (double)plane[2];
6691 oglPlane[3] = (double)plane[3];
6692
6693 glClipPlane(GL_CLIP_PLANE0 + index, oglPlane);
6694 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6695
6696 return VINF_SUCCESS;
6697}
6698
6699int vmsvga3dSetScissorRect(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
6700{
6701 PVMSVGA3DCONTEXT pContext;
6702 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6703 AssertReturn(pState, VERR_NO_MEMORY);
6704
6705 Log(("vmsvga3dSetScissorRect cid=%x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
6706
6707 if ( cid >= pState->cContexts
6708 || pState->papContexts[cid]->id != cid)
6709 {
6710 Log(("vmsvga3dSetScissorRect invalid context id!\n"));
6711 return VERR_INVALID_PARAMETER;
6712 }
6713 pContext = pState->papContexts[cid];
6714 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6715
6716 /* Store for vm state save/restore. */
6717 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_SCISSORRECT;
6718 pContext->state.RectScissor = *pRect;
6719
6720 glScissor(pRect->x, pRect->y, pRect->w, pRect->h);
6721 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6722
6723 return VINF_SUCCESS;
6724}
6725
6726static void vmsvgaColor2GLFloatArray(uint32_t color, GLfloat *pRed, GLfloat *pGreen, GLfloat *pBlue, GLfloat *pAlpha)
6727{
6728 /* Convert byte color components to float (0-1.0) */
6729 *pAlpha = (GLfloat)(color >> 24) / 255.0;
6730 *pRed = (GLfloat)((color >> 16) & 0xff) / 255.0;
6731 *pGreen = (GLfloat)((color >> 8) & 0xff) / 255.0;
6732 *pBlue = (GLfloat)(color & 0xff) / 255.0;
6733}
6734
6735int vmsvga3dCommandClear(PVGASTATE pThis, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth, uint32_t stencil,
6736 uint32_t cRects, SVGA3dRect *pRect)
6737{
6738 GLbitfield mask = 0;
6739 PVMSVGA3DCONTEXT pContext;
6740 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6741 AssertReturn(pState, VERR_NO_MEMORY);
6742 GLboolean fDepthWriteEnabled = GL_FALSE;
6743
6744 Log(("vmsvga3dCommandClear cid=%x clearFlag=%x color=%x depth=%d stencil=%x cRects=%d\n", cid, clearFlag, color, (uint32_t)(depth * 100.0), stencil, cRects));
6745
6746 if ( cid >= pState->cContexts
6747 || pState->papContexts[cid]->id != cid)
6748 {
6749 Log(("vmsvga3dCommandClear invalid context id!\n"));
6750 return VERR_INVALID_PARAMETER;
6751 }
6752 pContext = pState->papContexts[cid];
6753 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6754
6755 if (clearFlag & SVGA3D_CLEAR_COLOR)
6756 {
6757 GLfloat red, green, blue, alpha;
6758
6759 vmsvgaColor2GLFloatArray(color, &red, &green, &blue, &alpha);
6760
6761 /* Set the color clear value. */
6762 glClearColor(red, green, blue, alpha);
6763
6764 mask |= GL_COLOR_BUFFER_BIT;
6765 }
6766 if (clearFlag & SVGA3D_CLEAR_STENCIL)
6767 {
6768 /* @todo possibly the same problem as with glDepthMask */
6769 glClearStencil(stencil);
6770 mask |= GL_STENCIL_BUFFER_BIT;
6771 }
6772 if (clearFlag & SVGA3D_CLEAR_DEPTH)
6773 {
6774 glClearDepth((GLdouble)depth);
6775 mask |= GL_DEPTH_BUFFER_BIT;
6776
6777 /* glClear will not clear the depth buffer if writing is disabled. */
6778 glGetBooleanv(GL_DEPTH_WRITEMASK, &fDepthWriteEnabled);
6779 if (fDepthWriteEnabled == GL_FALSE)
6780 glDepthMask(GL_TRUE);
6781 }
6782
6783 if (cRects)
6784 {
6785 /* Save the current scissor test bit and scissor box. */
6786 glPushAttrib(GL_SCISSOR_BIT);
6787 glEnable(GL_SCISSOR_TEST);
6788 for (unsigned i=0; i < cRects; i++)
6789 {
6790 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));
6791 glScissor(pRect[i].x, pRect[i].y, pRect[i].w, pRect[i].h);
6792 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6793 glClear(mask);
6794 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6795 }
6796 /* Restore the old scissor test bit and box */
6797 glPopAttrib();
6798 }
6799 else
6800 {
6801 glClear(mask);
6802 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6803 }
6804
6805 /* Restore depth write state. */
6806 if ( (clearFlag & SVGA3D_CLEAR_DEPTH)
6807 && fDepthWriteEnabled == GL_FALSE)
6808 glDepthMask(GL_FALSE);
6809
6810 return VINF_SUCCESS;
6811}
6812
6813/* Convert VMWare vertex declaration to its OpenGL equivalent. */
6814int vmsvga3dVertexDecl2OGL(SVGA3dVertexArrayIdentity &identity, GLint &size, GLenum &type, GLboolean &normalized)
6815{
6816 normalized = GL_FALSE;
6817 switch (identity.type)
6818 {
6819 case SVGA3D_DECLTYPE_FLOAT1:
6820 size = 1;
6821 type = GL_FLOAT;
6822 break;
6823 case SVGA3D_DECLTYPE_FLOAT2:
6824 size = 2;
6825 type = GL_FLOAT;
6826 break;
6827 case SVGA3D_DECLTYPE_FLOAT3:
6828 size = 3;
6829 type = GL_FLOAT;
6830 break;
6831 case SVGA3D_DECLTYPE_FLOAT4:
6832 size = 4;
6833 type = GL_FLOAT;
6834 break;
6835
6836 case SVGA3D_DECLTYPE_D3DCOLOR:
6837 size = GL_BGRA; /* @note requires GL_ARB_vertex_array_bgra */
6838 type = GL_UNSIGNED_BYTE;
6839 normalized = GL_TRUE; /* glVertexAttribPointer fails otherwise */
6840 break;
6841
6842 case SVGA3D_DECLTYPE_UBYTE4N:
6843 normalized = GL_TRUE;
6844 /* no break */
6845 case SVGA3D_DECLTYPE_UBYTE4:
6846 size = 4;
6847 type = GL_UNSIGNED_BYTE;
6848 break;
6849
6850 case SVGA3D_DECLTYPE_SHORT2N:
6851 normalized = GL_TRUE;
6852 /* no break */
6853 case SVGA3D_DECLTYPE_SHORT2:
6854 size = 2;
6855 type = GL_SHORT;
6856 break;
6857
6858 case SVGA3D_DECLTYPE_SHORT4N:
6859 normalized = GL_TRUE;
6860 /* no break */
6861 case SVGA3D_DECLTYPE_SHORT4:
6862 size = 4;
6863 type = GL_SHORT;
6864 break;
6865
6866 case SVGA3D_DECLTYPE_USHORT4N:
6867 normalized = GL_TRUE;
6868 size = 4;
6869 type = GL_UNSIGNED_SHORT;
6870 break;
6871
6872 case SVGA3D_DECLTYPE_USHORT2N:
6873 normalized = GL_TRUE;
6874 size = 2;
6875 type = GL_UNSIGNED_SHORT;
6876 break;
6877
6878 case SVGA3D_DECLTYPE_UDEC3:
6879 size = 3;
6880 type = GL_UNSIGNED_INT_2_10_10_10_REV; /* @todo correct? */
6881 break;
6882
6883 case SVGA3D_DECLTYPE_DEC3N:
6884 normalized = true;
6885 size = 3;
6886 type = GL_INT_2_10_10_10_REV; /* @todo correct? */
6887 break;
6888
6889 case SVGA3D_DECLTYPE_FLOAT16_2:
6890 size = 2;
6891 type = GL_HALF_FLOAT;
6892 break;
6893 case SVGA3D_DECLTYPE_FLOAT16_4:
6894 size = 4;
6895 type = GL_HALF_FLOAT;
6896 break;
6897 default:
6898 AssertFailedReturn(VERR_INVALID_PARAMETER);
6899 }
6900
6901 //pVertexElement->Method = identity.method;
6902 //pVertexElement->Usage = identity.usage;
6903
6904 return VINF_SUCCESS;
6905}
6906
6907/* Convert VMWare primitive type to its OpenGL equivalent. */
6908/* Calculate the vertex count based on the primitive type and nr of primitives. */
6909int vmsvga3dPrimitiveType2OGL(SVGA3dPrimitiveType PrimitiveType, GLenum *pMode, uint32_t cPrimitiveCount, uint32_t *pcVertices)
6910{
6911 switch (PrimitiveType)
6912 {
6913 case SVGA3D_PRIMITIVE_TRIANGLELIST:
6914 *pMode = GL_TRIANGLES;
6915 *pcVertices = cPrimitiveCount * 3;
6916 break;
6917 case SVGA3D_PRIMITIVE_POINTLIST:
6918 *pMode = GL_POINTS;
6919 *pcVertices = cPrimitiveCount;
6920 break;
6921 case SVGA3D_PRIMITIVE_LINELIST:
6922 *pMode = GL_LINES;
6923 *pcVertices = cPrimitiveCount * 2;
6924 break;
6925 case SVGA3D_PRIMITIVE_LINESTRIP:
6926 *pMode = GL_LINE_STRIP;
6927 *pcVertices = cPrimitiveCount + 1;
6928 break;
6929 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
6930 *pMode = GL_TRIANGLE_STRIP;
6931 *pcVertices = cPrimitiveCount + 2;
6932 break;
6933 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
6934 *pMode = GL_TRIANGLE_FAN;
6935 *pcVertices = cPrimitiveCount + 2;
6936 break;
6937 default:
6938 return VERR_INVALID_PARAMETER;
6939 }
6940 return VINF_SUCCESS;
6941}
6942
6943int vmsvga3dDrawPrimitivesProcessVertexDecls(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t iVertexDeclBase, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl)
6944{
6945 unsigned sidVertex = pVertexDecl[0].array.surfaceId;
6946 PVMSVGA3DSURFACE pVertexSurface;
6947
6948 AssertReturn(sidVertex < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
6949 AssertReturn(sidVertex < pState->cSurfaces && pState->papSurfaces[sidVertex]->id == sidVertex, VERR_INVALID_PARAMETER);
6950
6951 pVertexSurface = pState->papSurfaces[sidVertex];
6952 Log(("vmsvga3dDrawPrimitives: vertex surface %x\n", sidVertex));
6953
6954 /* Create and/or bind the vertex buffer. */
6955 if (pVertexSurface->oglId.buffer == OPENGL_INVALID_ID)
6956 {
6957 Log(("vmsvga3dDrawPrimitives: create vertex buffer fDirty=%d size=%x bytes\n", pVertexSurface->fDirty, pVertexSurface->pMipmapLevels[0].cbSurface));
6958#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
6959 PVMSVGA3DCONTEXT pSavedCtx = pContext;
6960 pContext = &pState->SharedCtx;
6961 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6962#endif
6963
6964 pState->ext.glGenBuffers(1, &pVertexSurface->oglId.buffer);
6965 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6966
6967 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
6968 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6969
6970 Assert(pVertexSurface->fDirty);
6971 /* @todo rethink usage dynamic/static */
6972 pState->ext.glBufferData(GL_ARRAY_BUFFER, pVertexSurface->pMipmapLevels[0].cbSurface, pVertexSurface->pMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
6973 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6974
6975 pVertexSurface->pMipmapLevels[0].fDirty = false;
6976 pVertexSurface->fDirty = false;
6977
6978 pVertexSurface->flags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
6979
6980#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
6981 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, OPENGL_INVALID_ID);
6982 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6983
6984 pContext = pSavedCtx;
6985 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6986#endif
6987 }
6988#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
6989 else
6990#endif
6991 {
6992 Assert(pVertexSurface->fDirty == false);
6993 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
6994 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6995 }
6996#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
6997 pVertexSurface->idAssociatedContext = pContext->id;
6998 LogFlow(("vmsvga3dDrawPrimitivesProcessVertexDecls: sid=%x idAssociatedContext %#x -> %#x\n", pVertexSurface->id, pVertexSurface->idAssociatedContext, pContext->id));
6999#endif
7000
7001 /* Setup the vertex declarations. */
7002 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
7003 {
7004 GLint size;
7005 GLenum type;
7006 GLboolean normalized;
7007 GLuint index = iVertexDeclBase + iVertex;
7008
7009 Log(("vmsvga3dDrawPrimitives: array index %d type=%s (%d) method=%s (%d) usage=%s (%d) usageIndex=%d stride=%d offset=%d\n", index, 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));
7010
7011 int rc = vmsvga3dVertexDecl2OGL(pVertexDecl[iVertex].identity, size, type, normalized);
7012 AssertRCReturn(rc, rc);
7013
7014 if (pContext->state.shidVertex != SVGA_ID_INVALID)
7015 {
7016 /* Use numbered vertex arrays when shaders are active. */
7017 pState->ext.glEnableVertexAttribArray(index);
7018 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7019 pState->ext.glVertexAttribPointer(index, size, type, normalized, pVertexDecl[iVertex].array.stride,
7020 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
7021 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7022 /* case SVGA3D_DECLUSAGE_COLOR: @todo color component order not identical!! test GL_BGRA!! */
7023 }
7024 else
7025 {
7026 /* Use the predefined selection of vertex streams for the fixed pipeline. */
7027 switch (pVertexDecl[iVertex].identity.usage)
7028 {
7029 case SVGA3D_DECLUSAGE_POSITION:
7030 glEnableClientState(GL_VERTEX_ARRAY);
7031 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7032 glVertexPointer(size, type, pVertexDecl[iVertex].array.stride,
7033 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
7034 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7035 break;
7036 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
7037 AssertFailed();
7038 break;
7039 case SVGA3D_DECLUSAGE_BLENDINDICES:
7040 AssertFailed();
7041 break;
7042 case SVGA3D_DECLUSAGE_NORMAL:
7043 glEnableClientState(GL_NORMAL_ARRAY);
7044 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7045 glNormalPointer(type, pVertexDecl[iVertex].array.stride,
7046 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
7047 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7048 break;
7049 case SVGA3D_DECLUSAGE_PSIZE:
7050 AssertFailed();
7051 break;
7052 case SVGA3D_DECLUSAGE_TEXCOORD:
7053 /* Specify the affected texture unit. */
7054#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
7055 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
7056#else
7057 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
7058#endif
7059 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
7060 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7061 glTexCoordPointer(size, type, pVertexDecl[iVertex].array.stride,
7062 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
7063 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7064 break;
7065 case SVGA3D_DECLUSAGE_TANGENT:
7066 AssertFailed();
7067 break;
7068 case SVGA3D_DECLUSAGE_BINORMAL:
7069 AssertFailed();
7070 break;
7071 case SVGA3D_DECLUSAGE_TESSFACTOR:
7072 AssertFailed();
7073 break;
7074 case SVGA3D_DECLUSAGE_POSITIONT:
7075 AssertFailed(); /* see position_transformed in Wine */
7076 break;
7077 case SVGA3D_DECLUSAGE_COLOR: /** @todo color component order not identical!! test GL_BGRA!! */
7078 glEnableClientState(GL_COLOR_ARRAY);
7079 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7080 glColorPointer(size, type, pVertexDecl[iVertex].array.stride,
7081 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
7082 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7083 break;
7084 case SVGA3D_DECLUSAGE_FOG:
7085 glEnableClientState(GL_FOG_COORD_ARRAY);
7086 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7087 pState->ext.glFogCoordPointer(type, pVertexDecl[iVertex].array.stride,
7088 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
7089 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7090 break;
7091 case SVGA3D_DECLUSAGE_DEPTH:
7092 AssertFailed();
7093 break;
7094 case SVGA3D_DECLUSAGE_SAMPLE:
7095 AssertFailed();
7096 break;
7097 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
7098 }
7099 }
7100
7101#ifdef LOG_ENABLED
7102 if (pVertexDecl[iVertex].array.stride == 0)
7103 Log(("vmsvga3dDrawPrimitives: stride == 0! Can be valid\n"));
7104#endif
7105 }
7106
7107 return VINF_SUCCESS;
7108}
7109
7110int vmsvga3dDrawPrimitivesCleanupVertexDecls(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t iVertexDeclBase, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl)
7111{
7112 /* Setup the vertex declarations. */
7113 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
7114 {
7115 if (pContext->state.shidVertex != SVGA_ID_INVALID)
7116 {
7117 /* Use numbered vertex arrays when shaders are active. */
7118 pState->ext.glDisableVertexAttribArray(iVertexDeclBase + iVertex);
7119 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7120 }
7121 else
7122 {
7123 /* Use the predefined selection of vertex streams for the fixed pipeline. */
7124 switch (pVertexDecl[iVertex].identity.usage)
7125 {
7126 case SVGA3D_DECLUSAGE_POSITION:
7127 glDisableClientState(GL_VERTEX_ARRAY);
7128 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7129 break;
7130 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
7131 break;
7132 case SVGA3D_DECLUSAGE_BLENDINDICES:
7133 break;
7134 case SVGA3D_DECLUSAGE_NORMAL:
7135 glDisableClientState(GL_NORMAL_ARRAY);
7136 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7137 break;
7138 case SVGA3D_DECLUSAGE_PSIZE:
7139 break;
7140 case SVGA3D_DECLUSAGE_TEXCOORD:
7141 /* Specify the affected texture unit. */
7142#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
7143 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
7144#else
7145 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
7146#endif
7147 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
7148 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7149 break;
7150 case SVGA3D_DECLUSAGE_TANGENT:
7151 break;
7152 case SVGA3D_DECLUSAGE_BINORMAL:
7153 break;
7154 case SVGA3D_DECLUSAGE_TESSFACTOR:
7155 break;
7156 case SVGA3D_DECLUSAGE_POSITIONT:
7157 break;
7158 case SVGA3D_DECLUSAGE_COLOR: /* @todo color component order not identical!! */
7159 glDisableClientState(GL_COLOR_ARRAY);
7160 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7161 break;
7162 case SVGA3D_DECLUSAGE_FOG:
7163 glDisableClientState(GL_FOG_COORD_ARRAY);
7164 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7165 break;
7166 case SVGA3D_DECLUSAGE_DEPTH:
7167 break;
7168 case SVGA3D_DECLUSAGE_SAMPLE:
7169 break;
7170 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
7171 }
7172 }
7173 }
7174 /* Unbind the vertex buffer after usage. */
7175 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0);
7176 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7177 return VINF_SUCCESS;
7178}
7179
7180int vmsvga3dDrawPrimitives(PVGASTATE pThis, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl, uint32_t numRanges, SVGA3dPrimitiveRange *pRange, uint32_t cVertexDivisor, SVGA3dVertexDivisor *pVertexDivisor)
7181{
7182 PVMSVGA3DCONTEXT pContext;
7183 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
7184 AssertReturn(pState, VERR_INTERNAL_ERROR);
7185 int rc = VERR_NOT_IMPLEMENTED;
7186 uint32_t iCurrentVertex;
7187
7188 Log(("vmsvga3dDrawPrimitives cid=%x numVertexDecls=%d numRanges=%d, cVertexDivisor=%d\n", cid, numVertexDecls, numRanges, cVertexDivisor));
7189
7190 AssertReturn(numVertexDecls && numVertexDecls <= SVGA3D_MAX_VERTEX_ARRAYS, VERR_INVALID_PARAMETER);
7191 AssertReturn(numRanges && numRanges <= SVGA3D_MAX_DRAW_PRIMITIVE_RANGES, VERR_INVALID_PARAMETER);
7192 AssertReturn(!cVertexDivisor || cVertexDivisor == numVertexDecls, VERR_INVALID_PARAMETER);
7193 /* @todo */
7194 Assert(!cVertexDivisor);
7195
7196 if ( cid >= pState->cContexts
7197 || pState->papContexts[cid]->id != cid)
7198 {
7199 Log(("vmsvga3dDrawPrimitives invalid context id!\n"));
7200 return VERR_INVALID_PARAMETER;
7201 }
7202 pContext = pState->papContexts[cid];
7203 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7204
7205 /* Flush any shader changes. */
7206 if (pContext->pShaderContext)
7207 {
7208 uint32_t rtHeight = 0;
7209
7210 if (pContext->sidRenderTarget != SVGA_ID_INVALID)
7211 {
7212 PVMSVGA3DSURFACE pRenderTarget = pState->papSurfaces[pContext->sidRenderTarget];
7213 rtHeight = pRenderTarget->pMipmapLevels[0].size.height;
7214 }
7215
7216 ShaderUpdateState(pContext->pShaderContext, rtHeight);
7217 }
7218
7219 /* Process all vertex declarations. Each vertex buffer is represented by one stream. */
7220 iCurrentVertex = 0;
7221 while (iCurrentVertex < numVertexDecls)
7222 {
7223 uint32_t sidVertex = SVGA_ID_INVALID;
7224 uint32_t iVertex;
7225
7226 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
7227 {
7228 if ( sidVertex != SVGA_ID_INVALID
7229 && pVertexDecl[iVertex].array.surfaceId != sidVertex
7230 )
7231 break;
7232 sidVertex = pVertexDecl[iVertex].array.surfaceId;
7233 }
7234
7235 rc = vmsvga3dDrawPrimitivesProcessVertexDecls(pState, pContext, iCurrentVertex, iVertex - iCurrentVertex, &pVertexDecl[iCurrentVertex]);
7236 AssertRCReturn(rc, rc);
7237
7238 iCurrentVertex = iVertex;
7239 }
7240
7241 /* Now draw the primitives. */
7242 for (unsigned iPrimitive = 0; iPrimitive < numRanges; iPrimitive++)
7243 {
7244 GLenum modeDraw;
7245 unsigned sidIndex = pRange[iPrimitive].indexArray.surfaceId;
7246 PVMSVGA3DSURFACE pIndexSurface = NULL;
7247 unsigned cVertices;
7248
7249 Log(("Primitive %d: type %s\n", iPrimitive, vmsvga3dPrimitiveType2String(pRange[iPrimitive].primType)));
7250 rc = vmsvga3dPrimitiveType2OGL(pRange[iPrimitive].primType, &modeDraw, pRange[iPrimitive].primitiveCount, &cVertices);
7251 if (RT_FAILURE(rc))
7252 {
7253 AssertRC(rc);
7254 goto internal_error;
7255 }
7256
7257 if (sidIndex != SVGA3D_INVALID_ID)
7258 {
7259 AssertMsg(pRange[iPrimitive].indexWidth == sizeof(uint32_t) || pRange[iPrimitive].indexWidth == sizeof(uint16_t), ("Unsupported primitive width %d\n", pRange[iPrimitive].indexWidth));
7260
7261 if ( sidIndex >= SVGA3D_MAX_SURFACE_IDS
7262 || sidIndex >= pState->cSurfaces
7263 || pState->papSurfaces[sidIndex]->id != sidIndex)
7264 {
7265 Assert(sidIndex < SVGA3D_MAX_SURFACE_IDS);
7266 Assert(sidIndex < pState->cSurfaces && pState->papSurfaces[sidIndex]->id == sidIndex);
7267 rc = VERR_INVALID_PARAMETER;
7268 goto internal_error;
7269 }
7270 pIndexSurface = pState->papSurfaces[sidIndex];
7271 Log(("vmsvga3dDrawPrimitives: index surface %x\n", sidIndex));
7272
7273 if (pIndexSurface->oglId.buffer == OPENGL_INVALID_ID)
7274 {
7275 Log(("vmsvga3dDrawPrimitives: create index buffer fDirty=%d size=%x bytes\n", pIndexSurface->fDirty, pIndexSurface->pMipmapLevels[0].cbSurface));
7276#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
7277 pContext = &pState->SharedCtx;
7278 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7279#endif
7280
7281 pState->ext.glGenBuffers(1, &pIndexSurface->oglId.buffer);
7282 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7283
7284 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
7285 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7286
7287 Assert(pIndexSurface->fDirty);
7288
7289 /* @todo rethink usage dynamic/static */
7290 pState->ext.glBufferData(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->pMipmapLevels[0].cbSurface, pIndexSurface->pMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
7291 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7292
7293 pIndexSurface->pMipmapLevels[0].fDirty = false;
7294 pIndexSurface->fDirty = false;
7295
7296 pIndexSurface->flags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
7297
7298#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
7299 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, OPENGL_INVALID_ID);
7300 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7301
7302 pContext = pState->papContexts[cid];
7303 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7304#endif
7305 }
7306#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
7307 else
7308#endif
7309 {
7310 Assert(pIndexSurface->fDirty == false);
7311
7312 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
7313 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7314 }
7315#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
7316 LogFlow(("vmsvga3dDrawPrimitives: sid=%x idAssociatedContext %#x -> %#x\n", pIndexSurface->id, pIndexSurface->idAssociatedContext, pContext->id));
7317 pIndexSurface->idAssociatedContext = pContext->id;
7318#endif
7319 }
7320
7321 if (!pIndexSurface)
7322 {
7323 /* Render without an index buffer */
7324 Log(("DrawPrimitive %x cPrimitives=%d cVertices=%d index index bias=%d\n", modeDraw, pRange[iPrimitive].primitiveCount, cVertices, pRange[iPrimitive].indexBias));
7325 glDrawArrays(modeDraw, pRange[iPrimitive].indexBias, cVertices);
7326 }
7327 else
7328 {
7329 Assert(pRange[iPrimitive].indexBias >= 0); /* @todo */
7330 Assert(pRange[iPrimitive].indexWidth == pRange[iPrimitive].indexArray.stride);
7331
7332 /* Render with an index buffer */
7333 Log(("DrawIndexedPrimitive %x cPrimitives=%d cVertices=%d hint.first=%d hint.last=%d index offset=%d primitivecount=%d index width=%d index bias=%d\n", modeDraw, pRange[iPrimitive].primitiveCount, cVertices, pVertexDecl[0].rangeHint.first, pVertexDecl[0].rangeHint.last, pRange[iPrimitive].indexArray.offset, pRange[iPrimitive].primitiveCount, pRange[iPrimitive].indexWidth, pRange[iPrimitive].indexBias));
7334 if (pRange[iPrimitive].indexBias == 0)
7335 glDrawElements(modeDraw,
7336 cVertices,
7337 (pRange[iPrimitive].indexWidth == sizeof(uint16_t)) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
7338 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset); /* byte offset in indices buffer */
7339 else
7340 pState->ext.glDrawElementsBaseVertex(modeDraw,
7341 cVertices,
7342 (pRange[iPrimitive].indexWidth == sizeof(uint16_t)) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
7343 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
7344 pRange[iPrimitive].indexBias); /* basevertex */
7345
7346 /* Unbind the index buffer after usage. */
7347 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
7348 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7349 }
7350 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7351 }
7352
7353internal_error:
7354
7355 /* Deactivate the vertex declarations. */
7356 iCurrentVertex = 0;
7357 while (iCurrentVertex < numVertexDecls)
7358 {
7359 uint32_t sidVertex = SVGA_ID_INVALID;
7360 uint32_t iVertex;
7361
7362 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
7363 {
7364 if ( sidVertex != SVGA_ID_INVALID
7365 && pVertexDecl[iVertex].array.surfaceId != sidVertex
7366 )
7367 break;
7368 sidVertex = pVertexDecl[iVertex].array.surfaceId;
7369 }
7370
7371 rc = vmsvga3dDrawPrimitivesCleanupVertexDecls(pState, pContext, iCurrentVertex, iVertex - iCurrentVertex, &pVertexDecl[iCurrentVertex]);
7372 AssertRCReturn(rc, rc);
7373
7374 iCurrentVertex = iVertex;
7375 }
7376#ifdef DEBUG
7377 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTexture); i++)
7378 {
7379 if (pContext->aSidActiveTexture[i] != SVGA3D_INVALID_ID)
7380 {
7381 GLint activeTexture = 0;
7382 GLint activeTextureUnit = 0;
7383
7384 glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
7385 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7386 pState->ext.glActiveTexture(GL_TEXTURE0 + i);
7387 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7388
7389 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
7390 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7391 pState->ext.glActiveTexture(activeTextureUnit);
7392 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7393
7394# if 0 /* Aren't we checking whether 'activeTexture' on texture unit 'i' matches what we expected? This works if only one unit is active, but if both are it _will_ fail for one of them. */
7395 if (pContext->aSidActiveTexture[activeTextureUnit - GL_TEXTURE0] != SVGA3D_INVALID_ID)
7396 {
7397 PVMSVGA3DSURFACE pTexture;
7398 pTexture = pState->papSurfaces[pContext->aSidActiveTexture[activeTextureUnit - GL_TEXTURE0]];
7399
7400 AssertMsg(pTexture->oglId.texture == (GLuint)activeTexture, ("%x vs %x unit %d - %d\n", pTexture->oglId.texture, activeTexture, i, activeTextureUnit - GL_TEXTURE0));
7401 }
7402# else
7403 PVMSVGA3DSURFACE pTexture = pState->papSurfaces[pContext->aSidActiveTexture[i]];
7404 AssertMsg(pTexture->id == pContext->aSidActiveTexture[i], ("%x vs %x\n", pTexture->id == pContext->aSidActiveTexture[i]));
7405 AssertMsg(pTexture->oglId.texture == (GLuint)activeTexture,
7406 ("%x vs %x unit %d (active unit %d) sid=%x\n", pTexture->oglId.texture, activeTexture, i,
7407 activeTextureUnit - GL_TEXTURE0, pContext->aSidActiveTexture[i]));
7408# endif
7409 }
7410 }
7411#endif
7412
7413#ifdef DEBUG_GFX_WINDOW
7414 if (pContext->aSidActiveTexture[0])
7415 {
7416 SVGA3dCopyRect rect;
7417
7418 rect.srcx = rect.srcy = rect.x = rect.y = 0;
7419 rect.w = 800;
7420 rect.h = 600;
7421 vmsvga3dCommandPresent(pThis, pContext->sidRenderTarget, 0, NULL);
7422 }
7423#endif
7424 return rc;
7425}
7426
7427
7428int vmsvga3dShaderDefine(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type, uint32_t cbData, uint32_t *pShaderData)
7429{
7430 PVMSVGA3DCONTEXT pContext;
7431 PVMSVGA3DSHADER pShader;
7432 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
7433 AssertReturn(pState, VERR_NO_MEMORY);
7434 int rc;
7435
7436 Log(("vmsvga3dShaderDefine cid=%x shid=%x type=%s cbData=%x\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cbData));
7437 Log3(("shader code:\n%.*Rhxd\n", cbData, pShaderData));
7438
7439 if ( cid >= pState->cContexts
7440 || pState->papContexts[cid]->id != cid)
7441 {
7442 Log(("vmsvga3dShaderDefine invalid context id!\n"));
7443 return VERR_INVALID_PARAMETER;
7444 }
7445 pContext = pState->papContexts[cid];
7446 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7447
7448 AssertReturn(shid < SVGA3D_MAX_SHADER_IDS, VERR_INVALID_PARAMETER);
7449 if (type == SVGA3D_SHADERTYPE_VS)
7450 {
7451 if (shid >= pContext->cVertexShaders)
7452 {
7453 void *pvNew = RTMemRealloc(pContext->paVertexShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
7454 AssertReturn(pvNew, VERR_NO_MEMORY);
7455 pContext->paVertexShader = (PVMSVGA3DSHADER)pvNew;
7456 memset(&pContext->paVertexShader[pContext->cVertexShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cVertexShaders));
7457 for (uint32_t i = pContext->cVertexShaders; i < shid + 1; i++)
7458 pContext->paVertexShader[i].id = SVGA3D_INVALID_ID;
7459 pContext->cVertexShaders = shid + 1;
7460 }
7461 /* If one already exists with this id, then destroy it now. */
7462 if (pContext->paVertexShader[shid].id != SVGA3D_INVALID_ID)
7463 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paVertexShader[shid].type);
7464
7465 pShader = &pContext->paVertexShader[shid];
7466 }
7467 else
7468 {
7469 Assert(type == SVGA3D_SHADERTYPE_PS);
7470 if (shid >= pContext->cPixelShaders)
7471 {
7472 void *pvNew = RTMemRealloc(pContext->paPixelShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
7473 AssertReturn(pvNew, VERR_NO_MEMORY);
7474 pContext->paPixelShader = (PVMSVGA3DSHADER)pvNew;
7475 memset(&pContext->paPixelShader[pContext->cPixelShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cPixelShaders));
7476 for (uint32_t i = pContext->cPixelShaders; i < shid + 1; i++)
7477 pContext->paPixelShader[i].id = SVGA3D_INVALID_ID;
7478 pContext->cPixelShaders = shid + 1;
7479 }
7480 /* If one already exists with this id, then destroy it now. */
7481 if (pContext->paPixelShader[shid].id != SVGA3D_INVALID_ID)
7482 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paPixelShader[shid].type);
7483
7484 pShader = &pContext->paPixelShader[shid];
7485 }
7486
7487 memset(pShader, 0, sizeof(*pShader));
7488 pShader->id = shid;
7489 pShader->cid = cid;
7490 pShader->type = type;
7491 pShader->cbData = cbData;
7492 pShader->pShaderProgram = RTMemAllocZ(cbData);
7493 AssertReturn(pShader->pShaderProgram, VERR_NO_MEMORY);
7494 memcpy(pShader->pShaderProgram, pShaderData, cbData);
7495
7496#ifdef DUMP_SHADER_DISASSEMBLY
7497 LPD3DXBUFFER pDisassembly;
7498 HRESULT hr = D3DXDisassembleShader((const DWORD *)pShaderData, FALSE, NULL, &pDisassembly);
7499 if (hr == D3D_OK)
7500 {
7501 Log(("Shader disassembly:\n%s\n", pDisassembly->GetBufferPointer()));
7502 pDisassembly->Release();
7503 }
7504#endif
7505
7506 switch (type)
7507 {
7508 case SVGA3D_SHADERTYPE_VS:
7509 rc = ShaderCreateVertexShader(pContext->pShaderContext, (const uint32_t *)pShaderData, &pShader->u.pVertexShader);
7510 break;
7511
7512 case SVGA3D_SHADERTYPE_PS:
7513 rc = ShaderCreatePixelShader(pContext->pShaderContext, (const uint32_t *)pShaderData, &pShader->u.pPixelShader);
7514 break;
7515
7516 default:
7517 AssertFailedReturn(VERR_INVALID_PARAMETER);
7518 }
7519 if (rc != VINF_SUCCESS)
7520 {
7521 RTMemFree(pShader->pShaderProgram);
7522 memset(pShader, 0, sizeof(*pShader));
7523 pShader->id = SVGA3D_INVALID_ID;
7524 }
7525
7526 return rc;
7527}
7528
7529int vmsvga3dShaderDestroy(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type)
7530{
7531 PVMSVGA3DCONTEXT pContext;
7532 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
7533 AssertReturn(pState, VERR_NO_MEMORY);
7534 PVMSVGA3DSHADER pShader = NULL;
7535 int rc;
7536
7537 Log(("vmsvga3dShaderDestroy cid=%x shid=%x type=%s\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL"));
7538
7539 if ( cid >= pState->cContexts
7540 || pState->papContexts[cid]->id != cid)
7541 {
7542 Log(("vmsvga3dShaderDestroy invalid context id!\n"));
7543 return VERR_INVALID_PARAMETER;
7544 }
7545 pContext = pState->papContexts[cid];
7546 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7547
7548 if (type == SVGA3D_SHADERTYPE_VS)
7549 {
7550 if ( shid < pContext->cVertexShaders
7551 && pContext->paVertexShader[shid].id == shid)
7552 {
7553 pShader = &pContext->paVertexShader[shid];
7554 rc = ShaderDestroyVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
7555 AssertRC(rc);
7556 }
7557 }
7558 else
7559 {
7560 Assert(type == SVGA3D_SHADERTYPE_PS);
7561 if ( shid < pContext->cPixelShaders
7562 && pContext->paPixelShader[shid].id == shid)
7563 {
7564 pShader = &pContext->paPixelShader[shid];
7565 rc = ShaderDestroyPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
7566 AssertRC(rc);
7567 }
7568 }
7569
7570 if (pShader)
7571 {
7572 if (pShader->pShaderProgram)
7573 RTMemFree(pShader->pShaderProgram);
7574 memset(pShader, 0, sizeof(*pShader));
7575 pShader->id = SVGA3D_INVALID_ID;
7576 }
7577 else
7578 AssertFailedReturn(VERR_INVALID_PARAMETER);
7579
7580 return VINF_SUCCESS;
7581}
7582
7583int vmsvga3dShaderSet(PVGASTATE pThis, uint32_t cid, SVGA3dShaderType type, uint32_t shid)
7584{
7585 PVMSVGA3DCONTEXT pContext;
7586 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
7587 AssertReturn(pState, VERR_NO_MEMORY);
7588 int rc;
7589
7590 Log(("vmsvga3dShaderSet cid=%x type=%s shid=%d\n", cid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", shid));
7591
7592 if ( cid >= pState->cContexts
7593 || pState->papContexts[cid]->id != cid)
7594 {
7595 Log(("vmsvga3dShaderSet invalid context id!\n"));
7596 return VERR_INVALID_PARAMETER;
7597 }
7598 pContext = pState->papContexts[cid];
7599 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7600
7601 if (type == SVGA3D_SHADERTYPE_VS)
7602 {
7603 /* Save for vm state save/restore. */
7604 pContext->state.shidVertex = shid;
7605 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VERTEXSHADER;
7606
7607 if ( shid < pContext->cVertexShaders
7608 && pContext->paVertexShader[shid].id == shid)
7609 {
7610 PVMSVGA3DSHADER pShader = &pContext->paVertexShader[shid];
7611 Assert(type == pShader->type);
7612
7613 rc = ShaderSetVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
7614 AssertRCReturn(rc, rc);
7615 }
7616 else
7617 if (shid == SVGA_ID_INVALID)
7618 {
7619 /* Unselect shader. */
7620 rc = ShaderSetVertexShader(pContext->pShaderContext, NULL);
7621 AssertRCReturn(rc, rc);
7622 }
7623 else
7624 AssertFailedReturn(VERR_INVALID_PARAMETER);
7625 }
7626 else
7627 {
7628 /* Save for vm state save/restore. */
7629 pContext->state.shidPixel = shid;
7630 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_PIXELSHADER;
7631
7632 Assert(type == SVGA3D_SHADERTYPE_PS);
7633 if ( shid < pContext->cPixelShaders
7634 && pContext->paPixelShader[shid].id == shid)
7635 {
7636 PVMSVGA3DSHADER pShader = &pContext->paPixelShader[shid];
7637 Assert(type == pShader->type);
7638
7639 rc = ShaderSetPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
7640 AssertRCReturn(rc, rc);
7641 }
7642 else
7643 if (shid == SVGA_ID_INVALID)
7644 {
7645 /* Unselect shader. */
7646 rc = ShaderSetPixelShader(pContext->pShaderContext, NULL);
7647 AssertRCReturn(rc, rc);
7648 }
7649 else
7650 AssertFailedReturn(VERR_INVALID_PARAMETER);
7651 }
7652
7653 return VINF_SUCCESS;
7654}
7655
7656int vmsvga3dShaderSetConst(PVGASTATE pThis, uint32_t cid, uint32_t reg, SVGA3dShaderType type, SVGA3dShaderConstType ctype, uint32_t cRegisters, uint32_t *pValues)
7657{
7658 PVMSVGA3DCONTEXT pContext;
7659 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
7660 AssertReturn(pState, VERR_NO_MEMORY);
7661 int rc;
7662
7663 Log(("vmsvga3dShaderSetConst cid=%x reg=%x type=%s cregs=%d ctype=%x\n", cid, reg, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cRegisters, ctype));
7664
7665 if ( cid >= pState->cContexts
7666 || pState->papContexts[cid]->id != cid)
7667 {
7668 Log(("vmsvga3dShaderSetConst invalid context id!\n"));
7669 return VERR_INVALID_PARAMETER;
7670 }
7671 pContext = pState->papContexts[cid];
7672 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7673
7674 for (uint32_t i = 0; i < cRegisters; i++)
7675 {
7676#ifdef LOG_ENABLED
7677 switch (ctype)
7678 {
7679 case SVGA3D_CONST_TYPE_FLOAT:
7680 {
7681 float *pValuesF = (float *)pValues;
7682 Log(("Constant %d: value=%d-%d-%d-%d\n", reg + i, (int)(pValuesF[i*4 + 0] * 100.0), (int)(pValuesF[i*4 + 1] * 100.0), (int)(pValuesF[i*4 + 2] * 100.0), (int)(pValuesF[i*4 + 3] * 100.0)));
7683 break;
7684 }
7685
7686 case SVGA3D_CONST_TYPE_INT:
7687 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]));
7688 break;
7689
7690 case SVGA3D_CONST_TYPE_BOOL:
7691 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]));
7692 break;
7693 }
7694#endif
7695 vmsvga3dSaveShaderConst(pContext, reg + i, type, ctype, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]);
7696 }
7697
7698 switch (type)
7699 {
7700 case SVGA3D_SHADERTYPE_VS:
7701 switch (ctype)
7702 {
7703 case SVGA3D_CONST_TYPE_FLOAT:
7704 rc = ShaderSetVertexShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
7705 break;
7706
7707 case SVGA3D_CONST_TYPE_INT:
7708 rc = ShaderSetVertexShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
7709 break;
7710
7711 case SVGA3D_CONST_TYPE_BOOL:
7712 rc = ShaderSetVertexShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
7713 break;
7714
7715 default:
7716 AssertFailedReturn(VERR_INVALID_PARAMETER);
7717 }
7718 AssertRCReturn(rc, rc);
7719 break;
7720
7721 case SVGA3D_SHADERTYPE_PS:
7722 switch (ctype)
7723 {
7724 case SVGA3D_CONST_TYPE_FLOAT:
7725 rc = ShaderSetPixelShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
7726 break;
7727
7728 case SVGA3D_CONST_TYPE_INT:
7729 rc = ShaderSetPixelShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
7730 break;
7731
7732 case SVGA3D_CONST_TYPE_BOOL:
7733 rc = ShaderSetPixelShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
7734 break;
7735
7736 default:
7737 AssertFailedReturn(VERR_INVALID_PARAMETER);
7738 }
7739 AssertRCReturn(rc, rc);
7740 break;
7741
7742 default:
7743 AssertFailedReturn(VERR_INVALID_PARAMETER);
7744 }
7745
7746 return VINF_SUCCESS;
7747}
7748
7749
7750int vmsvga3dQueryBegin(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type)
7751{
7752 AssertFailed();
7753 return VERR_NOT_IMPLEMENTED;
7754}
7755
7756int vmsvga3dQueryEnd(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type, SVGAGuestPtr guestResult)
7757{
7758 AssertFailed();
7759 return VERR_NOT_IMPLEMENTED;
7760}
7761
7762int vmsvga3dQueryWait(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type, SVGAGuestPtr guestResult)
7763{
7764 AssertFailed();
7765 return VERR_NOT_IMPLEMENTED;
7766}
7767
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