VirtualBox

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

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

VMSVGA3d: Changed the context arrays into pointer arrays to reduce the copying requried when growing (entry size > 8K, I think). This also means the context pointers remain valid across growths.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 303.2 KB
Line 
1/* $Id: DevVGA-SVGA3d-ogl.cpp 54785 2015-03-16 13:59:11Z 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 paSurface;
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, paSurface),
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->paSurface[i].id != SVGA3D_INVALID_ID)
1828 vmsvga3dSurfaceDestroy(pThis, pState->paSurface[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 void *pvNew = RTMemRealloc(pState->paSurface, sizeof(VMSVGA3DSURFACE) * (sid + 1));
2630 AssertReturn(pvNew, VERR_NO_MEMORY);
2631 pState->paSurface = (PVMSVGA3DSURFACE)pvNew;
2632 memset(&pState->paSurface[pState->cSurfaces], 0, sizeof(VMSVGA3DSURFACE) * (sid + 1 - pState->cSurfaces));
2633 for (uint32_t i = pState->cSurfaces; i < sid + 1; i++)
2634 pState->paSurface[i].id = SVGA3D_INVALID_ID;
2635
2636 pState->cSurfaces = sid + 1;
2637 }
2638 /* If one already exists with this id, then destroy it now. */
2639 if (pState->paSurface[sid].id != SVGA3D_INVALID_ID)
2640 vmsvga3dSurfaceDestroy(pThis, sid);
2641
2642 pSurface = &pState->paSurface[sid];
2643 memset(pSurface, 0, sizeof(*pSurface));
2644 pSurface->id = sid;
2645#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
2646 pSurface->idAssociatedContextUnused = SVGA3D_INVALID_ID;
2647#else
2648 pSurface->idAssociatedContext = SVGA3D_INVALID_ID;
2649#endif
2650 vmsvga3dSurfaceFormat2OGL(pSurface, format);
2651
2652 pSurface->oglId.buffer = OPENGL_INVALID_ID;
2653
2654 /* The surface type is sort of undefined now, even though the hints and format can help to clear that up.
2655 * In some case we'll have to wait until the surface is used to create the D3D object.
2656 */
2657 switch (format)
2658 {
2659 case SVGA3D_Z_D32:
2660 case SVGA3D_Z_D16:
2661 case SVGA3D_Z_D24S8:
2662 case SVGA3D_Z_D15S1:
2663 case SVGA3D_Z_D24X8:
2664 case SVGA3D_Z_DF16:
2665 case SVGA3D_Z_DF24:
2666 case SVGA3D_Z_D24S8_INT:
2667 surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
2668 break;
2669
2670 /* Texture compression formats */
2671 case SVGA3D_DXT1:
2672 case SVGA3D_DXT2:
2673 case SVGA3D_DXT3:
2674 case SVGA3D_DXT4:
2675 case SVGA3D_DXT5:
2676 /* Bump-map formats */
2677 case SVGA3D_BUMPU8V8:
2678 case SVGA3D_BUMPL6V5U5:
2679 case SVGA3D_BUMPX8L8V8U8:
2680 case SVGA3D_BUMPL8V8U8:
2681 case SVGA3D_V8U8:
2682 case SVGA3D_Q8W8V8U8:
2683 case SVGA3D_CxV8U8:
2684 case SVGA3D_X8L8V8U8:
2685 case SVGA3D_A2W10V10U10:
2686 case SVGA3D_V16U16:
2687 /* Typical render target formats; we should allow render target buffers to be used as textures. */
2688 case SVGA3D_X8R8G8B8:
2689 case SVGA3D_A8R8G8B8:
2690 case SVGA3D_R5G6B5:
2691 case SVGA3D_X1R5G5B5:
2692 case SVGA3D_A1R5G5B5:
2693 case SVGA3D_A4R4G4B4:
2694 surfaceFlags |= SVGA3D_SURFACE_HINT_TEXTURE;
2695 break;
2696
2697 case SVGA3D_LUMINANCE8:
2698 case SVGA3D_LUMINANCE4_ALPHA4:
2699 case SVGA3D_LUMINANCE16:
2700 case SVGA3D_LUMINANCE8_ALPHA8:
2701 case SVGA3D_ARGB_S10E5: /* 16-bit floating-point ARGB */
2702 case SVGA3D_ARGB_S23E8: /* 32-bit floating-point ARGB */
2703 case SVGA3D_A2R10G10B10:
2704 case SVGA3D_ALPHA8:
2705 case SVGA3D_R_S10E5:
2706 case SVGA3D_R_S23E8:
2707 case SVGA3D_RG_S10E5:
2708 case SVGA3D_RG_S23E8:
2709 case SVGA3D_G16R16:
2710 case SVGA3D_A16B16G16R16:
2711 case SVGA3D_UYVY:
2712 case SVGA3D_YUY2:
2713 case SVGA3D_NV12:
2714 case SVGA3D_AYUV:
2715 case SVGA3D_BC4_UNORM:
2716 case SVGA3D_BC5_UNORM:
2717 break;
2718
2719 /*
2720 * Any surface can be used as a buffer object, but SVGA3D_BUFFER is
2721 * the most efficient format to use when creating new surfaces
2722 * expressly for index or vertex data.
2723 */
2724 case SVGA3D_BUFFER:
2725 break;
2726
2727 default:
2728 break;
2729 }
2730
2731 pSurface->flags = surfaceFlags;
2732 pSurface->format = format;
2733 memcpy(pSurface->faces, face, sizeof(pSurface->faces));
2734 pSurface->cFaces = 1; /* check for cube maps later */
2735 pSurface->multiSampleCount = multisampleCount;
2736 pSurface->autogenFilter = autogenFilter;
2737 Assert(autogenFilter != SVGA3D_TEX_FILTER_FLATCUBIC);
2738 Assert(autogenFilter != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
2739 pSurface->pMipmapLevels = (PVMSVGA3DMIPMAPLEVEL)RTMemAllocZ(cMipLevels * sizeof(VMSVGA3DMIPMAPLEVEL));
2740 AssertReturn(pSurface->pMipmapLevels, VERR_NO_MEMORY);
2741
2742 for (uint32_t i=0; i < cMipLevels; i++)
2743 pSurface->pMipmapLevels[i].size = pMipLevelSize[i];
2744
2745 pSurface->cbBlock = vmsvga3dSurfaceFormatSize(format);
2746
2747 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))
2748 {
2749 case SVGA3D_SURFACE_CUBEMAP:
2750 Log(("SVGA3D_SURFACE_CUBEMAP\n"));
2751 pSurface->cFaces = 6;
2752 break;
2753
2754 case SVGA3D_SURFACE_HINT_INDEXBUFFER:
2755 Log(("SVGA3D_SURFACE_HINT_INDEXBUFFER\n"));
2756 /* else type unknown at this time; postpone buffer creation */
2757 break;
2758
2759 case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2760 Log(("SVGA3D_SURFACE_HINT_VERTEXBUFFER\n"));
2761 /* Type unknown at this time; postpone buffer creation */
2762 break;
2763
2764 case SVGA3D_SURFACE_HINT_TEXTURE:
2765 Log(("SVGA3D_SURFACE_HINT_TEXTURE\n"));
2766 break;
2767
2768 case SVGA3D_SURFACE_HINT_RENDERTARGET:
2769 Log(("SVGA3D_SURFACE_HINT_RENDERTARGET\n"));
2770 break;
2771
2772 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
2773 Log(("SVGA3D_SURFACE_HINT_DEPTHSTENCIL\n"));
2774 break;
2775
2776 default:
2777 /* Unknown; decide later. */
2778 break;
2779 }
2780
2781 /* Allocate buffer to hold the surface data until we can move it into a D3D object */
2782 for (uint32_t iFace=0; iFace < pSurface->cFaces; iFace++)
2783 {
2784 for (uint32_t i=0; i < pSurface->faces[iFace].numMipLevels; i++)
2785 {
2786 uint32_t idx = i + iFace * pSurface->faces[0].numMipLevels;
2787
2788 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));
2789 Log(("vmsvga3dSurfaceDefine: cbPitch=%x cbBlock=%x \n", pSurface->cbBlock * pSurface->pMipmapLevels[idx].size.width, pSurface->cbBlock));
2790
2791 pSurface->pMipmapLevels[idx].cbSurfacePitch = pSurface->cbBlock * pSurface->pMipmapLevels[idx].size.width;
2792 pSurface->pMipmapLevels[idx].cbSurface = pSurface->pMipmapLevels[idx].cbSurfacePitch * pSurface->pMipmapLevels[idx].size.height * pSurface->pMipmapLevels[idx].size.depth;
2793 pSurface->pMipmapLevels[idx].pSurfaceData = RTMemAllocZ(pSurface->pMipmapLevels[idx].cbSurface);
2794 AssertReturn(pSurface->pMipmapLevels[idx].pSurfaceData, VERR_NO_MEMORY);
2795 }
2796 }
2797 return VINF_SUCCESS;
2798}
2799
2800int vmsvga3dSurfaceDestroy(PVGASTATE pThis, uint32_t sid)
2801{
2802 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
2803 AssertReturn(pState, VERR_NO_MEMORY);
2804
2805 if ( sid < pState->cSurfaces
2806 && pState->paSurface[sid].id == sid)
2807 {
2808 PVMSVGA3DSURFACE pSurface = &pState->paSurface[sid];
2809 PVMSVGA3DCONTEXT pContext;
2810
2811 Log(("vmsvga3dSurfaceDestroy id %x\n", sid));
2812
2813#if 1 /* Windows is doing this, guess it makes sense here as well... */
2814 /* Check all contexts if this surface is used as a render target or active texture. */
2815 for (uint32_t cid = 0; cid < pState->cContexts; cid++)
2816 {
2817 pContext = pState->papContexts[cid];
2818 if (pContext->id == cid)
2819 {
2820 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTexture); i++)
2821 if (pContext->aSidActiveTexture[i] == sid)
2822 pContext->aSidActiveTexture[i] = SVGA3D_INVALID_ID;
2823 if (pContext->sidRenderTarget == sid)
2824 pContext->sidRenderTarget = SVGA3D_INVALID_ID;
2825 }
2826 }
2827#endif
2828
2829#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
2830 pContext = &pState->SharedCtx;
2831 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2832#else
2833 /* @todo stricter checks for associated context */
2834 uint32_t cid = pSurface->idAssociatedContext;
2835 if ( cid <= pState->cContexts
2836 && pState->papContexts[cid]->id == cid)
2837 {
2838 pContext = pState->papContexts[cid];
2839 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2840 }
2841 /* If there is a GL buffer or something associated with the surface, we
2842 really need something here, so pick any active context. */
2843 else if (pSurface->oglId.buffer != OPENGL_INVALID_ID)
2844 {
2845 for (cid = 0; cid < pState->cContexts; cid++)
2846 {
2847 if (pState->papContexts[cid]->id == cid)
2848 {
2849 pContext = pState->papContexts[cid];
2850 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2851 break;
2852 }
2853 }
2854 AssertReturn(pContext, VERR_INTERNAL_ERROR); /* otherwise crashes/fails; create temp context if this ever triggers! */
2855 }
2856#endif
2857
2858 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))
2859 {
2860 case SVGA3D_SURFACE_CUBEMAP:
2861 AssertFailed(); /* @todo */
2862 break;
2863
2864 case SVGA3D_SURFACE_HINT_INDEXBUFFER:
2865 case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2866 if (pSurface->oglId.buffer != OPENGL_INVALID_ID)
2867 {
2868 pState->ext.glDeleteBuffers(1, &pSurface->oglId.buffer);
2869 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2870 }
2871 break;
2872
2873 case SVGA3D_SURFACE_HINT_TEXTURE:
2874 case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2875 if (pSurface->oglId.texture != OPENGL_INVALID_ID)
2876 {
2877 glDeleteTextures(1, &pSurface->oglId.texture);
2878 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2879 }
2880 break;
2881
2882 case SVGA3D_SURFACE_HINT_RENDERTARGET:
2883 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
2884 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL | SVGA3D_SURFACE_HINT_TEXTURE: /* @todo actual texture surface not supported */
2885 if (pSurface->oglId.renderbuffer != OPENGL_INVALID_ID)
2886 {
2887 pState->ext.glDeleteRenderbuffers(1, &pSurface->oglId.renderbuffer);
2888 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2889 }
2890 break;
2891
2892 default:
2893 break;
2894 }
2895
2896 if (pSurface->pMipmapLevels)
2897 {
2898 for (uint32_t face=0; face < pSurface->cFaces; face++)
2899 {
2900 for (uint32_t i=0; i < pSurface->faces[face].numMipLevels; i++)
2901 {
2902 uint32_t idx = i + face * pSurface->faces[0].numMipLevels;
2903 if (pSurface->pMipmapLevels[idx].pSurfaceData)
2904 RTMemFree(pSurface->pMipmapLevels[idx].pSurfaceData);
2905 }
2906 }
2907 RTMemFree(pSurface->pMipmapLevels);
2908 }
2909
2910 memset(pSurface, 0, sizeof(*pSurface));
2911 pSurface->id = SVGA3D_INVALID_ID;
2912 }
2913 else
2914 AssertFailedReturn(VERR_INVALID_PARAMETER);
2915
2916 return VINF_SUCCESS;
2917}
2918
2919int vmsvga3dSurfaceCopy(PVGASTATE pThis, SVGA3dSurfaceImageId dest, SVGA3dSurfaceImageId src, uint32_t cCopyBoxes, SVGA3dCopyBox *pBox)
2920{
2921 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
2922 uint32_t sidSrc = src.sid;
2923 uint32_t sidDest = dest.sid;
2924 int rc = VINF_SUCCESS;
2925
2926 AssertReturn(pState, VERR_NO_MEMORY);
2927 AssertReturn(sidSrc < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
2928 AssertReturn(sidSrc < pState->cSurfaces && pState->paSurface[sidSrc].id == sidSrc, VERR_INVALID_PARAMETER);
2929 AssertReturn(sidDest < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
2930 AssertReturn(sidDest < pState->cSurfaces && pState->paSurface[sidDest].id == sidDest, VERR_INVALID_PARAMETER);
2931
2932 for (uint32_t i = 0; i < cCopyBoxes; i++)
2933 {
2934 SVGA3dBox destBox, srcBox;
2935
2936 srcBox.x = pBox[i].srcx;
2937 srcBox.y = pBox[i].srcy;
2938 srcBox.z = pBox[i].srcz;
2939 srcBox.w = pBox[i].w;
2940 srcBox.h = pBox[i].h;
2941 srcBox.d = pBox[i].z;
2942
2943 destBox.x = pBox[i].x;
2944 destBox.y = pBox[i].y;
2945 destBox.z = pBox[i].z;
2946 destBox.w = pBox[i].w;
2947 destBox.h = pBox[i].h;
2948 destBox.z = pBox[i].z;
2949
2950 rc = vmsvga3dSurfaceStretchBlt(pThis, dest, destBox, src, srcBox, SVGA3D_STRETCH_BLT_LINEAR);
2951 AssertRCReturn(rc, rc);
2952 }
2953 return VINF_SUCCESS;
2954}
2955
2956
2957/**
2958 * Save texture unpacking parameters and loads those appropriate for the given
2959 * surface.
2960 *
2961 * @param pState The VMSVGA3D state structure.
2962 * @param pContext The active context.
2963 * @param pSurface The surface.
2964 * @param pSave Where to save stuff.
2965 */
2966static void vmsvga3dSetUnpackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
2967 PVMSVGAPACKPARAMS pSave)
2968{
2969 /*
2970 * Save (ignore errors, setting the defaults we want and avoids restore).
2971 */
2972 pSave->iAlignment = 1;
2973 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_UNPACK_ALIGNMENT, &pSave->iAlignment), pState, pContext);
2974 pSave->cxRow = 0;
2975 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_UNPACK_ROW_LENGTH, &pSave->cxRow), pState, pContext);
2976
2977#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
2978 pSave->cyImage = 0;
2979 glGetIntegerv(GL_UNPACK_IMAGE_HEIGHT, &pSave->cyImage);
2980 Assert(pSave->cyImage == 0);
2981
2982 pSave->fSwapBytes = GL_FALSE;
2983 glGetBooleanv(GL_UNPACK_SWAP_BYTES, &pSave->fSwapBytes);
2984 Assert(pSave->fSwapBytes == GL_FALSE);
2985
2986 pSave->fLsbFirst = GL_FALSE;
2987 glGetBooleanv(GL_UNPACK_LSB_FIRST, &pSave->fLsbFirst);
2988 Assert(pSave->fLsbFirst == GL_FALSE);
2989
2990 pSave->cSkipRows = 0;
2991 glGetIntegerv(GL_UNPACK_SKIP_ROWS, &pSave->cSkipRows);
2992 Assert(pSave->cSkipRows == 0);
2993
2994 pSave->cSkipPixels = 0;
2995 glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &pSave->cSkipPixels);
2996 Assert(pSave->cSkipPixels == 0);
2997
2998 pSave->cSkipImages = 0;
2999 glGetIntegerv(GL_UNPACK_SKIP_IMAGES, &pSave->cSkipImages);
3000 Assert(pSave->cSkipImages == 0);
3001
3002 VMSVGA3D_CLEAR_GL_ERRORS();
3003#endif
3004
3005 /*
3006 * Setup unpack.
3007 *
3008 * Note! We use 1 as alignment here because we currently don't do any
3009 * aligning of line pitches anywhere.
3010 */
3011 NOREF(pSurface);
3012 if (pSave->iAlignment != 1)
3013 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_ALIGNMENT, 1), pState, pContext);
3014 if (pSave->cxRow != 0)
3015 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0), pState, pContext);
3016#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
3017 if (pSave->cyImage != 0)
3018 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0), pState, pContext);
3019 if (pSave->fSwapBytes != 0)
3020 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE), pState, pContext);
3021 if (pSave->fLsbFirst != 0)
3022 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE), pState, pContext);
3023 if (pSave->cSkipRows != 0)
3024 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_ROWS, 0), pState, pContext);
3025 if (pSave->cSkipPixels != 0)
3026 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0), pState, pContext);
3027 if (pSave->cSkipImages != 0)
3028 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0), pState, pContext);
3029#endif
3030}
3031
3032
3033/**
3034 * Restores texture unpacking parameters.
3035 *
3036 * @param pState The VMSVGA3D state structure.
3037 * @param pContext The active context.
3038 * @param pSurface The surface.
3039 * @param pSave Where stuff was saved.
3040 */
3041static void vmsvga3dRestoreUnpackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
3042 PCVMSVGAPACKPARAMS pSave)
3043{
3044 NOREF(pSurface);
3045 if (pSave->iAlignment != 1)
3046 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_ALIGNMENT, pSave->iAlignment), pState, pContext);
3047 if (pSave->cxRow != 0)
3048 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, pSave->cxRow), pState, pContext);
3049#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
3050 if (pSave->cyImage != 0)
3051 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, pSave->cyImage), pState, pContext);
3052 if (pSave->fSwapBytes != 0)
3053 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SWAP_BYTES, pSave->fSwapBytes), pState, pContext);
3054 if (pSave->fLsbFirst != 0)
3055 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_LSB_FIRST, pSave->fLsbFirst), pState, pContext);
3056 if (pSave->cSkipRows != 0)
3057 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_ROWS, pSave->cSkipRows), pState, pContext);
3058 if (pSave->cSkipPixels != 0)
3059 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS, pSave->cSkipPixels), pState, pContext);
3060 if (pSave->cSkipImages != 0)
3061 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_IMAGES, pSave->cSkipImages), pState, pContext);
3062#endif
3063}
3064
3065
3066/* Create D3D texture object for the specified surface. */
3067static int vmsvga3dCreateTexture(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t idAssociatedContext,
3068 PVMSVGA3DSURFACE pSurface)
3069{
3070 GLint activeTexture = 0;
3071#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
3072 uint32_t idPrevCtx = pState->idActiveContext;
3073 pContext = &pState->SharedCtx;
3074 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3075#endif
3076
3077 glGenTextures(1, &pSurface->oglId.texture);
3078 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3079 /* @todo Set the mip map generation filter settings. */
3080
3081 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
3082 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3083
3084 /* Must bind texture to the current context in order to change it. */
3085 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
3086 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3087
3088 /* Set the unpacking parameters. */
3089 VMSVGAPACKPARAMS SavedParams;
3090 vmsvga3dSetUnpackParams(pState, pContext, pSurface, &SavedParams);
3091
3092 if (pSurface->fDirty)
3093 {
3094 Log(("vmsvga3dCreateTexture: sync dirty texture\n"));
3095 for (uint32_t i = 0; i < pSurface->faces[0].numMipLevels; i++)
3096 {
3097 /* Paranoia: Always do level 0 here to mirror the non-dirty case. */
3098 if (pSurface->pMipmapLevels[i].fDirty || i == 0)
3099 {
3100 if (pSurface->pMipmapLevels[i].fDirty)
3101 Log(("vmsvga3dCreateTexture: sync dirty texture mipmap level %d (pitch %x)\n", i, pSurface->pMipmapLevels[i].cbSurfacePitch));
3102
3103 glTexImage2D(GL_TEXTURE_2D,
3104 i,
3105 pSurface->internalFormatGL,
3106 pSurface->pMipmapLevels[i].size.width,
3107 pSurface->pMipmapLevels[i].size.height,
3108 0,
3109 pSurface->formatGL,
3110 pSurface->typeGL,
3111 pSurface->pMipmapLevels[i].pSurfaceData);
3112
3113 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3114
3115 pSurface->pMipmapLevels[i].fDirty = false;
3116 }
3117 }
3118 pSurface->fDirty = false;
3119 }
3120 else
3121 {
3122 /* Allocate and initialize texture memory. Passing the zero filled pSurfaceData avoids
3123 exposing random host memory to the guest and helps a with the fedora 21 surface
3124 corruption issues (launchpad, background, search field, login). */
3125 glTexImage2D(GL_TEXTURE_2D,
3126 0,
3127 pSurface->internalFormatGL,
3128 pSurface->pMipmapLevels[0].size.width,
3129 pSurface->pMipmapLevels[0].size.height,
3130 0,
3131 pSurface->formatGL,
3132 pSurface->typeGL,
3133 pSurface->pMipmapLevels[0].pSurfaceData);
3134 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3135 }
3136
3137 /* Restore unpacking parameters. */
3138 vmsvga3dRestoreUnpackParams(pState, pContext, pSurface, &SavedParams);
3139
3140 /* Restore the old active texture. */
3141 glBindTexture(GL_TEXTURE_2D, activeTexture);
3142 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3143
3144 pSurface->flags |= SVGA3D_SURFACE_HINT_TEXTURE;
3145#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
3146 LogFlow(("vmsvga3dCreateTexture: sid=%x idAssociatedContext %#x -> %#x; oglId.texture=%#x\n",
3147 pSurface->id, pSurface->idAssociatedContext, idAssociatedContext, pSurface->oglId.texture));
3148 pSurface->idAssociatedContext = idAssociatedContext;
3149#endif
3150
3151#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
3152 if (idPrevCtx < pState->cContexts && pState->papContexts[idPrevCtx]->id == idPrevCtx)
3153 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pState->papContexts[idPrevCtx]);
3154#endif
3155 return VINF_SUCCESS;
3156}
3157
3158int vmsvga3dSurfaceStretchBlt(PVGASTATE pThis, SVGA3dSurfaceImageId dest, SVGA3dBox destBox,
3159 SVGA3dSurfaceImageId src, SVGA3dBox srcBox, SVGA3dStretchBltMode mode)
3160{
3161 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
3162 PVMSVGA3DSURFACE pSurfaceSrc;
3163 uint32_t sidSrc = src.sid;
3164 PVMSVGA3DSURFACE pSurfaceDest;
3165 uint32_t sidDest = dest.sid;
3166 int rc = VINF_SUCCESS;
3167 uint32_t cid;
3168 PVMSVGA3DCONTEXT pContext;
3169
3170 AssertReturn(pState, VERR_NO_MEMORY);
3171 AssertReturn(sidSrc < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
3172 AssertReturn(sidSrc < pState->cSurfaces && pState->paSurface[sidSrc].id == sidSrc, VERR_INVALID_PARAMETER);
3173 AssertReturn(sidDest < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
3174 AssertReturn(sidDest < pState->cSurfaces && pState->paSurface[sidDest].id == sidDest, VERR_INVALID_PARAMETER);
3175
3176 pSurfaceSrc = &pState->paSurface[sidSrc];
3177 pSurfaceDest = &pState->paSurface[sidDest];
3178 AssertReturn(pSurfaceSrc->faces[0].numMipLevels > src.mipmap, VERR_INVALID_PARAMETER);
3179 AssertReturn(pSurfaceDest->faces[0].numMipLevels > dest.mipmap, VERR_INVALID_PARAMETER);
3180
3181#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
3182 Log(("vmsvga3dSurfaceStretchBlt: src sid=%x (%d,%d)(%d,%d) dest sid=%x (%d,%d)(%d,%d) mode=%x\n",
3183 src.sid, srcBox.x, srcBox.y, srcBox.x + srcBox.w, srcBox.y + srcBox.h,
3184 dest.sid, destBox.x, destBox.y, destBox.x + destBox.w, destBox.y + destBox.h, mode));
3185 cid = SVGA3D_INVALID_ID;
3186 pContext = &pState->SharedCtx;
3187 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3188#else
3189 Log(("vmsvga3dSurfaceStretchBlt: src sid=%x cid=%x (%d,%d)(%d,%d) dest sid=%x cid=%x (%d,%d)(%d,%d) mode=%x\n",
3190 src.sid, pSurfaceSrc->idAssociatedContext, srcBox.x, srcBox.y, srcBox.x + srcBox.w, srcBox.y + srcBox.h,
3191 dest.sid, pSurfaceDest->idAssociatedContext, destBox.x, destBox.y, destBox.x + destBox.w, destBox.y + destBox.h, mode));
3192
3193 /* @todo stricter checks for associated context */
3194 cid = pSurfaceDest->idAssociatedContext;
3195 if (cid == SVGA3D_INVALID_ID)
3196 cid = pSurfaceSrc->idAssociatedContext;
3197
3198 if ( cid >= pState->cContexts
3199 || pState->papContexts[cid]->id != cid)
3200 {
3201 Log(("vmsvga3dSurfaceStretchBlt invalid context id!\n"));
3202 AssertFailedReturn(VERR_INVALID_PARAMETER);
3203 }
3204 pContext = pState->papContexts[cid];
3205 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3206#endif
3207
3208 if (pSurfaceSrc->oglId.texture == OPENGL_INVALID_ID)
3209 {
3210 /* Unknown surface type; turn it into a texture, which can be used for other purposes too. */
3211 Log(("vmsvga3dSurfaceStretchBlt: unknown src surface id=%x type=%d format=%d -> create texture\n", sidSrc, pSurfaceSrc->flags, pSurfaceSrc->format));
3212 rc = vmsvga3dCreateTexture(pState, pContext, cid, pSurfaceSrc);
3213 AssertRCReturn(rc, rc);
3214 }
3215
3216 if (pSurfaceDest->oglId.texture == OPENGL_INVALID_ID)
3217 {
3218 /* Unknown surface type; turn it into a texture, which can be used for other purposes too. */
3219 Log(("vmsvga3dSurfaceStretchBlt: unknown dest surface id=%x type=%d format=%d -> create texture\n", sidDest, pSurfaceDest->flags, pSurfaceDest->format));
3220 rc = vmsvga3dCreateTexture(pState, pContext, cid, pSurfaceDest);
3221 AssertRCReturn(rc, rc);
3222 }
3223
3224 /* Activate the read and draw framebuffer objects. */
3225 pState->ext.glBindFramebuffer(GL_READ_FRAMEBUFFER, pContext->idReadFramebuffer);
3226 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3227 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, pContext->idDrawFramebuffer);
3228 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3229
3230 /* Bind the source and destination objects to the right place. */
3231 pState->ext.glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
3232 pSurfaceSrc->oglId.texture, src.mipmap);
3233 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3234 pState->ext.glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
3235 pSurfaceDest->oglId.texture, dest.mipmap);
3236 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3237
3238 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),
3239 srcBox.x + srcBox.w, D3D_TO_OGL_Y_COORD(pSurfaceSrc, srcBox.y), destBox.x, D3D_TO_OGL_Y_COORD(pSurfaceDest, destBox.y + destBox.h),
3240 destBox.x + destBox.w, D3D_TO_OGL_Y_COORD(pSurfaceDest, destBox.y)));
3241
3242 pState->ext.glBlitFramebuffer(srcBox.x,
3243#ifdef MANUAL_FLIP_SURFACE_DATA
3244 D3D_TO_OGL_Y_COORD(pSurfaceSrc, srcBox.y + srcBox.h), /* inclusive */
3245#else
3246 srcBox.y,
3247#endif
3248 srcBox.x + srcBox.w, /* exclusive. */
3249#ifdef MANUAL_FLIP_SURFACE_DATA
3250 D3D_TO_OGL_Y_COORD(pSurfaceSrc, srcBox.y), /* exclusive */
3251#else
3252 srcBox.y + srcBox.h,
3253#endif
3254 destBox.x,
3255#ifdef MANUAL_FLIP_SURFACE_DATA
3256 D3D_TO_OGL_Y_COORD(pSurfaceDest, destBox.y + destBox.h), /* inclusive. */
3257#else
3258 destBox.y,
3259#endif
3260 destBox.x + destBox.w, /* exclusive. */
3261#ifdef MANUAL_FLIP_SURFACE_DATA
3262 D3D_TO_OGL_Y_COORD(pSurfaceDest, destBox.y), /* exclusive */
3263#else
3264 destBox.y + destBox.h,
3265#endif
3266 GL_COLOR_BUFFER_BIT,
3267 (mode == SVGA3D_STRETCH_BLT_POINT) ? GL_NEAREST : GL_LINEAR);
3268 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3269
3270 /* Reset the frame buffer association */
3271 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
3272 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3273
3274 return VINF_SUCCESS;
3275}
3276
3277/**
3278 * Save texture packing parameters and loads those appropriate for the given
3279 * surface.
3280 *
3281 * @param pState The VMSVGA3D state structure.
3282 * @param pContext The active context.
3283 * @param pSurface The surface.
3284 * @param pSave Where to save stuff.
3285 */
3286static void vmsvga3dSetPackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
3287 PVMSVGAPACKPARAMS pSave)
3288{
3289 /*
3290 * Save (ignore errors, setting the defaults we want and avoids restore).
3291 */
3292 pSave->iAlignment = 1;
3293 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_UNPACK_ALIGNMENT, &pSave->iAlignment), pState, pContext);
3294 pSave->cxRow = 0;
3295 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_UNPACK_ROW_LENGTH, &pSave->cxRow), pState, pContext);
3296
3297#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
3298 pSave->cyImage = 0;
3299 glGetIntegerv(GL_PACK_IMAGE_HEIGHT, &pSave->cyImage);
3300 Assert(pSave->cyImage == 0);
3301
3302 pSave->fSwapBytes = GL_FALSE;
3303 glGetBooleanv(GL_PACK_SWAP_BYTES, &pSave->fSwapBytes);
3304 Assert(pSave->fSwapBytes == GL_FALSE);
3305
3306 pSave->fLsbFirst = GL_FALSE;
3307 glGetBooleanv(GL_PACK_LSB_FIRST, &pSave->fLsbFirst);
3308 Assert(pSave->fLsbFirst == GL_FALSE);
3309
3310 pSave->cSkipRows = 0;
3311 glGetIntegerv(GL_PACK_SKIP_ROWS, &pSave->cSkipRows);
3312 Assert(pSave->cSkipRows == 0);
3313
3314 pSave->cSkipPixels = 0;
3315 glGetIntegerv(GL_PACK_SKIP_PIXELS, &pSave->cSkipPixels);
3316 Assert(pSave->cSkipPixels == 0);
3317
3318 pSave->cSkipImages = 0;
3319 glGetIntegerv(GL_PACK_SKIP_IMAGES, &pSave->cSkipImages);
3320 Assert(pSave->cSkipImages == 0);
3321
3322 VMSVGA3D_CLEAR_GL_ERRORS();
3323#endif
3324
3325 /*
3326 * Setup unpack.
3327 *
3328 * Note! We use 1 as alignment here because we currently don't do any
3329 * aligning of line pitches anywhere.
3330 */
3331 NOREF(pSurface);
3332 if (pSave->iAlignment != 1)
3333 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ALIGNMENT, 1), pState, pContext);
3334 if (pSave->cxRow != 0)
3335 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ROW_LENGTH, 0), pState, pContext);
3336#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
3337 if (pSave->cyImage != 0)
3338 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0), pState, pContext);
3339 if (pSave->fSwapBytes != 0)
3340 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE), pState, pContext);
3341 if (pSave->fLsbFirst != 0)
3342 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE), pState, pContext);
3343 if (pSave->cSkipRows != 0)
3344 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_ROWS, 0), pState, pContext);
3345 if (pSave->cSkipPixels != 0)
3346 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_PIXELS, 0), pState, pContext);
3347 if (pSave->cSkipImages != 0)
3348 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_IMAGES, 0), pState, pContext);
3349#endif
3350}
3351
3352
3353/**
3354 * Restores texture packing parameters.
3355 *
3356 * @param pState The VMSVGA3D state structure.
3357 * @param pContext The active context.
3358 * @param pSurface The surface.
3359 * @param pSave Where stuff was saved.
3360 */
3361static void vmsvga3dRestorePackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
3362 PCVMSVGAPACKPARAMS pSave)
3363{
3364 NOREF(pSurface);
3365 if (pSave->iAlignment != 1)
3366 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ALIGNMENT, pSave->iAlignment), pState, pContext);
3367 if (pSave->cxRow != 0)
3368 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ROW_LENGTH, pSave->cxRow), pState, pContext);
3369#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
3370 if (pSave->cyImage != 0)
3371 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_IMAGE_HEIGHT, pSave->cyImage), pState, pContext);
3372 if (pSave->fSwapBytes != 0)
3373 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SWAP_BYTES, pSave->fSwapBytes), pState, pContext);
3374 if (pSave->fLsbFirst != 0)
3375 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_LSB_FIRST, pSave->fLsbFirst), pState, pContext);
3376 if (pSave->cSkipRows != 0)
3377 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_ROWS, pSave->cSkipRows), pState, pContext);
3378 if (pSave->cSkipPixels != 0)
3379 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_PIXELS, pSave->cSkipPixels), pState, pContext);
3380 if (pSave->cSkipImages != 0)
3381 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_IMAGES, pSave->cSkipImages), pState, pContext);
3382#endif
3383}
3384
3385
3386int vmsvga3dSurfaceDMA(PVGASTATE pThis, SVGA3dGuestImage guest, SVGA3dSurfaceImageId host, SVGA3dTransferType transfer,
3387 uint32_t cCopyBoxes, SVGA3dCopyBox *pBoxes)
3388{
3389 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
3390 PVMSVGA3DSURFACE pSurface;
3391 PVMSVGA3DMIPMAPLEVEL pMipLevel;
3392 uint32_t sid = host.sid;
3393 int rc = VINF_SUCCESS;
3394
3395 AssertReturn(pState, VERR_NO_MEMORY);
3396 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
3397 AssertReturn(sid < pState->cSurfaces && pState->paSurface[sid].id == sid, VERR_INVALID_PARAMETER);
3398
3399 pSurface = &pState->paSurface[sid];
3400 AssertReturn(pSurface->faces[0].numMipLevels > host.mipmap, VERR_INVALID_PARAMETER);
3401 pMipLevel = &pSurface->pMipmapLevels[host.mipmap];
3402
3403 if (pSurface->flags & SVGA3D_SURFACE_HINT_TEXTURE)
3404 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));
3405 else
3406 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));
3407
3408 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
3409 {
3410 AssertReturn(pSurface->pMipmapLevels[host.mipmap].pSurfaceData, VERR_INTERNAL_ERROR);
3411
3412 for (unsigned i = 0; i < cCopyBoxes; i++)
3413 {
3414 unsigned uDestOffset;
3415 unsigned cbSrcPitch;
3416 uint8_t *pBufferStart;
3417
3418 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));
3419 /* Apparently we're supposed to clip it (gmr test sample) */
3420 if (pBoxes[i].x + pBoxes[i].w > pMipLevel->size.width)
3421 pBoxes[i].w = pMipLevel->size.width - pBoxes[i].x;
3422 if (pBoxes[i].y + pBoxes[i].h > pMipLevel->size.height)
3423 pBoxes[i].h = pMipLevel->size.height - pBoxes[i].y;
3424 if (pBoxes[i].z + pBoxes[i].d > pMipLevel->size.depth)
3425 pBoxes[i].d = pMipLevel->size.depth - pBoxes[i].z;
3426
3427 if ( !pBoxes[i].w
3428 || !pBoxes[i].h
3429 || !pBoxes[i].d
3430 || pBoxes[i].x > pMipLevel->size.width
3431 || pBoxes[i].y > pMipLevel->size.height
3432 || pBoxes[i].z > pMipLevel->size.depth)
3433 {
3434 Log(("Empty box; skip\n"));
3435 continue;
3436 }
3437
3438 uDestOffset = pBoxes[i].x * pSurface->cbBlock + pBoxes[i].y * pMipLevel->cbSurfacePitch + pBoxes[i].z * pMipLevel->size.height * pMipLevel->cbSurfacePitch;
3439 AssertReturn(uDestOffset + pBoxes[i].w * pSurface->cbBlock * pBoxes[i].h * pBoxes[i].d <= pMipLevel->cbSurface, VERR_INTERNAL_ERROR);
3440
3441 cbSrcPitch = (guest.pitch == 0) ? pBoxes[i].w * pSurface->cbBlock : guest.pitch;
3442#ifdef MANUAL_FLIP_SURFACE_DATA
3443 pBufferStart = (uint8_t *)pMipLevel->pSurfaceData
3444 + pBoxes[i].x * pSurface->cbBlock
3445 + pMipLevel->cbSurface - pBoxes[i].y * pMipLevel->cbSurfacePitch
3446 - pMipLevel->cbSurfacePitch; /* flip image during copy */
3447#else
3448 pBufferStart = (uint8_t *)pMipLevel->pSurfaceData + uDestOffset;
3449#endif
3450 rc = vmsvgaGMRTransfer(pThis,
3451 transfer,
3452 pBufferStart,
3453#ifdef MANUAL_FLIP_SURFACE_DATA
3454 -(int32_t)pMipLevel->cbSurfacePitch,
3455#else
3456 (int32_t)pMipLevel->cbSurfacePitch,
3457#endif
3458 guest.ptr,
3459 pBoxes[i].srcx * pSurface->cbBlock + (pBoxes[i].srcy + pBoxes[i].srcz * pBoxes[i].h) * cbSrcPitch,
3460 cbSrcPitch,
3461 pBoxes[i].w * pSurface->cbBlock,
3462 pBoxes[i].d * pBoxes[i].h);
3463
3464 Log4(("first line:\n%.*Rhxd\n", pMipLevel->cbSurface, pMipLevel->pSurfaceData));
3465
3466 AssertRC(rc);
3467 }
3468 pSurface->pMipmapLevels[host.mipmap].fDirty = true;
3469 pSurface->fDirty = true;
3470 }
3471 else
3472 {
3473#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
3474 PVMSVGA3DCONTEXT pContext = &pState->SharedCtx;
3475#else
3476 /* @todo stricter checks for associated context */
3477 uint32_t cid = pSurface->idAssociatedContext;
3478 if ( cid >= pState->cContexts
3479 || pState->papContexts[cid]->id != cid)
3480 {
3481 Log(("vmsvga3dSurfaceDMA invalid context id (%x - %x)!\n", cid, (cid >= pState->cContexts) ? -1 : pState->papContexts[cid]->id));
3482 AssertFailedReturn(VERR_INVALID_PARAMETER);
3483 }
3484 PVMSVGA3DCONTEXT pContext = pState->papContexts[cid];
3485#endif
3486 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3487
3488 for (unsigned i = 0; i < cCopyBoxes; i++)
3489 {
3490 bool fVertex = false;
3491 unsigned cbSrcPitch;
3492
3493 /* Apparently we're supposed to clip it (gmr test sample) */
3494 if (pBoxes[i].x + pBoxes[i].w > pMipLevel->size.width)
3495 pBoxes[i].w = pMipLevel->size.width - pBoxes[i].x;
3496 if (pBoxes[i].y + pBoxes[i].h > pMipLevel->size.height)
3497 pBoxes[i].h = pMipLevel->size.height - pBoxes[i].y;
3498 if (pBoxes[i].z + pBoxes[i].d > pMipLevel->size.depth)
3499 pBoxes[i].d = pMipLevel->size.depth - pBoxes[i].z;
3500
3501 Assert((pBoxes[i].d == 1 || pBoxes[i].d == 0) && pBoxes[i].z == 0);
3502
3503 if ( !pBoxes[i].w
3504 || !pBoxes[i].h
3505 || pBoxes[i].x > pMipLevel->size.width
3506 || pBoxes[i].y > pMipLevel->size.height)
3507 {
3508 Log(("Empty box; skip\n"));
3509 continue;
3510 }
3511
3512 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));
3513
3514 cbSrcPitch = (guest.pitch == 0) ? pBoxes[i].w * pSurface->cbBlock : guest.pitch;
3515
3516 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))
3517 {
3518 case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
3519 case SVGA3D_SURFACE_HINT_TEXTURE:
3520 case SVGA3D_SURFACE_HINT_RENDERTARGET:
3521 {
3522 uint32_t cbSurfacePitch;
3523 uint8_t *pDoubleBuffer, *pBufferStart;
3524 unsigned uDestOffset = 0;
3525
3526 pDoubleBuffer = (uint8_t *)RTMemAlloc(pMipLevel->cbSurface);
3527 AssertReturn(pDoubleBuffer, VERR_NO_MEMORY);
3528
3529 if (transfer == SVGA3D_READ_HOST_VRAM)
3530 {
3531 GLint activeTexture;
3532
3533 /* Must bind texture to the current context in order to read it. */
3534 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
3535 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3536
3537 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
3538 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3539
3540 /* Set row length and alignment of the input data. */
3541 VMSVGAPACKPARAMS SavedParams;
3542 vmsvga3dSetPackParams(pState, pContext, pSurface, &SavedParams);
3543
3544 glGetTexImage(GL_TEXTURE_2D,
3545 host.mipmap,
3546 pSurface->formatGL,
3547 pSurface->typeGL,
3548 pDoubleBuffer);
3549 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3550
3551 vmsvga3dRestorePackParams(pState, pContext, pSurface, &SavedParams);
3552
3553 /* Restore the old active texture. */
3554 glBindTexture(GL_TEXTURE_2D, activeTexture);
3555 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3556
3557 uDestOffset = pBoxes[i].x * pSurface->cbBlock + pBoxes[i].y * pMipLevel->cbSurfacePitch;
3558 AssertReturnStmt( uDestOffset + pBoxes[i].w * pSurface->cbBlock + (pBoxes[i].h - 1) * pMipLevel->cbSurfacePitch
3559 <= pMipLevel->cbSurface,
3560 RTMemFree(pDoubleBuffer),
3561 VERR_INTERNAL_ERROR);
3562
3563 cbSurfacePitch = pMipLevel->cbSurfacePitch;
3564
3565#ifdef MANUAL_FLIP_SURFACE_DATA
3566 pBufferStart = pDoubleBuffer
3567 + pBoxes[i].x * pSurface->cbBlock
3568 + pMipLevel->cbSurface - pBoxes[i].y * cbSurfacePitch
3569 - cbSurfacePitch; /* flip image during copy */
3570#else
3571 pBufferStart = pDoubleBuffer + uDestOffset;
3572#endif
3573 }
3574 else
3575 {
3576 cbSurfacePitch = pBoxes[i].w * pSurface->cbBlock;
3577#ifdef MANUAL_FLIP_SURFACE_DATA
3578 pBufferStart = pDoubleBuffer + cbSurfacePitch * pBoxes[i].h - cbSurfacePitch; /* flip image during copy */
3579#else
3580 pBufferStart = pDoubleBuffer;
3581#endif
3582 }
3583
3584 rc = vmsvgaGMRTransfer(pThis,
3585 transfer,
3586 pBufferStart,
3587#ifdef MANUAL_FLIP_SURFACE_DATA
3588 -(int32_t)cbSurfacePitch,
3589#else
3590 (int32_t)cbSurfacePitch,
3591#endif
3592 guest.ptr,
3593 pBoxes[i].srcx * pSurface->cbBlock + pBoxes[i].srcy * cbSrcPitch,
3594 cbSrcPitch,
3595 pBoxes[i].w * pSurface->cbBlock,
3596 pBoxes[i].h);
3597 AssertRC(rc);
3598
3599 /* Update the opengl surface data. */
3600 if (transfer == SVGA3D_WRITE_HOST_VRAM)
3601 {
3602 GLint activeTexture = 0;
3603
3604 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
3605 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3606
3607 /* Must bind texture to the current context in order to change it. */
3608 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
3609 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3610
3611 Log(("vmsvga3dSurfaceDMA: copy texture mipmap level %d (pitch %x)\n", host.mipmap, pMipLevel->cbSurfacePitch));
3612
3613 /* Set row length and alignment of the input data. */
3614 VMSVGAPACKPARAMS SavedParams;
3615 vmsvga3dSetUnpackParams(pState, pContext, pSurface, &SavedParams); /** @todo do we need to set ROW_LENGTH to w here? */
3616
3617 glTexSubImage2D(GL_TEXTURE_2D,
3618 host.mipmap,
3619 pBoxes[i].x,
3620 pBoxes[i].y,
3621 pBoxes[i].w,
3622 pBoxes[i].h,
3623 pSurface->formatGL,
3624 pSurface->typeGL,
3625 pDoubleBuffer);
3626
3627 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3628
3629 /* Restore old values. */
3630 vmsvga3dRestoreUnpackParams(pState, pContext, pSurface, &SavedParams);
3631
3632 /* Restore the old active texture. */
3633 glBindTexture(GL_TEXTURE_2D, activeTexture);
3634 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3635 }
3636
3637 Log4(("first line:\n%.*Rhxd\n", pBoxes[i].w * pSurface->cbBlock, pDoubleBuffer));
3638
3639 /* Free the double buffer. */
3640 RTMemFree(pDoubleBuffer);
3641 break;
3642 }
3643
3644 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
3645 AssertFailed(); /* @todo */
3646 break;
3647
3648 case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
3649 case SVGA3D_SURFACE_HINT_INDEXBUFFER:
3650 {
3651 Assert(pBoxes[i].h == 1);
3652
3653 VMSVGA3D_CLEAR_GL_ERRORS();
3654 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pSurface->oglId.buffer);
3655 if (VMSVGA3D_GL_IS_SUCCESS(pContext))
3656 {
3657 GLenum enmGlTransfer = (transfer == SVGA3D_READ_HOST_VRAM) ? GL_READ_ONLY : GL_WRITE_ONLY;
3658 uint8_t *pbData = (uint8_t *)pState->ext.glMapBuffer(GL_ARRAY_BUFFER, enmGlTransfer);
3659 if (RT_LIKELY(pbData != NULL))
3660 {
3661#if defined(VBOX_STRICT) && defined(RT_OS_DARWIN)
3662 GLint cbStrictBufSize;
3663 glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &cbStrictBufSize);
3664 Assert(VMSVGA3D_GL_IS_SUCCESS(pContext));
3665# ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
3666 AssertMsg(cbStrictBufSize >= (int32_t)pMipLevel->cbSurface,
3667 ("cbStrictBufSize=%#x cbSurface=%#x pContext->id=%#x\n", (uint32_t)cbStrictBufSize, pMipLevel->cbSurface, pContext->id));
3668# else
3669 AssertMsg(cbStrictBufSize >= (int32_t)pMipLevel->cbSurface,
3670 ("cbStrictBufSize=%#x cbSurface=%#x isAssociatedContext=%#x pContext->id=%#x\n", (uint32_t)cbStrictBufSize, pMipLevel->cbSurface, pSurface->idAssociatedContext, pContext->id));
3671# endif
3672#endif
3673
3674 unsigned offDst = pBoxes[i].x * pSurface->cbBlock + pBoxes[i].y * pMipLevel->cbSurfacePitch;
3675 if (RT_LIKELY( offDst + pBoxes[i].w * pSurface->cbBlock + (pBoxes[i].h - 1) * pMipLevel->cbSurfacePitch
3676 <= pMipLevel->cbSurface))
3677 {
3678 Log(("Lock %s memory for rectangle (%d,%d)(%d,%d)\n", (fVertex) ? "vertex" : "index",
3679 pBoxes[i].x, pBoxes[i].y, pBoxes[i].x + pBoxes[i].w, pBoxes[i].y + pBoxes[i].h));
3680
3681 rc = vmsvgaGMRTransfer(pThis,
3682 transfer,
3683 pbData + offDst,
3684 pMipLevel->cbSurfacePitch,
3685 guest.ptr,
3686 pBoxes[i].srcx * pSurface->cbBlock + pBoxes[i].srcy * cbSrcPitch,
3687 cbSrcPitch,
3688 pBoxes[i].w * pSurface->cbBlock,
3689 pBoxes[i].h);
3690 AssertRC(rc);
3691
3692 Log4(("first line:\n%.*Rhxd\n", cbSrcPitch, pbData));
3693 }
3694 else
3695 {
3696 AssertFailed();
3697 rc = VERR_INTERNAL_ERROR;
3698 }
3699
3700 pState->ext.glUnmapBuffer(GL_ARRAY_BUFFER);
3701 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3702 }
3703 else
3704 VMSVGA3D_GL_GET_AND_COMPLAIN(pState, pContext, ("glMapBuffer(GL_ARRAY_BUFFER, %#x) -> NULL\n", enmGlTransfer));
3705 }
3706 else
3707 VMSVGA3D_GL_COMPLAIN(pState, pContext, ("glBindBuffer(GL_ARRAY_BUFFER, %#x)\n", pSurface->oglId.buffer));
3708 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0);
3709 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3710 break;
3711 }
3712
3713 default:
3714 AssertFailed();
3715 break;
3716 }
3717 }
3718 }
3719 return rc;
3720}
3721
3722int vmsvga3dSurfaceBlitToScreen(PVGASTATE pThis, uint32_t dest, SVGASignedRect destRect, SVGA3dSurfaceImageId src, SVGASignedRect srcRect, uint32_t cRects, SVGASignedRect *pRect)
3723{
3724 /* Requires SVGA_FIFO_CAP_SCREEN_OBJECT support */
3725 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));
3726 for (uint32_t i = 0; i < cRects; i++)
3727 {
3728 Log(("vmsvga3dSurfaceBlitToScreen: clipping rect %d (%d,%d)(%d,%d)\n", i, pRect[i].left, pRect[i].top, pRect[i].right, pRect[i].bottom));
3729 }
3730
3731 /* @todo Only screen 0 for now. */
3732 AssertReturn(dest == 0, VERR_INTERNAL_ERROR);
3733 AssertReturn(src.mipmap == 0 && src.face == 0, VERR_INVALID_PARAMETER);
3734 /* @todo scaling */
3735 AssertReturn(destRect.right - destRect.left == srcRect.right - srcRect.left && destRect.bottom - destRect.top == srcRect.bottom - srcRect.top, VERR_INVALID_PARAMETER);
3736
3737 if (cRects == 0)
3738 {
3739 /* easy case; no clipping */
3740 SVGA3dCopyBox box;
3741 SVGA3dGuestImage dst;
3742
3743 box.x = destRect.left;
3744 box.y = destRect.top;
3745 box.z = 0;
3746 box.w = destRect.right - destRect.left;
3747 box.h = destRect.bottom - destRect.top;
3748 box.d = 1;
3749 box.srcx = srcRect.left;
3750 box.srcy = srcRect.top;
3751 box.srcz = 0;
3752
3753 dst.ptr.gmrId = SVGA_GMR_FRAMEBUFFER;
3754 dst.ptr.offset = 0;
3755 dst.pitch = pThis->svga.cbScanline;
3756
3757 int rc = vmsvga3dSurfaceDMA(pThis, dst, src, SVGA3D_READ_HOST_VRAM, 1, &box);
3758 AssertRCReturn(rc, rc);
3759
3760 vgaR3UpdateDisplay(pThis, box.x, box.y, box.w, box.h);
3761 return VINF_SUCCESS;
3762 }
3763 else
3764 {
3765 SVGA3dGuestImage dst;
3766 SVGA3dCopyBox box;
3767
3768 box.srcz = 0;
3769 box.z = 0;
3770 box.d = 1;
3771
3772 dst.ptr.gmrId = SVGA_GMR_FRAMEBUFFER;
3773 dst.ptr.offset = 0;
3774 dst.pitch = pThis->svga.cbScanline;
3775
3776 /* @todo merge into one SurfaceDMA call */
3777 for (uint32_t i = 0; i < cRects; i++)
3778 {
3779 /* The clipping rectangle is relative to the top-left corner of srcRect & destRect. Adjust here. */
3780 box.srcx = srcRect.left + pRect[i].left;
3781 box.srcy = srcRect.top + pRect[i].top;
3782
3783 box.x = pRect[i].left + destRect.left;
3784 box.y = pRect[i].top + destRect.top;
3785 box.z = 0;
3786 box.w = pRect[i].right - pRect[i].left;
3787 box.h = pRect[i].bottom - pRect[i].top;
3788
3789 int rc = vmsvga3dSurfaceDMA(pThis, dst, src, SVGA3D_READ_HOST_VRAM, 1, &box);
3790 AssertRCReturn(rc, rc);
3791
3792 vgaR3UpdateDisplay(pThis, box.x, box.y, box.w, box.h);
3793 }
3794
3795 return VINF_SUCCESS;
3796 }
3797}
3798
3799int vmsvga3dGenerateMipmaps(PVGASTATE pThis, uint32_t sid, SVGA3dTextureFilter filter)
3800{
3801 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
3802 PVMSVGA3DSURFACE pSurface;
3803 int rc = VINF_SUCCESS;
3804 PVMSVGA3DCONTEXT pContext;
3805 uint32_t cid;
3806 GLint activeTexture = 0;
3807
3808 AssertReturn(pState, VERR_NO_MEMORY);
3809 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
3810 AssertReturn(sid < pState->cSurfaces && pState->paSurface[sid].id == sid, VERR_INVALID_PARAMETER);
3811
3812 pSurface = &pState->paSurface[sid];
3813#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
3814 AssertReturn(pSurface->idAssociatedContext != SVGA3D_INVALID_ID, VERR_INTERNAL_ERROR);
3815#endif
3816
3817 Assert(filter != SVGA3D_TEX_FILTER_FLATCUBIC);
3818 Assert(filter != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
3819 pSurface->autogenFilter = filter;
3820
3821 Log(("vmsvga3dGenerateMipmaps: sid=%x filter=%d\n", sid, filter));
3822
3823#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
3824 cid = SVGA3D_INVALID_ID;
3825 pContext = &pState->SharedCtx;
3826 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3827#else
3828 /* @todo stricter checks for associated context */
3829 cid = pSurface->idAssociatedContext;
3830
3831 if ( cid >= pState->cContexts
3832 || pState->papContexts[cid]->id != cid)
3833 {
3834 Log(("vmsvga3dGenerateMipmaps invalid context id!\n"));
3835 return VERR_INVALID_PARAMETER;
3836 }
3837 pContext = pState->papContexts[cid];
3838 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3839#endif
3840
3841 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
3842 {
3843 /* Unknown surface type; turn it into a texture. */
3844 Log(("vmsvga3dGenerateMipmaps: unknown src surface id=%x type=%d format=%d -> create texture\n", sid, pSurface->flags, pSurface->format));
3845 rc = vmsvga3dCreateTexture(pState, pContext, cid, pSurface);
3846 AssertRCReturn(rc, rc);
3847 }
3848 else
3849 {
3850 /* @todo new filter */
3851 AssertFailed();
3852 }
3853
3854 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
3855 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3856
3857 /* Must bind texture to the current context in order to change it. */
3858 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
3859 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3860
3861 /* Generate the mip maps. */
3862 pState->ext.glGenerateMipmap(GL_TEXTURE_2D);
3863 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3864
3865 /* Restore the old texture. */
3866 glBindTexture(GL_TEXTURE_2D, activeTexture);
3867 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3868
3869 return VINF_SUCCESS;
3870}
3871
3872int vmsvga3dCommandPresent(PVGASTATE pThis, uint32_t sid, uint32_t cRects, SVGA3dCopyRect *pRect)
3873{
3874 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
3875 PVMSVGA3DSURFACE pSurface;
3876 int rc = VINF_SUCCESS;
3877 PVMSVGA3DCONTEXT pContext;
3878 uint32_t cid;
3879 struct
3880 {
3881 uint32_t x;
3882 uint32_t y;
3883 uint32_t cx;
3884 uint32_t cy;
3885 } srcViewPort;
3886
3887 AssertReturn(pState, VERR_NO_MEMORY);
3888 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
3889 AssertReturn(sid < pState->cSurfaces && pState->paSurface[sid].id == sid, VERR_INVALID_PARAMETER);
3890
3891 pSurface = &pState->paSurface[sid];
3892#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
3893 AssertReturn(pSurface->idAssociatedContext != SVGA3D_INVALID_ID, VERR_INTERNAL_ERROR);
3894#endif
3895
3896#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
3897 /* @todo stricter checks for associated context */
3898 Log(("vmsvga3dCommandPresent: sid=%x cRects=%d\n", sid, cRects));
3899 for (uint32_t i=0; i < cRects; i++)
3900 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));
3901
3902 cid = SVGA3D_INVALID_ID;
3903 pContext = &pState->SharedCtx;
3904 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3905#else
3906 /* @todo stricter checks for associated context */
3907 cid = pSurface->idAssociatedContext;
3908 Log(("vmsvga3dCommandPresent: sid=%x cRects=%d cid=%x\n", sid, cRects, cid));
3909 for (uint32_t i=0; i < cRects; i++)
3910 {
3911 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));
3912 }
3913
3914 if ( cid >= pState->cContexts
3915 || pState->papContexts[cid]->id != cid)
3916 {
3917 Log(("vmsvga3dCommandPresent invalid context id!\n"));
3918 return VERR_INVALID_PARAMETER;
3919 }
3920 pContext = pState->papContexts[cid];
3921 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3922#endif
3923
3924 /* Source surface different size? */
3925 if (pSurface->pMipmapLevels[0].size.width != pThis->svga.uWidth ||
3926 pSurface->pMipmapLevels[0].size.height != pThis->svga.uHeight)
3927 {
3928 float xMultiplier = (float)pSurface->pMipmapLevels[0].size.width / (float)pThis->svga.uWidth;
3929 float yMultiplier = (float)pSurface->pMipmapLevels[0].size.height / (float)pThis->svga.uHeight;
3930
3931 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)));
3932
3933 srcViewPort.x = (uint32_t)((float)pThis->svga.viewport.x * xMultiplier);
3934 srcViewPort.y = (uint32_t)((float)pThis->svga.viewport.y * yMultiplier);
3935 srcViewPort.cx = (uint32_t)((float)pThis->svga.viewport.cx * xMultiplier);
3936 srcViewPort.cy = (uint32_t)((float)pThis->svga.viewport.cy * yMultiplier);
3937 }
3938 else
3939 {
3940 srcViewPort.x = pThis->svga.viewport.x;
3941 srcViewPort.y = pThis->svga.viewport.y;
3942 srcViewPort.cx = pThis->svga.viewport.cx;
3943 srcViewPort.cy = pThis->svga.viewport.cy;
3944 }
3945
3946#if 1
3947 /* @note this path is slightly faster than the glBlitFrameBuffer path below. */
3948 SVGA3dCopyRect rect;
3949 uint32_t oldVShader, oldPShader;
3950 GLint oldTextureId;
3951
3952 if (cRects == 0)
3953 {
3954 rect.x = rect.y = rect.srcx = rect.srcy = 0;
3955 rect.w = pSurface->pMipmapLevels[0].size.width;
3956 rect.h = pSurface->pMipmapLevels[0].size.height;
3957 pRect = &rect;
3958 cRects = 1;
3959 }
3960
3961 //glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
3962
3963#if 0
3964 glDisable(GL_CULL_FACE);
3965 glDisable(GL_BLEND);
3966 glDisable(GL_ALPHA_TEST);
3967 glDisable(GL_SCISSOR_TEST);
3968 glDisable(GL_STENCIL_TEST);
3969 glEnable(GL_DEPTH_TEST);
3970 glDepthFunc(GL_ALWAYS);
3971 glDepthMask(GL_TRUE);
3972 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
3973 glViewport(0, 0, pSurface->pMipmapLevels[0].size.width, pSurface->pMipmapLevels[0].size.height);
3974#endif
3975
3976 glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTextureId);
3977
3978 oldVShader = pContext->state.shidVertex;
3979 oldPShader = pContext->state.shidPixel;
3980 vmsvga3dShaderSet(pThis, cid, SVGA3D_SHADERTYPE_VS, SVGA_ID_INVALID);
3981 vmsvga3dShaderSet(pThis, cid, SVGA3D_SHADERTYPE_PS, SVGA_ID_INVALID);
3982
3983 /* Flush shader changes. */
3984 if (pContext->pShaderContext)
3985 ShaderUpdateState(pContext->pShaderContext, 0);
3986
3987 /* Activate the read and draw framebuffer objects. */
3988 pState->ext.glBindFramebuffer(GL_READ_FRAMEBUFFER, pContext->idReadFramebuffer);
3989 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3990 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0 /* back buffer */);
3991 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3992
3993 pState->ext.glActiveTexture(GL_TEXTURE0);
3994 glEnable(GL_TEXTURE_2D);
3995 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
3996 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3997
3998 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
3999 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
4000
4001// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
4002// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
4003
4004 /* Reset the transformation matrices. */
4005 glMatrixMode(GL_MODELVIEW);
4006 glPushMatrix();
4007 glLoadIdentity();
4008 glMatrixMode(GL_PROJECTION);
4009 glPushMatrix();
4010 glLoadIdentity();
4011 glScalef(1.0f, -1.0f, 1.0f);
4012 glOrtho(0, pThis->svga.uWidth, pThis->svga.uHeight, 0, 0.0, -1.0);
4013
4014 for (uint32_t i = 0; i < cRects; i++)
4015 {
4016 float left, right, top, bottom; /* Texture coordinates */
4017 int vertexLeft, vertexRight, vertexTop, vertexBottom;
4018
4019 pRect[i].srcx = RT_MAX(pRect[i].srcx, srcViewPort.x);
4020 pRect[i].srcy = RT_MAX(pRect[i].srcy, srcViewPort.y);
4021 pRect[i].x = RT_MAX(pRect[i].x, pThis->svga.viewport.x) - pThis->svga.viewport.x;
4022 pRect[i].y = RT_MAX(pRect[i].y, pThis->svga.viewport.y) - pThis->svga.viewport.y;
4023 pRect[i].w = pThis->svga.viewport.cx;
4024 pRect[i].h = pThis->svga.viewport.cy;
4025
4026 if ( pRect[i].x + pRect[i].w <= pThis->svga.viewport.x
4027 || pThis->svga.viewport.x + pThis->svga.viewport.cx <= pRect[i].x
4028 || pRect[i].y + pRect[i].h <= pThis->svga.viewport.y
4029 || pThis->svga.viewport.y + pThis->svga.viewport.cy <= pRect[i].y)
4030 {
4031 /* Intersection is empty; skip */
4032 continue;
4033 }
4034
4035 left = pRect[i].srcx;
4036 right = pRect[i].srcx + pRect[i].w;
4037 top = pRect[i].srcy + pRect[i].h;
4038 bottom = pRect[i].srcy;
4039
4040 left /= pSurface->pMipmapLevels[0].size.width;
4041 right /= pSurface->pMipmapLevels[0].size.width;
4042 top /= pSurface->pMipmapLevels[0].size.height;
4043 bottom /= pSurface->pMipmapLevels[0].size.height;
4044
4045 vertexLeft = pRect[i].x;
4046 vertexRight = pRect[i].x + pRect[i].w;
4047 vertexTop = ((uint32_t)pThis->svga.uHeight >= pRect[i].y + pRect[i].h) ? pThis->svga.uHeight - pRect[i].y - pRect[i].h : 0;
4048 vertexBottom = pThis->svga.uHeight - pRect[i].y;
4049
4050 Log(("view port (%d,%d)(%d,%d)\n", srcViewPort.x, srcViewPort.y, srcViewPort.cx, srcViewPort.cy));
4051 Log(("vertex (%d,%d) (%d,%d) (%d,%d) (%d,%d)\n", vertexLeft, vertexBottom, vertexLeft, vertexTop, vertexRight, vertexTop, vertexRight, vertexBottom));
4052 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)));
4053
4054 glBegin(GL_QUADS);
4055 /* bottom left */
4056 glTexCoord2f(left, bottom);
4057 glVertex2i(vertexLeft, vertexBottom);
4058
4059 /* top left */
4060 glTexCoord2f(left, top);
4061 glVertex2i(vertexLeft, vertexTop);
4062
4063 /* top right */
4064 glTexCoord2f(right, top);
4065 glVertex2i(vertexRight, vertexTop);
4066
4067 /* bottom right */
4068 glTexCoord2f(right, bottom);
4069 glVertex2i(vertexRight, vertexBottom);
4070
4071 glEnd();
4072 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4073 }
4074
4075 /* Restore old settings. */
4076 glMatrixMode(GL_PROJECTION);
4077 glPopMatrix();
4078 glMatrixMode(GL_MODELVIEW);
4079 glPopMatrix();
4080
4081 //glPopAttrib();
4082
4083 glBindTexture(GL_TEXTURE_2D, oldTextureId);
4084 vmsvga3dShaderSet(pThis, cid, SVGA3D_SHADERTYPE_VS, oldVShader);
4085 vmsvga3dShaderSet(pThis, cid, SVGA3D_SHADERTYPE_PS, oldPShader);
4086
4087 /* Reset the frame buffer association */
4088 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
4089 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4090
4091#else
4092 /* Activate the read and draw framebuffer objects. */
4093 pState->ext.glBindFramebuffer(GL_READ_FRAMEBUFFER, pContext->idReadFramebuffer);
4094 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
4095 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0 /* back buffer */);
4096 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
4097
4098 /* Bind the source objects to the right place. */
4099 pState->ext.glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pSurface->oglId.texture, 0 /* level 0 */);
4100 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
4101
4102 /* Blit the surface rectangle(s) to the back buffer. */
4103 if (cRects == 0)
4104 {
4105 Log(("view port (%d,%d)(%d,%d)\n", srcViewPort.x, srcViewPort.y, srcViewPort.cx, srcViewPort.cy));
4106 pState->ext.glBlitFramebuffer(srcViewPort.x,
4107 srcViewPort.y,
4108 srcViewPort.x + srcViewPort.cx, /* exclusive. */
4109 srcViewPort.y + srcViewPort.cy, /* exclusive. (reverse to flip the image) */
4110 0,
4111 pThis->svga.viewport.cy, /* exclusive. */
4112 pThis->svga.viewport.cx, /* exclusive. */
4113 0,
4114 GL_COLOR_BUFFER_BIT,
4115 GL_LINEAR);
4116 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
4117 }
4118 else
4119 {
4120 for (uint32_t i = 0; i < cRects; i++)
4121 {
4122 if ( pRect[i].x + pRect[i].w <= pThis->svga.viewport.x
4123 || pThis->svga.viewport.x + pThis->svga.viewport.cx <= pRect[i].x
4124 || pRect[i].y + pRect[i].h <= pThis->svga.viewport.y
4125 || pThis->svga.viewport.y + pThis->svga.viewport.cy <= pRect[i].y)
4126 {
4127 /* Intersection is empty; skip */
4128 continue;
4129 }
4130
4131 pState->ext.glBlitFramebuffer(RT_MAX(pRect[i].srcx, srcViewPort.x),
4132 pSurface->pMipmapLevels[0].size.width - RT_MAX(pRect[i].srcy, srcViewPort.y), /* exclusive. (reverse to flip the image) */
4133 RT_MIN(pRect[i].srcx + pRect[i].w, srcViewPort.x + srcViewPort.cx), /* exclusive. */
4134 pSurface->pMipmapLevels[0].size.width - RT_MIN(pRect[i].srcy + pRect[i].h, srcViewPort.y + srcViewPort.cy),
4135 RT_MAX(pRect[i].x, pThis->svga.viewport.x) - pThis->svga.viewport.x,
4136 pThis->svga.uHeight - (RT_MIN(pRect[i].y + pRect[i].h, pThis->svga.viewport.y + pThis->svga.viewport.cy) - pThis->svga.viewport.y), /* exclusive. */
4137 RT_MIN(pRect[i].x + pRect[i].w, pThis->svga.viewport.x + pThis->svga.viewport.cx) - pThis->svga.viewport.x, /* exclusive. */
4138 pThis->svga.uHeight - (RT_MAX(pRect[i].y, pThis->svga.viewport.y) - pThis->svga.viewport.y),
4139 GL_COLOR_BUFFER_BIT,
4140 GL_LINEAR);
4141 }
4142 }
4143 /* Reset the frame buffer association */
4144 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
4145 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
4146#endif
4147
4148 /* Flip the front and back buffers. */
4149#ifdef RT_OS_WINDOWS
4150 BOOL ret = SwapBuffers(pContext->hdc);
4151 AssertMsg(ret, ("SwapBuffers failed with %d\n", GetLastError()));
4152#elif defined(RT_OS_DARWIN)
4153 vmsvga3dCocoaSwapBuffers(pContext->cocoaView, pContext->cocoaContext);
4154#else
4155 /* show the window if not already done */
4156 if (!pContext->fMapped)
4157 {
4158 XMapWindow(pState->display, pContext->window);
4159 pContext->fMapped = true;
4160 }
4161 /* now swap the buffers, i.e. display the rendering result */
4162 glXSwapBuffers(pState->display, pContext->window);
4163#endif
4164 return VINF_SUCCESS;
4165}
4166
4167#ifdef RT_OS_LINUX
4168/**
4169 * X11 event handling thread
4170 * @param ThreadSelf thread handle
4171 * @param pvUser pointer to pState structure
4172 * @returns VBox status code
4173 */
4174DECLCALLBACK(int) vmsvga3dXEventThread(RTTHREAD ThreadSelf, void *pvUser)
4175{
4176 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pvUser;
4177 while (!pState->bTerminate)
4178 {
4179 while (XPending(pState->display) > 0)
4180 {
4181 XEvent event;
4182 XNextEvent(pState->display, &event);
4183
4184 switch (event.type)
4185 {
4186 default:
4187 break;
4188 }
4189 }
4190 /* sleep for 16ms to not burn too many cycles */
4191 RTThreadSleep(16);
4192 }
4193 return VINF_SUCCESS;
4194}
4195#endif // RT_OS_LINUX
4196
4197
4198/**
4199 * Create a new 3d context
4200 *
4201 * @returns VBox status code.
4202 * @param pThis VGA device instance data.
4203 * @param cid Context id
4204 * @param fFlags VMSVGA3D_DEF_CTX_F_XXX.
4205 */
4206static int vmsvga3dContextDefineOgl(PVGASTATE pThis, uint32_t cid, uint32_t fFlags)
4207{
4208 int rc;
4209 PVMSVGA3DCONTEXT pContext;
4210 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4211
4212 AssertReturn(pState, VERR_NO_MEMORY);
4213#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
4214 AssertReturn( cid < SVGA3D_MAX_CONTEXT_IDS
4215 || (cid == VMSVGA3D_SHARED_CTX_ID && (fFlags & VMSVGA3D_DEF_CTX_F_SHARED_CTX)), VERR_INVALID_PARAMETER);
4216#else
4217 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
4218#endif
4219#if !defined(VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE) || !(defined(RT_OS_DARWIN))
4220 AssertReturn(!(fFlags & VMSVGA3D_DEF_CTX_F_OTHER_PROFILE), VERR_INTERNAL_ERROR_3);
4221#endif
4222
4223 Log(("vmsvga3dContextDefine id %x\n", cid));
4224#ifdef DEBUG_DEBUG_GFX_WINDOW_TEST_CONTEXT
4225 if (pState->idTestContext == SVGA_ID_INVALID)
4226 {
4227 pState->idTestContext = 207;
4228 rc = vmsvga3dContextDefine(pThis, pState->idTestContext);
4229 AssertRCReturn(rc, rc);
4230 }
4231#endif
4232
4233#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
4234 if (cid == VMSVGA3D_SHARED_CTX_ID)
4235 pContext = &pState->SharedCtx;
4236 else
4237#endif
4238 {
4239 if (cid >= pState->cContexts)
4240 {
4241 /* Grow the array. */
4242 uint32_t cNew = RT_ALIGN(cid + 15, 16);
4243 void *pvNew = RTMemRealloc(pState->papContexts, sizeof(pState->papContexts[0]) * cNew);
4244 AssertReturn(pvNew, VERR_NO_MEMORY);
4245 pState->papContexts = (PVMSVGA3DCONTEXT *)pvNew;
4246 while (pState->cContexts < cNew)
4247 {
4248 pContext = (PVMSVGA3DCONTEXT)RTMemAllocZ(sizeof(*pContext));
4249 AssertReturn(pContext, VERR_NO_MEMORY);
4250 pContext->id = SVGA3D_INVALID_ID;
4251 pState->papContexts[pState->cContexts++] = pContext;
4252 }
4253 }
4254 /* If one already exists with this id, then destroy it now. */
4255 if (pState->papContexts[cid]->id != SVGA3D_INVALID_ID)
4256 vmsvga3dContextDestroy(pThis, cid);
4257
4258 pContext = pState->papContexts[cid];
4259 }
4260
4261 /*
4262 * Find the shared context (necessary for sharing e.g. textures between contexts).
4263 */
4264#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
4265 PVMSVGA3DCONTEXT pSharedCtx = NULL;
4266 if (!(fFlags & (VMSVGA3D_DEF_CTX_F_INIT | VMSVGA3D_DEF_CTX_F_SHARED_CTX)))
4267 {
4268 pSharedCtx = &pState->SharedCtx;
4269 if (pSharedCtx->id != VMSVGA3D_SHARED_CTX_ID)
4270 {
4271 rc = vmsvga3dContextDefineOgl(pThis, VMSVGA3D_SHARED_CTX_ID, VMSVGA3D_DEF_CTX_F_SHARED_CTX);
4272 AssertLogRelRCReturn(rc, rc);
4273 }
4274 }
4275#else
4276 // TODO isn't this default on Linux since OpenGL 1.1?
4277 /* Find the first active context to share the display list with (necessary for sharing e.g. textures between contexts). */
4278 PVMSVGA3DCONTEXT pSharedCtx = NULL;
4279 for (uint32_t i = 0; i < pState->cContexts; i++)
4280 if ( pState->papContexts[i]->id != SVGA3D_INVALID_ID
4281 && i != cid
4282# ifdef VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE
4283 && pState->papContexts[i]->fOtherProfile == RT_BOOL(fFlags & VMSVGA3D_DEF_CTX_F_OTHER_PROFILE)
4284# endif
4285 )
4286 {
4287 Log(("Sharing display lists between cid=%d and cid=%d\n", pContext->id, i));
4288 pSharedCtx = pState->papContexts[i];
4289 break;
4290 }
4291#endif
4292
4293 /*
4294 * Initialize the context.
4295 */
4296 memset(pContext, 0, sizeof(*pContext));
4297 pContext->id = cid;
4298 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTexture); i++)
4299 pContext->aSidActiveTexture[i] = SVGA3D_INVALID_ID;
4300
4301 pContext->sidRenderTarget = SVGA3D_INVALID_ID;
4302 pContext->state.shidVertex = SVGA3D_INVALID_ID;
4303 pContext->state.shidPixel = SVGA3D_INVALID_ID;
4304 pContext->idFramebuffer = OPENGL_INVALID_ID;
4305 pContext->idReadFramebuffer = OPENGL_INVALID_ID;
4306 pContext->idDrawFramebuffer = OPENGL_INVALID_ID;
4307
4308 rc = ShaderContextCreate(&pContext->pShaderContext);
4309 AssertRCReturn(rc, rc);
4310
4311 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); i++)
4312 pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
4313
4314 AssertReturn(pThis->svga.u64HostWindowId, VERR_INTERNAL_ERROR);
4315
4316#ifdef RT_OS_WINDOWS
4317 /* Create a context window. */
4318 CREATESTRUCT cs;
4319 cs.lpCreateParams = NULL;
4320 cs.dwExStyle = WS_EX_NOACTIVATE | WS_EX_NOPARENTNOTIFY | WS_EX_TRANSPARENT;
4321# ifdef DEBUG_GFX_WINDOW
4322 cs.lpszName = (char *)RTMemAllocZ(256);
4323 RTStrPrintf((char *)cs.lpszName, 256, "Context %d OpenGL Window", cid);
4324# else
4325 cs.lpszName = NULL;
4326# endif
4327 cs.lpszClass = 0;
4328# ifdef DEBUG_GFX_WINDOW
4329 cs.style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE | WS_CAPTION;
4330# else
4331 cs.style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED | WS_CHILD | WS_VISIBLE;
4332# endif
4333 cs.x = 0;
4334 cs.y = 0;
4335 cs.cx = pThis->svga.uWidth;
4336 cs.cy = pThis->svga.uHeight;
4337 cs.hwndParent = (HWND)pThis->svga.u64HostWindowId;
4338 cs.hMenu = NULL;
4339 cs.hInstance = pState->hInstance;
4340
4341 rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_CREATEWINDOW, (WPARAM)&pContext->hwnd, (LPARAM)&cs);
4342 AssertRCReturn(rc, rc);
4343
4344 pContext->hdc = GetDC(pContext->hwnd);
4345 AssertMsgReturn(pContext->hdc, ("GetDC %x failed with %d\n", pContext->hwnd, GetLastError()), VERR_INTERNAL_ERROR);
4346
4347 PIXELFORMATDESCRIPTOR pfd = {
4348 sizeof(PIXELFORMATDESCRIPTOR), /* size of this pfd */
4349 1, /* version number */
4350 PFD_DRAW_TO_WINDOW | /* support window */
4351 PFD_DOUBLEBUFFER | /* support double buffering */
4352 PFD_SUPPORT_OPENGL, /* support OpenGL */
4353 PFD_TYPE_RGBA, /* RGBA type */
4354 24, /* 24-bit color depth */
4355 0, 0, 0, 0, 0, 0, /* color bits ignored */
4356 8, /* alpha buffer */
4357 0, /* shift bit ignored */
4358 0, /* no accumulation buffer */
4359 0, 0, 0, 0, /* accum bits ignored */
4360 16, /* set depth buffer */
4361 16, /* set stencil buffer */
4362 0, /* no auxiliary buffer */
4363 PFD_MAIN_PLANE, /* main layer */
4364 0, /* reserved */
4365 0, 0, 0 /* layer masks ignored */
4366 };
4367 int pixelFormat;
4368 BOOL ret;
4369
4370 pixelFormat = ChoosePixelFormat(pContext->hdc, &pfd);
4371 /* @todo is this really necessary?? */
4372 pixelFormat = ChoosePixelFormat(pContext->hdc, &pfd);
4373 AssertMsgReturn(pixelFormat != 0, ("ChoosePixelFormat failed with %d\n", GetLastError()), VERR_INTERNAL_ERROR);
4374
4375 ret = SetPixelFormat(pContext->hdc, pixelFormat, &pfd);
4376 AssertMsgReturn(ret == TRUE, ("SetPixelFormat failed with %d\n", GetLastError()), VERR_INTERNAL_ERROR);
4377
4378 pContext->hglrc = wglCreateContext(pContext->hdc);
4379 AssertMsgReturn(pContext->hglrc, ("wglCreateContext %x failed with %d\n", pContext->hdc, GetLastError()), VERR_INTERNAL_ERROR);
4380
4381 if (pSharedCtx)
4382 {
4383 ret = wglShareLists(pSharedCtx->hglrc, pContext->hglrc);
4384 AssertMsg(ret == TRUE, ("wglShareLists(%p, %p) failed with %d\n", pSharedCtx->hglrc, pContext->hglrc, GetLastError()));
4385 }
4386
4387#elif defined(RT_OS_DARWIN)
4388 pContext->fOtherProfile = RT_BOOL(fFlags & VMSVGA3D_DEF_CTX_F_OTHER_PROFILE);
4389
4390 NativeNSOpenGLContextRef shareContext = pSharedCtx ? pSharedCtx->cocoaContext : NULL;
4391 vmsvga3dCocoaCreateContext(&pContext->cocoaContext, shareContext, pContext->fOtherProfile);
4392 NativeNSViewRef pHostView = (NativeNSViewRef)pThis->svga.u64HostWindowId;
4393 vmsvga3dCocoaCreateView(&pContext->cocoaView, pHostView);
4394
4395#else
4396 Window hostWindow = (Window)pThis->svga.u64HostWindowId;
4397
4398 if (pState->display == NULL)
4399 {
4400 /* get an X display and make sure we have glX 1.3 */
4401 pState->display = XOpenDisplay(0);
4402 Assert(pState->display);
4403 int glxMajor, glxMinor;
4404 Bool ret = glXQueryVersion(pState->display, &glxMajor, &glxMinor);
4405 AssertMsgReturn(ret && glxMajor == 1 && glxMinor >= 3, ("glX >=1.3 not present"), VERR_INTERNAL_ERROR);
4406 /* start our X event handling thread */
4407 rc = RTThreadCreate(&pState->pWindowThread, vmsvga3dXEventThread, pState, 0, RTTHREADTYPE_GUI, RTTHREADFLAGS_WAITABLE, "VMSVGA3DXEVENT");
4408 if (RT_FAILURE(rc))
4409 {
4410 AssertMsgFailed(("%s: Async IO Thread creation for 3d window handling failed rc=%d\n", __FUNCTION__, rc));
4411 return rc;
4412 }
4413 }
4414 int attrib[] =
4415 {
4416 GLX_RGBA,
4417 GLX_RED_SIZE, 1,
4418 GLX_GREEN_SIZE, 1,
4419 GLX_BLUE_SIZE, 1,
4420 //GLX_ALPHA_SIZE, 1, this flips the bbos screen
4421 GLX_DOUBLEBUFFER,
4422 None
4423 };
4424 XVisualInfo *vi = glXChooseVisual(pState->display, DefaultScreen(pState->display), attrib);
4425 XSetWindowAttributes swa;
4426 swa.colormap = XCreateColormap(pState->display, XDefaultRootWindow(pState->display), vi->visual, AllocNone);
4427 swa.border_pixel = 0;
4428 swa.background_pixel = 0;
4429 swa.event_mask = StructureNotifyMask | ExposureMask;
4430 unsigned long flags = CWBorderPixel | CWBackPixel | CWColormap | CWEventMask;
4431 pContext->window = XCreateWindow(pState->display, hostWindow,//XDefaultRootWindow(pState->display),//hostWindow,
4432 0, 0, pThis->svga.uWidth, pThis->svga.uHeight,
4433 0, vi->depth, InputOutput,
4434 vi->visual, flags, &swa);
4435 AssertMsgReturn(pContext->window, ("XCreateWindow failed"), VERR_INTERNAL_ERROR);
4436 uint32_t cardinal_alpha = (uint32_t) (0.5 * (uint32_t)-1) ;
4437
4438 /* the window is hidden by default and only mapped when CommandPresent is executed on it */
4439
4440 GLXContext shareContext = pSharedCtx ? pSharedCtx->glxContext : NULL;
4441 pContext->glxContext = glXCreateContext(pState->display, vi, shareContext, GL_TRUE);
4442 AssertMsgReturn(pContext->glxContext, ("glXCreateContext failed"), VERR_INTERNAL_ERROR);
4443#endif
4444
4445 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4446
4447 /* NULL during the first PowerOn call. */
4448 if (pState->ext.glGenFramebuffers)
4449 {
4450 /* Create a framebuffer object for this context. */
4451 pState->ext.glGenFramebuffers(1, &pContext->idFramebuffer);
4452 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4453
4454 /* Bind the object to the framebuffer target. */
4455 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
4456 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4457
4458 /* Create read and draw framebuffer objects for this context. */
4459 pState->ext.glGenFramebuffers(1, &pContext->idReadFramebuffer);
4460 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4461
4462 pState->ext.glGenFramebuffers(1, &pContext->idDrawFramebuffer);
4463 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4464
4465 }
4466#if 0
4467 /* @todo move to shader lib!!! */
4468 /* Clear the screen */
4469 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4470
4471 glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
4472 glClearIndex(0);
4473 glClearDepth(1);
4474 glClearStencil(0xffff);
4475 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
4476 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
4477 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
4478 if (pState->ext.glProvokingVertex)
4479 pState->ext.glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);
4480 /* @todo move to shader lib!!! */
4481#endif
4482 return VINF_SUCCESS;
4483}
4484
4485
4486/**
4487 * Create a new 3d context
4488 *
4489 * @returns VBox status code.
4490 * @param pThis VGA device instance data.
4491 * @param cid Context id
4492 */
4493int vmsvga3dContextDefine(PVGASTATE pThis, uint32_t cid)
4494{
4495 return vmsvga3dContextDefineOgl(pThis, cid, 0/*fFlags*/);
4496}
4497
4498
4499/**
4500 * Destroy an existing 3d context
4501 *
4502 * @returns VBox status code.
4503 * @param pThis VGA device instance data.
4504 * @param cid Context id
4505 */
4506int vmsvga3dContextDestroy(PVGASTATE pThis, uint32_t cid)
4507{
4508 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4509 AssertReturn(pState, VERR_NO_MEMORY);
4510
4511 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
4512
4513 if ( cid < pState->cContexts
4514 && pState->papContexts[cid]->id == cid)
4515 {
4516 PVMSVGA3DCONTEXT pContext = pState->papContexts[cid];
4517
4518 Log(("vmsvga3dContextDestroy id %x\n", cid));
4519
4520 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4521
4522 /* Destroy all leftover pixel shaders. */
4523 for (uint32_t i = 0; i < pContext->cPixelShaders; i++)
4524 {
4525 if (pContext->paPixelShader[i].id != SVGA3D_INVALID_ID)
4526 vmsvga3dShaderDestroy(pThis, pContext->paPixelShader[i].cid, pContext->paPixelShader[i].id, pContext->paPixelShader[i].type);
4527 }
4528 if (pContext->paPixelShader)
4529 RTMemFree(pContext->paPixelShader);
4530
4531 /* Destroy all leftover vertex shaders. */
4532 for (uint32_t i = 0; i < pContext->cVertexShaders; i++)
4533 {
4534 if (pContext->paVertexShader[i].id != SVGA3D_INVALID_ID)
4535 vmsvga3dShaderDestroy(pThis, pContext->paVertexShader[i].cid, pContext->paVertexShader[i].id, pContext->paVertexShader[i].type);
4536 }
4537 if (pContext->paVertexShader)
4538 RTMemFree(pContext->paVertexShader);
4539
4540 if (pContext->state.paVertexShaderConst)
4541 RTMemFree(pContext->state.paVertexShaderConst);
4542 if (pContext->state.paPixelShaderConst)
4543 RTMemFree(pContext->state.paPixelShaderConst);
4544
4545 if (pContext->pShaderContext)
4546 {
4547 int rc = ShaderContextDestroy(pContext->pShaderContext);
4548 AssertRC(rc);
4549 }
4550
4551#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. */
4552 /* Check for all surfaces that are associated with this context to remove all dependencies */
4553 for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
4554 {
4555 PVMSVGA3DSURFACE pSurface = &pState->paSurface[sid];
4556 if ( pSurface->idAssociatedContext == cid
4557 && pSurface->id == sid)
4558 {
4559 int rc;
4560
4561 Log(("vmsvga3dContextDestroy: remove all dependencies for surface %x\n", sid));
4562
4563 uint32_t surfaceFlags = pSurface->flags;
4564 SVGA3dSurfaceFormat format = pSurface->format;
4565 SVGA3dSurfaceFace face[SVGA3D_MAX_SURFACE_FACES];
4566 uint32_t multisampleCount = pSurface->multiSampleCount;
4567 SVGA3dTextureFilter autogenFilter = pSurface->autogenFilter;
4568 SVGA3dSize *pMipLevelSize;
4569 uint32_t cFaces = pSurface->cFaces;
4570
4571 pMipLevelSize = (SVGA3dSize *)RTMemAllocZ(pSurface->faces[0].numMipLevels * pSurface->cFaces * sizeof(SVGA3dSize));
4572 AssertReturn(pMipLevelSize, VERR_NO_MEMORY);
4573
4574 for (uint32_t iFace = 0; iFace < pSurface->cFaces; iFace++)
4575 {
4576 for (uint32_t i = 0; i < pSurface->faces[0].numMipLevels; i++)
4577 {
4578 uint32_t idx = i + iFace * pSurface->faces[0].numMipLevels;
4579 memcpy(&pMipLevelSize[idx], &pSurface->pMipmapLevels[idx].size, sizeof(SVGA3dSize));
4580 }
4581 }
4582 memcpy(face, pSurface->faces, sizeof(pSurface->faces));
4583
4584 /* Recreate the surface with the original settings; destroys the contents, but that seems fairly safe since the context is also destroyed. */
4585 rc = vmsvga3dSurfaceDestroy(pThis, sid);
4586 AssertRC(rc);
4587
4588 rc = vmsvga3dSurfaceDefine(pThis, sid, surfaceFlags, format, face, multisampleCount, autogenFilter, face[0].numMipLevels * cFaces, pMipLevelSize);
4589 AssertRC(rc);
4590 }
4591 }
4592#endif
4593
4594 if (pContext->idFramebuffer != OPENGL_INVALID_ID)
4595 {
4596 /* Unbind the object from the framebuffer target. */
4597 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, 0 /* back buffer */);
4598 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4599 pState->ext.glDeleteFramebuffers(1, &pContext->idFramebuffer);
4600 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4601
4602 if (pContext->idReadFramebuffer != OPENGL_INVALID_ID)
4603 {
4604 pState->ext.glDeleteFramebuffers(1, &pContext->idReadFramebuffer);
4605 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4606 }
4607 if (pContext->idDrawFramebuffer != OPENGL_INVALID_ID)
4608 {
4609 pState->ext.glDeleteFramebuffers(1, &pContext->idDrawFramebuffer);
4610 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4611 }
4612 }
4613#ifdef RT_OS_WINDOWS
4614 wglMakeCurrent(pContext->hdc, NULL);
4615 wglDeleteContext(pContext->hglrc);
4616 ReleaseDC(pContext->hwnd, pContext->hdc);
4617
4618 /* Destroy the window we've created. */
4619 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_DESTROYWINDOW, (WPARAM)pContext->hwnd, 0);
4620 AssertRC(rc);
4621#elif defined(RT_OS_DARWIN)
4622 vmsvga3dCocoaDestroyView(pContext->cocoaView);
4623 vmsvga3dCocoaDestroyContext(pContext->cocoaContext);
4624#elif defined(RT_OS_LINUX)
4625 glXMakeCurrent(pState->display, None, NULL);
4626 glXDestroyContext(pState->display, pContext->glxContext);
4627 XDestroyWindow(pState->display, pContext->window);
4628#endif
4629
4630 memset(pContext, 0, sizeof(*pContext));
4631 pContext->id = SVGA3D_INVALID_ID;
4632
4633 VMSVGA3D_CLEAR_CURRENT_CONTEXT(pState);
4634 }
4635 else
4636 AssertFailed();
4637
4638 return VINF_SUCCESS;
4639}
4640
4641/* Handle resize */
4642int vmsvga3dChangeMode(PVGASTATE pThis)
4643{
4644 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4645 AssertReturn(pState, VERR_NO_MEMORY);
4646
4647 /* Resize all active contexts. */
4648 for (uint32_t i = 0; i < pState->cContexts; i++)
4649 {
4650 PVMSVGA3DCONTEXT pContext = pState->papContexts[i];
4651 uint32_t cid = pContext->id;
4652
4653 if (cid != SVGA3D_INVALID_ID)
4654 {
4655#ifdef RT_OS_WINDOWS
4656 CREATESTRUCT cs;
4657
4658 memset(&cs, 0, sizeof(cs));
4659 cs.cx = pThis->svga.uWidth;
4660 cs.cy = pThis->svga.uHeight;
4661
4662 /* Resize the window. */
4663 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_RESIZEWINDOW, (WPARAM)pContext->hwnd, (LPARAM)&cs);
4664 AssertRC(rc);
4665#elif defined(RT_OS_DARWIN)
4666 vmsvga3dCocoaViewSetSize(pContext->cocoaView, pThis->svga.uWidth, pThis->svga.uHeight);
4667#elif defined(RT_OS_LINUX)
4668 XWindowChanges wc;
4669 wc.width = pThis->svga.uWidth;
4670 wc.height = pThis->svga.uHeight;
4671 XConfigureWindow(pState->display, pContext->window, CWWidth | CWHeight, &wc);
4672#endif
4673 }
4674 }
4675 return VINF_SUCCESS;
4676}
4677
4678
4679int vmsvga3dSetTransform(PVGASTATE pThis, uint32_t cid, SVGA3dTransformType type, float matrix[16])
4680{
4681 PVMSVGA3DCONTEXT pContext;
4682 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4683 AssertReturn(pState, VERR_NO_MEMORY);
4684 bool fModelViewChanged = false;
4685
4686 Log(("vmsvga3dSetTransform cid=%x %s\n", cid, vmsvgaTransformToString(type)));
4687
4688 if ( cid >= pState->cContexts
4689 || pState->papContexts[cid]->id != cid)
4690 {
4691 Log(("vmsvga3dSetTransform invalid context id!\n"));
4692 return VERR_INVALID_PARAMETER;
4693 }
4694 pContext = pState->papContexts[cid];
4695 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4696
4697 /* Save this matrix for vm state save/restore. */
4698 pContext->state.aTransformState[type].fValid = true;
4699 memcpy(pContext->state.aTransformState[type].matrix, matrix, sizeof(pContext->state.aTransformState[type].matrix));
4700 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_TRANSFORM;
4701
4702 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)));
4703 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)));
4704 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)));
4705 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)));
4706
4707 switch (type)
4708 {
4709 case SVGA3D_TRANSFORM_VIEW:
4710 /* View * World = Model View */
4711 glMatrixMode(GL_MODELVIEW);
4712 glLoadMatrixf(matrix);
4713 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_WORLD].fValid)
4714 glMultMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_WORLD].matrix);
4715 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4716 fModelViewChanged = true;
4717 break;
4718
4719 case SVGA3D_TRANSFORM_PROJECTION:
4720 {
4721 int rc = ShaderTransformProjection(pContext->state.RectViewPort.w, pContext->state.RectViewPort.h, matrix);
4722 AssertRCReturn(rc, rc);
4723 break;
4724 }
4725
4726 case SVGA3D_TRANSFORM_TEXTURE0:
4727 glMatrixMode(GL_TEXTURE);
4728 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4729 glLoadMatrixf(matrix);
4730 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4731 break;
4732
4733 case SVGA3D_TRANSFORM_TEXTURE1:
4734 case SVGA3D_TRANSFORM_TEXTURE2:
4735 case SVGA3D_TRANSFORM_TEXTURE3:
4736 case SVGA3D_TRANSFORM_TEXTURE4:
4737 case SVGA3D_TRANSFORM_TEXTURE5:
4738 case SVGA3D_TRANSFORM_TEXTURE6:
4739 case SVGA3D_TRANSFORM_TEXTURE7:
4740 Log(("vmsvga3dSetTransform: unsupported SVGA3D_TRANSFORM_TEXTUREx transform!!\n"));
4741 return VERR_INVALID_PARAMETER;
4742
4743 case SVGA3D_TRANSFORM_WORLD:
4744 /* View * World = Model View */
4745 glMatrixMode(GL_MODELVIEW);
4746 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].fValid)
4747 glLoadMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
4748 else
4749 glLoadIdentity();
4750 glMultMatrixf(matrix);
4751 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4752 fModelViewChanged = true;
4753 break;
4754
4755 case SVGA3D_TRANSFORM_WORLD1:
4756 case SVGA3D_TRANSFORM_WORLD2:
4757 case SVGA3D_TRANSFORM_WORLD3:
4758 Log(("vmsvga3dSetTransform: unsupported SVGA3D_TRANSFORM_WORLDx transform!!\n"));
4759 return VERR_INVALID_PARAMETER;
4760
4761 default:
4762 Log(("vmsvga3dSetTransform: unknown type!!\n"));
4763 return VERR_INVALID_PARAMETER;
4764 }
4765
4766 /* Apparently we need to reset the light and clip data after modifying the modelview matrix. */
4767 if (fModelViewChanged)
4768 {
4769 /* Reprogram the clip planes. */
4770 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aClipPlane); j++)
4771 {
4772 if (pContext->state.aClipPlane[j].fValid == true)
4773 vmsvga3dSetClipPlane(pThis, cid, j, pContext->state.aClipPlane[j].plane);
4774 }
4775
4776 /* Reprogram the light data. */
4777 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aLightData); j++)
4778 {
4779 if (pContext->state.aLightData[j].fValidData == true)
4780 vmsvga3dSetLightData(pThis, cid, j, &pContext->state.aLightData[j].data);
4781 }
4782 }
4783
4784 return VINF_SUCCESS;
4785}
4786
4787int vmsvga3dSetZRange(PVGASTATE pThis, uint32_t cid, SVGA3dZRange zRange)
4788{
4789 PVMSVGA3DCONTEXT pContext;
4790 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4791 AssertReturn(pState, VERR_NO_MEMORY);
4792
4793 Log(("vmsvga3dSetZRange cid=%x min=%d max=%d\n", cid, (uint32_t)(zRange.min * 100.0), (uint32_t)(zRange.max * 100.0)));
4794
4795 if ( cid >= pState->cContexts
4796 || pState->papContexts[cid]->id != cid)
4797 {
4798 Log(("vmsvga3dSetZRange invalid context id!\n"));
4799 return VERR_INVALID_PARAMETER;
4800 }
4801 pContext = pState->papContexts[cid];
4802 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4803
4804 pContext->state.zRange = zRange;
4805 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_ZRANGE;
4806
4807 if (zRange.min < -1.0)
4808 zRange.min = -1.0;
4809 if (zRange.max > 1.0)
4810 zRange.max = 1.0;
4811
4812 glDepthRange((GLdouble)zRange.min, (GLdouble)zRange.max);
4813 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4814 return VINF_SUCCESS;
4815}
4816
4817/**
4818 * Convert SVGA blend op value to its OpenGL equivalent
4819 */
4820static GLenum vmsvga3dBlendOp2GL(uint32_t blendOp)
4821{
4822 switch (blendOp)
4823 {
4824 case SVGA3D_BLENDOP_ZERO:
4825 return GL_ZERO;
4826 case SVGA3D_BLENDOP_ONE:
4827 return GL_ONE;
4828 case SVGA3D_BLENDOP_SRCCOLOR:
4829 return GL_SRC_COLOR;
4830 case SVGA3D_BLENDOP_INVSRCCOLOR:
4831 return GL_ONE_MINUS_SRC_COLOR;
4832 case SVGA3D_BLENDOP_SRCALPHA:
4833 return GL_SRC_ALPHA;
4834 case SVGA3D_BLENDOP_INVSRCALPHA:
4835 return GL_ONE_MINUS_SRC_ALPHA;
4836 case SVGA3D_BLENDOP_DESTALPHA:
4837 return GL_DST_ALPHA;
4838 case SVGA3D_BLENDOP_INVDESTALPHA:
4839 return GL_ONE_MINUS_DST_ALPHA;
4840 case SVGA3D_BLENDOP_DESTCOLOR:
4841 return GL_DST_COLOR;
4842 case SVGA3D_BLENDOP_INVDESTCOLOR:
4843 return GL_ONE_MINUS_DST_COLOR;
4844 case SVGA3D_BLENDOP_SRCALPHASAT:
4845 return GL_SRC_ALPHA_SATURATE;
4846 case SVGA3D_BLENDOP_BLENDFACTOR:
4847 return GL_CONSTANT_ALPHA; /* @todo correct?? */
4848 case SVGA3D_BLENDOP_INVBLENDFACTOR:
4849 return GL_ONE_MINUS_CONSTANT_ALPHA; /* @todo correct?? */
4850 default:
4851 AssertFailed();
4852 return GL_ONE;
4853 }
4854}
4855
4856static GLenum vmsvga3dBlendEquation2GL(uint32_t blendEq)
4857{
4858 switch (blendEq)
4859 {
4860 case SVGA3D_BLENDEQ_ADD:
4861 return GL_FUNC_ADD;
4862 case SVGA3D_BLENDEQ_SUBTRACT:
4863 return GL_FUNC_SUBTRACT;
4864 case SVGA3D_BLENDEQ_REVSUBTRACT:
4865 return GL_FUNC_REVERSE_SUBTRACT;
4866 case SVGA3D_BLENDEQ_MINIMUM:
4867 return GL_MIN;
4868 case SVGA3D_BLENDEQ_MAXIMUM:
4869 return GL_MAX;
4870 default:
4871 AssertFailed();
4872 return GL_FUNC_ADD;
4873 }
4874}
4875
4876static GLenum vmsvgaCmpFunc2GL(uint32_t cmpFunc)
4877{
4878 switch (cmpFunc)
4879 {
4880 case SVGA3D_CMP_NEVER:
4881 return GL_NEVER;
4882 case SVGA3D_CMP_LESS:
4883 return GL_LESS;
4884 case SVGA3D_CMP_EQUAL:
4885 return GL_EQUAL;
4886 case SVGA3D_CMP_LESSEQUAL:
4887 return GL_LEQUAL;
4888 case SVGA3D_CMP_GREATER:
4889 return GL_GREATER;
4890 case SVGA3D_CMP_NOTEQUAL:
4891 return GL_NOTEQUAL;
4892 case SVGA3D_CMP_GREATEREQUAL:
4893 return GL_GEQUAL;
4894 case SVGA3D_CMP_ALWAYS:
4895 return GL_ALWAYS;
4896 default:
4897 AssertFailed();
4898 return GL_LESS;
4899 }
4900}
4901
4902static GLenum vmsvgaStencipOp2GL(uint32_t stencilOp)
4903{
4904 switch (stencilOp)
4905 {
4906 case SVGA3D_STENCILOP_KEEP:
4907 return GL_KEEP;
4908 case SVGA3D_STENCILOP_ZERO:
4909 return GL_ZERO;
4910 case SVGA3D_STENCILOP_REPLACE:
4911 return GL_REPLACE;
4912 case SVGA3D_STENCILOP_INCRSAT:
4913 return GL_INCR_WRAP;
4914 case SVGA3D_STENCILOP_DECRSAT:
4915 return GL_DECR_WRAP;
4916 case SVGA3D_STENCILOP_INVERT:
4917 return GL_INVERT;
4918 case SVGA3D_STENCILOP_INCR:
4919 return GL_INCR;
4920 case SVGA3D_STENCILOP_DECR:
4921 return GL_DECR;
4922 default:
4923 AssertFailed();
4924 return GL_KEEP;
4925 }
4926}
4927
4928int vmsvga3dSetRenderState(PVGASTATE pThis, uint32_t cid, uint32_t cRenderStates, SVGA3dRenderState *pRenderState)
4929{
4930 uint32_t val;
4931 PVMSVGA3DCONTEXT pContext;
4932 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4933 AssertReturn(pState, VERR_NO_MEMORY);
4934
4935 Log(("vmsvga3dSetRenderState cid=%x cRenderStates=%d\n", cid, cRenderStates));
4936
4937 if ( cid >= pState->cContexts
4938 || pState->papContexts[cid]->id != cid)
4939 {
4940 Log(("vmsvga3dSetRenderState invalid context id!\n"));
4941 return VERR_INVALID_PARAMETER;
4942 }
4943 pContext = pState->papContexts[cid];
4944 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4945
4946 for (unsigned i = 0; i < cRenderStates; i++)
4947 {
4948 GLenum enableCap = ~0U;
4949 Log(("vmsvga3dSetRenderState: cid=%x state=%s (%d) val=%x\n", cid, vmsvga3dGetRenderStateName(pRenderState[i].state), pRenderState[i].state, pRenderState[i].uintValue));
4950 /* Save the render state for vm state saving. */
4951 if (pRenderState[i].state < SVGA3D_RS_MAX)
4952 pContext->state.aRenderState[pRenderState[i].state] = pRenderState[i];
4953
4954 switch (pRenderState[i].state)
4955 {
4956 case SVGA3D_RS_ZENABLE: /* SVGA3dBool */
4957 enableCap = GL_DEPTH_TEST;
4958 val = pRenderState[i].uintValue;
4959 break;
4960
4961 case SVGA3D_RS_ZWRITEENABLE: /* SVGA3dBool */
4962 glDepthMask(!!pRenderState[i].uintValue);
4963 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4964 break;
4965
4966 case SVGA3D_RS_ALPHATESTENABLE: /* SVGA3dBool */
4967 enableCap = GL_ALPHA_TEST;
4968 val = pRenderState[i].uintValue;
4969 break;
4970
4971 case SVGA3D_RS_DITHERENABLE: /* SVGA3dBool */
4972 enableCap = GL_DITHER;
4973 val = pRenderState[i].uintValue;
4974 break;
4975
4976 case SVGA3D_RS_FOGENABLE: /* SVGA3dBool */
4977 enableCap = GL_FOG;
4978 val = pRenderState[i].uintValue;
4979 break;
4980
4981 case SVGA3D_RS_SPECULARENABLE: /* SVGA3dBool */
4982 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
4983 break;
4984
4985 case SVGA3D_RS_LIGHTINGENABLE: /* SVGA3dBool */
4986 enableCap = GL_LIGHTING;
4987 val = pRenderState[i].uintValue;
4988 break;
4989
4990 case SVGA3D_RS_NORMALIZENORMALS: /* SVGA3dBool */
4991 /* not applicable */
4992 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
4993 break;
4994
4995 case SVGA3D_RS_POINTSPRITEENABLE: /* SVGA3dBool */
4996 enableCap = GL_POINT_SPRITE_ARB;
4997 val = pRenderState[i].uintValue;
4998 break;
4999
5000 case SVGA3D_RS_POINTSIZE: /* float */
5001 /* @todo we need to apply scaling for point sizes below the min or above the max; see Wine) */
5002 if (pRenderState[i].floatValue < pState->caps.flPointSize[0])
5003 pRenderState[i].floatValue = pState->caps.flPointSize[0];
5004 if (pRenderState[i].floatValue > pState->caps.flPointSize[1])
5005 pRenderState[i].floatValue = pState->caps.flPointSize[1];
5006
5007 glPointSize(pRenderState[i].floatValue);
5008 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5009 Log(("SVGA3D_RS_POINTSIZE: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
5010 break;
5011
5012 case SVGA3D_RS_POINTSIZEMIN: /* float */
5013 pState->ext.glPointParameterf(GL_POINT_SIZE_MIN, pRenderState[i].floatValue);
5014 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5015 Log(("SVGA3D_RS_POINTSIZEMIN: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
5016 break;
5017
5018 case SVGA3D_RS_POINTSIZEMAX: /* float */
5019 pState->ext.glPointParameterf(GL_POINT_SIZE_MAX, pRenderState[i].floatValue);
5020 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5021 Log(("SVGA3D_RS_POINTSIZEMAX: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
5022 break;
5023
5024 case SVGA3D_RS_POINTSCALEENABLE: /* SVGA3dBool */
5025 case SVGA3D_RS_POINTSCALE_A: /* float */
5026 case SVGA3D_RS_POINTSCALE_B: /* float */
5027 case SVGA3D_RS_POINTSCALE_C: /* float */
5028 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
5029 break;
5030
5031 case SVGA3D_RS_AMBIENT: /* SVGA3dColor */
5032 {
5033 GLfloat color[4]; /* red, green, blue, alpha */
5034
5035 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &color[0], &color[1], &color[2], &color[3]);
5036
5037 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, color);
5038 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5039 break;
5040 }
5041
5042 case SVGA3D_RS_CLIPPLANEENABLE: /* SVGA3dClipPlanes */
5043 {
5044 AssertCompile(SVGA3D_CLIPPLANE_MAX == (1 << 5));
5045 for (uint32_t j = 0; j <= 5; j++)
5046 {
5047 if (pRenderState[i].uintValue & RT_BIT(j))
5048 glEnable(GL_CLIP_PLANE0 + j);
5049 else
5050 glDisable(GL_CLIP_PLANE0 + j);
5051 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5052 }
5053 break;
5054 }
5055
5056 case SVGA3D_RS_FOGCOLOR: /* SVGA3dColor */
5057 {
5058 GLfloat color[4]; /* red, green, blue, alpha */
5059
5060 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &color[0], &color[1], &color[2], &color[3]);
5061
5062 glFogfv(GL_FOG_COLOR, color);
5063 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5064 break;
5065 }
5066
5067 case SVGA3D_RS_FOGSTART: /* float */
5068 glFogf(GL_FOG_START, pRenderState[i].floatValue);
5069 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5070 break;
5071
5072 case SVGA3D_RS_FOGEND: /* float */
5073 glFogf(GL_FOG_END, pRenderState[i].floatValue);
5074 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5075 break;
5076
5077 case SVGA3D_RS_FOGDENSITY: /* float */
5078 glFogf(GL_FOG_DENSITY, pRenderState[i].floatValue);
5079 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5080 break;
5081
5082 case SVGA3D_RS_RANGEFOGENABLE: /* SVGA3dBool */
5083 glFogi(GL_FOG_COORD_SRC, (pRenderState[i].uintValue) ? GL_FOG_COORD : GL_FRAGMENT_DEPTH);
5084 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5085 break;
5086
5087 case SVGA3D_RS_FOGMODE: /* SVGA3dFogMode */
5088 {
5089 SVGA3dFogMode mode;
5090 mode.uintValue = pRenderState[i].uintValue;
5091
5092 enableCap = GL_FOG_MODE;
5093 switch (mode.s.function)
5094 {
5095 case SVGA3D_FOGFUNC_EXP:
5096 val = GL_EXP;
5097 break;
5098 case SVGA3D_FOGFUNC_EXP2:
5099 val = GL_EXP2;
5100 break;
5101 case SVGA3D_FOGFUNC_LINEAR:
5102 val = GL_LINEAR;
5103 break;
5104 default:
5105 AssertMsgFailedReturn(("Unexpected fog function %d\n", mode.s.function), VERR_INTERNAL_ERROR);
5106 break;
5107 }
5108
5109 /* @todo how to switch between vertex and pixel fog modes??? */
5110 Assert(mode.s.type == SVGA3D_FOGTYPE_PIXEL);
5111#if 0
5112 /* The fog type determines the render state. */
5113 switch (mode.s.type)
5114 {
5115 case SVGA3D_FOGTYPE_VERTEX:
5116 renderState = D3DRS_FOGVERTEXMODE;
5117 break;
5118 case SVGA3D_FOGTYPE_PIXEL:
5119 renderState = D3DRS_FOGTABLEMODE;
5120 break;
5121 default:
5122 AssertMsgFailedReturn(("Unexpected fog type %d\n", mode.s.type), VERR_INTERNAL_ERROR);
5123 break;
5124 }
5125#endif
5126
5127 /* Set the fog base to depth or range. */
5128 switch (mode.s.base)
5129 {
5130 case SVGA3D_FOGBASE_DEPTHBASED:
5131 glFogi(GL_FOG_COORD_SRC, GL_FRAGMENT_DEPTH);
5132 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5133 break;
5134 case SVGA3D_FOGBASE_RANGEBASED:
5135 glFogi(GL_FOG_COORD_SRC, GL_FOG_COORD);
5136 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5137 break;
5138 default:
5139 /* ignore */
5140 AssertMsgFailed(("Unexpected fog base %d\n", mode.s.base));
5141 break;
5142 }
5143 break;
5144 }
5145
5146 case SVGA3D_RS_FILLMODE: /* SVGA3dFillMode */
5147 {
5148 SVGA3dFillMode mode;
5149
5150 mode.uintValue = pRenderState[i].uintValue;
5151
5152 switch (mode.s.mode)
5153 {
5154 case SVGA3D_FILLMODE_POINT:
5155 val = GL_POINT;
5156 break;
5157 case SVGA3D_FILLMODE_LINE:
5158 val = GL_LINE;
5159 break;
5160 case SVGA3D_FILLMODE_FILL:
5161 val = GL_FILL;
5162 break;
5163 default:
5164 AssertMsgFailedReturn(("Unexpected fill mode %d\n", mode.s.mode), VERR_INTERNAL_ERROR);
5165 break;
5166 }
5167 /* @note only front and back faces */
5168 Assert(mode.s.face == SVGA3D_FACE_FRONT_BACK);
5169 glPolygonMode(GL_FRONT_AND_BACK, val);
5170 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5171 break;
5172 }
5173
5174 case SVGA3D_RS_SHADEMODE: /* SVGA3dShadeMode */
5175 switch (pRenderState[i].uintValue)
5176 {
5177 case SVGA3D_SHADEMODE_FLAT:
5178 val = GL_FLAT;
5179 break;
5180
5181 case SVGA3D_SHADEMODE_SMOOTH:
5182 val = GL_SMOOTH;
5183 break;
5184
5185 default:
5186 AssertMsgFailedReturn(("Unexpected shade mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
5187 break;
5188 }
5189
5190 glShadeModel(val);
5191 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5192 break;
5193
5194 case SVGA3D_RS_LINEPATTERN: /* SVGA3dLinePattern */
5195 /* No longer supported by d3d; mesagl comments suggest not all backends support it */
5196 /* @todo */
5197 Log(("WARNING: SVGA3D_RS_LINEPATTERN %x not supported!!\n", pRenderState[i].uintValue));
5198 /*
5199 renderState = D3DRS_LINEPATTERN;
5200 val = pRenderState[i].uintValue;
5201 */
5202 break;
5203
5204 case SVGA3D_RS_LINEAA: /* SVGA3dBool */
5205 enableCap = GL_LINE_SMOOTH;
5206 val = pRenderState[i].uintValue;
5207 break;
5208
5209 case SVGA3D_RS_LINEWIDTH: /* float */
5210 glLineWidth(pRenderState[i].floatValue);
5211 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5212 break;
5213
5214 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
5215 {
5216 /* Refresh the blending state based on the new enable setting. */
5217 SVGA3dRenderState renderstate[2];
5218
5219 renderstate[0].state = SVGA3D_RS_SRCBLEND;
5220 renderstate[0].uintValue = pContext->state.aRenderState[SVGA3D_RS_SRCBLEND].uintValue;
5221 renderstate[1].state = SVGA3D_RS_BLENDEQUATION;
5222 renderstate[1].uintValue = pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATION].uintValue;
5223
5224 int rc = vmsvga3dSetRenderState(pThis, cid, 2, renderstate);
5225 AssertRCReturn(rc, rc);
5226
5227 if (pContext->state.aRenderState[SVGA3D_RS_BLENDENABLE].uintValue != 0)
5228 continue; /* ignore if blend is already enabled */
5229 /* no break */
5230 }
5231
5232 case SVGA3D_RS_BLENDENABLE: /* SVGA3dBool */
5233 enableCap = GL_BLEND;
5234 val = pRenderState[i].uintValue;
5235 break;
5236
5237 case SVGA3D_RS_SRCBLENDALPHA: /* SVGA3dBlendOp */
5238 case SVGA3D_RS_DSTBLENDALPHA: /* SVGA3dBlendOp */
5239 case SVGA3D_RS_SRCBLEND: /* SVGA3dBlendOp */
5240 case SVGA3D_RS_DSTBLEND: /* SVGA3dBlendOp */
5241 {
5242 GLint srcRGB, srcAlpha, dstRGB, dstAlpha;
5243 GLint blendop = vmsvga3dBlendOp2GL(pRenderState[i].uintValue);
5244
5245 glGetIntegerv(GL_BLEND_SRC_RGB, &srcRGB);
5246 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5247 glGetIntegerv(GL_BLEND_DST_RGB, &dstRGB);
5248 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5249 glGetIntegerv(GL_BLEND_DST_ALPHA, &dstAlpha);
5250 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5251 glGetIntegerv(GL_BLEND_SRC_ALPHA, &srcAlpha);
5252 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5253
5254 switch (pRenderState[i].state)
5255 {
5256 case SVGA3D_RS_SRCBLEND:
5257 srcRGB = blendop;
5258 break;
5259 case SVGA3D_RS_DSTBLEND:
5260 dstRGB = blendop;
5261 break;
5262 case SVGA3D_RS_SRCBLENDALPHA:
5263 srcAlpha = blendop;
5264 break;
5265 case SVGA3D_RS_DSTBLENDALPHA:
5266 dstAlpha = blendop;
5267 break;
5268 default:
5269 /* not possible; shut up gcc */
5270 AssertFailed();
5271 break;
5272 }
5273
5274 if (pContext->state.aRenderState[SVGA3D_RS_SEPARATEALPHABLENDENABLE].uintValue != 0)
5275 pState->ext.glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
5276 else
5277 glBlendFunc(srcRGB, dstRGB);
5278 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5279 break;
5280 }
5281
5282 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation */
5283 case SVGA3D_RS_BLENDEQUATION: /* SVGA3dBlendEquation */
5284 if (pContext->state.aRenderState[SVGA3D_RS_SEPARATEALPHABLENDENABLE].uintValue != 0)
5285 pState->ext.glBlendEquationSeparate(vmsvga3dBlendEquation2GL(pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATION].uintValue),
5286 vmsvga3dBlendEquation2GL(pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATIONALPHA].uintValue));
5287 else
5288 {
5289#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x102
5290 glBlendEquation(vmsvga3dBlendEquation2GL(pRenderState[i].uintValue));
5291#else
5292 pState->ext.glBlendEquation(vmsvga3dBlendEquation2GL(pRenderState[i].uintValue));
5293#endif
5294 }
5295 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5296 break;
5297
5298 case SVGA3D_RS_BLENDCOLOR: /* SVGA3dColor */
5299 {
5300 GLfloat red, green, blue, alpha;
5301
5302 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &red, &green, &blue, &alpha);
5303
5304#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x102
5305 glBlendColor(red, green, blue, alpha);
5306#else
5307 pState->ext.glBlendColor(red, green, blue, alpha);
5308#endif
5309 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5310 break;
5311 }
5312
5313 case SVGA3D_RS_CULLMODE: /* SVGA3dFace */
5314 {
5315 GLenum mode = GL_BACK; /* default for OpenGL */
5316
5317 switch (pRenderState[i].uintValue)
5318 {
5319 case SVGA3D_FACE_NONE:
5320 break;
5321 case SVGA3D_FACE_FRONT:
5322 mode = GL_FRONT;
5323 break;
5324 case SVGA3D_FACE_BACK:
5325 mode = GL_BACK;
5326 break;
5327 case SVGA3D_FACE_FRONT_BACK:
5328 mode = GL_FRONT_AND_BACK;
5329 break;
5330 default:
5331 AssertMsgFailedReturn(("Unexpected cull mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
5332 break;
5333 }
5334 enableCap = GL_CULL_FACE;
5335 if (pRenderState[i].uintValue != SVGA3D_FACE_NONE)
5336 {
5337 glCullFace(mode);
5338 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5339 val = 1;
5340 }
5341 else
5342 val = 0;
5343 break;
5344 }
5345
5346 case SVGA3D_RS_ZFUNC: /* SVGA3dCmpFunc */
5347 glDepthFunc(vmsvgaCmpFunc2GL(pRenderState[i].uintValue));
5348 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5349 break;
5350
5351 case SVGA3D_RS_ALPHAFUNC: /* SVGA3dCmpFunc */
5352 {
5353 GLclampf ref;
5354
5355 glGetFloatv(GL_ALPHA_TEST_REF, &ref);
5356 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5357 glAlphaFunc(vmsvgaCmpFunc2GL(pRenderState[i].uintValue), ref);
5358 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5359 break;
5360 }
5361
5362 case SVGA3D_RS_ALPHAREF: /* float (0.0 .. 1.0) */
5363 {
5364 GLint func;
5365
5366 glGetIntegerv(GL_ALPHA_TEST_FUNC, &func);
5367 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5368 glAlphaFunc(func, pRenderState[i].floatValue);
5369 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5370 break;
5371 }
5372
5373 case SVGA3D_RS_STENCILENABLE: /* SVGA3dBool */
5374 enableCap = GL_STENCIL_TEST;
5375 val = pRenderState[i].uintValue;
5376 break;
5377
5378 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc */
5379 case SVGA3D_RS_STENCILREF: /* uint32_t */
5380 case SVGA3D_RS_STENCILMASK: /* uint32_t */
5381 {
5382 GLint func, ref;
5383 GLuint mask;
5384
5385 glGetIntegerv(GL_STENCIL_FUNC, &func);
5386 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5387 glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)&mask);
5388 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5389 glGetIntegerv(GL_STENCIL_REF, &ref);
5390 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5391
5392 switch (pRenderState[i].state)
5393 {
5394 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc */
5395 func = vmsvgaCmpFunc2GL(pRenderState[i].uintValue);
5396 break;
5397
5398 case SVGA3D_RS_STENCILREF: /* uint32_t */
5399 ref = pRenderState[i].uintValue;
5400 break;
5401
5402 case SVGA3D_RS_STENCILMASK: /* uint32_t */
5403 mask = pRenderState[i].uintValue;
5404 break;
5405
5406 default:
5407 /* not possible; shut up gcc */
5408 AssertFailed();
5409 break;
5410 }
5411
5412 glStencilFunc(func, ref, mask);
5413 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5414 break;
5415 }
5416
5417 case SVGA3D_RS_STENCILWRITEMASK: /* uint32_t */
5418 glStencilMask(pRenderState[i].uintValue);
5419 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5420 break;
5421
5422 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp */
5423 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp */
5424 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp */
5425 {
5426 GLint sfail, dpfail, dppass;
5427 GLenum stencilop = vmsvgaStencipOp2GL(pRenderState[i].uintValue);
5428
5429 glGetIntegerv(GL_STENCIL_FAIL, &sfail);
5430 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5431 glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, &dpfail);
5432 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5433 glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, &dppass);
5434 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5435
5436 switch (pRenderState[i].state)
5437 {
5438 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp */
5439 sfail = stencilop;
5440 break;
5441 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp */
5442 dpfail = stencilop;
5443 break;
5444 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp */
5445 dppass = stencilop;
5446 break;
5447 default:
5448 /* not possible; shut up gcc */
5449 AssertFailed();
5450 break;
5451 }
5452 glStencilOp(sfail, dpfail, dppass);
5453 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5454 break;
5455 }
5456
5457 case SVGA3D_RS_STENCILENABLE2SIDED: /* SVGA3dBool */
5458 /* @note GL_EXT_stencil_two_side required! */
5459 if (pState->ext.fEXT_stencil_two_side)
5460 {
5461 enableCap = GL_STENCIL_TEST_TWO_SIDE_EXT;
5462 val = pRenderState[i].uintValue;
5463 }
5464 else
5465 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_STENCILENABLE2SIDED\n"));
5466 break;
5467
5468 case SVGA3D_RS_CCWSTENCILFUNC: /* SVGA3dCmpFunc */
5469 {
5470 /* @todo SVGA3D_RS_STENCILFAIL/ZFAIL/PASS for front & back faces
5471 * SVGA3D_RS_CCWSTENCILFAIL/ZFAIL/PASS for back faces ??
5472 */
5473 GLint ref;
5474 GLuint mask;
5475
5476 glGetIntegerv(GL_STENCIL_BACK_VALUE_MASK, (GLint *)&mask);
5477 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5478 glGetIntegerv(GL_STENCIL_BACK_REF, &ref);
5479 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5480
5481 pState->ext.glStencilFuncSeparate(GL_BACK, vmsvgaCmpFunc2GL(pRenderState[i].uintValue), ref, mask);
5482 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5483 break;
5484 }
5485
5486 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp */
5487 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp */
5488 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp */
5489 {
5490 /* @todo SVGA3D_RS_STENCILFAIL/ZFAIL/PASS for front & back faces
5491 * SVGA3D_RS_CCWSTENCILFAIL/ZFAIL/PASS for back faces ??
5492 */
5493 GLint sfail, dpfail, dppass;
5494 GLenum stencilop = vmsvgaStencipOp2GL(pRenderState[i].uintValue);
5495
5496 glGetIntegerv(GL_STENCIL_BACK_FAIL, &sfail);
5497 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5498 glGetIntegerv(GL_STENCIL_BACK_PASS_DEPTH_FAIL, &dpfail);
5499 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5500 glGetIntegerv(GL_STENCIL_BACK_PASS_DEPTH_PASS, &dppass);
5501 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5502
5503 switch (pRenderState[i].state)
5504 {
5505 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp */
5506 sfail = stencilop;
5507 break;
5508 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp */
5509 dpfail = stencilop;
5510 break;
5511 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp */
5512 dppass = stencilop;
5513 break;
5514 default:
5515 /* not possible; shut up gcc */
5516 AssertFailed();
5517 break;
5518 }
5519 pState->ext.glStencilOpSeparate(GL_BACK, sfail, dpfail, dppass);
5520 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5521 break;
5522 }
5523
5524 case SVGA3D_RS_ZBIAS: /* float */
5525 /* @todo unknown meaning; depth bias is not identical
5526 renderState = D3DRS_DEPTHBIAS;
5527 val = pRenderState[i].uintValue;
5528 */
5529 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_ZBIAS\n"));
5530 break;
5531
5532 case SVGA3D_RS_DEPTHBIAS: /* float */
5533 {
5534 GLfloat factor;
5535
5536 /* @todo not sure if the d3d & ogl definitions are identical. */
5537
5538 /* Do not change the factor part. */
5539 glGetFloatv(GL_POLYGON_OFFSET_FACTOR, &factor);
5540 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5541
5542 glPolygonOffset(factor, pRenderState[i].floatValue);
5543 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5544 break;
5545 }
5546
5547 case SVGA3D_RS_SLOPESCALEDEPTHBIAS: /* float */
5548 {
5549 GLfloat units;
5550
5551 /* @todo not sure if the d3d & ogl definitions are identical. */
5552
5553 /* Do not change the factor part. */
5554 glGetFloatv(GL_POLYGON_OFFSET_UNITS, &units);
5555 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5556
5557 glPolygonOffset(pRenderState[i].floatValue, units);
5558 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5559 break;
5560 }
5561
5562 case SVGA3D_RS_COLORWRITEENABLE: /* SVGA3dColorMask */
5563 {
5564 GLboolean red, green, blue, alpha;
5565 SVGA3dColorMask mask;
5566
5567 mask.uintValue = pRenderState[i].uintValue;
5568
5569 red = mask.s.red;
5570 green = mask.s.green;
5571 blue = mask.s.blue;
5572 alpha = mask.s.alpha;
5573
5574 glColorMask(red, green, blue, alpha);
5575 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5576 break;
5577 }
5578
5579 case SVGA3D_RS_COLORWRITEENABLE1: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5580 case SVGA3D_RS_COLORWRITEENABLE2: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5581 case SVGA3D_RS_COLORWRITEENABLE3: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5582 Log(("vmsvga3dSetRenderState: WARNING SVGA3D_RS_COLORWRITEENABLEx not supported!!\n"));
5583 break;
5584
5585 case SVGA3D_RS_SCISSORTESTENABLE: /* SVGA3dBool */
5586 enableCap = GL_SCISSOR_TEST;
5587 val = pRenderState[i].uintValue;
5588 break;
5589
5590#if 0
5591 case SVGA3D_RS_DIFFUSEMATERIALSOURCE: /* SVGA3dVertexMaterial */
5592 AssertCompile(D3DMCS_COLOR2 == SVGA3D_VERTEXMATERIAL_SPECULAR);
5593 renderState = D3DRS_DIFFUSEMATERIALSOURCE;
5594 val = pRenderState[i].uintValue;
5595 break;
5596
5597 case SVGA3D_RS_SPECULARMATERIALSOURCE: /* SVGA3dVertexMaterial */
5598 renderState = D3DRS_SPECULARMATERIALSOURCE;
5599 val = pRenderState[i].uintValue;
5600 break;
5601
5602 case SVGA3D_RS_AMBIENTMATERIALSOURCE: /* SVGA3dVertexMaterial */
5603 renderState = D3DRS_AMBIENTMATERIALSOURCE;
5604 val = pRenderState[i].uintValue;
5605 break;
5606
5607 case SVGA3D_RS_EMISSIVEMATERIALSOURCE: /* SVGA3dVertexMaterial */
5608 renderState = D3DRS_EMISSIVEMATERIALSOURCE;
5609 val = pRenderState[i].uintValue;
5610 break;
5611#endif
5612
5613 case SVGA3D_RS_WRAP3: /* SVGA3dWrapFlags */
5614 case SVGA3D_RS_WRAP4: /* SVGA3dWrapFlags */
5615 case SVGA3D_RS_WRAP5: /* SVGA3dWrapFlags */
5616 case SVGA3D_RS_WRAP6: /* SVGA3dWrapFlags */
5617 case SVGA3D_RS_WRAP7: /* SVGA3dWrapFlags */
5618 case SVGA3D_RS_WRAP8: /* SVGA3dWrapFlags */
5619 case SVGA3D_RS_WRAP9: /* SVGA3dWrapFlags */
5620 case SVGA3D_RS_WRAP10: /* SVGA3dWrapFlags */
5621 case SVGA3D_RS_WRAP11: /* SVGA3dWrapFlags */
5622 case SVGA3D_RS_WRAP12: /* SVGA3dWrapFlags */
5623 case SVGA3D_RS_WRAP13: /* SVGA3dWrapFlags */
5624 case SVGA3D_RS_WRAP14: /* SVGA3dWrapFlags */
5625 case SVGA3D_RS_WRAP15: /* SVGA3dWrapFlags */
5626 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_WRAPx (x >= 3)\n"));
5627 break;
5628
5629 case SVGA3D_RS_LASTPIXEL: /* SVGA3dBool */
5630 case SVGA3D_RS_TWEENFACTOR: /* float */
5631 case SVGA3D_RS_INDEXEDVERTEXBLENDENABLE: /* SVGA3dBool */
5632 case SVGA3D_RS_VERTEXBLEND: /* SVGA3dVertexBlendFlags */
5633 Log(("vmsvga3dSetRenderState: WARNING not applicable!!\n"));
5634 break;
5635
5636 case SVGA3D_RS_MULTISAMPLEANTIALIAS: /* SVGA3dBool */
5637 enableCap = GL_MULTISAMPLE;
5638 val = pRenderState[i].uintValue;
5639 break;
5640
5641 case SVGA3D_RS_MULTISAMPLEMASK: /* uint32_t */
5642 case SVGA3D_RS_ANTIALIASEDLINEENABLE: /* SVGA3dBool */
5643 Log(("vmsvga3dSetRenderState: WARNING not applicable??!!\n"));
5644 break;
5645
5646 case SVGA3D_RS_COORDINATETYPE: /* SVGA3dCoordinateType */
5647 Assert(pRenderState[i].uintValue == SVGA3D_COORDINATE_LEFTHANDED);
5648 /* @todo setup a view matrix to scale the world space by -1 in the z-direction for right handed coordinates. */
5649 /*
5650 renderState = D3DRS_COORDINATETYPE;
5651 val = pRenderState[i].uintValue;
5652 */
5653 break;
5654
5655 case SVGA3D_RS_FRONTWINDING: /* SVGA3dFrontWinding */
5656 Assert(pRenderState[i].uintValue == SVGA3D_FRONTWINDING_CW);
5657 /* Invert the selected mode because of y-inversion (?) */
5658 glFrontFace((pRenderState[i].uintValue != SVGA3D_FRONTWINDING_CW) ? GL_CW : GL_CCW);
5659 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5660 break;
5661
5662 case SVGA3D_RS_OUTPUTGAMMA: /* float */
5663 //AssertFailed();
5664 /*
5665 D3DRS_SRGBWRITEENABLE ??
5666 renderState = D3DRS_OUTPUTGAMMA;
5667 val = pRenderState[i].uintValue;
5668 */
5669 break;
5670
5671#if 0
5672
5673 case SVGA3D_RS_VERTEXMATERIALENABLE: /* SVGA3dBool */
5674 //AssertFailed();
5675 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE; /* correct?? */
5676 val = pRenderState[i].uintValue;
5677 break;
5678
5679 case SVGA3D_RS_TEXTUREFACTOR: /* SVGA3dColor */
5680 renderState = D3DRS_TEXTUREFACTOR;
5681 val = pRenderState[i].uintValue;
5682 break;
5683
5684 case SVGA3D_RS_LOCALVIEWER: /* SVGA3dBool */
5685 renderState = D3DRS_LOCALVIEWER;
5686 val = pRenderState[i].uintValue;
5687 break;
5688
5689 case SVGA3D_RS_ZVISIBLE: /* SVGA3dBool */
5690 AssertFailed();
5691 /*
5692 renderState = D3DRS_ZVISIBLE;
5693 val = pRenderState[i].uintValue;
5694 */
5695 break;
5696
5697 case SVGA3D_RS_CLIPPING: /* SVGA3dBool */
5698 renderState = D3DRS_CLIPPING;
5699 val = pRenderState[i].uintValue;
5700 break;
5701
5702 case SVGA3D_RS_WRAP0: /* SVGA3dWrapFlags */
5703 glTexParameter GL_TEXTURE_WRAP_S
5704 Assert(SVGA3D_WRAPCOORD_3 == D3DWRAPCOORD_3);
5705 renderState = D3DRS_WRAP0;
5706 val = pRenderState[i].uintValue;
5707 break;
5708
5709 case SVGA3D_RS_WRAP1: /* SVGA3dWrapFlags */
5710 glTexParameter GL_TEXTURE_WRAP_T
5711 renderState = D3DRS_WRAP1;
5712 val = pRenderState[i].uintValue;
5713 break;
5714
5715 case SVGA3D_RS_WRAP2: /* SVGA3dWrapFlags */
5716 glTexParameter GL_TEXTURE_WRAP_R
5717 renderState = D3DRS_WRAP2;
5718 val = pRenderState[i].uintValue;
5719 break;
5720
5721
5722 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
5723 renderState = D3DRS_SEPARATEALPHABLENDENABLE;
5724 val = pRenderState[i].uintValue;
5725 break;
5726
5727
5728 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation */
5729 renderState = D3DRS_BLENDOPALPHA;
5730 val = pRenderState[i].uintValue;
5731 break;
5732
5733 case SVGA3D_RS_TRANSPARENCYANTIALIAS: /* SVGA3dTransparencyAntialiasType */
5734 AssertFailed();
5735 /*
5736 renderState = D3DRS_TRANSPARENCYANTIALIAS;
5737 val = pRenderState[i].uintValue;
5738 */
5739 break;
5740
5741#endif
5742 default:
5743 AssertFailed();
5744 break;
5745 }
5746
5747 if (enableCap != ~0U)
5748 {
5749 if (val)
5750 glEnable(enableCap);
5751 else
5752 glDisable(enableCap);
5753 }
5754 }
5755
5756 return VINF_SUCCESS;
5757}
5758
5759int vmsvga3dSetRenderTarget(PVGASTATE pThis, uint32_t cid, SVGA3dRenderTargetType type, SVGA3dSurfaceImageId target)
5760{
5761 PVMSVGA3DCONTEXT pContext;
5762 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
5763 PVMSVGA3DSURFACE pRenderTarget;
5764
5765 AssertReturn(pState, VERR_NO_MEMORY);
5766 AssertReturn(type < SVGA3D_RT_MAX, VERR_INVALID_PARAMETER);
5767 AssertReturn(target.face == 0, VERR_INVALID_PARAMETER);
5768 AssertReturn(target.mipmap == 0, VERR_INVALID_PARAMETER);
5769
5770 Log(("vmsvga3dSetRenderTarget cid=%x type=%x surface id=%x\n", cid, type, target.sid));
5771
5772 if ( cid >= pState->cContexts
5773 || pState->papContexts[cid]->id != cid)
5774 {
5775 Log(("vmsvga3dSetRenderTarget invalid context id!\n"));
5776 return VERR_INVALID_PARAMETER;
5777 }
5778 pContext = pState->papContexts[cid];
5779 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5780
5781 /* Save for vm state save/restore. */
5782 pContext->state.aRenderTargets[type] = target.sid;
5783
5784 if (target.sid == SVGA3D_INVALID_ID)
5785 {
5786 /* Disable render target. */
5787 switch (type)
5788 {
5789 case SVGA3D_RT_DEPTH:
5790 case SVGA3D_RT_STENCIL:
5791 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER, (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
5792 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5793 break;
5794
5795 case SVGA3D_RT_COLOR0:
5796 case SVGA3D_RT_COLOR1:
5797 case SVGA3D_RT_COLOR2:
5798 case SVGA3D_RT_COLOR3:
5799 case SVGA3D_RT_COLOR4:
5800 case SVGA3D_RT_COLOR5:
5801 case SVGA3D_RT_COLOR6:
5802 case SVGA3D_RT_COLOR7:
5803 pContext->sidRenderTarget = SVGA3D_INVALID_ID;
5804 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0, 0, 0, 0);
5805 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5806 break;
5807
5808 default:
5809 AssertFailedReturn(VERR_INVALID_PARAMETER);
5810 }
5811 return VINF_SUCCESS;
5812 }
5813
5814 AssertReturn(target.sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
5815 AssertReturn(target.sid < pState->cSurfaces && pState->paSurface[target.sid].id == target.sid, VERR_INVALID_PARAMETER);
5816 pRenderTarget = &pState->paSurface[target.sid];
5817
5818 switch (type)
5819 {
5820 case SVGA3D_RT_DEPTH:
5821 case SVGA3D_RT_STENCIL:
5822 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
5823 {
5824 Log(("vmsvga3dSetRenderTarget: create renderbuffer to be used as render target; surface id=%x type=%d format=%d\n", target.sid, pRenderTarget->flags, pRenderTarget->internalFormatGL));
5825#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
5826 pContext = &pState->SharedCtx;
5827 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5828#endif
5829 pState->ext.glGenRenderbuffers(1, &pRenderTarget->oglId.renderbuffer);
5830 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5831
5832 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5833 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5834
5835 pState->ext.glRenderbufferStorage(GL_RENDERBUFFER,
5836 pRenderTarget->internalFormatGL,
5837 pRenderTarget->pMipmapLevels[0].size.width,
5838 pRenderTarget->pMipmapLevels[0].size.height);
5839 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5840
5841#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
5842 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, OPENGL_INVALID_ID);
5843 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5844
5845 pContext = pState->papContexts[cid];
5846 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5847#else
5848 LogFlow(("vmsvga3dSetRenderTarget: sid=%x idAssociatedContext %#x -> %#x\n", pRenderTarget->id, pRenderTarget->idAssociatedContext, cid));
5849 pRenderTarget->idAssociatedContext = cid;
5850#endif
5851 }
5852#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
5853 else
5854#endif
5855 {
5856 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5857 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5858 }
5859#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
5860 Assert(pRenderTarget->idAssociatedContext == cid);
5861#endif
5862 Assert(!pRenderTarget->fDirty);
5863 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
5864
5865 pRenderTarget->flags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
5866
5867 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
5868 (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT,
5869 GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5870 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5871 break;
5872
5873 case SVGA3D_RT_COLOR0:
5874 case SVGA3D_RT_COLOR1:
5875 case SVGA3D_RT_COLOR2:
5876 case SVGA3D_RT_COLOR3:
5877 case SVGA3D_RT_COLOR4:
5878 case SVGA3D_RT_COLOR5:
5879 case SVGA3D_RT_COLOR6:
5880 case SVGA3D_RT_COLOR7:
5881 {
5882 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
5883 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
5884 {
5885 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));
5886 int rc = vmsvga3dCreateTexture(pState, pContext, cid, pRenderTarget);
5887 AssertRCReturn(rc, rc);
5888 }
5889
5890 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
5891 Assert(!pRenderTarget->fDirty);
5892
5893 pRenderTarget->flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
5894
5895 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0, GL_TEXTURE_2D, pRenderTarget->oglId.texture, target.mipmap);
5896 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5897
5898 pContext->sidRenderTarget = target.sid;
5899
5900#ifdef DEBUG
5901 GLenum status = pState->ext.glCheckFramebufferStatus(GL_FRAMEBUFFER);
5902 if (status != GL_FRAMEBUFFER_COMPLETE)
5903 Log(("vmsvga3dSetRenderTarget: WARNING: glCheckFramebufferStatus returned %x\n", status));
5904#endif
5905 /* @todo use glDrawBuffers too? */
5906 break;
5907 }
5908
5909 default:
5910 AssertFailedReturn(VERR_INVALID_PARAMETER);
5911 }
5912
5913 return VINF_SUCCESS;
5914}
5915
5916#if 0
5917/**
5918 * Convert SVGA texture combiner value to its D3D equivalent
5919 */
5920static DWORD vmsvga3dTextureCombiner2D3D(uint32_t value)
5921{
5922 switch (value)
5923 {
5924 case SVGA3D_TC_DISABLE:
5925 return D3DTOP_DISABLE;
5926 case SVGA3D_TC_SELECTARG1:
5927 return D3DTOP_SELECTARG1;
5928 case SVGA3D_TC_SELECTARG2:
5929 return D3DTOP_SELECTARG2;
5930 case SVGA3D_TC_MODULATE:
5931 return D3DTOP_MODULATE;
5932 case SVGA3D_TC_ADD:
5933 return D3DTOP_ADD;
5934 case SVGA3D_TC_ADDSIGNED:
5935 return D3DTOP_ADDSIGNED;
5936 case SVGA3D_TC_SUBTRACT:
5937 return D3DTOP_SUBTRACT;
5938 case SVGA3D_TC_BLENDTEXTUREALPHA:
5939 return D3DTOP_BLENDTEXTUREALPHA;
5940 case SVGA3D_TC_BLENDDIFFUSEALPHA:
5941 return D3DTOP_BLENDDIFFUSEALPHA;
5942 case SVGA3D_TC_BLENDCURRENTALPHA:
5943 return D3DTOP_BLENDCURRENTALPHA;
5944 case SVGA3D_TC_BLENDFACTORALPHA:
5945 return D3DTOP_BLENDFACTORALPHA;
5946 case SVGA3D_TC_MODULATE2X:
5947 return D3DTOP_MODULATE2X;
5948 case SVGA3D_TC_MODULATE4X:
5949 return D3DTOP_MODULATE4X;
5950 case SVGA3D_TC_DSDT:
5951 AssertFailed(); /* @todo ??? */
5952 return D3DTOP_DISABLE;
5953 case SVGA3D_TC_DOTPRODUCT3:
5954 return D3DTOP_DOTPRODUCT3;
5955 case SVGA3D_TC_BLENDTEXTUREALPHAPM:
5956 return D3DTOP_BLENDTEXTUREALPHAPM;
5957 case SVGA3D_TC_ADDSIGNED2X:
5958 return D3DTOP_ADDSIGNED2X;
5959 case SVGA3D_TC_ADDSMOOTH:
5960 return D3DTOP_ADDSMOOTH;
5961 case SVGA3D_TC_PREMODULATE:
5962 return D3DTOP_PREMODULATE;
5963 case SVGA3D_TC_MODULATEALPHA_ADDCOLOR:
5964 return D3DTOP_MODULATEALPHA_ADDCOLOR;
5965 case SVGA3D_TC_MODULATECOLOR_ADDALPHA:
5966 return D3DTOP_MODULATECOLOR_ADDALPHA;
5967 case SVGA3D_TC_MODULATEINVALPHA_ADDCOLOR:
5968 return D3DTOP_MODULATEINVALPHA_ADDCOLOR;
5969 case SVGA3D_TC_MODULATEINVCOLOR_ADDALPHA:
5970 return D3DTOP_MODULATEINVCOLOR_ADDALPHA;
5971 case SVGA3D_TC_BUMPENVMAPLUMINANCE:
5972 return D3DTOP_BUMPENVMAPLUMINANCE;
5973 case SVGA3D_TC_MULTIPLYADD:
5974 return D3DTOP_MULTIPLYADD;
5975 case SVGA3D_TC_LERP:
5976 return D3DTOP_LERP;
5977 default:
5978 AssertFailed();
5979 return D3DTOP_DISABLE;
5980 }
5981}
5982
5983/**
5984 * Convert SVGA texture arg data value to its D3D equivalent
5985 */
5986static DWORD vmsvga3dTextureArgData2D3D(uint32_t value)
5987{
5988 switch (value)
5989 {
5990 case SVGA3D_TA_CONSTANT:
5991 return D3DTA_CONSTANT;
5992 case SVGA3D_TA_PREVIOUS:
5993 return D3DTA_CURRENT; /* current = previous */
5994 case SVGA3D_TA_DIFFUSE:
5995 return D3DTA_DIFFUSE;
5996 case SVGA3D_TA_TEXTURE:
5997 return D3DTA_TEXTURE;
5998 case SVGA3D_TA_SPECULAR:
5999 return D3DTA_SPECULAR;
6000 default:
6001 AssertFailed();
6002 return 0;
6003 }
6004}
6005
6006/**
6007 * Convert SVGA texture transform flag value to its D3D equivalent
6008 */
6009static DWORD vmsvga3dTextTransformFlags2D3D(uint32_t value)
6010{
6011 switch (value)
6012 {
6013 case SVGA3D_TEX_TRANSFORM_OFF:
6014 return D3DTTFF_DISABLE;
6015 case SVGA3D_TEX_TRANSFORM_S:
6016 return D3DTTFF_COUNT1; /* @todo correct? */
6017 case SVGA3D_TEX_TRANSFORM_T:
6018 return D3DTTFF_COUNT2; /* @todo correct? */
6019 case SVGA3D_TEX_TRANSFORM_R:
6020 return D3DTTFF_COUNT3; /* @todo correct? */
6021 case SVGA3D_TEX_TRANSFORM_Q:
6022 return D3DTTFF_COUNT4; /* @todo correct? */
6023 case SVGA3D_TEX_PROJECTED:
6024 return D3DTTFF_PROJECTED;
6025 default:
6026 AssertFailed();
6027 return 0;
6028 }
6029}
6030#endif
6031
6032static GLenum vmsvga3dTextureAddress2OGL(SVGA3dTextureAddress value)
6033{
6034 switch (value)
6035 {
6036 case SVGA3D_TEX_ADDRESS_WRAP:
6037 return GL_REPEAT;
6038 case SVGA3D_TEX_ADDRESS_MIRROR:
6039 return GL_MIRRORED_REPEAT;
6040 case SVGA3D_TEX_ADDRESS_CLAMP:
6041 return GL_CLAMP_TO_EDGE;
6042 case SVGA3D_TEX_ADDRESS_BORDER:
6043 return GL_CLAMP_TO_BORDER;
6044 case SVGA3D_TEX_ADDRESS_MIRRORONCE:
6045 AssertFailed();
6046 return GL_CLAMP_TO_EDGE_SGIS; /* @todo correct? */
6047
6048 case SVGA3D_TEX_ADDRESS_EDGE:
6049 case SVGA3D_TEX_ADDRESS_INVALID:
6050 default:
6051 AssertFailed();
6052 return GL_REPEAT; /* default */
6053 }
6054}
6055
6056static GLenum vmsvga3dTextureFilter2OGL(SVGA3dTextureFilter value)
6057{
6058 switch (value)
6059 {
6060 case SVGA3D_TEX_FILTER_NONE:
6061 case SVGA3D_TEX_FILTER_LINEAR:
6062 return GL_LINEAR;
6063 case SVGA3D_TEX_FILTER_NEAREST:
6064 return GL_NEAREST;
6065 case SVGA3D_TEX_FILTER_ANISOTROPIC:
6066 /* @todo */
6067 case SVGA3D_TEX_FILTER_FLATCUBIC: // Deprecated, not implemented
6068 case SVGA3D_TEX_FILTER_GAUSSIANCUBIC: // Deprecated, not implemented
6069 case SVGA3D_TEX_FILTER_PYRAMIDALQUAD: // Not currently implemented
6070 case SVGA3D_TEX_FILTER_GAUSSIANQUAD: // Not currently implemented
6071 default:
6072 AssertFailed();
6073 return GL_LINEAR; /* default */
6074 }
6075}
6076
6077uint32_t vmsvga3dSVGA3dColor2RGBA(SVGA3dColor value)
6078{
6079 /* flip the red and blue bytes */
6080 uint8_t blue = value & 0xff;
6081 uint8_t red = (value >> 16) & 0xff;
6082 return (value & 0xff00ff00) | red | (blue << 16);
6083}
6084
6085int vmsvga3dSetTextureState(PVGASTATE pThis, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
6086{
6087 GLenum val;
6088 GLenum currentStage = ~0L;
6089 PVMSVGA3DCONTEXT pContext;
6090 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6091 AssertReturn(pState, VERR_NO_MEMORY);
6092
6093 Log(("vmsvga3dSetTextureState %x cTextureState=%d\n", cid, cTextureStates));
6094
6095 if ( cid >= pState->cContexts
6096 || pState->papContexts[cid]->id != cid)
6097 {
6098 Log(("vmsvga3dSetTextureState invalid context id!\n"));
6099 return VERR_INVALID_PARAMETER;
6100 }
6101 pContext = pState->papContexts[cid];
6102 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6103
6104 for (unsigned i = 0; i < cTextureStates; i++)
6105 {
6106 GLenum textureType = ~0U;
6107 GLenum samplerType = ~0U;
6108
6109 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));
6110 /* Record the texture state for vm state saving. */
6111 if ( pTextureState[i].stage < SVGA3D_MAX_TEXTURE_STAGE
6112 && pTextureState[i].name < SVGA3D_TS_MAX)
6113 {
6114 pContext->state.aTextureState[pTextureState[i].stage][pTextureState[i].name] = pTextureState[i];
6115 }
6116
6117 /* Active the right texture unit for subsequent texture state changes. */
6118 if (pTextureState[i].stage != currentStage)
6119 {
6120 pState->ext.glActiveTexture(GL_TEXTURE0 + pTextureState[i].stage);
6121 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6122 currentStage = pTextureState[i].stage;
6123 }
6124
6125 switch (pTextureState[i].name)
6126 {
6127 case SVGA3D_TS_BUMPENVMAT00: /* float */
6128 case SVGA3D_TS_BUMPENVMAT01: /* float */
6129 case SVGA3D_TS_BUMPENVMAT10: /* float */
6130 case SVGA3D_TS_BUMPENVMAT11: /* float */
6131 case SVGA3D_TS_BUMPENVLSCALE: /* float */
6132 case SVGA3D_TS_BUMPENVLOFFSET: /* float */
6133 Log(("vmsvga3dSetTextureState: bump mapping texture options not supported!!\n"));
6134 break;
6135
6136 case SVGA3D_TS_COLOROP: /* SVGA3dTextureCombiner */
6137 case SVGA3D_TS_COLORARG0: /* SVGA3dTextureArgData */
6138 case SVGA3D_TS_COLORARG1: /* SVGA3dTextureArgData */
6139 case SVGA3D_TS_COLORARG2: /* SVGA3dTextureArgData */
6140 case SVGA3D_TS_ALPHAOP: /* SVGA3dTextureCombiner */
6141 case SVGA3D_TS_ALPHAARG0: /* SVGA3dTextureArgData */
6142 case SVGA3D_TS_ALPHAARG1: /* SVGA3dTextureArgData */
6143 case SVGA3D_TS_ALPHAARG2: /* SVGA3dTextureArgData */
6144 /* @todo; not used by MesaGL */
6145 Log(("vmsvga3dSetTextureState: colorop/alphaop not yet supported!!\n"));
6146 break;
6147#if 0
6148
6149 case SVGA3D_TS_TEXCOORDINDEX: /* uint32_t */
6150 textureType = D3DTSS_TEXCOORDINDEX;
6151 val = pTextureState[i].value;
6152 break;
6153
6154 case SVGA3D_TS_TEXTURETRANSFORMFLAGS: /* SVGA3dTexTransformFlags */
6155 textureType = D3DTSS_TEXTURETRANSFORMFLAGS;
6156 val = vmsvga3dTextTransformFlags2D3D(pTextureState[i].value);
6157 break;
6158#endif
6159
6160 case SVGA3D_TS_BIND_TEXTURE: /* SVGA3dSurfaceId */
6161 if (pTextureState[i].value == SVGA3D_INVALID_ID)
6162 {
6163 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture surface id=%x replacing=%x\n",
6164 pTextureState[i].stage, pTextureState[i].value, pContext->aSidActiveTexture[currentStage]));
6165
6166 pContext->aSidActiveTexture[currentStage] = SVGA3D_INVALID_ID;
6167 /* Unselect the currently associated texture. */
6168 glBindTexture(GL_TEXTURE_2D, 0);
6169 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6170 /* Necessary for the fixed pipeline. */
6171 glDisable(GL_TEXTURE_2D);
6172 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6173 }
6174 else
6175 {
6176 uint32_t sid = pTextureState[i].value;
6177
6178 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
6179 AssertReturn(sid < pState->cSurfaces && pState->paSurface[sid].id == sid, VERR_INVALID_PARAMETER);
6180
6181 PVMSVGA3DSURFACE pSurface = &pState->paSurface[sid];
6182
6183 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture surface id=%x (%d,%d) replacing=%x\n",
6184 pTextureState[i].stage, pTextureState[i].value, pSurface->pMipmapLevels[0].size.width,
6185 pSurface->pMipmapLevels[0].size.height, pContext->aSidActiveTexture[currentStage]));
6186
6187 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
6188 {
6189#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
6190 Assert(pSurface->idAssociatedContext == SVGA3D_INVALID_ID);
6191#endif
6192 Log(("CreateTexture (%d,%d) level=%d\n", pSurface->pMipmapLevels[0].size.width, pSurface->pMipmapLevels[0].size.height, pSurface->faces[0].numMipLevels));
6193 int rc = vmsvga3dCreateTexture(pState, pContext, cid, pSurface);
6194 AssertRCReturn(rc, rc);
6195 }
6196
6197 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
6198 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6199
6200 /* Necessary for the fixed pipeline. */
6201 glEnable(GL_TEXTURE_2D);
6202 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6203
6204 if (pContext->aSidActiveTexture[currentStage] != sid)
6205 {
6206 /* Recreate the texture state as glBindTexture resets them all (sigh). */
6207 for (uint32_t iStage = 0; iStage < SVGA3D_MAX_TEXTURE_STAGE; iStage++)
6208 {
6209 for (uint32_t j = 0; j < SVGA3D_TS_MAX; j++)
6210 {
6211 SVGA3dTextureState *pTextureStateIter = &pContext->state.aTextureState[iStage][j];
6212
6213 if ( pTextureStateIter->name != SVGA3D_TS_INVALID
6214 && pTextureStateIter->name != SVGA3D_TS_BIND_TEXTURE)
6215 vmsvga3dSetTextureState(pThis, pContext->id, 1, pTextureStateIter);
6216 }
6217 }
6218 }
6219 pContext->aSidActiveTexture[currentStage] = sid;
6220 }
6221 /* Finished; continue with the next one. */
6222 continue;
6223
6224 case SVGA3D_TS_ADDRESSW: /* SVGA3dTextureAddress */
6225 textureType = GL_TEXTURE_WRAP_R; /* R = W */
6226 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
6227 break;
6228
6229 case SVGA3D_TS_ADDRESSU: /* SVGA3dTextureAddress */
6230 textureType = GL_TEXTURE_WRAP_S; /* S = U */
6231 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
6232 break;
6233
6234 case SVGA3D_TS_ADDRESSV: /* SVGA3dTextureAddress */
6235 textureType = GL_TEXTURE_WRAP_T; /* T = V */
6236 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
6237 break;
6238
6239 case SVGA3D_TS_MIPFILTER: /* SVGA3dTextureFilter */
6240 case SVGA3D_TS_MINFILTER: /* SVGA3dTextureFilter */
6241 {
6242 uint32_t mipFilter = pContext->state.aTextureState[currentStage][SVGA3D_TS_MIPFILTER].value;
6243 uint32_t minFilter = pContext->state.aTextureState[currentStage][SVGA3D_TS_MINFILTER].value;
6244
6245 /* If SVGA3D_TS_MIPFILTER is set to NONE, then use SVGA3D_TS_MIPFILTER, otherwise SVGA3D_TS_MIPFILTER enables mipmap minification. */
6246 textureType = GL_TEXTURE_MIN_FILTER;
6247 if (mipFilter != SVGA3D_TEX_FILTER_NONE)
6248 {
6249 if (minFilter == SVGA3D_TEX_FILTER_NEAREST)
6250 {
6251 if (mipFilter == SVGA3D_TEX_FILTER_LINEAR)
6252 val = GL_NEAREST_MIPMAP_LINEAR;
6253 else
6254 val = GL_NEAREST_MIPMAP_NEAREST;
6255 }
6256 else
6257 {
6258 if (mipFilter == SVGA3D_TEX_FILTER_LINEAR)
6259 val = GL_LINEAR_MIPMAP_LINEAR;
6260 else
6261 val = GL_LINEAR_MIPMAP_NEAREST;
6262 }
6263 }
6264 else
6265 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)minFilter);
6266
6267 break;
6268 }
6269
6270 case SVGA3D_TS_MAGFILTER: /* SVGA3dTextureFilter */
6271 textureType = GL_TEXTURE_MAG_FILTER;
6272 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)pTextureState[i].value);
6273 Assert(val == GL_NEAREST || val == GL_LINEAR);
6274 break;
6275
6276 case SVGA3D_TS_BORDERCOLOR: /* SVGA3dColor */
6277 {
6278 GLfloat color[4]; /* red, green, blue, alpha */
6279
6280 vmsvgaColor2GLFloatArray(pTextureState[i].value, &color[0], &color[1], &color[2], &color[3]);
6281
6282 glTexParameterfv(GL_TEXTURE_2D /* @todo flexible type */, GL_TEXTURE_BORDER_COLOR, color); /* Identical; default 0.0 identical too */
6283 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6284 break;
6285 }
6286
6287 case SVGA3D_TS_TEXTURE_LOD_BIAS: /* float */
6288 glTexParameterf(GL_TEXTURE_2D /* @todo flexible type */, GL_TEXTURE_LOD_BIAS, pTextureState[i].value); /* Identical; default 0.0 identical too */
6289 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6290 break;
6291
6292 case SVGA3D_TS_TEXTURE_MIPMAP_LEVEL: /* uint32_t */
6293 textureType = GL_TEXTURE_BASE_LEVEL;
6294 val = pTextureState[i].value;
6295 break;
6296
6297#if 0
6298 case SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL: /* uint32_t */
6299 samplerType = D3DSAMP_MAXANISOTROPY;
6300 val = pTextureState[i].value; /* Identical?? */
6301 break;
6302
6303 case SVGA3D_TS_GAMMA: /* float */
6304 samplerType = D3DSAMP_SRGBTEXTURE;
6305 /* Boolean in D3D */
6306 if (pTextureState[i].floatValue == 1.0f)
6307 val = FALSE;
6308 else
6309 val = TRUE;
6310 break;
6311#endif
6312 /* Internal commands, that don't map directly to the SetTextureStageState API. */
6313 case SVGA3D_TS_TEXCOORDGEN: /* SVGA3dTextureCoordGen */
6314 AssertFailed();
6315 break;
6316
6317 default:
6318 //AssertFailed();
6319 break;
6320 }
6321
6322 if (textureType != ~0U)
6323 {
6324 glTexParameteri(GL_TEXTURE_2D /* @todo flexible type */, textureType, val);
6325 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6326 }
6327 }
6328
6329 return VINF_SUCCESS;
6330}
6331
6332int vmsvga3dSetMaterial(PVGASTATE pThis, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
6333{
6334 PVMSVGA3DCONTEXT pContext;
6335 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6336 AssertReturn(pState, VERR_NO_MEMORY);
6337 GLenum oglFace;
6338
6339 Log(("vmsvga3dSetMaterial cid=%x face %d\n", cid, face));
6340
6341 if ( cid >= pState->cContexts
6342 || pState->papContexts[cid]->id != cid)
6343 {
6344 Log(("vmsvga3dSetMaterial invalid context id!\n"));
6345 return VERR_INVALID_PARAMETER;
6346 }
6347 pContext = pState->papContexts[cid];
6348 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6349
6350 switch (face)
6351 {
6352 case SVGA3D_FACE_NONE:
6353 case SVGA3D_FACE_FRONT:
6354 oglFace = GL_FRONT;
6355 break;
6356
6357 case SVGA3D_FACE_BACK:
6358 oglFace = GL_BACK;
6359 break;
6360
6361 case SVGA3D_FACE_FRONT_BACK:
6362 oglFace = GL_FRONT_AND_BACK;
6363 break;
6364
6365 default:
6366 AssertFailedReturn(VERR_INVALID_PARAMETER);
6367 }
6368
6369 /* Save for vm state save/restore. */
6370 pContext->state.aMaterial[face].fValid = true;
6371 pContext->state.aMaterial[face].material = *pMaterial;
6372 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_MATERIAL;
6373
6374 glMaterialfv(oglFace, GL_DIFFUSE, pMaterial->diffuse);
6375 glMaterialfv(oglFace, GL_AMBIENT, pMaterial->ambient);
6376 glMaterialfv(oglFace, GL_SPECULAR, pMaterial->specular);
6377 glMaterialfv(oglFace, GL_EMISSION, pMaterial->emissive);
6378 glMaterialfv(oglFace, GL_SHININESS, &pMaterial->shininess);
6379 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6380
6381 return VINF_SUCCESS;
6382}
6383
6384/* @todo Move into separate library as we are using logic from Wine here. */
6385int vmsvga3dSetLightData(PVGASTATE pThis, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
6386{
6387 PVMSVGA3DCONTEXT pContext;
6388 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6389 AssertReturn(pState, VERR_NO_MEMORY);
6390 float QuadAttenuation;
6391
6392 Log(("vmsvga3dSetLightData cid=%x index=%d type=%d\n", cid, index, pData->type));
6393
6394 if ( cid >= pState->cContexts
6395 || pState->papContexts[cid]->id != cid)
6396 {
6397 Log(("vmsvga3dSetLightData invalid context id!\n"));
6398 return VERR_INVALID_PARAMETER;
6399 }
6400 pContext = pState->papContexts[cid];
6401 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6402
6403 /* Store for vm state save/restore */
6404 if (index < SVGA3D_MAX_LIGHTS)
6405 {
6406 pContext->state.aLightData[index].fValidData = true;
6407 pContext->state.aLightData[index].data = *pData;
6408 }
6409 else
6410 AssertFailed();
6411
6412 if ( pData->attenuation0 < 0.0f
6413 || pData->attenuation1 < 0.0f
6414 || pData->attenuation2 < 0.0f)
6415 {
6416 Log(("vmsvga3dSetLightData: invalid negative attenuation values!!\n"));
6417 return VINF_SUCCESS; /* ignore; could crash the GL driver */
6418 }
6419
6420 /* Light settings are affected by the model view in OpenGL, the View transform in direct3d */
6421 glMatrixMode(GL_MODELVIEW);
6422 glPushMatrix();
6423 glLoadMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
6424
6425 glLightfv(GL_LIGHT0 + index, GL_DIFFUSE, pData->diffuse);
6426 glLightfv(GL_LIGHT0 + index, GL_SPECULAR, pData->specular);
6427 glLightfv(GL_LIGHT0 + index, GL_AMBIENT, pData->ambient);
6428
6429 if (pData->range * pData->range >= FLT_MIN)
6430 QuadAttenuation = 1.4f / (pData->range * pData->range);
6431 else
6432 QuadAttenuation = 0.0f;
6433
6434 switch (pData->type)
6435 {
6436 case SVGA3D_LIGHTTYPE_POINT:
6437 {
6438 GLfloat position[4];
6439
6440 position[0] = pData->position[0];
6441 position[1] = pData->position[1];
6442 position[2] = pData->position[2];
6443 position[3] = 1.0f;
6444
6445 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
6446 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6447
6448 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
6449 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6450
6451 /* Attenuation - Are these right? guessing... */
6452 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
6453 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6454
6455 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
6456 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6457
6458 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
6459 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6460
6461 /* @todo range */
6462 break;
6463 }
6464
6465 case SVGA3D_LIGHTTYPE_SPOT1:
6466 {
6467 GLfloat exponent;
6468 GLfloat position[4];
6469 const GLfloat pi = 4.0f * atanf(1.0f);
6470
6471 position[0] = pData->position[0];
6472 position[1] = pData->position[1];
6473 position[2] = pData->position[2];
6474 position[3] = 1.0f;
6475
6476 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
6477 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6478
6479 position[0] = pData->direction[0];
6480 position[1] = pData->direction[1];
6481 position[2] = pData->direction[2];
6482 position[3] = 1.0f;
6483
6484 glLightfv(GL_LIGHT0 + index, GL_SPOT_DIRECTION, position);
6485 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6486
6487 /*
6488 * opengl-ish and d3d-ish spot lights use too different models for the
6489 * light "intensity" as a function of the angle towards the main light direction,
6490 * so we only can approximate very roughly.
6491 * however spot lights are rather rarely used in games (if ever used at all).
6492 * furthermore if still used, probably nobody pays attention to such details.
6493 */
6494 if (pData->falloff == 0)
6495 {
6496 /* Falloff = 0 is easy, because d3d's and opengl's spot light equations have the
6497 * falloff resp. exponent parameter as an exponent, so the spot light lighting
6498 * will always be 1.0 for both of them, and we don't have to care for the
6499 * rest of the rather complex calculation
6500 */
6501 exponent = 0.0f;
6502 }
6503 else
6504 {
6505 float rho = pData->theta + (pData->phi - pData->theta) / (2 * pData->falloff);
6506 if (rho < 0.0001f)
6507 rho = 0.0001f;
6508 exponent = -0.3f/log(cos(rho/2));
6509 }
6510 if (exponent > 128.0f)
6511 exponent = 128.0f;
6512
6513 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, exponent);
6514 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6515
6516 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, pData->phi * 90.0 / pi);
6517 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6518
6519 /* Attenuation - Are these right? guessing... */
6520 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
6521 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6522
6523 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
6524 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6525
6526 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
6527 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6528
6529 /* @todo range */
6530 break;
6531 }
6532
6533 case SVGA3D_LIGHTTYPE_DIRECTIONAL:
6534 {
6535 GLfloat position[4];
6536
6537 position[0] = -pData->direction[0];
6538 position[1] = -pData->direction[1];
6539 position[2] = -pData->direction[2];
6540 position[3] = 0.0f;
6541
6542 glLightfv(GL_LIGHT0 + index, GL_POSITION, position); /* Note gl uses w position of 0 for direction! */
6543 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6544
6545 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
6546 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6547
6548 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, 0.0f);
6549 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6550 break;
6551 }
6552
6553 case SVGA3D_LIGHTTYPE_SPOT2:
6554 default:
6555 Log(("Unsupported light type!!\n"));
6556 return VERR_INVALID_PARAMETER;
6557 }
6558
6559 /* Restore the modelview matrix */
6560 glPopMatrix();
6561
6562 return VINF_SUCCESS;
6563}
6564
6565int vmsvga3dSetLightEnabled(PVGASTATE pThis, uint32_t cid, uint32_t index, uint32_t enabled)
6566{
6567 PVMSVGA3DCONTEXT pContext;
6568 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6569 AssertReturn(pState, VERR_NO_MEMORY);
6570
6571 Log(("vmsvga3dSetLightEnabled cid=%x %d -> %d\n", cid, index, enabled));
6572
6573 if ( cid >= pState->cContexts
6574 || pState->papContexts[cid]->id != cid)
6575 {
6576 Log(("vmsvga3dSetLightEnabled invalid context id!\n"));
6577 return VERR_INVALID_PARAMETER;
6578 }
6579 pContext = pState->papContexts[cid];
6580 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6581
6582 /* Store for vm state save/restore */
6583 if (index < SVGA3D_MAX_LIGHTS)
6584 pContext->state.aLightData[index].fEnabled = !!enabled;
6585 else
6586 AssertFailed();
6587
6588 if (enabled)
6589 {
6590 /* Load the default settings if none have been set yet. */
6591 if (!pContext->state.aLightData[index].fValidData)
6592 vmsvga3dSetLightData(pThis, cid, index, (SVGA3dLightData *)&vmsvga3d_default_light);
6593 glEnable(GL_LIGHT0 + index);
6594 }
6595 else
6596 glDisable(GL_LIGHT0 + index);
6597
6598 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6599 return VINF_SUCCESS;
6600}
6601
6602int vmsvga3dSetViewPort(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
6603{
6604 PVMSVGA3DCONTEXT pContext;
6605 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6606 AssertReturn(pState, VERR_NO_MEMORY);
6607
6608 Log(("vmsvga3dSetViewPort cid=%x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
6609
6610 if ( cid >= pState->cContexts
6611 || pState->papContexts[cid]->id != cid)
6612 {
6613 Log(("vmsvga3dSetViewPort invalid context id!\n"));
6614 return VERR_INVALID_PARAMETER;
6615 }
6616 pContext = pState->papContexts[cid];
6617 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6618
6619 /* Save for vm state save/restore. */
6620 pContext->state.RectViewPort = *pRect;
6621 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VIEWPORT;
6622
6623 /* @todo y-inversion for partial viewport coordinates? */
6624 glViewport(pRect->x, pRect->y, pRect->w, pRect->h);
6625 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6626
6627 /* Reset the projection matrix as that relies on the viewport setting. */
6628 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].fValid == true)
6629 {
6630 vmsvga3dSetTransform(pThis, cid, SVGA3D_TRANSFORM_PROJECTION, pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].matrix);
6631 }
6632 else
6633 {
6634 float matrix[16];
6635
6636 /* identity matrix if no matrix set. */
6637 memset(matrix, 0, sizeof(matrix));
6638 matrix[0] = 1.0;
6639 matrix[5] = 1.0;
6640 matrix[10] = 1.0;
6641 matrix[15] = 1.0;
6642 vmsvga3dSetTransform(pThis, cid, SVGA3D_TRANSFORM_PROJECTION, matrix);
6643 }
6644
6645 return VINF_SUCCESS;
6646}
6647
6648int vmsvga3dSetClipPlane(PVGASTATE pThis, uint32_t cid, uint32_t index, float plane[4])
6649{
6650 PVMSVGA3DCONTEXT pContext;
6651 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6652 AssertReturn(pState, VERR_NO_MEMORY);
6653 double oglPlane[4];
6654
6655 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)));
6656 AssertReturn(index < SVGA3D_CLIPPLANE_MAX, VERR_INVALID_PARAMETER);
6657
6658 if ( cid >= pState->cContexts
6659 || pState->papContexts[cid]->id != cid)
6660 {
6661 Log(("vmsvga3dSetClipPlane invalid context id!\n"));
6662 return VERR_INVALID_PARAMETER;
6663 }
6664 pContext = pState->papContexts[cid];
6665 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6666
6667 /* Store for vm state save/restore. */
6668 pContext->state.aClipPlane[index].fValid = true;
6669 memcpy(pContext->state.aClipPlane[index].plane, plane, sizeof(pContext->state.aClipPlane[index].plane));
6670
6671 /** @todo clip plane affected by model view in OpenGL & view in D3D + vertex shader -> not transformed (see Wine; state.c clipplane) */
6672 oglPlane[0] = (double)plane[0];
6673 oglPlane[1] = (double)plane[1];
6674 oglPlane[2] = (double)plane[2];
6675 oglPlane[3] = (double)plane[3];
6676
6677 glClipPlane(GL_CLIP_PLANE0 + index, oglPlane);
6678 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6679
6680 return VINF_SUCCESS;
6681}
6682
6683int vmsvga3dSetScissorRect(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
6684{
6685 PVMSVGA3DCONTEXT pContext;
6686 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6687 AssertReturn(pState, VERR_NO_MEMORY);
6688
6689 Log(("vmsvga3dSetScissorRect cid=%x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
6690
6691 if ( cid >= pState->cContexts
6692 || pState->papContexts[cid]->id != cid)
6693 {
6694 Log(("vmsvga3dSetScissorRect invalid context id!\n"));
6695 return VERR_INVALID_PARAMETER;
6696 }
6697 pContext = pState->papContexts[cid];
6698 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6699
6700 /* Store for vm state save/restore. */
6701 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_SCISSORRECT;
6702 pContext->state.RectScissor = *pRect;
6703
6704 glScissor(pRect->x, pRect->y, pRect->w, pRect->h);
6705 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6706
6707 return VINF_SUCCESS;
6708}
6709
6710static void vmsvgaColor2GLFloatArray(uint32_t color, GLfloat *pRed, GLfloat *pGreen, GLfloat *pBlue, GLfloat *pAlpha)
6711{
6712 /* Convert byte color components to float (0-1.0) */
6713 *pAlpha = (GLfloat)(color >> 24) / 255.0;
6714 *pRed = (GLfloat)((color >> 16) & 0xff) / 255.0;
6715 *pGreen = (GLfloat)((color >> 8) & 0xff) / 255.0;
6716 *pBlue = (GLfloat)(color & 0xff) / 255.0;
6717}
6718
6719int vmsvga3dCommandClear(PVGASTATE pThis, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth, uint32_t stencil,
6720 uint32_t cRects, SVGA3dRect *pRect)
6721{
6722 GLbitfield mask = 0;
6723 PVMSVGA3DCONTEXT pContext;
6724 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6725 AssertReturn(pState, VERR_NO_MEMORY);
6726 GLboolean fDepthWriteEnabled = GL_FALSE;
6727
6728 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));
6729
6730 if ( cid >= pState->cContexts
6731 || pState->papContexts[cid]->id != cid)
6732 {
6733 Log(("vmsvga3dCommandClear invalid context id!\n"));
6734 return VERR_INVALID_PARAMETER;
6735 }
6736 pContext = pState->papContexts[cid];
6737 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6738
6739 if (clearFlag & SVGA3D_CLEAR_COLOR)
6740 {
6741 GLfloat red, green, blue, alpha;
6742
6743 vmsvgaColor2GLFloatArray(color, &red, &green, &blue, &alpha);
6744
6745 /* Set the color clear value. */
6746 glClearColor(red, green, blue, alpha);
6747
6748 mask |= GL_COLOR_BUFFER_BIT;
6749 }
6750 if (clearFlag & SVGA3D_CLEAR_STENCIL)
6751 {
6752 /* @todo possibly the same problem as with glDepthMask */
6753 glClearStencil(stencil);
6754 mask |= GL_STENCIL_BUFFER_BIT;
6755 }
6756 if (clearFlag & SVGA3D_CLEAR_DEPTH)
6757 {
6758 glClearDepth((GLdouble)depth);
6759 mask |= GL_DEPTH_BUFFER_BIT;
6760
6761 /* glClear will not clear the depth buffer if writing is disabled. */
6762 glGetBooleanv(GL_DEPTH_WRITEMASK, &fDepthWriteEnabled);
6763 if (fDepthWriteEnabled == GL_FALSE)
6764 glDepthMask(GL_TRUE);
6765 }
6766
6767 if (cRects)
6768 {
6769 /* Save the current scissor test bit and scissor box. */
6770 glPushAttrib(GL_SCISSOR_BIT);
6771 glEnable(GL_SCISSOR_TEST);
6772 for (unsigned i=0; i < cRects; i++)
6773 {
6774 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));
6775 glScissor(pRect[i].x, pRect[i].y, pRect[i].w, pRect[i].h);
6776 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6777 glClear(mask);
6778 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6779 }
6780 /* Restore the old scissor test bit and box */
6781 glPopAttrib();
6782 }
6783 else
6784 {
6785 glClear(mask);
6786 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6787 }
6788
6789 /* Restore depth write state. */
6790 if ( (clearFlag & SVGA3D_CLEAR_DEPTH)
6791 && fDepthWriteEnabled == GL_FALSE)
6792 glDepthMask(GL_FALSE);
6793
6794 return VINF_SUCCESS;
6795}
6796
6797/* Convert VMWare vertex declaration to its OpenGL equivalent. */
6798int vmsvga3dVertexDecl2OGL(SVGA3dVertexArrayIdentity &identity, GLint &size, GLenum &type, GLboolean &normalized)
6799{
6800 normalized = GL_FALSE;
6801 switch (identity.type)
6802 {
6803 case SVGA3D_DECLTYPE_FLOAT1:
6804 size = 1;
6805 type = GL_FLOAT;
6806 break;
6807 case SVGA3D_DECLTYPE_FLOAT2:
6808 size = 2;
6809 type = GL_FLOAT;
6810 break;
6811 case SVGA3D_DECLTYPE_FLOAT3:
6812 size = 3;
6813 type = GL_FLOAT;
6814 break;
6815 case SVGA3D_DECLTYPE_FLOAT4:
6816 size = 4;
6817 type = GL_FLOAT;
6818 break;
6819
6820 case SVGA3D_DECLTYPE_D3DCOLOR:
6821 size = GL_BGRA; /* @note requires GL_ARB_vertex_array_bgra */
6822 type = GL_UNSIGNED_BYTE;
6823 normalized = GL_TRUE; /* glVertexAttribPointer fails otherwise */
6824 break;
6825
6826 case SVGA3D_DECLTYPE_UBYTE4N:
6827 normalized = GL_TRUE;
6828 /* no break */
6829 case SVGA3D_DECLTYPE_UBYTE4:
6830 size = 4;
6831 type = GL_UNSIGNED_BYTE;
6832 break;
6833
6834 case SVGA3D_DECLTYPE_SHORT2N:
6835 normalized = GL_TRUE;
6836 /* no break */
6837 case SVGA3D_DECLTYPE_SHORT2:
6838 size = 2;
6839 type = GL_SHORT;
6840 break;
6841
6842 case SVGA3D_DECLTYPE_SHORT4N:
6843 normalized = GL_TRUE;
6844 /* no break */
6845 case SVGA3D_DECLTYPE_SHORT4:
6846 size = 4;
6847 type = GL_SHORT;
6848 break;
6849
6850 case SVGA3D_DECLTYPE_USHORT4N:
6851 normalized = GL_TRUE;
6852 size = 4;
6853 type = GL_UNSIGNED_SHORT;
6854 break;
6855
6856 case SVGA3D_DECLTYPE_USHORT2N:
6857 normalized = GL_TRUE;
6858 size = 2;
6859 type = GL_UNSIGNED_SHORT;
6860 break;
6861
6862 case SVGA3D_DECLTYPE_UDEC3:
6863 size = 3;
6864 type = GL_UNSIGNED_INT_2_10_10_10_REV; /* @todo correct? */
6865 break;
6866
6867 case SVGA3D_DECLTYPE_DEC3N:
6868 normalized = true;
6869 size = 3;
6870 type = GL_INT_2_10_10_10_REV; /* @todo correct? */
6871 break;
6872
6873 case SVGA3D_DECLTYPE_FLOAT16_2:
6874 size = 2;
6875 type = GL_HALF_FLOAT;
6876 break;
6877 case SVGA3D_DECLTYPE_FLOAT16_4:
6878 size = 4;
6879 type = GL_HALF_FLOAT;
6880 break;
6881 default:
6882 AssertFailedReturn(VERR_INVALID_PARAMETER);
6883 }
6884
6885 //pVertexElement->Method = identity.method;
6886 //pVertexElement->Usage = identity.usage;
6887
6888 return VINF_SUCCESS;
6889}
6890
6891/* Convert VMWare primitive type to its OpenGL equivalent. */
6892/* Calculate the vertex count based on the primitive type and nr of primitives. */
6893int vmsvga3dPrimitiveType2OGL(SVGA3dPrimitiveType PrimitiveType, GLenum *pMode, uint32_t cPrimitiveCount, uint32_t *pcVertices)
6894{
6895 switch (PrimitiveType)
6896 {
6897 case SVGA3D_PRIMITIVE_TRIANGLELIST:
6898 *pMode = GL_TRIANGLES;
6899 *pcVertices = cPrimitiveCount * 3;
6900 break;
6901 case SVGA3D_PRIMITIVE_POINTLIST:
6902 *pMode = GL_POINTS;
6903 *pcVertices = cPrimitiveCount;
6904 break;
6905 case SVGA3D_PRIMITIVE_LINELIST:
6906 *pMode = GL_LINES;
6907 *pcVertices = cPrimitiveCount * 2;
6908 break;
6909 case SVGA3D_PRIMITIVE_LINESTRIP:
6910 *pMode = GL_LINE_STRIP;
6911 *pcVertices = cPrimitiveCount + 1;
6912 break;
6913 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
6914 *pMode = GL_TRIANGLE_STRIP;
6915 *pcVertices = cPrimitiveCount + 2;
6916 break;
6917 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
6918 *pMode = GL_TRIANGLE_FAN;
6919 *pcVertices = cPrimitiveCount + 2;
6920 break;
6921 default:
6922 return VERR_INVALID_PARAMETER;
6923 }
6924 return VINF_SUCCESS;
6925}
6926
6927int vmsvga3dDrawPrimitivesProcessVertexDecls(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t iVertexDeclBase, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl)
6928{
6929 unsigned sidVertex = pVertexDecl[0].array.surfaceId;
6930 PVMSVGA3DSURFACE pVertexSurface;
6931
6932 AssertReturn(sidVertex < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
6933 AssertReturn(sidVertex < pState->cSurfaces && pState->paSurface[sidVertex].id == sidVertex, VERR_INVALID_PARAMETER);
6934
6935 pVertexSurface = &pState->paSurface[sidVertex];
6936 Log(("vmsvga3dDrawPrimitives: vertex surface %x\n", sidVertex));
6937
6938 /* Create and/or bind the vertex buffer. */
6939 if (pVertexSurface->oglId.buffer == OPENGL_INVALID_ID)
6940 {
6941 Log(("vmsvga3dDrawPrimitives: create vertex buffer fDirty=%d size=%x bytes\n", pVertexSurface->fDirty, pVertexSurface->pMipmapLevels[0].cbSurface));
6942#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
6943 PVMSVGA3DCONTEXT pSavedCtx = pContext;
6944 pContext = &pState->SharedCtx;
6945 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6946#endif
6947
6948 pState->ext.glGenBuffers(1, &pVertexSurface->oglId.buffer);
6949 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6950
6951 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
6952 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6953
6954 Assert(pVertexSurface->fDirty);
6955 /* @todo rethink usage dynamic/static */
6956 pState->ext.glBufferData(GL_ARRAY_BUFFER, pVertexSurface->pMipmapLevels[0].cbSurface, pVertexSurface->pMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
6957 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6958
6959 pVertexSurface->pMipmapLevels[0].fDirty = false;
6960 pVertexSurface->fDirty = false;
6961
6962 pVertexSurface->flags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
6963
6964#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
6965 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, OPENGL_INVALID_ID);
6966 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6967
6968 pContext = pSavedCtx;
6969 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6970#endif
6971 }
6972#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
6973 else
6974#endif
6975 {
6976 Assert(pVertexSurface->fDirty == false);
6977 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
6978 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6979 }
6980#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
6981 pVertexSurface->idAssociatedContext = pContext->id;
6982 LogFlow(("vmsvga3dDrawPrimitivesProcessVertexDecls: sid=%x idAssociatedContext %#x -> %#x\n", pVertexSurface->id, pVertexSurface->idAssociatedContext, pContext->id));
6983#endif
6984
6985 /* Setup the vertex declarations. */
6986 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
6987 {
6988 GLint size;
6989 GLenum type;
6990 GLboolean normalized;
6991 GLuint index = iVertexDeclBase + iVertex;
6992
6993 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));
6994
6995 int rc = vmsvga3dVertexDecl2OGL(pVertexDecl[iVertex].identity, size, type, normalized);
6996 AssertRCReturn(rc, rc);
6997
6998 if (pContext->state.shidVertex != SVGA_ID_INVALID)
6999 {
7000 /* Use numbered vertex arrays when shaders are active. */
7001 pState->ext.glEnableVertexAttribArray(index);
7002 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7003 pState->ext.glVertexAttribPointer(index, size, type, normalized, pVertexDecl[iVertex].array.stride,
7004 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
7005 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7006 /* case SVGA3D_DECLUSAGE_COLOR: @todo color component order not identical!! test GL_BGRA!! */
7007 }
7008 else
7009 {
7010 /* Use the predefined selection of vertex streams for the fixed pipeline. */
7011 switch (pVertexDecl[iVertex].identity.usage)
7012 {
7013 case SVGA3D_DECLUSAGE_POSITION:
7014 glEnableClientState(GL_VERTEX_ARRAY);
7015 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7016 glVertexPointer(size, type, pVertexDecl[iVertex].array.stride,
7017 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
7018 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7019 break;
7020 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
7021 AssertFailed();
7022 break;
7023 case SVGA3D_DECLUSAGE_BLENDINDICES:
7024 AssertFailed();
7025 break;
7026 case SVGA3D_DECLUSAGE_NORMAL:
7027 glEnableClientState(GL_NORMAL_ARRAY);
7028 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7029 glNormalPointer(type, pVertexDecl[iVertex].array.stride,
7030 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
7031 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7032 break;
7033 case SVGA3D_DECLUSAGE_PSIZE:
7034 AssertFailed();
7035 break;
7036 case SVGA3D_DECLUSAGE_TEXCOORD:
7037 /* Specify the affected texture unit. */
7038#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
7039 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
7040#else
7041 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
7042#endif
7043 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
7044 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7045 glTexCoordPointer(size, 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_TANGENT:
7050 AssertFailed();
7051 break;
7052 case SVGA3D_DECLUSAGE_BINORMAL:
7053 AssertFailed();
7054 break;
7055 case SVGA3D_DECLUSAGE_TESSFACTOR:
7056 AssertFailed();
7057 break;
7058 case SVGA3D_DECLUSAGE_POSITIONT:
7059 AssertFailed(); /* see position_transformed in Wine */
7060 break;
7061 case SVGA3D_DECLUSAGE_COLOR: /** @todo color component order not identical!! test GL_BGRA!! */
7062 glEnableClientState(GL_COLOR_ARRAY);
7063 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7064 glColorPointer(size, type, pVertexDecl[iVertex].array.stride,
7065 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
7066 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7067 break;
7068 case SVGA3D_DECLUSAGE_FOG:
7069 glEnableClientState(GL_FOG_COORD_ARRAY);
7070 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7071 pState->ext.glFogCoordPointer(type, pVertexDecl[iVertex].array.stride,
7072 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
7073 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7074 break;
7075 case SVGA3D_DECLUSAGE_DEPTH:
7076 AssertFailed();
7077 break;
7078 case SVGA3D_DECLUSAGE_SAMPLE:
7079 AssertFailed();
7080 break;
7081 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
7082 }
7083 }
7084
7085#ifdef LOG_ENABLED
7086 if (pVertexDecl[iVertex].array.stride == 0)
7087 Log(("vmsvga3dDrawPrimitives: stride == 0! Can be valid\n"));
7088#endif
7089 }
7090
7091 return VINF_SUCCESS;
7092}
7093
7094int vmsvga3dDrawPrimitivesCleanupVertexDecls(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t iVertexDeclBase, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl)
7095{
7096 /* Setup the vertex declarations. */
7097 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
7098 {
7099 if (pContext->state.shidVertex != SVGA_ID_INVALID)
7100 {
7101 /* Use numbered vertex arrays when shaders are active. */
7102 pState->ext.glDisableVertexAttribArray(iVertexDeclBase + iVertex);
7103 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7104 }
7105 else
7106 {
7107 /* Use the predefined selection of vertex streams for the fixed pipeline. */
7108 switch (pVertexDecl[iVertex].identity.usage)
7109 {
7110 case SVGA3D_DECLUSAGE_POSITION:
7111 glDisableClientState(GL_VERTEX_ARRAY);
7112 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7113 break;
7114 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
7115 break;
7116 case SVGA3D_DECLUSAGE_BLENDINDICES:
7117 break;
7118 case SVGA3D_DECLUSAGE_NORMAL:
7119 glDisableClientState(GL_NORMAL_ARRAY);
7120 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7121 break;
7122 case SVGA3D_DECLUSAGE_PSIZE:
7123 break;
7124 case SVGA3D_DECLUSAGE_TEXCOORD:
7125 /* Specify the affected texture unit. */
7126#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
7127 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
7128#else
7129 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
7130#endif
7131 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
7132 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7133 break;
7134 case SVGA3D_DECLUSAGE_TANGENT:
7135 break;
7136 case SVGA3D_DECLUSAGE_BINORMAL:
7137 break;
7138 case SVGA3D_DECLUSAGE_TESSFACTOR:
7139 break;
7140 case SVGA3D_DECLUSAGE_POSITIONT:
7141 break;
7142 case SVGA3D_DECLUSAGE_COLOR: /* @todo color component order not identical!! */
7143 glDisableClientState(GL_COLOR_ARRAY);
7144 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7145 break;
7146 case SVGA3D_DECLUSAGE_FOG:
7147 glDisableClientState(GL_FOG_COORD_ARRAY);
7148 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7149 break;
7150 case SVGA3D_DECLUSAGE_DEPTH:
7151 break;
7152 case SVGA3D_DECLUSAGE_SAMPLE:
7153 break;
7154 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
7155 }
7156 }
7157 }
7158 /* Unbind the vertex buffer after usage. */
7159 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0);
7160 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7161 return VINF_SUCCESS;
7162}
7163
7164int vmsvga3dDrawPrimitives(PVGASTATE pThis, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl, uint32_t numRanges, SVGA3dPrimitiveRange *pRange, uint32_t cVertexDivisor, SVGA3dVertexDivisor *pVertexDivisor)
7165{
7166 PVMSVGA3DCONTEXT pContext;
7167 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
7168 AssertReturn(pState, VERR_INTERNAL_ERROR);
7169 int rc = VERR_NOT_IMPLEMENTED;
7170 uint32_t iCurrentVertex;
7171
7172 Log(("vmsvga3dDrawPrimitives cid=%x numVertexDecls=%d numRanges=%d, cVertexDivisor=%d\n", cid, numVertexDecls, numRanges, cVertexDivisor));
7173
7174 AssertReturn(numVertexDecls && numVertexDecls <= SVGA3D_MAX_VERTEX_ARRAYS, VERR_INVALID_PARAMETER);
7175 AssertReturn(numRanges && numRanges <= SVGA3D_MAX_DRAW_PRIMITIVE_RANGES, VERR_INVALID_PARAMETER);
7176 AssertReturn(!cVertexDivisor || cVertexDivisor == numVertexDecls, VERR_INVALID_PARAMETER);
7177 /* @todo */
7178 Assert(!cVertexDivisor);
7179
7180 if ( cid >= pState->cContexts
7181 || pState->papContexts[cid]->id != cid)
7182 {
7183 Log(("vmsvga3dDrawPrimitives invalid context id!\n"));
7184 return VERR_INVALID_PARAMETER;
7185 }
7186 pContext = pState->papContexts[cid];
7187 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7188
7189 /* Flush any shader changes. */
7190 if (pContext->pShaderContext)
7191 {
7192 uint32_t rtHeight = 0;
7193
7194 if (pContext->sidRenderTarget != SVGA_ID_INVALID)
7195 {
7196 PVMSVGA3DSURFACE pRenderTarget = &pState->paSurface[pContext->sidRenderTarget];
7197 rtHeight = pRenderTarget->pMipmapLevels[0].size.height;
7198 }
7199
7200 ShaderUpdateState(pContext->pShaderContext, rtHeight);
7201 }
7202
7203 /* Process all vertex declarations. Each vertex buffer is represented by one stream. */
7204 iCurrentVertex = 0;
7205 while (iCurrentVertex < numVertexDecls)
7206 {
7207 uint32_t sidVertex = SVGA_ID_INVALID;
7208 uint32_t iVertex;
7209
7210 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
7211 {
7212 if ( sidVertex != SVGA_ID_INVALID
7213 && pVertexDecl[iVertex].array.surfaceId != sidVertex
7214 )
7215 break;
7216 sidVertex = pVertexDecl[iVertex].array.surfaceId;
7217 }
7218
7219 rc = vmsvga3dDrawPrimitivesProcessVertexDecls(pState, pContext, iCurrentVertex, iVertex - iCurrentVertex, &pVertexDecl[iCurrentVertex]);
7220 AssertRCReturn(rc, rc);
7221
7222 iCurrentVertex = iVertex;
7223 }
7224
7225 /* Now draw the primitives. */
7226 for (unsigned iPrimitive = 0; iPrimitive < numRanges; iPrimitive++)
7227 {
7228 GLenum modeDraw;
7229 unsigned sidIndex = pRange[iPrimitive].indexArray.surfaceId;
7230 PVMSVGA3DSURFACE pIndexSurface = NULL;
7231 unsigned cVertices;
7232
7233 Log(("Primitive %d: type %s\n", iPrimitive, vmsvga3dPrimitiveType2String(pRange[iPrimitive].primType)));
7234 rc = vmsvga3dPrimitiveType2OGL(pRange[iPrimitive].primType, &modeDraw, pRange[iPrimitive].primitiveCount, &cVertices);
7235 if (RT_FAILURE(rc))
7236 {
7237 AssertRC(rc);
7238 goto internal_error;
7239 }
7240
7241 if (sidIndex != SVGA3D_INVALID_ID)
7242 {
7243 AssertMsg(pRange[iPrimitive].indexWidth == sizeof(uint32_t) || pRange[iPrimitive].indexWidth == sizeof(uint16_t), ("Unsupported primitive width %d\n", pRange[iPrimitive].indexWidth));
7244
7245 if ( sidIndex >= SVGA3D_MAX_SURFACE_IDS
7246 || sidIndex >= pState->cSurfaces
7247 || pState->paSurface[sidIndex].id != sidIndex)
7248 {
7249 Assert(sidIndex < SVGA3D_MAX_SURFACE_IDS);
7250 Assert(sidIndex < pState->cSurfaces && pState->paSurface[sidIndex].id == sidIndex);
7251 rc = VERR_INVALID_PARAMETER;
7252 goto internal_error;
7253 }
7254 pIndexSurface = &pState->paSurface[sidIndex];
7255 Log(("vmsvga3dDrawPrimitives: index surface %x\n", sidIndex));
7256
7257 if (pIndexSurface->oglId.buffer == OPENGL_INVALID_ID)
7258 {
7259 Log(("vmsvga3dDrawPrimitives: create index buffer fDirty=%d size=%x bytes\n", pIndexSurface->fDirty, pIndexSurface->pMipmapLevels[0].cbSurface));
7260#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
7261 pContext = &pState->SharedCtx;
7262 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7263#endif
7264
7265 pState->ext.glGenBuffers(1, &pIndexSurface->oglId.buffer);
7266 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7267
7268 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
7269 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7270
7271 Assert(pIndexSurface->fDirty);
7272
7273 /* @todo rethink usage dynamic/static */
7274 pState->ext.glBufferData(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->pMipmapLevels[0].cbSurface, pIndexSurface->pMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
7275 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7276
7277 pIndexSurface->pMipmapLevels[0].fDirty = false;
7278 pIndexSurface->fDirty = false;
7279
7280 pIndexSurface->flags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
7281
7282#ifdef VMSVGA3D_OGL_WITH_SHARED_CTX
7283 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, OPENGL_INVALID_ID);
7284 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7285
7286 pContext = pState->papContexts[cid];
7287 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7288#endif
7289 }
7290#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
7291 else
7292#endif
7293 {
7294 Assert(pIndexSurface->fDirty == false);
7295
7296 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
7297 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7298 }
7299#ifndef VMSVGA3D_OGL_WITH_SHARED_CTX
7300 LogFlow(("vmsvga3dDrawPrimitives: sid=%x idAssociatedContext %#x -> %#x\n", pIndexSurface->id, pIndexSurface->idAssociatedContext, pContext->id));
7301 pIndexSurface->idAssociatedContext = pContext->id;
7302#endif
7303 }
7304
7305 if (!pIndexSurface)
7306 {
7307 /* Render without an index buffer */
7308 Log(("DrawPrimitive %x cPrimitives=%d cVertices=%d index index bias=%d\n", modeDraw, pRange[iPrimitive].primitiveCount, cVertices, pRange[iPrimitive].indexBias));
7309 glDrawArrays(modeDraw, pRange[iPrimitive].indexBias, cVertices);
7310 }
7311 else
7312 {
7313 Assert(pRange[iPrimitive].indexBias >= 0); /* @todo */
7314 Assert(pRange[iPrimitive].indexWidth == pRange[iPrimitive].indexArray.stride);
7315
7316 /* Render with an index buffer */
7317 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));
7318 if (pRange[iPrimitive].indexBias == 0)
7319 glDrawElements(modeDraw,
7320 cVertices,
7321 (pRange[iPrimitive].indexWidth == sizeof(uint16_t)) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
7322 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset); /* byte offset in indices buffer */
7323 else
7324 pState->ext.glDrawElementsBaseVertex(modeDraw,
7325 cVertices,
7326 (pRange[iPrimitive].indexWidth == sizeof(uint16_t)) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
7327 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
7328 pRange[iPrimitive].indexBias); /* basevertex */
7329
7330 /* Unbind the index buffer after usage. */
7331 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
7332 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7333 }
7334 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7335 }
7336
7337internal_error:
7338
7339 /* Deactivate the vertex declarations. */
7340 iCurrentVertex = 0;
7341 while (iCurrentVertex < numVertexDecls)
7342 {
7343 uint32_t sidVertex = SVGA_ID_INVALID;
7344 uint32_t iVertex;
7345
7346 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
7347 {
7348 if ( sidVertex != SVGA_ID_INVALID
7349 && pVertexDecl[iVertex].array.surfaceId != sidVertex
7350 )
7351 break;
7352 sidVertex = pVertexDecl[iVertex].array.surfaceId;
7353 }
7354
7355 rc = vmsvga3dDrawPrimitivesCleanupVertexDecls(pState, pContext, iCurrentVertex, iVertex - iCurrentVertex, &pVertexDecl[iCurrentVertex]);
7356 AssertRCReturn(rc, rc);
7357
7358 iCurrentVertex = iVertex;
7359 }
7360#ifdef DEBUG
7361 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTexture); i++)
7362 {
7363 if (pContext->aSidActiveTexture[i] != SVGA3D_INVALID_ID)
7364 {
7365 GLint activeTexture = 0;
7366 GLint activeTextureUnit = 0;
7367
7368 glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
7369 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7370 pState->ext.glActiveTexture(GL_TEXTURE0 + i);
7371 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7372
7373 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
7374 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7375 pState->ext.glActiveTexture(activeTextureUnit);
7376 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7377
7378# 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. */
7379 if (pContext->aSidActiveTexture[activeTextureUnit - GL_TEXTURE0] != SVGA3D_INVALID_ID)
7380 {
7381 PVMSVGA3DSURFACE pTexture;
7382 pTexture = &pState->paSurface[pContext->aSidActiveTexture[activeTextureUnit - GL_TEXTURE0]];
7383
7384 AssertMsg(pTexture->oglId.texture == (GLuint)activeTexture, ("%x vs %x unit %d - %d\n", pTexture->oglId.texture, activeTexture, i, activeTextureUnit - GL_TEXTURE0));
7385 }
7386# else
7387 PVMSVGA3DSURFACE pTexture = &pState->paSurface[pContext->aSidActiveTexture[i]];
7388 AssertMsg(pTexture->id == pContext->aSidActiveTexture[i], ("%x vs %x\n", pTexture->id == pContext->aSidActiveTexture[i]));
7389 AssertMsg(pTexture->oglId.texture == (GLuint)activeTexture,
7390 ("%x vs %x unit %d (active unit %d) sid=%x\n", pTexture->oglId.texture, activeTexture, i,
7391 activeTextureUnit - GL_TEXTURE0, pContext->aSidActiveTexture[i]));
7392# endif
7393 }
7394 }
7395#endif
7396
7397#ifdef DEBUG_GFX_WINDOW
7398 if (pContext->aSidActiveTexture[0])
7399 {
7400 SVGA3dCopyRect rect;
7401
7402 rect.srcx = rect.srcy = rect.x = rect.y = 0;
7403 rect.w = 800;
7404 rect.h = 600;
7405 vmsvga3dCommandPresent(pThis, pContext->sidRenderTarget, 0, NULL);
7406 }
7407#endif
7408 return rc;
7409}
7410
7411
7412int vmsvga3dShaderDefine(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type, uint32_t cbData, uint32_t *pShaderData)
7413{
7414 PVMSVGA3DCONTEXT pContext;
7415 PVMSVGA3DSHADER pShader;
7416 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
7417 AssertReturn(pState, VERR_NO_MEMORY);
7418 int rc;
7419
7420 Log(("vmsvga3dShaderDefine cid=%x shid=%x type=%s cbData=%x\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cbData));
7421 Log3(("shader code:\n%.*Rhxd\n", cbData, pShaderData));
7422
7423 if ( cid >= pState->cContexts
7424 || pState->papContexts[cid]->id != cid)
7425 {
7426 Log(("vmsvga3dShaderDefine invalid context id!\n"));
7427 return VERR_INVALID_PARAMETER;
7428 }
7429 pContext = pState->papContexts[cid];
7430 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7431
7432 AssertReturn(shid < SVGA3D_MAX_SHADER_IDS, VERR_INVALID_PARAMETER);
7433 if (type == SVGA3D_SHADERTYPE_VS)
7434 {
7435 if (shid >= pContext->cVertexShaders)
7436 {
7437 void *pvNew = RTMemRealloc(pContext->paVertexShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
7438 AssertReturn(pvNew, VERR_NO_MEMORY);
7439 pContext->paVertexShader = (PVMSVGA3DSHADER)pvNew;
7440 memset(&pContext->paVertexShader[pContext->cVertexShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cVertexShaders));
7441 for (uint32_t i = pContext->cVertexShaders; i < shid + 1; i++)
7442 pContext->paVertexShader[i].id = SVGA3D_INVALID_ID;
7443 pContext->cVertexShaders = shid + 1;
7444 }
7445 /* If one already exists with this id, then destroy it now. */
7446 if (pContext->paVertexShader[shid].id != SVGA3D_INVALID_ID)
7447 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paVertexShader[shid].type);
7448
7449 pShader = &pContext->paVertexShader[shid];
7450 }
7451 else
7452 {
7453 Assert(type == SVGA3D_SHADERTYPE_PS);
7454 if (shid >= pContext->cPixelShaders)
7455 {
7456 void *pvNew = RTMemRealloc(pContext->paPixelShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
7457 AssertReturn(pvNew, VERR_NO_MEMORY);
7458 pContext->paPixelShader = (PVMSVGA3DSHADER)pvNew;
7459 memset(&pContext->paPixelShader[pContext->cPixelShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cPixelShaders));
7460 for (uint32_t i = pContext->cPixelShaders; i < shid + 1; i++)
7461 pContext->paPixelShader[i].id = SVGA3D_INVALID_ID;
7462 pContext->cPixelShaders = shid + 1;
7463 }
7464 /* If one already exists with this id, then destroy it now. */
7465 if (pContext->paPixelShader[shid].id != SVGA3D_INVALID_ID)
7466 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paPixelShader[shid].type);
7467
7468 pShader = &pContext->paPixelShader[shid];
7469 }
7470
7471 memset(pShader, 0, sizeof(*pShader));
7472 pShader->id = shid;
7473 pShader->cid = cid;
7474 pShader->type = type;
7475 pShader->cbData = cbData;
7476 pShader->pShaderProgram = RTMemAllocZ(cbData);
7477 AssertReturn(pShader->pShaderProgram, VERR_NO_MEMORY);
7478 memcpy(pShader->pShaderProgram, pShaderData, cbData);
7479
7480#ifdef DUMP_SHADER_DISASSEMBLY
7481 LPD3DXBUFFER pDisassembly;
7482 HRESULT hr = D3DXDisassembleShader((const DWORD *)pShaderData, FALSE, NULL, &pDisassembly);
7483 if (hr == D3D_OK)
7484 {
7485 Log(("Shader disassembly:\n%s\n", pDisassembly->GetBufferPointer()));
7486 pDisassembly->Release();
7487 }
7488#endif
7489
7490 switch (type)
7491 {
7492 case SVGA3D_SHADERTYPE_VS:
7493 rc = ShaderCreateVertexShader(pContext->pShaderContext, (const uint32_t *)pShaderData, &pShader->u.pVertexShader);
7494 break;
7495
7496 case SVGA3D_SHADERTYPE_PS:
7497 rc = ShaderCreatePixelShader(pContext->pShaderContext, (const uint32_t *)pShaderData, &pShader->u.pPixelShader);
7498 break;
7499
7500 default:
7501 AssertFailedReturn(VERR_INVALID_PARAMETER);
7502 }
7503 if (rc != VINF_SUCCESS)
7504 {
7505 RTMemFree(pShader->pShaderProgram);
7506 memset(pShader, 0, sizeof(*pShader));
7507 pShader->id = SVGA3D_INVALID_ID;
7508 }
7509
7510 return rc;
7511}
7512
7513int vmsvga3dShaderDestroy(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type)
7514{
7515 PVMSVGA3DCONTEXT pContext;
7516 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
7517 AssertReturn(pState, VERR_NO_MEMORY);
7518 PVMSVGA3DSHADER pShader = NULL;
7519 int rc;
7520
7521 Log(("vmsvga3dShaderDestroy cid=%x shid=%x type=%s\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL"));
7522
7523 if ( cid >= pState->cContexts
7524 || pState->papContexts[cid]->id != cid)
7525 {
7526 Log(("vmsvga3dShaderDestroy invalid context id!\n"));
7527 return VERR_INVALID_PARAMETER;
7528 }
7529 pContext = pState->papContexts[cid];
7530 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7531
7532 if (type == SVGA3D_SHADERTYPE_VS)
7533 {
7534 if ( shid < pContext->cVertexShaders
7535 && pContext->paVertexShader[shid].id == shid)
7536 {
7537 pShader = &pContext->paVertexShader[shid];
7538 rc = ShaderDestroyVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
7539 AssertRC(rc);
7540 }
7541 }
7542 else
7543 {
7544 Assert(type == SVGA3D_SHADERTYPE_PS);
7545 if ( shid < pContext->cPixelShaders
7546 && pContext->paPixelShader[shid].id == shid)
7547 {
7548 pShader = &pContext->paPixelShader[shid];
7549 rc = ShaderDestroyPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
7550 AssertRC(rc);
7551 }
7552 }
7553
7554 if (pShader)
7555 {
7556 if (pShader->pShaderProgram)
7557 RTMemFree(pShader->pShaderProgram);
7558 memset(pShader, 0, sizeof(*pShader));
7559 pShader->id = SVGA3D_INVALID_ID;
7560 }
7561 else
7562 AssertFailedReturn(VERR_INVALID_PARAMETER);
7563
7564 return VINF_SUCCESS;
7565}
7566
7567int vmsvga3dShaderSet(PVGASTATE pThis, uint32_t cid, SVGA3dShaderType type, uint32_t shid)
7568{
7569 PVMSVGA3DCONTEXT pContext;
7570 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
7571 AssertReturn(pState, VERR_NO_MEMORY);
7572 int rc;
7573
7574 Log(("vmsvga3dShaderSet cid=%x type=%s shid=%d\n", cid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", shid));
7575
7576 if ( cid >= pState->cContexts
7577 || pState->papContexts[cid]->id != cid)
7578 {
7579 Log(("vmsvga3dShaderSet invalid context id!\n"));
7580 return VERR_INVALID_PARAMETER;
7581 }
7582 pContext = pState->papContexts[cid];
7583 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7584
7585 if (type == SVGA3D_SHADERTYPE_VS)
7586 {
7587 /* Save for vm state save/restore. */
7588 pContext->state.shidVertex = shid;
7589 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VERTEXSHADER;
7590
7591 if ( shid < pContext->cVertexShaders
7592 && pContext->paVertexShader[shid].id == shid)
7593 {
7594 PVMSVGA3DSHADER pShader = &pContext->paVertexShader[shid];
7595 Assert(type == pShader->type);
7596
7597 rc = ShaderSetVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
7598 AssertRCReturn(rc, rc);
7599 }
7600 else
7601 if (shid == SVGA_ID_INVALID)
7602 {
7603 /* Unselect shader. */
7604 rc = ShaderSetVertexShader(pContext->pShaderContext, NULL);
7605 AssertRCReturn(rc, rc);
7606 }
7607 else
7608 AssertFailedReturn(VERR_INVALID_PARAMETER);
7609 }
7610 else
7611 {
7612 /* Save for vm state save/restore. */
7613 pContext->state.shidPixel = shid;
7614 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_PIXELSHADER;
7615
7616 Assert(type == SVGA3D_SHADERTYPE_PS);
7617 if ( shid < pContext->cPixelShaders
7618 && pContext->paPixelShader[shid].id == shid)
7619 {
7620 PVMSVGA3DSHADER pShader = &pContext->paPixelShader[shid];
7621 Assert(type == pShader->type);
7622
7623 rc = ShaderSetPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
7624 AssertRCReturn(rc, rc);
7625 }
7626 else
7627 if (shid == SVGA_ID_INVALID)
7628 {
7629 /* Unselect shader. */
7630 rc = ShaderSetPixelShader(pContext->pShaderContext, NULL);
7631 AssertRCReturn(rc, rc);
7632 }
7633 else
7634 AssertFailedReturn(VERR_INVALID_PARAMETER);
7635 }
7636
7637 return VINF_SUCCESS;
7638}
7639
7640int vmsvga3dShaderSetConst(PVGASTATE pThis, uint32_t cid, uint32_t reg, SVGA3dShaderType type, SVGA3dShaderConstType ctype, uint32_t cRegisters, uint32_t *pValues)
7641{
7642 PVMSVGA3DCONTEXT pContext;
7643 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
7644 AssertReturn(pState, VERR_NO_MEMORY);
7645 int rc;
7646
7647 Log(("vmsvga3dShaderSetConst cid=%x reg=%x type=%s cregs=%d ctype=%x\n", cid, reg, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cRegisters, ctype));
7648
7649 if ( cid >= pState->cContexts
7650 || pState->papContexts[cid]->id != cid)
7651 {
7652 Log(("vmsvga3dShaderSetConst invalid context id!\n"));
7653 return VERR_INVALID_PARAMETER;
7654 }
7655 pContext = pState->papContexts[cid];
7656 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7657
7658 for (uint32_t i = 0; i < cRegisters; i++)
7659 {
7660#ifdef LOG_ENABLED
7661 switch (ctype)
7662 {
7663 case SVGA3D_CONST_TYPE_FLOAT:
7664 {
7665 float *pValuesF = (float *)pValues;
7666 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)));
7667 break;
7668 }
7669
7670 case SVGA3D_CONST_TYPE_INT:
7671 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]));
7672 break;
7673
7674 case SVGA3D_CONST_TYPE_BOOL:
7675 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]));
7676 break;
7677 }
7678#endif
7679 vmsvga3dSaveShaderConst(pContext, reg + i, type, ctype, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]);
7680 }
7681
7682 switch (type)
7683 {
7684 case SVGA3D_SHADERTYPE_VS:
7685 switch (ctype)
7686 {
7687 case SVGA3D_CONST_TYPE_FLOAT:
7688 rc = ShaderSetVertexShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
7689 break;
7690
7691 case SVGA3D_CONST_TYPE_INT:
7692 rc = ShaderSetVertexShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
7693 break;
7694
7695 case SVGA3D_CONST_TYPE_BOOL:
7696 rc = ShaderSetVertexShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
7697 break;
7698
7699 default:
7700 AssertFailedReturn(VERR_INVALID_PARAMETER);
7701 }
7702 AssertRCReturn(rc, rc);
7703 break;
7704
7705 case SVGA3D_SHADERTYPE_PS:
7706 switch (ctype)
7707 {
7708 case SVGA3D_CONST_TYPE_FLOAT:
7709 rc = ShaderSetPixelShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
7710 break;
7711
7712 case SVGA3D_CONST_TYPE_INT:
7713 rc = ShaderSetPixelShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
7714 break;
7715
7716 case SVGA3D_CONST_TYPE_BOOL:
7717 rc = ShaderSetPixelShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
7718 break;
7719
7720 default:
7721 AssertFailedReturn(VERR_INVALID_PARAMETER);
7722 }
7723 AssertRCReturn(rc, rc);
7724 break;
7725
7726 default:
7727 AssertFailedReturn(VERR_INVALID_PARAMETER);
7728 }
7729
7730 return VINF_SUCCESS;
7731}
7732
7733
7734int vmsvga3dQueryBegin(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type)
7735{
7736 AssertFailed();
7737 return VERR_NOT_IMPLEMENTED;
7738}
7739
7740int vmsvga3dQueryEnd(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type, SVGAGuestPtr guestResult)
7741{
7742 AssertFailed();
7743 return VERR_NOT_IMPLEMENTED;
7744}
7745
7746int vmsvga3dQueryWait(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type, SVGAGuestPtr guestResult)
7747{
7748 AssertFailed();
7749 return VERR_NOT_IMPLEMENTED;
7750}
7751
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