VirtualBox

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

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

DevVGA-SVGA3d-ogl.cpp: Texture (surface) load and saving (to memory) fixes. Fixed crash when loading a 1 pixel wide texture. Fixed crashes loading and saving SVGA3D_A1R5G5B5 formatted textures.

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