VirtualBox

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

Last change on this file since 57034 was 57010, checked in by vboxsync, 9 years ago

vmsvga/ogl: Must destroy shared context on reset and termination, otherwise OpenGL32.dll may take a while to clean up on process termination (at least with ATI drivers).

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