VirtualBox

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

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

VMSVGA3d: Changed the surface arrays into pointer arrays to reduce the copying requried when growing (there maybe lots of them and growing by 1 is tedious). This also means the surface pointers remain valid across growths.

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