VirtualBox

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

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

vmsvga3d/darwin/ogl: Resize the shared context, save and restore the shared context. Don't try too hard to do the NSOpenGLContext::setView thing if it doesn't want to work, it's pretty noisy on 10.8.5/nvidia. Do the vmsvga3dPowerOn on EMT, not the FIFO when restoring state (darwin only atm).

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