VirtualBox

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

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

vmsvga3d/mac: Fixed problem with missing back/front buffers. Stop use renderspu_cocoa_helper.m and use DevVGA-SVGA3d-cocoa.m, only now its even more stripped down that earlier.

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