VirtualBox

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

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

DevVGA=SVGA3d-ogl.cpp: AssertReturn -> AssertReturnStmt and free double buffer.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette