VirtualBox

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

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

DevVGA-SVGA3d-ogl.cpp: LogFlow idAssociatedContext changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 275.1 KB
Line 
1/* $Id: DevVGA-SVGA3d-ogl.cpp 54626 2015-03-04 14:40:39Z 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
1724/**
1725 * Worker for vmsvga3dQueryCaps that figures out supported operations for a
1726 * given surface format capability.
1727 *
1728 * @returns Supported/indented operations (SVGA3DFORMAT_OP_XXX).
1729 * @param pState3D The 3D state.
1730 * @param idx3dCaps The SVGA3D_CAPS_XXX value of the surface format.
1731 *
1732 * @remarks See fromat_cap_table in svga_format.c (mesa/gallium) for a reference
1733 * of implicit guest expectations:
1734 * http://cgit.freedesktop.org/mesa/mesa/tree/src/gallium/drivers/svga/svga_format.c
1735 */
1736static uint32_t vmsvga3dGetSurfaceFormatSupport(PVMSVGA3DSTATE pState3D, uint32_t idx3dCaps)
1737{
1738 uint32_t result = 0;
1739
1740 /* @todo missing:
1741 *
1742 * SVGA3DFORMAT_OP_PIXELSIZE
1743 */
1744
1745 switch (idx3dCaps)
1746 {
1747 case SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8:
1748 case SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5:
1749 case SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
1750 result |= SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB
1751 | SVGA3DFORMAT_OP_CONVERT_TO_ARGB
1752 | SVGA3DFORMAT_OP_DISPLAYMODE /* Should not be set for alpha formats. */
1753 | SVGA3DFORMAT_OP_3DACCELERATION; /* implies OP_DISPLAYMODE */
1754 break;
1755
1756 case SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8:
1757 case SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10:
1758 case SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5:
1759 case SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4:
1760 result |= SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB
1761 | SVGA3DFORMAT_OP_CONVERT_TO_ARGB
1762 | SVGA3DFORMAT_OP_SAME_FORMAT_UP_TO_ALPHA_RENDERTARGET;
1763 break;
1764 }
1765
1766 /* @todo check hardware caps! */
1767 switch (idx3dCaps)
1768 {
1769 case SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8:
1770 case SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8:
1771 case SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10:
1772 case SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5:
1773 case SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5:
1774 case SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4:
1775 case SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
1776 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE16:
1777 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8_ALPHA8:
1778 case SVGA3D_DEVCAP_SURFACEFMT_ALPHA8:
1779 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8:
1780 result |= SVGA3DFORMAT_OP_TEXTURE
1781 | SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
1782 | SVGA3DFORMAT_OP_OFFSCREENPLAIN
1783 | SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET
1784 | SVGA3DFORMAT_OP_VOLUMETEXTURE
1785 | SVGA3DFORMAT_OP_CUBETEXTURE
1786 | SVGA3DFORMAT_OP_SRGBREAD
1787 | SVGA3DFORMAT_OP_SRGBWRITE;
1788 break;
1789
1790 case SVGA3D_DEVCAP_SURFACEFMT_Z_D16:
1791 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8:
1792 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24X8:
1793 case SVGA3D_DEVCAP_SURFACEFMT_Z_DF16:
1794 case SVGA3D_DEVCAP_SURFACEFMT_Z_DF24:
1795 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8_INT:
1796 result |= SVGA3DFORMAT_OP_ZSTENCIL
1797 | SVGA3DFORMAT_OP_ZSTENCIL_WITH_ARBITRARY_COLOR_DEPTH
1798 | SVGA3DFORMAT_OP_TEXTURE /* Necessary for Ubuntu Unity */;
1799 break;
1800
1801 case SVGA3D_DEVCAP_SURFACEFMT_DXT1:
1802 case SVGA3D_DEVCAP_SURFACEFMT_DXT3:
1803 case SVGA3D_DEVCAP_SURFACEFMT_DXT5:
1804 result |= SVGA3DFORMAT_OP_TEXTURE
1805 | SVGA3DFORMAT_OP_VOLUMETEXTURE
1806 | SVGA3DFORMAT_OP_CUBETEXTURE
1807 | SVGA3DFORMAT_OP_SRGBREAD;
1808 break;
1809
1810 case SVGA3D_DEVCAP_SURFACEFMT_BUMPX8L8V8U8:
1811 case SVGA3D_DEVCAP_SURFACEFMT_A2W10V10U10:
1812 case SVGA3D_DEVCAP_SURFACEFMT_BUMPU8V8:
1813 case SVGA3D_DEVCAP_SURFACEFMT_Q8W8V8U8:
1814 case SVGA3D_DEVCAP_SURFACEFMT_CxV8U8:
1815 break;
1816
1817 case SVGA3D_DEVCAP_SURFACEFMT_R_S10E5:
1818 case SVGA3D_DEVCAP_SURFACEFMT_R_S23E8:
1819 case SVGA3D_DEVCAP_SURFACEFMT_RG_S10E5:
1820 case SVGA3D_DEVCAP_SURFACEFMT_RG_S23E8:
1821 case SVGA3D_DEVCAP_SURFACEFMT_ARGB_S10E5:
1822 case SVGA3D_DEVCAP_SURFACEFMT_ARGB_S23E8:
1823 break;
1824
1825 case SVGA3D_DEVCAP_SURFACEFMT_V16U16:
1826 case SVGA3D_DEVCAP_SURFACEFMT_G16R16:
1827 case SVGA3D_DEVCAP_SURFACEFMT_A16B16G16R16:
1828
1829 case SVGA3D_DEVCAP_SURFACEFMT_UYVY:
1830 case SVGA3D_DEVCAP_SURFACEFMT_YUY2:
1831 case SVGA3D_DEVCAP_SURFACEFMT_NV12:
1832 case SVGA3D_DEVCAP_SURFACEFMT_AYUV:
1833 break;
1834 }
1835 Log(("CAPS: %s =\n%s\n", vmsvga3dGetCapString(idx3dCaps), vmsvga3dGet3dFormatString(result)));
1836
1837 return result;
1838}
1839
1840static uint32_t vmsvga3dGetDepthFormatSupport(PVMSVGA3DSTATE pState3D, uint32_t idx3dCaps)
1841{
1842 uint32_t result = 0;
1843
1844 /* @todo test this somehow */
1845 result = SVGA3DFORMAT_OP_ZSTENCIL | SVGA3DFORMAT_OP_ZSTENCIL_WITH_ARBITRARY_COLOR_DEPTH;
1846
1847 Log(("CAPS: %s =\n%s\n", vmsvga3dGetCapString(idx3dCaps), vmsvga3dGet3dFormatString(result)));
1848 return result;
1849}
1850
1851
1852int vmsvga3dQueryCaps(PVGASTATE pThis, uint32_t idx3dCaps, uint32_t *pu32Val)
1853{
1854 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
1855 AssertReturn(pState, VERR_NO_MEMORY);
1856 int rc = VINF_SUCCESS;
1857
1858 *pu32Val = 0;
1859
1860 /*
1861 * The capabilities access by current (2015-03-01) linux sources (gallium,
1862 * vmwgfx, xorg-video-vmware) are annotated, caps without xref annotations
1863 * aren't access.
1864 */
1865
1866 switch (idx3dCaps)
1867 {
1868 /* Linux: vmwgfx_fifo.c in kmod; only used with SVGA_CAP_GBOBJECTS. */
1869 case SVGA3D_DEVCAP_3D:
1870 *pu32Val = 1; /* boolean? */
1871 break;
1872
1873 case SVGA3D_DEVCAP_MAX_LIGHTS:
1874 *pu32Val = pState->caps.maxActiveLights;
1875 break;
1876
1877 case SVGA3D_DEVCAP_MAX_TEXTURES:
1878 *pu32Val = pState->caps.maxTextures;
1879 break;
1880
1881 case SVGA3D_DEVCAP_MAX_CLIP_PLANES:
1882 *pu32Val = pState->caps.maxClipDistances;
1883 break;
1884
1885 /* Linux: svga_screen.c in gallium; 3.0 or later required. */
1886 case SVGA3D_DEVCAP_VERTEX_SHADER_VERSION:
1887 *pu32Val = pState->caps.vertexShaderVersion;
1888 break;
1889
1890 case SVGA3D_DEVCAP_VERTEX_SHADER:
1891 /* boolean? */
1892 *pu32Val = (pState->caps.vertexShaderVersion != SVGA3DVSVERSION_NONE);
1893 break;
1894
1895 /* Linux: svga_screen.c in gallium; 3.0 or later required. */
1896 case SVGA3D_DEVCAP_FRAGMENT_SHADER_VERSION:
1897 *pu32Val = pState->caps.fragmentShaderVersion;
1898 break;
1899
1900 case SVGA3D_DEVCAP_FRAGMENT_SHADER:
1901 /* boolean? */
1902 *pu32Val = (pState->caps.fragmentShaderVersion != SVGA3DPSVERSION_NONE);
1903 break;
1904
1905 case SVGA3D_DEVCAP_S23E8_TEXTURES:
1906 case SVGA3D_DEVCAP_S10E5_TEXTURES:
1907 /* Must be obsolete by now; surface format caps specify the same thing. */
1908 rc = VERR_INVALID_PARAMETER;
1909 break;
1910
1911 case SVGA3D_DEVCAP_MAX_FIXED_VERTEXBLEND:
1912 break;
1913
1914 /*
1915 * 2. The BUFFER_FORMAT capabilities are deprecated, and they always
1916 * return TRUE. Even on physical hardware that does not support
1917 * these formats natively, the SVGA3D device will provide an emulation
1918 * which should be invisible to the guest OS.
1919 */
1920 case SVGA3D_DEVCAP_D16_BUFFER_FORMAT:
1921 case SVGA3D_DEVCAP_D24S8_BUFFER_FORMAT:
1922 case SVGA3D_DEVCAP_D24X8_BUFFER_FORMAT:
1923 *pu32Val = 1;
1924 break;
1925
1926 case SVGA3D_DEVCAP_QUERY_TYPES:
1927 break;
1928
1929 case SVGA3D_DEVCAP_TEXTURE_GRADIENT_SAMPLING:
1930 break;
1931
1932 /* Linux: svga_screen.c in gallium; capped at 80.0, default 1.0. */
1933 case SVGA3D_DEVCAP_MAX_POINT_SIZE:
1934 AssertCompile(sizeof(uint32_t) == sizeof(float));
1935 *(float *)pu32Val = pState->caps.flPointSize[1];
1936 break;
1937
1938 case SVGA3D_DEVCAP_MAX_SHADER_TEXTURES:
1939 /* @todo ?? */
1940 rc = VERR_INVALID_PARAMETER;
1941 break;
1942
1943 /* Linux: svga_screen.c in gallium (for PIPE_CAP_MAX_TEXTURE_2D_LEVELS); have default if missing. */
1944 case SVGA3D_DEVCAP_MAX_TEXTURE_WIDTH:
1945 case SVGA3D_DEVCAP_MAX_TEXTURE_HEIGHT:
1946 *pu32Val = pState->caps.maxRectangleTextureSize;
1947 break;
1948
1949 /* Linux: svga_screen.c in gallium (for PIPE_CAP_MAX_TEXTURE_3D_LEVELS); have default if missing. */
1950 case SVGA3D_DEVCAP_MAX_VOLUME_EXTENT:
1951 //*pu32Val = pCaps->MaxVolumeExtent;
1952 break;
1953
1954 case SVGA3D_DEVCAP_MAX_TEXTURE_REPEAT:
1955 *pu32Val = 32768; /* hardcoded in Wine */
1956 break;
1957
1958 case SVGA3D_DEVCAP_MAX_TEXTURE_ASPECT_RATIO:
1959 //*pu32Val = pCaps->MaxTextureAspectRatio;
1960 break;
1961
1962 /* Linux: svga_screen.c in gallium (for PIPE_CAPF_MAX_TEXTURE_ANISOTROPY); defaults to 4.0. */
1963 case SVGA3D_DEVCAP_MAX_TEXTURE_ANISOTROPY:
1964 *pu32Val = pState->caps.maxTextureAnisotropy;
1965 break;
1966
1967 case SVGA3D_DEVCAP_MAX_PRIMITIVE_COUNT:
1968 case SVGA3D_DEVCAP_MAX_VERTEX_INDEX:
1969 *pu32Val = 0xFFFFF; /* hardcoded in Wine */
1970 break;
1971
1972 /* Linux: svga_screen.c in gallium (for PIPE_SHADER_VERTEX/PIPE_SHADER_CAP_MAX_INSTRUCTIONS); defaults to 512. */
1973 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_INSTRUCTIONS:
1974 *pu32Val = pState->caps.maxVertexShaderInstructions;
1975 break;
1976
1977 case SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_INSTRUCTIONS:
1978 *pu32Val = pState->caps.maxFragmentShaderInstructions;
1979 break;
1980
1981 /* Linux: svga_screen.c in gallium (for PIPE_SHADER_VERTEX/PIPE_SHADER_CAP_MAX_TEMPS); defaults to 32. */
1982 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEMPS:
1983 *pu32Val = pState->caps.maxVertexShaderTemps;
1984 break;
1985
1986 /* Linux: svga_screen.c in gallium (for PIPE_SHADER_FRAGMENT/PIPE_SHADER_CAP_MAX_TEMPS); defaults to 32. */
1987 case SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_TEMPS:
1988 *pu32Val = pState->caps.maxFragmentShaderTemps;
1989 break;
1990
1991 case SVGA3D_DEVCAP_TEXTURE_OPS:
1992 break;
1993
1994 case SVGA3D_DEVCAP_MULTISAMPLE_NONMASKABLESAMPLES:
1995 break;
1996
1997 case SVGA3D_DEVCAP_MULTISAMPLE_MASKABLESAMPLES:
1998 break;
1999
2000 case SVGA3D_DEVCAP_ALPHATOCOVERAGE:
2001 break;
2002
2003 case SVGA3D_DEVCAP_SUPERSAMPLE:
2004 break;
2005
2006 case SVGA3D_DEVCAP_AUTOGENMIPMAPS:
2007 //*pu32Val = !!(pCaps->Caps2 & D3DCAPS2_CANAUTOGENMIPMAP);
2008 break;
2009
2010 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEXTURES:
2011 break;
2012
2013 case SVGA3D_DEVCAP_MAX_RENDER_TARGETS: /* @todo same thing? */
2014 case SVGA3D_DEVCAP_MAX_SIMULTANEOUS_RENDER_TARGETS:
2015 *pu32Val = pState->caps.maxColorAttachments;
2016 break;
2017
2018 /*
2019 * This is the maximum number of SVGA context IDs that the guest
2020 * can define using SVGA_3D_CMD_CONTEXT_DEFINE.
2021 */
2022 case SVGA3D_DEVCAP_MAX_CONTEXT_IDS:
2023 *pu32Val = SVGA3D_MAX_CONTEXT_IDS;
2024 break;
2025
2026 /*
2027 * This is the maximum number of SVGA surface IDs that the guest
2028 * can define using SVGA_3D_CMD_SURFACE_DEFINE*.
2029 */
2030 case SVGA3D_DEVCAP_MAX_SURFACE_IDS:
2031 *pu32Val = SVGA3D_MAX_SURFACE_IDS;
2032 break;
2033
2034#if 0 /* Appeared more recently, not yet implemented. */
2035 /* Linux: svga_screen.c in gallium; defaults to FALSE. */
2036 case SVGA3D_DEVCAP_LINE_AA:
2037 break;
2038 /* Linux: svga_screen.c in gallium; defaults to FALSE. */
2039 case SVGA3D_DEVCAP_LINE_STIPPLE:
2040 break;
2041 /* Linux: svga_screen.c in gallium; defaults to 1.0. */
2042 case SVGA3D_DEVCAP_MAX_LINE_WIDTH:
2043 break;
2044 /* Linux: svga_screen.c in gallium; defaults to 1.0. */
2045 case SVGA3D_DEVCAP_MAX_AA_LINE_WIDTH:
2046 break;
2047#endif
2048
2049 /*
2050 * Supported surface formats.
2051 * Linux: svga_format.c in gallium, format_cap_table defines implicit expectations.
2052 */
2053 case SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8:
2054 case SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8:
2055 case SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10:
2056 case SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5:
2057 case SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5:
2058 case SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4:
2059 case SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
2060 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE16:
2061 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8_ALPHA8:
2062 case SVGA3D_DEVCAP_SURFACEFMT_ALPHA8:
2063 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8:
2064 case SVGA3D_DEVCAP_SURFACEFMT_Z_D16:
2065 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8:
2066 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24X8:
2067 case SVGA3D_DEVCAP_SURFACEFMT_Z_DF16:
2068 case SVGA3D_DEVCAP_SURFACEFMT_Z_DF24:
2069 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8_INT:
2070 case SVGA3D_DEVCAP_SURFACEFMT_DXT1:
2071 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps);
2072 break;
2073
2074 case SVGA3D_DEVCAP_SURFACEFMT_DXT2:
2075 case SVGA3D_DEVCAP_SURFACEFMT_DXT4:
2076 *pu32Val = 0; /* apparently not supported in OpenGL */
2077 break;
2078
2079 case SVGA3D_DEVCAP_SURFACEFMT_DXT3:
2080 case SVGA3D_DEVCAP_SURFACEFMT_DXT5:
2081 case SVGA3D_DEVCAP_SURFACEFMT_BUMPX8L8V8U8:
2082 case SVGA3D_DEVCAP_SURFACEFMT_A2W10V10U10:
2083 case SVGA3D_DEVCAP_SURFACEFMT_BUMPU8V8:
2084 case SVGA3D_DEVCAP_SURFACEFMT_Q8W8V8U8:
2085 case SVGA3D_DEVCAP_SURFACEFMT_CxV8U8:
2086 case SVGA3D_DEVCAP_SURFACEFMT_R_S10E5:
2087 case SVGA3D_DEVCAP_SURFACEFMT_R_S23E8:
2088 case SVGA3D_DEVCAP_SURFACEFMT_RG_S10E5:
2089 case SVGA3D_DEVCAP_SURFACEFMT_RG_S23E8:
2090 case SVGA3D_DEVCAP_SURFACEFMT_ARGB_S10E5:
2091 case SVGA3D_DEVCAP_SURFACEFMT_ARGB_S23E8:
2092 case SVGA3D_DEVCAP_SURFACEFMT_V16U16:
2093 case SVGA3D_DEVCAP_SURFACEFMT_G16R16:
2094 case SVGA3D_DEVCAP_SURFACEFMT_A16B16G16R16:
2095 case SVGA3D_DEVCAP_SURFACEFMT_UYVY:
2096 case SVGA3D_DEVCAP_SURFACEFMT_YUY2:
2097 case SVGA3D_DEVCAP_SURFACEFMT_NV12:
2098 case SVGA3D_DEVCAP_SURFACEFMT_AYUV:
2099 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps);
2100 break;
2101
2102 /* Linux: Not referenced in current sources. */
2103 case SVGA3D_DEVCAP_SURFACEFMT_BC4_UNORM:
2104 case SVGA3D_DEVCAP_SURFACEFMT_BC5_UNORM:
2105 Log(("CAPS: Unknown CAP %s\n", vmsvga3dGetCapString(idx3dCaps)));
2106 rc = VERR_INVALID_PARAMETER;
2107 *pu32Val = 0;
2108 break;
2109
2110 default:
2111 Log(("CAPS: Unexpected CAP %d\n", idx3dCaps));
2112 rc = VERR_INVALID_PARAMETER;
2113 break;
2114 }
2115
2116 Log(("CAPS: %s - %x\n", vmsvga3dGetCapString(idx3dCaps), *pu32Val));
2117 return rc;
2118}
2119
2120/**
2121 * Convert SVGA format value to its OpenGL equivalent
2122 */
2123static void vmsvga3dSurfaceFormat2OGL(PVMSVGA3DSURFACE pSurface, SVGA3dSurfaceFormat format)
2124{
2125 switch (format)
2126 {
2127 case SVGA3D_X8R8G8B8:
2128 pSurface->internalFormatGL = GL_RGB8;
2129 pSurface->formatGL = GL_BGRA;
2130 pSurface->typeGL = GL_UNSIGNED_INT_8_8_8_8_REV;
2131 break;
2132 case SVGA3D_A8R8G8B8:
2133 pSurface->internalFormatGL = GL_RGBA8;
2134 pSurface->formatGL = GL_BGRA;
2135 pSurface->typeGL = GL_UNSIGNED_INT_8_8_8_8_REV;
2136 break;
2137 case SVGA3D_R5G6B5:
2138 pSurface->internalFormatGL = GL_RGB;
2139 pSurface->formatGL = GL_RGB;
2140 pSurface->typeGL = GL_UNSIGNED_SHORT_5_6_5;
2141 break;
2142 case SVGA3D_X1R5G5B5:
2143 pSurface->internalFormatGL = GL_RGB;
2144 pSurface->formatGL = GL_RGB;
2145 pSurface->typeGL = GL_UNSIGNED_SHORT_1_5_5_5_REV;
2146 break;
2147 case SVGA3D_A1R5G5B5:
2148 pSurface->internalFormatGL = GL_RGBA;
2149 pSurface->formatGL = GL_RGB;
2150 pSurface->typeGL = GL_UNSIGNED_SHORT_1_5_5_5_REV;
2151 break;
2152 case SVGA3D_A4R4G4B4:
2153 pSurface->internalFormatGL = GL_RGBA;
2154 pSurface->formatGL = GL_RGBA;
2155 pSurface->typeGL = GL_UNSIGNED_SHORT_4_4_4_4;
2156 break;
2157
2158 case SVGA3D_Z_D32:
2159 pSurface->internalFormatGL = GL_DEPTH_COMPONENT32;
2160 pSurface->formatGL = GL_DEPTH_COMPONENT;
2161 pSurface->typeGL = GL_UNSIGNED_INT;
2162 break;
2163 case SVGA3D_Z_D16:
2164 pSurface->internalFormatGL = GL_DEPTH_COMPONENT16;
2165 pSurface->formatGL = GL_DEPTH_COMPONENT;
2166 pSurface->typeGL = GL_UNSIGNED_SHORT;
2167 break;
2168 case SVGA3D_Z_D24S8:
2169 pSurface->internalFormatGL = GL_DEPTH24_STENCIL8;
2170 pSurface->formatGL = GL_DEPTH_STENCIL;
2171 pSurface->typeGL = GL_UNSIGNED_INT;
2172 break;
2173 case SVGA3D_Z_D15S1:
2174 pSurface->internalFormatGL = GL_DEPTH_COMPONENT16; /* @todo ??? */
2175 pSurface->formatGL = GL_DEPTH_STENCIL;
2176 pSurface->typeGL = GL_UNSIGNED_SHORT;
2177 break;
2178 case SVGA3D_Z_D24X8:
2179 pSurface->internalFormatGL = GL_DEPTH_COMPONENT24;
2180 pSurface->formatGL = GL_DEPTH_COMPONENT;
2181 pSurface->typeGL = GL_UNSIGNED_INT;
2182 break;
2183
2184 /* Advanced D3D9 depth formats. */
2185 case SVGA3D_Z_DF16:
2186 pSurface->internalFormatGL = GL_DEPTH_COMPONENT16;
2187 pSurface->formatGL = GL_DEPTH_COMPONENT;
2188 pSurface->typeGL = GL_FLOAT;
2189 break;
2190
2191 case SVGA3D_Z_DF24:
2192 pSurface->internalFormatGL = GL_DEPTH_COMPONENT24;
2193 pSurface->formatGL = GL_DEPTH_COMPONENT;
2194 pSurface->typeGL = GL_FLOAT; /* ??? */
2195 break;
2196
2197 case SVGA3D_Z_D24S8_INT:
2198 pSurface->internalFormatGL = GL_DEPTH24_STENCIL8;
2199 pSurface->formatGL = GL_DEPTH_STENCIL;
2200 pSurface->typeGL = GL_INT; /* ??? */
2201 break;
2202
2203 case SVGA3D_DXT1:
2204 pSurface->internalFormatGL = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
2205 pSurface->formatGL = GL_RGBA_S3TC; /* ??? */
2206 pSurface->typeGL = GL_UNSIGNED_INT; /* ??? */
2207 break;
2208
2209 case SVGA3D_DXT3:
2210 pSurface->internalFormatGL = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
2211 pSurface->formatGL = GL_RGBA_S3TC; /* ??? */
2212 pSurface->typeGL = GL_UNSIGNED_INT; /* ??? */
2213 break;
2214
2215 case SVGA3D_DXT5:
2216 pSurface->internalFormatGL = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
2217 pSurface->formatGL = GL_RGBA_S3TC;
2218 pSurface->typeGL = GL_UNSIGNED_INT;
2219 break;
2220
2221 case SVGA3D_LUMINANCE8:
2222 pSurface->internalFormatGL = GL_LUMINANCE8_EXT;
2223 pSurface->formatGL = GL_LUMINANCE;
2224 pSurface->typeGL = GL_UNSIGNED_BYTE;
2225 break;
2226
2227 case SVGA3D_LUMINANCE16:
2228 pSurface->internalFormatGL = GL_LUMINANCE16_EXT;
2229 pSurface->formatGL = GL_LUMINANCE;
2230 pSurface->typeGL = GL_UNSIGNED_SHORT;
2231 break;
2232
2233 case SVGA3D_LUMINANCE4_ALPHA4:
2234 pSurface->internalFormatGL = GL_LUMINANCE4_ALPHA4_EXT;
2235 pSurface->formatGL = GL_LUMINANCE_ALPHA;
2236 pSurface->typeGL = GL_UNSIGNED_BYTE;
2237 break;
2238
2239 case SVGA3D_LUMINANCE8_ALPHA8:
2240 pSurface->internalFormatGL = GL_LUMINANCE8_ALPHA8_EXT;
2241 pSurface->formatGL = GL_LUMINANCE_ALPHA;
2242 pSurface->typeGL = GL_UNSIGNED_BYTE; /* unsigned_short causes issues even though this type should be 16-bit */
2243 break;
2244
2245 case SVGA3D_ALPHA8:
2246 pSurface->internalFormatGL = GL_ALPHA8_EXT;
2247 pSurface->formatGL = GL_ALPHA;
2248 pSurface->typeGL = GL_UNSIGNED_BYTE;
2249 break;
2250
2251#if 0
2252
2253 /* Bump-map formats */
2254 case SVGA3D_BUMPU8V8:
2255 return D3DFMT_V8U8;
2256 case SVGA3D_BUMPL6V5U5:
2257 return D3DFMT_L6V5U5;
2258 case SVGA3D_BUMPX8L8V8U8:
2259 return D3DFMT_X8L8V8U8;
2260 case SVGA3D_BUMPL8V8U8:
2261 /* No corresponding D3D9 equivalent. */
2262 AssertFailedReturn(D3DFMT_UNKNOWN);
2263 /* signed bump-map formats */
2264 case SVGA3D_V8U8:
2265 return D3DFMT_V8U8;
2266 case SVGA3D_Q8W8V8U8:
2267 return D3DFMT_Q8W8V8U8;
2268 case SVGA3D_CxV8U8:
2269 return D3DFMT_CxV8U8;
2270 /* mixed bump-map formats */
2271 case SVGA3D_X8L8V8U8:
2272 return D3DFMT_X8L8V8U8;
2273 case SVGA3D_A2W10V10U10:
2274 return D3DFMT_A2W10V10U10;
2275#endif
2276
2277 case SVGA3D_ARGB_S10E5: /* 16-bit floating-point ARGB */
2278 pSurface->internalFormatGL = GL_RGBA16F;
2279 pSurface->formatGL = GL_RGBA;
2280 pSurface->typeGL = GL_FLOAT;
2281 break;
2282
2283 case SVGA3D_ARGB_S23E8: /* 32-bit floating-point ARGB */
2284 pSurface->internalFormatGL = GL_RGBA32F;
2285 pSurface->formatGL = GL_RGBA;
2286 pSurface->typeGL = GL_FLOAT; /* ?? */
2287 break;
2288
2289 case SVGA3D_A2R10G10B10:
2290 pSurface->internalFormatGL = GL_RGB10_A2; /* ?? */
2291 pSurface->formatGL = GL_RGBA;
2292 pSurface->typeGL = GL_UNSIGNED_INT;
2293 break;
2294
2295
2296 /* Single- and dual-component floating point formats */
2297 case SVGA3D_R_S10E5:
2298 pSurface->internalFormatGL = GL_R16F;
2299 pSurface->formatGL = GL_RED;
2300 pSurface->typeGL = GL_FLOAT;
2301 break;
2302 case SVGA3D_R_S23E8:
2303 pSurface->internalFormatGL = GL_R32F;
2304 pSurface->formatGL = GL_RG;
2305 pSurface->typeGL = GL_FLOAT;
2306 break;
2307 case SVGA3D_RG_S10E5:
2308 pSurface->internalFormatGL = GL_RG16F;
2309 pSurface->formatGL = GL_RG;
2310 pSurface->typeGL = GL_FLOAT;
2311 break;
2312 case SVGA3D_RG_S23E8:
2313 pSurface->internalFormatGL = GL_RG32F;
2314 pSurface->formatGL = GL_RG;
2315 pSurface->typeGL = GL_FLOAT;
2316 break;
2317
2318 /*
2319 * Any surface can be used as a buffer object, but SVGA3D_BUFFER is
2320 * the most efficient format to use when creating new surfaces
2321 * expressly for index or vertex data.
2322 */
2323 case SVGA3D_BUFFER:
2324 pSurface->internalFormatGL = -1;
2325 pSurface->formatGL = -1;
2326 pSurface->typeGL = -1;
2327 break;
2328
2329#if 0
2330 return D3DFMT_UNKNOWN;
2331
2332 case SVGA3D_V16U16:
2333 return D3DFMT_V16U16;
2334#endif
2335
2336 case SVGA3D_G16R16:
2337 pSurface->internalFormatGL = GL_RG16;
2338 pSurface->formatGL = GL_RG;
2339 pSurface->typeGL = GL_UNSIGNED_INT;
2340 break;
2341
2342 case SVGA3D_A16B16G16R16:
2343 pSurface->internalFormatGL = GL_RGBA16;
2344 pSurface->formatGL = GL_RG;
2345 pSurface->typeGL = GL_UNSIGNED_INT; /* ??? */
2346 break;
2347
2348#if 0
2349 /* Packed Video formats */
2350 case SVGA3D_UYVY:
2351 return D3DFMT_UYVY;
2352 case SVGA3D_YUY2:
2353 return D3DFMT_YUY2;
2354
2355 /* Planar video formats */
2356 case SVGA3D_NV12:
2357 return (D3DFORMAT)MAKEFOURCC('N', 'V', '1', '2');
2358
2359 /* Video format with alpha */
2360 case SVGA3D_AYUV:
2361 return (D3DFORMAT)MAKEFOURCC('A', 'Y', 'U', 'V');
2362
2363 case SVGA3D_BC4_UNORM:
2364 case SVGA3D_BC5_UNORM:
2365 /* Unknown; only in DX10 & 11 */
2366 break;
2367#endif
2368 default:
2369 AssertMsgFailed(("Unsupported format %d\n", format));
2370 break;
2371 }
2372}
2373
2374
2375#if 0
2376/**
2377 * Convert SVGA multi sample count value to its D3D equivalent
2378 */
2379D3DMULTISAMPLE_TYPE vmsvga3dMultipeSampleCount2D3D(uint32_t multisampleCount)
2380{
2381 AssertCompile(D3DMULTISAMPLE_2_SAMPLES == 2);
2382 AssertCompile(D3DMULTISAMPLE_16_SAMPLES == 16);
2383
2384 if (multisampleCount > 16)
2385 return D3DMULTISAMPLE_NONE;
2386
2387 /* @todo exact same mapping as d3d? */
2388 return (D3DMULTISAMPLE_TYPE)multisampleCount;
2389}
2390#endif
2391
2392int vmsvga3dSurfaceDefine(PVGASTATE pThis, uint32_t sid, uint32_t surfaceFlags, SVGA3dSurfaceFormat format,
2393 SVGA3dSurfaceFace face[SVGA3D_MAX_SURFACE_FACES], uint32_t multisampleCount,
2394 SVGA3dTextureFilter autogenFilter, uint32_t cMipLevels, SVGA3dSize *pMipLevelSize)
2395{
2396 PVMSVGA3DSURFACE pSurface;
2397 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
2398 AssertReturn(pState, VERR_NO_MEMORY);
2399
2400 Log(("vmsvga3dSurfaceDefine: sid=%x surfaceFlags=%x format=%s (%x) multiSampleCount=%d autogenFilter=%d, cMipLevels=%d size=(%d,%d,%d)\n",
2401 sid, surfaceFlags, vmsvgaSurfaceType2String(format), format, multisampleCount, autogenFilter, cMipLevels, pMipLevelSize->width, pMipLevelSize->height, pMipLevelSize->depth));
2402
2403 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
2404 AssertReturn(cMipLevels >= 1, VERR_INVALID_PARAMETER);
2405 /* Assuming all faces have the same nr of mipmaps. */
2406 AssertReturn(!(surfaceFlags & SVGA3D_SURFACE_CUBEMAP) || cMipLevels == face[0].numMipLevels * 6, VERR_INVALID_PARAMETER);
2407 AssertReturn((surfaceFlags & SVGA3D_SURFACE_CUBEMAP) || cMipLevels == face[0].numMipLevels, VERR_INVALID_PARAMETER);
2408
2409 if (sid >= pState->cSurfaces)
2410 {
2411 pState->paSurface = (PVMSVGA3DSURFACE )RTMemRealloc(pState->paSurface, sizeof(VMSVGA3DSURFACE) * (sid + 1));
2412 AssertReturn(pState->paSurface, VERR_NO_MEMORY);
2413 memset(&pState->paSurface[pState->cSurfaces], 0, sizeof(VMSVGA3DSURFACE) * (sid + 1 - pState->cSurfaces));
2414 for (uint32_t i = pState->cSurfaces; i < sid + 1; i++)
2415 pState->paSurface[i].id = SVGA3D_INVALID_ID;
2416
2417 pState->cSurfaces = sid + 1;
2418 }
2419 /* If one already exists with this id, then destroy it now. */
2420 if (pState->paSurface[sid].id != SVGA3D_INVALID_ID)
2421 vmsvga3dSurfaceDestroy(pThis, sid);
2422
2423 pSurface = &pState->paSurface[sid];
2424 memset(pSurface, 0, sizeof(*pSurface));
2425 pSurface->id = sid;
2426 pSurface->idAssociatedContext = SVGA3D_INVALID_ID;
2427 vmsvga3dSurfaceFormat2OGL(pSurface, format);
2428
2429 pSurface->oglId.buffer = OPENGL_INVALID_ID;
2430
2431 /* The surface type is sort of undefined now, even though the hints and format can help to clear that up.
2432 * In some case we'll have to wait until the surface is used to create the D3D object.
2433 */
2434 switch (format)
2435 {
2436 case SVGA3D_Z_D32:
2437 case SVGA3D_Z_D16:
2438 case SVGA3D_Z_D24S8:
2439 case SVGA3D_Z_D15S1:
2440 case SVGA3D_Z_D24X8:
2441 case SVGA3D_Z_DF16:
2442 case SVGA3D_Z_DF24:
2443 case SVGA3D_Z_D24S8_INT:
2444 surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
2445 break;
2446
2447 /* Texture compression formats */
2448 case SVGA3D_DXT1:
2449 case SVGA3D_DXT2:
2450 case SVGA3D_DXT3:
2451 case SVGA3D_DXT4:
2452 case SVGA3D_DXT5:
2453 /* Bump-map formats */
2454 case SVGA3D_BUMPU8V8:
2455 case SVGA3D_BUMPL6V5U5:
2456 case SVGA3D_BUMPX8L8V8U8:
2457 case SVGA3D_BUMPL8V8U8:
2458 case SVGA3D_V8U8:
2459 case SVGA3D_Q8W8V8U8:
2460 case SVGA3D_CxV8U8:
2461 case SVGA3D_X8L8V8U8:
2462 case SVGA3D_A2W10V10U10:
2463 case SVGA3D_V16U16:
2464 /* Typical render target formats; we should allow render target buffers to be used as textures. */
2465 case SVGA3D_X8R8G8B8:
2466 case SVGA3D_A8R8G8B8:
2467 case SVGA3D_R5G6B5:
2468 case SVGA3D_X1R5G5B5:
2469 case SVGA3D_A1R5G5B5:
2470 case SVGA3D_A4R4G4B4:
2471 surfaceFlags |= SVGA3D_SURFACE_HINT_TEXTURE;
2472 break;
2473
2474 case SVGA3D_LUMINANCE8:
2475 case SVGA3D_LUMINANCE4_ALPHA4:
2476 case SVGA3D_LUMINANCE16:
2477 case SVGA3D_LUMINANCE8_ALPHA8:
2478 case SVGA3D_ARGB_S10E5: /* 16-bit floating-point ARGB */
2479 case SVGA3D_ARGB_S23E8: /* 32-bit floating-point ARGB */
2480 case SVGA3D_A2R10G10B10:
2481 case SVGA3D_ALPHA8:
2482 case SVGA3D_R_S10E5:
2483 case SVGA3D_R_S23E8:
2484 case SVGA3D_RG_S10E5:
2485 case SVGA3D_RG_S23E8:
2486 case SVGA3D_G16R16:
2487 case SVGA3D_A16B16G16R16:
2488 case SVGA3D_UYVY:
2489 case SVGA3D_YUY2:
2490 case SVGA3D_NV12:
2491 case SVGA3D_AYUV:
2492 case SVGA3D_BC4_UNORM:
2493 case SVGA3D_BC5_UNORM:
2494 break;
2495
2496 /*
2497 * Any surface can be used as a buffer object, but SVGA3D_BUFFER is
2498 * the most efficient format to use when creating new surfaces
2499 * expressly for index or vertex data.
2500 */
2501 case SVGA3D_BUFFER:
2502 break;
2503
2504 default:
2505 break;
2506 }
2507
2508 pSurface->flags = surfaceFlags;
2509 pSurface->format = format;
2510 memcpy(pSurface->faces, face, sizeof(pSurface->faces));
2511 pSurface->cFaces = 1; /* check for cube maps later */
2512 pSurface->multiSampleCount = multisampleCount;
2513 pSurface->autogenFilter = autogenFilter;
2514 Assert(autogenFilter != SVGA3D_TEX_FILTER_FLATCUBIC);
2515 Assert(autogenFilter != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
2516 pSurface->pMipmapLevels = (PVMSVGA3DMIPMAPLEVEL)RTMemAllocZ(cMipLevels * sizeof(VMSVGA3DMIPMAPLEVEL));
2517 AssertReturn(pSurface->pMipmapLevels, VERR_NO_MEMORY);
2518
2519 for (uint32_t i=0; i < cMipLevels; i++)
2520 pSurface->pMipmapLevels[i].size = pMipLevelSize[i];
2521
2522 pSurface->cbBlock = vmsvga3dSurfaceFormatSize(format);
2523
2524 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))
2525 {
2526 case SVGA3D_SURFACE_CUBEMAP:
2527 Log(("SVGA3D_SURFACE_CUBEMAP\n"));
2528 pSurface->cFaces = 6;
2529 break;
2530
2531 case SVGA3D_SURFACE_HINT_INDEXBUFFER:
2532 Log(("SVGA3D_SURFACE_HINT_INDEXBUFFER\n"));
2533 /* else type unknown at this time; postpone buffer creation */
2534 break;
2535
2536 case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2537 Log(("SVGA3D_SURFACE_HINT_VERTEXBUFFER\n"));
2538 /* Type unknown at this time; postpone buffer creation */
2539 break;
2540
2541 case SVGA3D_SURFACE_HINT_TEXTURE:
2542 Log(("SVGA3D_SURFACE_HINT_TEXTURE\n"));
2543 break;
2544
2545 case SVGA3D_SURFACE_HINT_RENDERTARGET:
2546 Log(("SVGA3D_SURFACE_HINT_RENDERTARGET\n"));
2547 break;
2548
2549 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
2550 Log(("SVGA3D_SURFACE_HINT_DEPTHSTENCIL\n"));
2551 break;
2552
2553 default:
2554 /* Unknown; decide later. */
2555 break;
2556 }
2557
2558 /* Allocate buffer to hold the surface data until we can move it into a D3D object */
2559 for (uint32_t iFace=0; iFace < pSurface->cFaces; iFace++)
2560 {
2561 for (uint32_t i=0; i < pSurface->faces[iFace].numMipLevels; i++)
2562 {
2563 uint32_t idx = i + iFace * pSurface->faces[0].numMipLevels;
2564
2565 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));
2566 Log(("vmsvga3dSurfaceDefine: cbPitch=%x cbBlock=%x \n", pSurface->cbBlock * pSurface->pMipmapLevels[idx].size.width, pSurface->cbBlock));
2567
2568 pSurface->pMipmapLevels[idx].cbSurfacePitch = pSurface->cbBlock * pSurface->pMipmapLevels[idx].size.width;
2569 pSurface->pMipmapLevels[idx].cbSurface = pSurface->pMipmapLevels[idx].cbSurfacePitch * pSurface->pMipmapLevels[idx].size.height * pSurface->pMipmapLevels[idx].size.depth;
2570 pSurface->pMipmapLevels[idx].pSurfaceData = RTMemAllocZ(pSurface->pMipmapLevels[idx].cbSurface);
2571 AssertReturn(pSurface->pMipmapLevels[idx].pSurfaceData, VERR_NO_MEMORY);
2572 }
2573 }
2574 return VINF_SUCCESS;
2575}
2576
2577int vmsvga3dSurfaceDestroy(PVGASTATE pThis, uint32_t sid)
2578{
2579 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
2580 AssertReturn(pState, VERR_NO_MEMORY);
2581
2582 if ( sid < pState->cSurfaces
2583 && pState->paSurface[sid].id == sid)
2584 {
2585 PVMSVGA3DSURFACE pSurface = &pState->paSurface[sid];
2586 PVMSVGA3DCONTEXT pContext = NULL;
2587
2588 Log(("vmsvga3dSurfaceDestroy id %x\n", sid));
2589
2590 /* @todo stricter checks for associated context */
2591 uint32_t cid = pSurface->idAssociatedContext;
2592 if ( cid <= pState->cContexts
2593 && pState->paContext[cid].id == cid)
2594 {
2595 pContext = &pState->paContext[cid];
2596 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2597 }
2598 /* If there is a GL buffer or something associated with the surface, we
2599 really need something here, so pick any active context. */
2600 else if (pSurface->oglId.buffer != OPENGL_INVALID_ID)
2601 {
2602 for (cid = 0; cid < pState->cContexts; cid++)
2603 {
2604 if (pState->paContext[cid].id == cid)
2605 {
2606 pContext = &pState->paContext[cid];
2607 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2608 break;
2609 }
2610 }
2611 AssertReturn(pContext, VERR_INTERNAL_ERROR); /* otherwise crashes/fails; create temp context if this ever triggers! */
2612 }
2613
2614 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))
2615 {
2616 case SVGA3D_SURFACE_CUBEMAP:
2617 AssertFailed(); /* @todo */
2618 break;
2619
2620 case SVGA3D_SURFACE_HINT_INDEXBUFFER:
2621 case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2622 if (pSurface->oglId.buffer != OPENGL_INVALID_ID)
2623 {
2624 pState->ext.glDeleteBuffers(1, &pSurface->oglId.buffer);
2625 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2626 }
2627 break;
2628
2629 case SVGA3D_SURFACE_HINT_TEXTURE:
2630 case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2631 if (pSurface->oglId.texture != OPENGL_INVALID_ID)
2632 {
2633 glDeleteTextures(1, &pSurface->oglId.texture);
2634 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2635 }
2636 break;
2637
2638 case SVGA3D_SURFACE_HINT_RENDERTARGET:
2639 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
2640 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL | SVGA3D_SURFACE_HINT_TEXTURE: /* @todo actual texture surface not supported */
2641 if (pSurface->oglId.renderbuffer != OPENGL_INVALID_ID)
2642 {
2643 pState->ext.glDeleteRenderbuffers(1, &pSurface->oglId.renderbuffer);
2644 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2645 }
2646 break;
2647
2648 default:
2649 break;
2650 }
2651
2652 if (pSurface->pMipmapLevels)
2653 {
2654 for (uint32_t face=0; face < pSurface->cFaces; face++)
2655 {
2656 for (uint32_t i=0; i < pSurface->faces[face].numMipLevels; i++)
2657 {
2658 uint32_t idx = i + face * pSurface->faces[0].numMipLevels;
2659 if (pSurface->pMipmapLevels[idx].pSurfaceData)
2660 RTMemFree(pSurface->pMipmapLevels[idx].pSurfaceData);
2661 }
2662 }
2663 RTMemFree(pSurface->pMipmapLevels);
2664 }
2665
2666 memset(pSurface, 0, sizeof(*pSurface));
2667 pSurface->id = SVGA3D_INVALID_ID;
2668 }
2669 else
2670 AssertFailedReturn(VERR_INVALID_PARAMETER);
2671
2672 return VINF_SUCCESS;
2673}
2674
2675int vmsvga3dSurfaceCopy(PVGASTATE pThis, SVGA3dSurfaceImageId dest, SVGA3dSurfaceImageId src, uint32_t cCopyBoxes, SVGA3dCopyBox *pBox)
2676{
2677 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
2678 uint32_t sidSrc = src.sid;
2679 uint32_t sidDest = dest.sid;
2680 int rc = VINF_SUCCESS;
2681
2682 AssertReturn(pState, VERR_NO_MEMORY);
2683 AssertReturn(sidSrc < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
2684 AssertReturn(sidSrc < pState->cSurfaces && pState->paSurface[sidSrc].id == sidSrc, VERR_INVALID_PARAMETER);
2685 AssertReturn(sidDest < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
2686 AssertReturn(sidDest < pState->cSurfaces && pState->paSurface[sidDest].id == sidDest, VERR_INVALID_PARAMETER);
2687
2688 for (uint32_t i = 0; i < cCopyBoxes; i++)
2689 {
2690 SVGA3dBox destBox, srcBox;
2691
2692 srcBox.x = pBox[i].srcx;
2693 srcBox.y = pBox[i].srcy;
2694 srcBox.z = pBox[i].srcz;
2695 srcBox.w = pBox[i].w;
2696 srcBox.h = pBox[i].h;
2697 srcBox.d = pBox[i].z;
2698
2699 destBox.x = pBox[i].x;
2700 destBox.y = pBox[i].y;
2701 destBox.z = pBox[i].z;
2702 destBox.w = pBox[i].w;
2703 destBox.h = pBox[i].h;
2704 destBox.z = pBox[i].z;
2705
2706 rc = vmsvga3dSurfaceStretchBlt(pThis, dest, destBox, src, srcBox, SVGA3D_STRETCH_BLT_LINEAR);
2707 AssertRCReturn(rc, rc);
2708 }
2709 return VINF_SUCCESS;
2710}
2711
2712/* Create D3D texture object for the specified surface. */
2713static int vmsvga3dCreateTexture(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t idAssociatedContext,
2714 PVMSVGA3DSURFACE pSurface)
2715{
2716 GLint activeTexture = 0;
2717
2718 glGenTextures(1, &pSurface->oglId.texture);
2719 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2720 /* @todo Set the mip map generation filter settings. */
2721
2722 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
2723 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2724
2725 /* Must bind texture to the current context in order to change it. */
2726 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
2727 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2728
2729 if (pSurface->fDirty)
2730 {
2731 Log(("vmsvga3dCreateTexture: sync dirty texture\n"));
2732 for (uint32_t i = 0; i < pSurface->faces[0].numMipLevels; i++)
2733 {
2734 if (pSurface->pMipmapLevels[i].fDirty)
2735 {
2736 Log(("vmsvga3dCreateTexture: sync dirty texture mipmap level %d (pitch %x)\n", i, pSurface->pMipmapLevels[i].cbSurfacePitch));
2737
2738 glTexImage2D(GL_TEXTURE_2D,
2739 i,
2740 pSurface->internalFormatGL,
2741 pSurface->pMipmapLevels[i].size.width,
2742 pSurface->pMipmapLevels[i].size.height,
2743 0,
2744 pSurface->formatGL,
2745 pSurface->typeGL,
2746 pSurface->pMipmapLevels[i].pSurfaceData);
2747
2748 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2749
2750 pSurface->pMipmapLevels[i].fDirty = false;
2751 }
2752 }
2753 pSurface->fDirty = false;
2754 }
2755 else
2756 /* Reserve texture memory. */
2757 glTexImage2D(GL_TEXTURE_2D,
2758 0,
2759 pSurface->internalFormatGL,
2760 pSurface->pMipmapLevels[0].size.width,
2761 pSurface->pMipmapLevels[0].size.height,
2762 0,
2763 pSurface->formatGL,
2764 pSurface->typeGL,
2765 NULL);
2766
2767 /* Restore the old active texture. */
2768 glBindTexture(GL_TEXTURE_2D, activeTexture);
2769 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2770
2771 pSurface->flags |= SVGA3D_SURFACE_HINT_TEXTURE;
2772 LogFlow(("vmsvga3dCreateTexture: sid=%#x idAssociatedContext %#x -> %#x\n", pSurface->id, pSurface->idAssociatedContext, idAssociatedContext));
2773 pSurface->idAssociatedContext = idAssociatedContext;
2774 return VINF_SUCCESS;
2775}
2776
2777int vmsvga3dSurfaceStretchBlt(PVGASTATE pThis, SVGA3dSurfaceImageId dest, SVGA3dBox destBox, SVGA3dSurfaceImageId src, SVGA3dBox srcBox, SVGA3dStretchBltMode mode)
2778{
2779 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
2780 PVMSVGA3DSURFACE pSurfaceSrc;
2781 uint32_t sidSrc = src.sid;
2782 PVMSVGA3DSURFACE pSurfaceDest;
2783 uint32_t sidDest = dest.sid;
2784 int rc = VINF_SUCCESS;
2785 uint32_t cid;
2786 PVMSVGA3DCONTEXT pContext;
2787
2788 AssertReturn(pState, VERR_NO_MEMORY);
2789 AssertReturn(sidSrc < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
2790 AssertReturn(sidSrc < pState->cSurfaces && pState->paSurface[sidSrc].id == sidSrc, VERR_INVALID_PARAMETER);
2791 AssertReturn(sidDest < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
2792 AssertReturn(sidDest < pState->cSurfaces && pState->paSurface[sidDest].id == sidDest, VERR_INVALID_PARAMETER);
2793
2794 pSurfaceSrc = &pState->paSurface[sidSrc];
2795 pSurfaceDest = &pState->paSurface[sidDest];
2796 AssertReturn(pSurfaceSrc->faces[0].numMipLevels > src.mipmap, VERR_INVALID_PARAMETER);
2797 AssertReturn(pSurfaceDest->faces[0].numMipLevels > dest.mipmap, VERR_INVALID_PARAMETER);
2798
2799 Log(("vmsvga3dSurfaceStretchBlt: src sid=%x cid=%#x (%d,%d)(%d,%d) dest sid=%x cid=%#x (%d,%d)(%d,%d) mode=%x\n",
2800 src.sid, pSurfaceSrc->idAssociatedContext, srcBox.x, srcBox.y, srcBox.x + srcBox.w, srcBox.y + srcBox.h,
2801 dest.sid, pSurfaceDest->idAssociatedContext, destBox.x, destBox.y, destBox.x + destBox.w, destBox.y + destBox.h, mode));
2802
2803 /* @todo stricter checks for associated context */
2804 cid = pSurfaceDest->idAssociatedContext;
2805 if (cid == SVGA3D_INVALID_ID)
2806 cid = pSurfaceSrc->idAssociatedContext;
2807
2808 if ( cid >= pState->cContexts
2809 || pState->paContext[cid].id != cid)
2810 {
2811 Log(("vmsvga3dSurfaceStretchBlt invalid context id!\n"));
2812 AssertFailedReturn(VERR_INVALID_PARAMETER);
2813 }
2814 pContext = &pState->paContext[cid];
2815 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2816
2817 if (pSurfaceSrc->oglId.texture == OPENGL_INVALID_ID)
2818 {
2819 /* Unknown surface type; turn it into a texture, which can be used for other purposes too. */
2820 Log(("vmsvga3dSurfaceStretchBlt: unknown src surface id=%x type=%d format=%d -> create texture\n", sidSrc, pSurfaceSrc->flags, pSurfaceSrc->format));
2821 rc = vmsvga3dCreateTexture(pState, pContext, cid, pSurfaceSrc);
2822 AssertRCReturn(rc, rc);
2823 }
2824
2825 if (pSurfaceDest->oglId.texture == OPENGL_INVALID_ID)
2826 {
2827 /* Unknown surface type; turn it into a texture, which can be used for other purposes too. */
2828 Log(("vmsvga3dSurfaceStretchBlt: unknown dest surface id=%x type=%d format=%d -> create texture\n", sidDest, pSurfaceDest->flags, pSurfaceDest->format));
2829 rc = vmsvga3dCreateTexture(pState, pContext, cid, pSurfaceDest);
2830 AssertRCReturn(rc, rc);
2831 }
2832
2833 /* Activate the read and draw framebuffer objects. */
2834 pState->ext.glBindFramebuffer(GL_READ_FRAMEBUFFER, pContext->idReadFramebuffer);
2835 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2836 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, pContext->idDrawFramebuffer);
2837 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2838
2839 /* Bind the source and destination objects to the right place. */
2840 pState->ext.glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pSurfaceSrc->oglId.texture, src.mipmap);
2841 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2842 pState->ext.glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pSurfaceDest->oglId.texture, dest.mipmap);
2843 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2844
2845 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),
2846 srcBox.x + srcBox.w, D3D_TO_OGL_Y_COORD(pSurfaceSrc, srcBox.y), destBox.x, D3D_TO_OGL_Y_COORD(pSurfaceDest, destBox.y + destBox.h),
2847 destBox.x + destBox.w, D3D_TO_OGL_Y_COORD(pSurfaceDest, destBox.y)));
2848
2849 pState->ext.glBlitFramebuffer(srcBox.x,
2850#ifdef MANUAL_FLIP_SURFACE_DATA
2851 D3D_TO_OGL_Y_COORD(pSurfaceSrc, srcBox.y + srcBox.h), /* inclusive */
2852#else
2853 srcBox.y,
2854#endif
2855 srcBox.x + srcBox.w, /* exclusive. */
2856#ifdef MANUAL_FLIP_SURFACE_DATA
2857 D3D_TO_OGL_Y_COORD(pSurfaceSrc, srcBox.y), /* exclusive */
2858#else
2859 srcBox.y + srcBox.h,
2860#endif
2861 destBox.x,
2862#ifdef MANUAL_FLIP_SURFACE_DATA
2863 D3D_TO_OGL_Y_COORD(pSurfaceDest, destBox.y + destBox.h), /* inclusive. */
2864#else
2865 destBox.y,
2866#endif
2867 destBox.x + destBox.w, /* exclusive. */
2868#ifdef MANUAL_FLIP_SURFACE_DATA
2869 D3D_TO_OGL_Y_COORD(pSurfaceDest, destBox.y), /* exclusive */
2870#else
2871 destBox.y + destBox.h,
2872#endif
2873 GL_COLOR_BUFFER_BIT,
2874 (mode == SVGA3D_STRETCH_BLT_POINT) ? GL_NEAREST : GL_LINEAR);
2875 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2876
2877 /* Reset the frame buffer association */
2878 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
2879 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2880
2881 return VINF_SUCCESS;
2882}
2883
2884int vmsvga3dSurfaceDMA(PVGASTATE pThis, SVGA3dGuestImage guest, SVGA3dSurfaceImageId host, SVGA3dTransferType transfer,
2885 uint32_t cCopyBoxes, SVGA3dCopyBox *pBoxes)
2886{
2887 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
2888 PVMSVGA3DSURFACE pSurface;
2889 PVMSVGA3DMIPMAPLEVEL pMipLevel;
2890 uint32_t sid = host.sid;
2891 int rc = VINF_SUCCESS;
2892
2893 AssertReturn(pState, VERR_NO_MEMORY);
2894 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
2895 AssertReturn(sid < pState->cSurfaces && pState->paSurface[sid].id == sid, VERR_INVALID_PARAMETER);
2896
2897 pSurface = &pState->paSurface[sid];
2898 AssertReturn(pSurface->faces[0].numMipLevels > host.mipmap, VERR_INVALID_PARAMETER);
2899 pMipLevel = &pSurface->pMipmapLevels[host.mipmap];
2900
2901 if (pSurface->flags & SVGA3D_SURFACE_HINT_TEXTURE)
2902 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));
2903 else
2904 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));
2905
2906 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
2907 {
2908 AssertReturn(pSurface->pMipmapLevels[host.mipmap].pSurfaceData, VERR_INTERNAL_ERROR);
2909
2910 for (unsigned i = 0; i < cCopyBoxes; i++)
2911 {
2912 unsigned uDestOffset;
2913 unsigned cbSrcPitch;
2914 uint8_t *pBufferStart;
2915
2916 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));
2917 /* Apparently we're supposed to clip it (gmr test sample) */
2918 if (pBoxes[i].x + pBoxes[i].w > pMipLevel->size.width)
2919 pBoxes[i].w = pMipLevel->size.width - pBoxes[i].x;
2920 if (pBoxes[i].y + pBoxes[i].h > pMipLevel->size.height)
2921 pBoxes[i].h = pMipLevel->size.height - pBoxes[i].y;
2922 if (pBoxes[i].z + pBoxes[i].d > pMipLevel->size.depth)
2923 pBoxes[i].d = pMipLevel->size.depth - pBoxes[i].z;
2924
2925 if ( !pBoxes[i].w
2926 || !pBoxes[i].h
2927 || !pBoxes[i].d
2928 || pBoxes[i].x > pMipLevel->size.width
2929 || pBoxes[i].y > pMipLevel->size.height
2930 || pBoxes[i].z > pMipLevel->size.depth)
2931 {
2932 Log(("Empty box; skip\n"));
2933 continue;
2934 }
2935
2936 uDestOffset = pBoxes[i].x * pSurface->cbBlock + pBoxes[i].y * pMipLevel->cbSurfacePitch + pBoxes[i].z * pMipLevel->size.height * pMipLevel->cbSurfacePitch;
2937 AssertReturn(uDestOffset + pBoxes[i].w * pSurface->cbBlock * pBoxes[i].h * pBoxes[i].d <= pMipLevel->cbSurface, VERR_INTERNAL_ERROR);
2938
2939 cbSrcPitch = (guest.pitch == 0) ? pBoxes[i].w * pSurface->cbBlock : guest.pitch;
2940#ifdef MANUAL_FLIP_SURFACE_DATA
2941 pBufferStart = (uint8_t *)pMipLevel->pSurfaceData
2942 + pBoxes[i].x * pSurface->cbBlock
2943 + pMipLevel->cbSurface - pBoxes[i].y * pMipLevel->cbSurfacePitch
2944 - pMipLevel->cbSurfacePitch; /* flip image during copy */
2945#else
2946 pBufferStart = (uint8_t *)pMipLevel->pSurfaceData + uDestOffset;
2947#endif
2948 rc = vmsvgaGMRTransfer(pThis,
2949 transfer,
2950 pBufferStart,
2951#ifdef MANUAL_FLIP_SURFACE_DATA
2952 -(int32_t)pMipLevel->cbSurfacePitch,
2953#else
2954 (int32_t)pMipLevel->cbSurfacePitch,
2955#endif
2956 guest.ptr,
2957 pBoxes[i].srcx * pSurface->cbBlock + (pBoxes[i].srcy + pBoxes[i].srcz * pBoxes[i].h) * cbSrcPitch,
2958 cbSrcPitch,
2959 pBoxes[i].w * pSurface->cbBlock,
2960 pBoxes[i].d * pBoxes[i].h);
2961
2962 Log4(("first line:\n%.*Rhxd\n", pMipLevel->cbSurface, pMipLevel->pSurfaceData));
2963
2964 AssertRC(rc);
2965 }
2966 pSurface->pMipmapLevels[host.mipmap].fDirty = true;
2967 pSurface->fDirty = true;
2968 }
2969 else
2970 {
2971 /* @todo stricter checks for associated context */
2972 uint32_t cid = pSurface->idAssociatedContext;
2973 if ( cid >= pState->cContexts
2974 || pState->paContext[cid].id != cid)
2975 {
2976 Log(("vmsvga3dSurfaceDMA invalid context id (%x - %x)!\n", cid, (cid >= pState->cContexts) ? -1 : pState->paContext[cid].id));
2977 AssertFailedReturn(VERR_INVALID_PARAMETER);
2978 }
2979 PVMSVGA3DCONTEXT pContext = &pState->paContext[cid];
2980 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2981
2982 for (unsigned i = 0; i < cCopyBoxes; i++)
2983 {
2984 bool fVertex = false;
2985 unsigned cbSrcPitch;
2986
2987 /* Apparently we're supposed to clip it (gmr test sample) */
2988 if (pBoxes[i].x + pBoxes[i].w > pMipLevel->size.width)
2989 pBoxes[i].w = pMipLevel->size.width - pBoxes[i].x;
2990 if (pBoxes[i].y + pBoxes[i].h > pMipLevel->size.height)
2991 pBoxes[i].h = pMipLevel->size.height - pBoxes[i].y;
2992 if (pBoxes[i].z + pBoxes[i].d > pMipLevel->size.depth)
2993 pBoxes[i].d = pMipLevel->size.depth - pBoxes[i].z;
2994
2995 Assert((pBoxes[i].d == 1 || pBoxes[i].d == 0) && pBoxes[i].z == 0);
2996
2997 if ( !pBoxes[i].w
2998 || !pBoxes[i].h
2999 || pBoxes[i].x > pMipLevel->size.width
3000 || pBoxes[i].y > pMipLevel->size.height)
3001 {
3002 Log(("Empty box; skip\n"));
3003 continue;
3004 }
3005
3006 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));
3007
3008 cbSrcPitch = (guest.pitch == 0) ? pBoxes[i].w * pSurface->cbBlock : guest.pitch;
3009
3010 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))
3011 {
3012 case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
3013 case SVGA3D_SURFACE_HINT_TEXTURE:
3014 case SVGA3D_SURFACE_HINT_RENDERTARGET:
3015 {
3016 uint32_t cbSurfacePitch;
3017 uint8_t *pDoubleBuffer, *pBufferStart;
3018 unsigned uDestOffset = 0;
3019
3020 pDoubleBuffer = (uint8_t *)RTMemAlloc(pMipLevel->cbSurface);
3021 AssertReturn(pDoubleBuffer, VERR_NO_MEMORY);
3022
3023 if (transfer == SVGA3D_READ_HOST_VRAM)
3024 {
3025 GLint activeTexture;
3026
3027 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
3028 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3029
3030 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
3031 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3032
3033 glGetTexImage(GL_TEXTURE_2D,
3034 host.mipmap,
3035 pSurface->formatGL,
3036 pSurface->typeGL,
3037 pDoubleBuffer);
3038 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3039
3040 /* Restore the old active texture. */
3041 glBindTexture(GL_TEXTURE_2D, activeTexture);
3042 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3043
3044 uDestOffset = pBoxes[i].x * pSurface->cbBlock + pBoxes[i].y * pMipLevel->cbSurfacePitch;
3045 AssertReturnStmt( uDestOffset + pBoxes[i].w * pSurface->cbBlock + (pBoxes[i].h - 1) * pMipLevel->cbSurfacePitch
3046 <= pMipLevel->cbSurface,
3047 RTMemFree(pDoubleBuffer),
3048 VERR_INTERNAL_ERROR);
3049
3050 cbSurfacePitch = pMipLevel->cbSurfacePitch;
3051
3052#ifdef MANUAL_FLIP_SURFACE_DATA
3053 pBufferStart = pDoubleBuffer
3054 + pBoxes[i].x * pSurface->cbBlock
3055 + pMipLevel->cbSurface - pBoxes[i].y * cbSurfacePitch
3056 - cbSurfacePitch; /* flip image during copy */
3057#else
3058 pBufferStart = pDoubleBuffer + uDestOffset;
3059#endif
3060 }
3061 else
3062 {
3063 cbSurfacePitch = pBoxes[i].w * pSurface->cbBlock;
3064#ifdef MANUAL_FLIP_SURFACE_DATA
3065 pBufferStart = pDoubleBuffer + cbSurfacePitch * pBoxes[i].h - cbSurfacePitch; /* flip image during copy */
3066#else
3067 pBufferStart = pDoubleBuffer;
3068#endif
3069 }
3070
3071 rc = vmsvgaGMRTransfer(pThis,
3072 transfer,
3073 pBufferStart,
3074#ifdef MANUAL_FLIP_SURFACE_DATA
3075 -(int32_t)cbSurfacePitch,
3076#else
3077 (int32_t)cbSurfacePitch,
3078#endif
3079 guest.ptr,
3080 pBoxes[i].srcx * pSurface->cbBlock + pBoxes[i].srcy * cbSrcPitch,
3081 cbSrcPitch,
3082 pBoxes[i].w * pSurface->cbBlock,
3083 pBoxes[i].h);
3084 AssertRC(rc);
3085
3086 /* Update the opengl surface data. */
3087 if (transfer == SVGA3D_WRITE_HOST_VRAM)
3088 {
3089 GLint activeTexture = 0;
3090 GLint alignment;
3091
3092 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
3093 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3094
3095 /* Must bind texture to the current context in order to change it. */
3096 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
3097 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3098
3099 Log(("vmsvga3dSurfaceDMA: copy texture mipmap level %d (pitch %x)\n", host.mipmap, pMipLevel->cbSurfacePitch));
3100
3101 /* Set row length and alignment of the input data. */
3102 glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
3103 glPixelStorei(GL_UNPACK_ROW_LENGTH, pBoxes[i].w);
3104 glPixelStorei(GL_UNPACK_ALIGNMENT, pSurface->cbBlock);
3105
3106 glTexSubImage2D(GL_TEXTURE_2D,
3107 host.mipmap,
3108 pBoxes[i].x,
3109 pBoxes[i].y,
3110 pBoxes[i].w,
3111 pBoxes[i].h,
3112 pSurface->formatGL,
3113 pSurface->typeGL,
3114 pDoubleBuffer);
3115
3116 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3117
3118 /* Restore old values. */
3119 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3120 glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
3121
3122 /* Restore the old active texture. */
3123 glBindTexture(GL_TEXTURE_2D, activeTexture);
3124 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3125 }
3126
3127 Log4(("first line:\n%.*Rhxd\n", pBoxes[i].w * pSurface->cbBlock, pDoubleBuffer));
3128
3129 /* Free the double buffer. */
3130 RTMemFree(pDoubleBuffer);
3131 break;
3132 }
3133
3134 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
3135 AssertFailed(); /* @todo */
3136 break;
3137
3138 case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
3139 case SVGA3D_SURFACE_HINT_INDEXBUFFER:
3140 {
3141 Assert(pBoxes[i].h == 1);
3142
3143 VMSVGA3D_CLEAR_GL_ERRORS();
3144 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pSurface->oglId.buffer);
3145 if (VMSVGA3D_GL_IS_SUCCESS(pContext))
3146 {
3147 GLenum enmGlTransfer = (transfer == SVGA3D_READ_HOST_VRAM) ? GL_READ_ONLY : GL_WRITE_ONLY;
3148 uint8_t *pbData = (uint8_t *)pState->ext.glMapBuffer(GL_ARRAY_BUFFER, enmGlTransfer);
3149 if (RT_LIKELY(pbData != NULL))
3150 {
3151#if defined(VBOX_STRICT) && defined(RT_OS_DARWIN)
3152 GLint cbStrictBufSize;
3153 glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &cbStrictBufSize);
3154 Assert(VMSVGA3D_GL_IS_SUCCESS(pContext));
3155 AssertMsg(cbStrictBufSize >= (int32_t)pMipLevel->cbSurface,
3156 ("cbStrictBufSize=%#x cbSurface=%#x isAssociatedContext=%#x pContext->id=%#x\n",
3157 (uint32_t)cbStrictBufSize, pMipLevel->cbSurface, pSurface->idAssociatedContext, pContext->id));
3158#endif
3159
3160 unsigned offDst = pBoxes[i].x * pSurface->cbBlock + pBoxes[i].y * pMipLevel->cbSurfacePitch;
3161 if (RT_LIKELY( offDst + pBoxes[i].w * pSurface->cbBlock + (pBoxes[i].h - 1) * pMipLevel->cbSurfacePitch
3162 <= pMipLevel->cbSurface))
3163 {
3164 Log(("Lock %s memory for rectangle (%d,%d)(%d,%d)\n", (fVertex) ? "vertex" : "index",
3165 pBoxes[i].x, pBoxes[i].y, pBoxes[i].x + pBoxes[i].w, pBoxes[i].y + pBoxes[i].h));
3166
3167 rc = vmsvgaGMRTransfer(pThis,
3168 transfer,
3169 pbData + offDst,
3170 pMipLevel->cbSurfacePitch,
3171 guest.ptr,
3172 pBoxes[i].srcx * pSurface->cbBlock + pBoxes[i].srcy * cbSrcPitch,
3173 cbSrcPitch,
3174 pBoxes[i].w * pSurface->cbBlock,
3175 pBoxes[i].h);
3176 AssertRC(rc);
3177
3178 Log4(("first line:\n%.*Rhxd\n", cbSrcPitch, pbData));
3179 }
3180 else
3181 {
3182 AssertFailed();
3183 rc = VERR_INTERNAL_ERROR;
3184 }
3185
3186 pState->ext.glUnmapBuffer(GL_ARRAY_BUFFER);
3187 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3188 }
3189 else
3190 VMSVGA3D_GL_GET_AND_COMPLAIN(pState, pContext, ("glMapBuffer(GL_ARRAY_BUFFER, %#x) -> NULL\n", enmGlTransfer));
3191 }
3192 else
3193 VMSVGA3D_GL_COMPLAIN(pState, pContext, ("glBindBuffer(GL_ARRAY_BUFFER, %#x)\n", pSurface->oglId.buffer));
3194 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0);
3195 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3196 break;
3197 }
3198
3199 default:
3200 AssertFailed();
3201 break;
3202 }
3203 }
3204 }
3205 return rc;
3206}
3207
3208int vmsvga3dSurfaceBlitToScreen(PVGASTATE pThis, uint32_t dest, SVGASignedRect destRect, SVGA3dSurfaceImageId src, SVGASignedRect srcRect, uint32_t cRects, SVGASignedRect *pRect)
3209{
3210 /* Requires SVGA_FIFO_CAP_SCREEN_OBJECT support */
3211 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));
3212 for (uint32_t i = 0; i < cRects; i++)
3213 {
3214 Log(("vmsvga3dSurfaceBlitToScreen: clipping rect %d (%d,%d)(%d,%d)\n", i, pRect[i].left, pRect[i].top, pRect[i].right, pRect[i].bottom));
3215 }
3216
3217 /* @todo Only screen 0 for now. */
3218 AssertReturn(dest == 0, VERR_INTERNAL_ERROR);
3219 AssertReturn(src.mipmap == 0 && src.face == 0, VERR_INVALID_PARAMETER);
3220 /* @todo scaling */
3221 AssertReturn(destRect.right - destRect.left == srcRect.right - srcRect.left && destRect.bottom - destRect.top == srcRect.bottom - srcRect.top, VERR_INVALID_PARAMETER);
3222
3223 if (cRects == 0)
3224 {
3225 /* easy case; no clipping */
3226 SVGA3dCopyBox box;
3227 SVGA3dGuestImage dst;
3228
3229 box.x = destRect.left;
3230 box.y = destRect.top;
3231 box.z = 0;
3232 box.w = destRect.right - destRect.left;
3233 box.h = destRect.bottom - destRect.top;
3234 box.d = 1;
3235 box.srcx = srcRect.left;
3236 box.srcy = srcRect.top;
3237 box.srcz = 0;
3238
3239 dst.ptr.gmrId = SVGA_GMR_FRAMEBUFFER;
3240 dst.ptr.offset = 0;
3241 dst.pitch = pThis->svga.cbScanline;
3242
3243 int rc = vmsvga3dSurfaceDMA(pThis, dst, src, SVGA3D_READ_HOST_VRAM, 1, &box);
3244 AssertRCReturn(rc, rc);
3245
3246 vgaR3UpdateDisplay(pThis, box.x, box.y, box.w, box.h);
3247 return VINF_SUCCESS;
3248 }
3249 else
3250 {
3251 SVGA3dGuestImage dst;
3252 SVGA3dCopyBox box;
3253
3254 box.srcz = 0;
3255 box.z = 0;
3256 box.d = 1;
3257
3258 dst.ptr.gmrId = SVGA_GMR_FRAMEBUFFER;
3259 dst.ptr.offset = 0;
3260 dst.pitch = pThis->svga.cbScanline;
3261
3262 /* @todo merge into one SurfaceDMA call */
3263 for (uint32_t i = 0; i < cRects; i++)
3264 {
3265 /* The clipping rectangle is relative to the top-left corner of srcRect & destRect. Adjust here. */
3266 box.srcx = srcRect.left + pRect[i].left;
3267 box.srcy = srcRect.top + pRect[i].top;
3268
3269 box.x = pRect[i].left + destRect.left;
3270 box.y = pRect[i].top + destRect.top;
3271 box.z = 0;
3272 box.w = pRect[i].right - pRect[i].left;
3273 box.h = pRect[i].bottom - pRect[i].top;
3274
3275 int rc = vmsvga3dSurfaceDMA(pThis, dst, src, SVGA3D_READ_HOST_VRAM, 1, &box);
3276 AssertRCReturn(rc, rc);
3277
3278 vgaR3UpdateDisplay(pThis, box.x, box.y, box.w, box.h);
3279 }
3280
3281 return VINF_SUCCESS;
3282 }
3283}
3284
3285int vmsvga3dGenerateMipmaps(PVGASTATE pThis, uint32_t sid, SVGA3dTextureFilter filter)
3286{
3287 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
3288 PVMSVGA3DSURFACE pSurface;
3289 int rc = VINF_SUCCESS;
3290 PVMSVGA3DCONTEXT pContext;
3291 uint32_t cid;
3292 GLint activeTexture = 0;
3293
3294 AssertReturn(pState, VERR_NO_MEMORY);
3295 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
3296 AssertReturn(sid < pState->cSurfaces && pState->paSurface[sid].id == sid, VERR_INVALID_PARAMETER);
3297
3298 pSurface = &pState->paSurface[sid];
3299 AssertReturn(pSurface->idAssociatedContext != SVGA3D_INVALID_ID, VERR_INTERNAL_ERROR);
3300
3301 Assert(filter != SVGA3D_TEX_FILTER_FLATCUBIC);
3302 Assert(filter != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
3303 pSurface->autogenFilter = filter;
3304
3305 Log(("vmsvga3dGenerateMipmaps: sid=%x filter=%d\n", sid, filter));
3306
3307 /* @todo stricter checks for associated context */
3308 cid = pSurface->idAssociatedContext;
3309
3310 if ( cid >= pState->cContexts
3311 || pState->paContext[cid].id != cid)
3312 {
3313 Log(("vmsvga3dGenerateMipmaps invalid context id!\n"));
3314 return VERR_INVALID_PARAMETER;
3315 }
3316 pContext = &pState->paContext[cid];
3317 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3318
3319 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
3320 {
3321 /* Unknown surface type; turn it into a texture. */
3322 Log(("vmsvga3dGenerateMipmaps: unknown src surface id=%x type=%d format=%d -> create texture\n", sid, pSurface->flags, pSurface->format));
3323 rc = vmsvga3dCreateTexture(pState, pContext, cid, pSurface);
3324 AssertRCReturn(rc, rc);
3325 }
3326 else
3327 {
3328 /* @todo new filter */
3329 AssertFailed();
3330 }
3331
3332 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
3333 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3334
3335 /* Must bind texture to the current context in order to change it. */
3336 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
3337 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3338
3339 /* Generate the mip maps. */
3340 pState->ext.glGenerateMipmap(GL_TEXTURE_2D);
3341 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3342
3343 /* Restore the old texture. */
3344 glBindTexture(GL_TEXTURE_2D, activeTexture);
3345 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3346
3347 return VINF_SUCCESS;
3348}
3349
3350int vmsvga3dCommandPresent(PVGASTATE pThis, uint32_t sid, uint32_t cRects, SVGA3dCopyRect *pRect)
3351{
3352 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
3353 PVMSVGA3DSURFACE pSurface;
3354 int rc = VINF_SUCCESS;
3355 PVMSVGA3DCONTEXT pContext;
3356 uint32_t cid;
3357 struct
3358 {
3359 uint32_t x;
3360 uint32_t y;
3361 uint32_t cx;
3362 uint32_t cy;
3363 } srcViewPort;
3364
3365 AssertReturn(pState, VERR_NO_MEMORY);
3366 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
3367 AssertReturn(sid < pState->cSurfaces && pState->paSurface[sid].id == sid, VERR_INVALID_PARAMETER);
3368
3369 pSurface = &pState->paSurface[sid];
3370 AssertReturn(pSurface->idAssociatedContext != SVGA3D_INVALID_ID, VERR_INTERNAL_ERROR);
3371
3372 /* @todo stricter checks for associated context */
3373 cid = pSurface->idAssociatedContext;
3374 Log(("vmsvga3dCommandPresent: sid=%x cRects=%d cid=%x\n", sid, cRects, cid));
3375 for (uint32_t i=0; i < cRects; i++)
3376 {
3377 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));
3378 }
3379
3380 if ( cid >= pState->cContexts
3381 || pState->paContext[cid].id != cid)
3382 {
3383 Log(("vmsvga3dCommandPresent invalid context id!\n"));
3384 return VERR_INVALID_PARAMETER;
3385 }
3386 pContext = &pState->paContext[cid];
3387 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3388
3389 /* Source surface different size? */
3390 if (pSurface->pMipmapLevels[0].size.width != pThis->svga.uWidth ||
3391 pSurface->pMipmapLevels[0].size.height != pThis->svga.uHeight)
3392 {
3393 float xMultiplier = (float)pSurface->pMipmapLevels[0].size.width / (float)pThis->svga.uWidth;
3394 float yMultiplier = (float)pSurface->pMipmapLevels[0].size.height / (float)pThis->svga.uHeight;
3395
3396 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)));
3397
3398 srcViewPort.x = (uint32_t)((float)pThis->svga.viewport.x * xMultiplier);
3399 srcViewPort.y = (uint32_t)((float)pThis->svga.viewport.y * yMultiplier);
3400 srcViewPort.cx = (uint32_t)((float)pThis->svga.viewport.cx * xMultiplier);
3401 srcViewPort.cy = (uint32_t)((float)pThis->svga.viewport.cy * yMultiplier);
3402 }
3403 else
3404 {
3405 srcViewPort.x = pThis->svga.viewport.x;
3406 srcViewPort.y = pThis->svga.viewport.y;
3407 srcViewPort.cx = pThis->svga.viewport.cx;
3408 srcViewPort.cy = pThis->svga.viewport.cy;
3409 }
3410
3411#if 1
3412 /* @note this path is slightly faster than the glBlitFrameBuffer path below. */
3413 SVGA3dCopyRect rect;
3414 uint32_t oldVShader, oldPShader;
3415 GLint oldTextureId;
3416
3417 if (cRects == 0)
3418 {
3419 rect.x = rect.y = rect.srcx = rect.srcy = 0;
3420 rect.w = pSurface->pMipmapLevels[0].size.width;
3421 rect.h = pSurface->pMipmapLevels[0].size.height;
3422 pRect = &rect;
3423 cRects = 1;
3424 }
3425
3426 //glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
3427
3428#if 0
3429 glDisable(GL_CULL_FACE);
3430 glDisable(GL_BLEND);
3431 glDisable(GL_ALPHA_TEST);
3432 glDisable(GL_SCISSOR_TEST);
3433 glDisable(GL_STENCIL_TEST);
3434 glEnable(GL_DEPTH_TEST);
3435 glDepthFunc(GL_ALWAYS);
3436 glDepthMask(GL_TRUE);
3437 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
3438 glViewport(0, 0, pSurface->pMipmapLevels[0].size.width, pSurface->pMipmapLevels[0].size.height);
3439#endif
3440
3441 glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTextureId);
3442
3443 oldVShader = pContext->state.shidVertex;
3444 oldPShader = pContext->state.shidPixel;
3445 vmsvga3dShaderSet(pThis, cid, SVGA3D_SHADERTYPE_VS, SVGA_ID_INVALID);
3446 vmsvga3dShaderSet(pThis, cid, SVGA3D_SHADERTYPE_PS, SVGA_ID_INVALID);
3447
3448 /* Flush shader changes. */
3449 if (pContext->pShaderContext)
3450 ShaderUpdateState(pContext->pShaderContext, 0);
3451
3452 /* Activate the read and draw framebuffer objects. */
3453 pState->ext.glBindFramebuffer(GL_READ_FRAMEBUFFER, pContext->idReadFramebuffer);
3454 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3455 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0 /* back buffer */);
3456 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3457
3458 pState->ext.glActiveTexture(GL_TEXTURE0);
3459 glEnable(GL_TEXTURE_2D);
3460 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
3461 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3462
3463 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
3464 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
3465
3466// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
3467// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
3468
3469 /* Reset the transformation matrices. */
3470 glMatrixMode(GL_MODELVIEW);
3471 glPushMatrix();
3472 glLoadIdentity();
3473 glMatrixMode(GL_PROJECTION);
3474 glPushMatrix();
3475 glLoadIdentity();
3476 glScalef(1.0f, -1.0f, 1.0f);
3477 glOrtho(0, pThis->svga.uWidth, pThis->svga.uHeight, 0, 0.0, -1.0);
3478
3479 for (uint32_t i = 0; i < cRects; i++)
3480 {
3481 float left, right, top, bottom; /* Texture coordinates */
3482 int vertexLeft, vertexRight, vertexTop, vertexBottom;
3483
3484 pRect[i].srcx = RT_MAX(pRect[i].srcx, srcViewPort.x);
3485 pRect[i].srcy = RT_MAX(pRect[i].srcy, srcViewPort.y);
3486 pRect[i].x = RT_MAX(pRect[i].x, pThis->svga.viewport.x) - pThis->svga.viewport.x;
3487 pRect[i].y = RT_MAX(pRect[i].y, pThis->svga.viewport.y) - pThis->svga.viewport.y;
3488 pRect[i].w = pThis->svga.viewport.cx;
3489 pRect[i].h = pThis->svga.viewport.cy;
3490
3491 if ( pRect[i].x + pRect[i].w <= pThis->svga.viewport.x
3492 || pThis->svga.viewport.x + pThis->svga.viewport.cx <= pRect[i].x
3493 || pRect[i].y + pRect[i].h <= pThis->svga.viewport.y
3494 || pThis->svga.viewport.y + pThis->svga.viewport.cy <= pRect[i].y)
3495 {
3496 /* Intersection is empty; skip */
3497 continue;
3498 }
3499
3500 left = pRect[i].srcx;
3501 right = pRect[i].srcx + pRect[i].w;
3502 top = pRect[i].srcy + pRect[i].h;
3503 bottom = pRect[i].srcy;
3504
3505 left /= pSurface->pMipmapLevels[0].size.width;
3506 right /= pSurface->pMipmapLevels[0].size.width;
3507 top /= pSurface->pMipmapLevels[0].size.height;
3508 bottom /= pSurface->pMipmapLevels[0].size.height;
3509
3510 vertexLeft = pRect[i].x;
3511 vertexRight = pRect[i].x + pRect[i].w;
3512 vertexTop = ((uint32_t)pThis->svga.uHeight >= pRect[i].y + pRect[i].h) ? pThis->svga.uHeight - pRect[i].y - pRect[i].h : 0;
3513 vertexBottom = pThis->svga.uHeight - pRect[i].y;
3514
3515 Log(("view port (%d,%d)(%d,%d)\n", srcViewPort.x, srcViewPort.y, srcViewPort.cx, srcViewPort.cy));
3516 Log(("vertex (%d,%d) (%d,%d) (%d,%d) (%d,%d)\n", vertexLeft, vertexBottom, vertexLeft, vertexTop, vertexRight, vertexTop, vertexRight, vertexBottom));
3517 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)));
3518
3519 glBegin(GL_QUADS);
3520 /* bottom left */
3521 glTexCoord2f(left, bottom);
3522 glVertex2i(vertexLeft, vertexBottom);
3523
3524 /* top left */
3525 glTexCoord2f(left, top);
3526 glVertex2i(vertexLeft, vertexTop);
3527
3528 /* top right */
3529 glTexCoord2f(right, top);
3530 glVertex2i(vertexRight, vertexTop);
3531
3532 /* bottom right */
3533 glTexCoord2f(right, bottom);
3534 glVertex2i(vertexRight, vertexBottom);
3535
3536 glEnd();
3537 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3538 }
3539
3540 /* Restore old settings. */
3541 glMatrixMode(GL_PROJECTION);
3542 glPopMatrix();
3543 glMatrixMode(GL_MODELVIEW);
3544 glPopMatrix();
3545
3546 //glPopAttrib();
3547
3548 glBindTexture(GL_TEXTURE_2D, oldTextureId);
3549 vmsvga3dShaderSet(pThis, cid, SVGA3D_SHADERTYPE_VS, oldVShader);
3550 vmsvga3dShaderSet(pThis, cid, SVGA3D_SHADERTYPE_PS, oldPShader);
3551
3552 /* Reset the frame buffer association */
3553 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
3554 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3555
3556#else
3557 /* Activate the read and draw framebuffer objects. */
3558 pState->ext.glBindFramebuffer(GL_READ_FRAMEBUFFER, pContext->idReadFramebuffer);
3559 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3560 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0 /* back buffer */);
3561 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3562
3563 /* Bind the source objects to the right place. */
3564 pState->ext.glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pSurface->oglId.texture, 0 /* level 0 */);
3565 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3566
3567 /* Blit the surface rectangle(s) to the back buffer. */
3568 if (cRects == 0)
3569 {
3570 Log(("view port (%d,%d)(%d,%d)\n", srcViewPort.x, srcViewPort.y, srcViewPort.cx, srcViewPort.cy));
3571 pState->ext.glBlitFramebuffer(srcViewPort.x,
3572 srcViewPort.y,
3573 srcViewPort.x + srcViewPort.cx, /* exclusive. */
3574 srcViewPort.y + srcViewPort.cy, /* exclusive. (reverse to flip the image) */
3575 0,
3576 pThis->svga.viewport.cy, /* exclusive. */
3577 pThis->svga.viewport.cx, /* exclusive. */
3578 0,
3579 GL_COLOR_BUFFER_BIT,
3580 GL_LINEAR);
3581 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3582 }
3583 else
3584 {
3585 for (uint32_t i = 0; i < cRects; i++)
3586 {
3587 if ( pRect[i].x + pRect[i].w <= pThis->svga.viewport.x
3588 || pThis->svga.viewport.x + pThis->svga.viewport.cx <= pRect[i].x
3589 || pRect[i].y + pRect[i].h <= pThis->svga.viewport.y
3590 || pThis->svga.viewport.y + pThis->svga.viewport.cy <= pRect[i].y)
3591 {
3592 /* Intersection is empty; skip */
3593 continue;
3594 }
3595
3596 pState->ext.glBlitFramebuffer(RT_MAX(pRect[i].srcx, srcViewPort.x),
3597 pSurface->pMipmapLevels[0].size.width - RT_MAX(pRect[i].srcy, srcViewPort.y), /* exclusive. (reverse to flip the image) */
3598 RT_MIN(pRect[i].srcx + pRect[i].w, srcViewPort.x + srcViewPort.cx), /* exclusive. */
3599 pSurface->pMipmapLevels[0].size.width - RT_MIN(pRect[i].srcy + pRect[i].h, srcViewPort.y + srcViewPort.cy),
3600 RT_MAX(pRect[i].x, pThis->svga.viewport.x) - pThis->svga.viewport.x,
3601 pThis->svga.uHeight - (RT_MIN(pRect[i].y + pRect[i].h, pThis->svga.viewport.y + pThis->svga.viewport.cy) - pThis->svga.viewport.y), /* exclusive. */
3602 RT_MIN(pRect[i].x + pRect[i].w, pThis->svga.viewport.x + pThis->svga.viewport.cx) - pThis->svga.viewport.x, /* exclusive. */
3603 pThis->svga.uHeight - (RT_MAX(pRect[i].y, pThis->svga.viewport.y) - pThis->svga.viewport.y),
3604 GL_COLOR_BUFFER_BIT,
3605 GL_LINEAR);
3606 }
3607 }
3608 /* Reset the frame buffer association */
3609 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
3610 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3611#endif
3612
3613 /* Flip the front and back buffers. */
3614#ifdef RT_OS_WINDOWS
3615 BOOL ret = SwapBuffers(pContext->hdc);
3616 AssertMsg(ret, ("SwapBuffers failed with %d\n", GetLastError()));
3617#elif defined(RT_OS_DARWIN)
3618 vmsvga3dCocoaSwapBuffers(pContext->cocoaView, pContext->cocoaContext);
3619#else
3620 /* show the window if not already done */
3621 if (!pContext->fMapped)
3622 {
3623 XMapWindow(pState->display, pContext->window);
3624 pContext->fMapped = true;
3625 }
3626 /* now swap the buffers, i.e. display the rendering result */
3627 glXSwapBuffers(pState->display, pContext->window);
3628#endif
3629 return VINF_SUCCESS;
3630}
3631
3632#ifdef RT_OS_LINUX
3633/**
3634 * X11 event handling thread
3635 * @param ThreadSelf thread handle
3636 * @param pvUser pointer to pState structure
3637 * @returns VBox status code
3638 */
3639DECLCALLBACK(int) vmsvga3dXEventThread(RTTHREAD ThreadSelf, void *pvUser)
3640{
3641 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pvUser;
3642 while (!pState->bTerminate)
3643 {
3644 while (XPending(pState->display) > 0)
3645 {
3646 XEvent event;
3647 XNextEvent(pState->display, &event);
3648
3649 switch (event.type)
3650 {
3651 default:
3652 break;
3653 }
3654 }
3655 /* sleep for 16ms to not burn too many cycles */
3656 RTThreadSleep(16);
3657 }
3658 return VINF_SUCCESS;
3659}
3660#endif // RT_OS_LINUX
3661
3662
3663/**
3664 * Create a new 3d context
3665 *
3666 * @returns VBox status code.
3667 * @param pThis VGA device instance data.
3668 * @param cid Context id
3669 * @param fOtherProfile When clear, the context is created using the default
3670 * OpenGL profile. When set, it's created using the
3671 * alternative profile. The latter is only allowed if
3672 * the VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE is set.
3673 */
3674int vmsvga3dContextDefine(PVGASTATE pThis, uint32_t cid, bool fOtherProfile)
3675{
3676 int rc;
3677 PVMSVGA3DCONTEXT pContext;
3678 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
3679
3680 AssertReturn(pState, VERR_NO_MEMORY);
3681 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
3682#if !defined(VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE) || !(defined(RT_OS_DARWIN))
3683 AssertReturn(!fOtherProfile, VERR_INTERNAL_ERROR_3);
3684#endif
3685
3686 Log(("vmsvga3dContextDefine id %x\n", cid));
3687#ifdef DEBUG_DEBUG_GFX_WINDOW_TEST_CONTEXT
3688 if (pState->idTestContext == SVGA_ID_INVALID)
3689 {
3690 pState->idTestContext = 207;
3691 rc = vmsvga3dContextDefine(pThis, pState->idTestContext, false /*fOtherProfile*/);
3692 AssertRCReturn(rc, rc);
3693 }
3694#endif
3695
3696 if (cid >= pState->cContexts)
3697 {
3698 pState->paContext = (PVMSVGA3DCONTEXT)RTMemRealloc(pState->paContext, sizeof(VMSVGA3DCONTEXT) * (cid + 1));
3699 AssertReturn(pState->paContext, VERR_NO_MEMORY);
3700 memset(&pState->paContext[pState->cContexts], 0, sizeof(VMSVGA3DCONTEXT) * (cid + 1 - pState->cContexts));
3701 for (uint32_t i = pState->cContexts; i < cid + 1; i++)
3702 pState->paContext[i].id = SVGA3D_INVALID_ID;
3703
3704 pState->cContexts = cid + 1;
3705 }
3706 /* If one already exists with this id, then destroy it now. */
3707 if (pState->paContext[cid].id != SVGA3D_INVALID_ID)
3708 vmsvga3dContextDestroy(pThis, cid);
3709
3710 pContext = &pState->paContext[cid];
3711 memset(pContext, 0, sizeof(*pContext));
3712 pContext->id = cid;
3713 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTexture); i++)
3714 pContext->aSidActiveTexture[i] = SVGA3D_INVALID_ID;
3715
3716 pContext->sidRenderTarget = SVGA3D_INVALID_ID;
3717 pContext->state.shidVertex = SVGA3D_INVALID_ID;
3718 pContext->state.shidPixel = SVGA3D_INVALID_ID;
3719 pContext->idFramebuffer = OPENGL_INVALID_ID;
3720 pContext->idReadFramebuffer = OPENGL_INVALID_ID;
3721 pContext->idDrawFramebuffer = OPENGL_INVALID_ID;
3722
3723 rc = ShaderContextCreate(&pContext->pShaderContext);
3724 AssertRCReturn(rc, rc);
3725
3726 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); i++)
3727 pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
3728
3729 AssertReturn(pThis->svga.u64HostWindowId, VERR_INTERNAL_ERROR);
3730
3731#ifdef RT_OS_WINDOWS
3732 /* Create a context window. */
3733 CREATESTRUCT cs;
3734 cs.lpCreateParams = NULL;
3735 cs.dwExStyle = WS_EX_NOACTIVATE | WS_EX_NOPARENTNOTIFY | WS_EX_TRANSPARENT;
3736# ifdef DEBUG_GFX_WINDOW
3737 cs.lpszName = (char *)RTMemAllocZ(256);
3738 RTStrPrintf((char *)cs.lpszName, 256, "Context %d OpenGL Window", cid);
3739# else
3740 cs.lpszName = NULL;
3741# endif
3742 cs.lpszClass = 0;
3743# ifdef DEBUG_GFX_WINDOW
3744 cs.style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE | WS_CAPTION;
3745# else
3746 cs.style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED | WS_CHILD | WS_VISIBLE;
3747# endif
3748 cs.x = 0;
3749 cs.y = 0;
3750 cs.cx = pThis->svga.uWidth;
3751 cs.cy = pThis->svga.uHeight;
3752 cs.hwndParent = (HWND)pThis->svga.u64HostWindowId;
3753 cs.hMenu = NULL;
3754 cs.hInstance = pState->hInstance;
3755
3756 rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_CREATEWINDOW, (WPARAM)&pContext->hwnd, (LPARAM)&cs);
3757 AssertRCReturn(rc, rc);
3758
3759 pContext->hdc = GetDC(pContext->hwnd);
3760 AssertMsgReturn(pContext->hdc, ("GetDC %x failed with %d\n", pContext->hwnd, GetLastError()), VERR_INTERNAL_ERROR);
3761
3762 PIXELFORMATDESCRIPTOR pfd = {
3763 sizeof(PIXELFORMATDESCRIPTOR), /* size of this pfd */
3764 1, /* version number */
3765 PFD_DRAW_TO_WINDOW | /* support window */
3766 PFD_DOUBLEBUFFER | /* support double buffering */
3767 PFD_SUPPORT_OPENGL, /* support OpenGL */
3768 PFD_TYPE_RGBA, /* RGBA type */
3769 24, /* 24-bit color depth */
3770 0, 0, 0, 0, 0, 0, /* color bits ignored */
3771 8, /* alpha buffer */
3772 0, /* shift bit ignored */
3773 0, /* no accumulation buffer */
3774 0, 0, 0, 0, /* accum bits ignored */
3775 16, /* set depth buffer */
3776 16, /* set stencil buffer */
3777 0, /* no auxiliary buffer */
3778 PFD_MAIN_PLANE, /* main layer */
3779 0, /* reserved */
3780 0, 0, 0 /* layer masks ignored */
3781 };
3782 int pixelFormat;
3783 BOOL ret;
3784
3785 pixelFormat = ChoosePixelFormat(pContext->hdc, &pfd);
3786 /* @todo is this really necessary?? */
3787 pixelFormat = ChoosePixelFormat(pContext->hdc, &pfd);
3788 AssertMsgReturn(pixelFormat != 0, ("ChoosePixelFormat failed with %d\n", GetLastError()), VERR_INTERNAL_ERROR);
3789
3790 ret = SetPixelFormat(pContext->hdc, pixelFormat, &pfd);
3791 AssertMsgReturn(ret == TRUE, ("SetPixelFormat failed with %d\n", GetLastError()), VERR_INTERNAL_ERROR);
3792
3793 pContext->hglrc = wglCreateContext(pContext->hdc);
3794 AssertMsgReturn(pContext->hglrc, ("wglCreateContext %x failed with %d\n", pContext->hdc, GetLastError()), VERR_INTERNAL_ERROR);
3795
3796 // TODO isn't this default on Linux since OpenGL 1.1?
3797 /* Find the first active context to share the display list with (necessary for sharing e.g. textures between contexts). */
3798 for (uint32_t i = 0; i < pState->cContexts; i++)
3799 {
3800 if ( pState->paContext[i].id != SVGA3D_INVALID_ID
3801 && i != pContext->id)
3802 {
3803 Log(("Sharing display lists between cid=%d and cid=%d\n", pContext->id, i));
3804 ret = wglShareLists(pState->paContext[i].hglrc, pContext->hglrc);
3805 Assert(ret == TRUE);
3806 break;
3807 }
3808 }
3809
3810#elif defined(RT_OS_DARWIN)
3811 pContext->fOtherProfile = fOtherProfile;
3812
3813 /* Find the first active context to share the display list with (necessary for sharing e.g. textures between contexts). */
3814 NativeNSOpenGLContextRef 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 && pState->paContext[i].fOtherProfile == fOtherProfile)
3820 {
3821 Log(("Sharing display lists between cid=%d and cid=%d\n", pContext->id, i));
3822 shareContext = pState->paContext[i].cocoaContext;
3823 break;
3824 }
3825 }
3826 vmsvga3dCocoaCreateContext(&pContext->cocoaContext, shareContext, fOtherProfile);
3827 NativeNSViewRef pHostView = (NativeNSViewRef)pThis->svga.u64HostWindowId;
3828 vmsvga3dCocoaCreateView(&pContext->cocoaView, pHostView);
3829
3830#else
3831 Window hostWindow = (Window)pThis->svga.u64HostWindowId;
3832
3833 if (pState->display == NULL)
3834 {
3835 /* get an X display and make sure we have glX 1.3 */
3836 pState->display = XOpenDisplay(0);
3837 Assert(pState->display);
3838 int glxMajor, glxMinor;
3839 Bool ret = glXQueryVersion(pState->display, &glxMajor, &glxMinor);
3840 AssertMsgReturn(ret && glxMajor == 1 && glxMinor >= 3, ("glX >=1.3 not present"), VERR_INTERNAL_ERROR);
3841 /* start our X event handling thread */
3842 rc = RTThreadCreate(&pState->pWindowThread, vmsvga3dXEventThread, pState, 0, RTTHREADTYPE_GUI, RTTHREADFLAGS_WAITABLE, "VMSVGA3DXEVENT");
3843 if (RT_FAILURE(rc))
3844 {
3845 AssertMsgFailed(("%s: Async IO Thread creation for 3d window handling failed rc=%d\n", __FUNCTION__, rc));
3846 return rc;
3847 }
3848 }
3849 int attrib[] =
3850 {
3851 GLX_RGBA,
3852 GLX_RED_SIZE, 1,
3853 GLX_GREEN_SIZE, 1,
3854 GLX_BLUE_SIZE, 1,
3855 //GLX_ALPHA_SIZE, 1, this flips the bbos screen
3856 GLX_DOUBLEBUFFER,
3857 None
3858 };
3859 XVisualInfo *vi = glXChooseVisual(pState->display, DefaultScreen(pState->display), attrib);
3860 XSetWindowAttributes swa;
3861 swa.colormap = XCreateColormap(pState->display, XDefaultRootWindow(pState->display), vi->visual, AllocNone);
3862 swa.border_pixel = 0;
3863 swa.background_pixel = 0;
3864 swa.event_mask = StructureNotifyMask | ExposureMask;
3865 unsigned long flags = CWBorderPixel | CWBackPixel | CWColormap | CWEventMask;
3866 pContext->window = XCreateWindow(pState->display, hostWindow,//XDefaultRootWindow(pState->display),//hostWindow,
3867 0, 0, pThis->svga.uWidth, pThis->svga.uHeight,
3868 0, vi->depth, InputOutput,
3869 vi->visual, flags, &swa);
3870 AssertMsgReturn(pContext->window, ("XCreateWindow failed"), VERR_INTERNAL_ERROR);
3871 uint32_t cardinal_alpha = (uint32_t) (0.5 * (uint32_t)-1) ;
3872
3873 /* the window is hidden by default and only mapped when CommandPresent is executed on it */
3874
3875 /* Find the first active context to share the display list with (necessary for sharing e.g. textures between contexts). */
3876 GLXContext shareContext = NULL;
3877 for (uint32_t i = 0; i < pState->cContexts; i++)
3878 {
3879 if ( pState->paContext[i].id != SVGA3D_INVALID_ID
3880 && i != pContext->id)
3881 {
3882 Log(("Sharing display lists between cid=%d and cid=%d\n", pContext->id, i));
3883 shareContext = pState->paContext[i].glxContext;
3884 break;
3885 }
3886 }
3887
3888 pContext->glxContext = glXCreateContext(pState->display, vi, shareContext, GL_TRUE);
3889 AssertMsgReturn(pContext->glxContext, ("glXCreateContext failed"), VERR_INTERNAL_ERROR);
3890#endif
3891
3892 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3893
3894 /* NULL during the first PowerOn call. */
3895 if (pState->ext.glGenFramebuffers)
3896 {
3897 /* Create a framebuffer object for this context. */
3898 pState->ext.glGenFramebuffers(1, &pContext->idFramebuffer);
3899 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3900
3901 /* Bind the object to the framebuffer target. */
3902 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
3903 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3904
3905 /* Create read and draw framebuffer objects for this context. */
3906 pState->ext.glGenFramebuffers(1, &pContext->idReadFramebuffer);
3907 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3908
3909 pState->ext.glGenFramebuffers(1, &pContext->idDrawFramebuffer);
3910 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3911
3912 }
3913#if 0
3914 /* @todo move to shader lib!!! */
3915 /* Clear the screen */
3916 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3917
3918 glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
3919 glClearIndex(0);
3920 glClearDepth(1);
3921 glClearStencil(0xffff);
3922 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
3923 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
3924 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
3925 if (pState->ext.glProvokingVertex)
3926 pState->ext.glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);
3927 /* @todo move to shader lib!!! */
3928#endif
3929 return VINF_SUCCESS;
3930}
3931
3932/**
3933 * Destroy an existing 3d context
3934 *
3935 * @returns VBox status code.
3936 * @param pThis VGA device instance data.
3937 * @param cid Context id
3938 */
3939int vmsvga3dContextDestroy(PVGASTATE pThis, uint32_t cid)
3940{
3941 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
3942 AssertReturn(pState, VERR_NO_MEMORY);
3943
3944 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
3945
3946 if ( cid < pState->cContexts
3947 && pState->paContext[cid].id == cid)
3948 {
3949 PVMSVGA3DCONTEXT pContext = &pState->paContext[cid];
3950
3951 Log(("vmsvga3dContextDestroy id %x\n", cid));
3952
3953 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3954
3955 /* Destroy all leftover pixel shaders. */
3956 for (uint32_t i = 0; i < pContext->cPixelShaders; i++)
3957 {
3958 if (pContext->paPixelShader[i].id != SVGA3D_INVALID_ID)
3959 vmsvga3dShaderDestroy(pThis, pContext->paPixelShader[i].cid, pContext->paPixelShader[i].id, pContext->paPixelShader[i].type);
3960 }
3961 if (pContext->paPixelShader)
3962 RTMemFree(pContext->paPixelShader);
3963
3964 /* Destroy all leftover vertex shaders. */
3965 for (uint32_t i = 0; i < pContext->cVertexShaders; i++)
3966 {
3967 if (pContext->paVertexShader[i].id != SVGA3D_INVALID_ID)
3968 vmsvga3dShaderDestroy(pThis, pContext->paVertexShader[i].cid, pContext->paVertexShader[i].id, pContext->paVertexShader[i].type);
3969 }
3970 if (pContext->paVertexShader)
3971 RTMemFree(pContext->paVertexShader);
3972
3973 if (pContext->state.paVertexShaderConst)
3974 RTMemFree(pContext->state.paVertexShaderConst);
3975 if (pContext->state.paPixelShaderConst)
3976 RTMemFree(pContext->state.paPixelShaderConst);
3977
3978 if (pContext->pShaderContext)
3979 {
3980 int rc = ShaderContextDestroy(pContext->pShaderContext);
3981 AssertRC(rc);
3982 }
3983
3984#if 1 /* This is done on windows - prevents various assertions at runtime, as well as shutdown & reset assertions when destroying surfaces. */
3985 /* Check for all surfaces that are associated with this context to remove all dependencies */
3986 for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
3987 {
3988 PVMSVGA3DSURFACE pSurface = &pState->paSurface[sid];
3989 if ( pSurface->idAssociatedContext == cid
3990 && pSurface->id == sid)
3991 {
3992 int rc;
3993
3994 Log(("vmsvga3dContextDestroy: remove all dependencies for surface %x\n", sid));
3995
3996 uint32_t surfaceFlags = pSurface->flags;
3997 SVGA3dSurfaceFormat format = pSurface->format;
3998 SVGA3dSurfaceFace face[SVGA3D_MAX_SURFACE_FACES];
3999 uint32_t multisampleCount = pSurface->multiSampleCount;
4000 SVGA3dTextureFilter autogenFilter = pSurface->autogenFilter;
4001 SVGA3dSize *pMipLevelSize;
4002 uint32_t cFaces = pSurface->cFaces;
4003
4004 pMipLevelSize = (SVGA3dSize *)RTMemAllocZ(pSurface->faces[0].numMipLevels * pSurface->cFaces * sizeof(SVGA3dSize));
4005 AssertReturn(pMipLevelSize, VERR_NO_MEMORY);
4006
4007 for (uint32_t iFace = 0; iFace < pSurface->cFaces; iFace++)
4008 {
4009 for (uint32_t i = 0; i < pSurface->faces[0].numMipLevels; i++)
4010 {
4011 uint32_t idx = i + iFace * pSurface->faces[0].numMipLevels;
4012 memcpy(&pMipLevelSize[idx], &pSurface->pMipmapLevels[idx].size, sizeof(SVGA3dSize));
4013 }
4014 }
4015 memcpy(face, pSurface->faces, sizeof(pSurface->faces));
4016
4017 /* Recreate the surface with the original settings; destroys the contents, but that seems fairly safe since the context is also destroyed. */
4018 rc = vmsvga3dSurfaceDestroy(pThis, sid);
4019 AssertRC(rc);
4020
4021 rc = vmsvga3dSurfaceDefine(pThis, sid, surfaceFlags, format, face, multisampleCount, autogenFilter, face[0].numMipLevels * cFaces, pMipLevelSize);
4022 AssertRC(rc);
4023 }
4024 }
4025#endif
4026
4027 if (pContext->idFramebuffer != OPENGL_INVALID_ID)
4028 {
4029 /* Unbind the object from the framebuffer target. */
4030 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, 0 /* back buffer */);
4031 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4032 pState->ext.glDeleteFramebuffers(1, &pContext->idFramebuffer);
4033 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4034
4035 if (pContext->idReadFramebuffer != OPENGL_INVALID_ID)
4036 {
4037 pState->ext.glDeleteFramebuffers(1, &pContext->idReadFramebuffer);
4038 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4039 }
4040 if (pContext->idDrawFramebuffer != OPENGL_INVALID_ID)
4041 {
4042 pState->ext.glDeleteFramebuffers(1, &pContext->idDrawFramebuffer);
4043 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4044 }
4045 }
4046#ifdef RT_OS_WINDOWS
4047 wglMakeCurrent(NULL, NULL);
4048 wglDeleteContext(pContext->hglrc);
4049 ReleaseDC(pContext->hwnd, pContext->hdc);
4050
4051 /* Destroy the window we've created. */
4052 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_DESTROYWINDOW, (WPARAM)pContext->hwnd, 0);
4053 AssertRC(rc);
4054#elif defined(RT_OS_DARWIN)
4055 vmsvga3dCocoaDestroyView(pContext->cocoaView);
4056 vmsvga3dCocoaDestroyContext(pContext->cocoaContext);
4057#elif defined(RT_OS_LINUX)
4058 glXMakeCurrent(pState->display, None, NULL);
4059 glXDestroyContext(pState->display, pContext->glxContext);
4060 XDestroyWindow(pState->display, pContext->window);
4061#endif
4062
4063 memset(pContext, 0, sizeof(*pContext));
4064 pContext->id = SVGA3D_INVALID_ID;
4065
4066 VMSVGA3D_CLEAR_CURRENT_CONTEXT(pState);
4067 }
4068 else
4069 AssertFailed();
4070
4071 return VINF_SUCCESS;
4072}
4073
4074/* Handle resize */
4075int vmsvga3dChangeMode(PVGASTATE pThis)
4076{
4077 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4078 AssertReturn(pState, VERR_NO_MEMORY);
4079
4080 /* Resize all active contexts. */
4081 for (uint32_t i = 0; i < pState->cContexts; i++)
4082 {
4083 PVMSVGA3DCONTEXT pContext = &pState->paContext[i];
4084 uint32_t cid = pContext->id;
4085
4086 if (cid != SVGA3D_INVALID_ID)
4087 {
4088#ifdef RT_OS_WINDOWS
4089 CREATESTRUCT cs;
4090
4091 memset(&cs, 0, sizeof(cs));
4092 cs.cx = pThis->svga.uWidth;
4093 cs.cy = pThis->svga.uHeight;
4094
4095 /* Resize the window. */
4096 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_RESIZEWINDOW, (WPARAM)pContext->hwnd, (LPARAM)&cs);
4097 AssertRC(rc);
4098#elif defined(RT_OS_DARWIN)
4099 vmsvga3dCocoaViewSetSize(pContext->cocoaView, pThis->svga.uWidth, pThis->svga.uHeight);
4100#elif defined(RT_OS_LINUX)
4101 XWindowChanges wc;
4102 wc.width = pThis->svga.uWidth;
4103 wc.height = pThis->svga.uHeight;
4104 XConfigureWindow(pState->display, pContext->window, CWWidth | CWHeight, &wc);
4105#endif
4106 }
4107 }
4108 return VINF_SUCCESS;
4109}
4110
4111
4112int vmsvga3dSetTransform(PVGASTATE pThis, uint32_t cid, SVGA3dTransformType type, float matrix[16])
4113{
4114 PVMSVGA3DCONTEXT pContext;
4115 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4116 AssertReturn(pState, VERR_NO_MEMORY);
4117 bool fModelViewChanged = false;
4118
4119 Log(("vmsvga3dSetTransform cid=%x %s\n", cid, vmsvgaTransformToString(type)));
4120
4121 if ( cid >= pState->cContexts
4122 || pState->paContext[cid].id != cid)
4123 {
4124 Log(("vmsvga3dSetTransform invalid context id!\n"));
4125 return VERR_INVALID_PARAMETER;
4126 }
4127 pContext = &pState->paContext[cid];
4128 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4129
4130 /* Save this matrix for vm state save/restore. */
4131 pContext->state.aTransformState[type].fValid = true;
4132 memcpy(pContext->state.aTransformState[type].matrix, matrix, sizeof(pContext->state.aTransformState[type].matrix));
4133 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_TRANSFORM;
4134
4135 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)));
4136 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)));
4137 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)));
4138 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)));
4139
4140 switch (type)
4141 {
4142 case SVGA3D_TRANSFORM_VIEW:
4143 /* View * World = Model View */
4144 glMatrixMode(GL_MODELVIEW);
4145 glLoadMatrixf(matrix);
4146 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_WORLD].fValid)
4147 glMultMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_WORLD].matrix);
4148 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4149 fModelViewChanged = true;
4150 break;
4151
4152 case SVGA3D_TRANSFORM_PROJECTION:
4153 {
4154 int rc = ShaderTransformProjection(pContext->state.RectViewPort.w, pContext->state.RectViewPort.h, matrix);
4155 AssertRCReturn(rc, rc);
4156 break;
4157 }
4158
4159 case SVGA3D_TRANSFORM_TEXTURE0:
4160 glMatrixMode(GL_TEXTURE);
4161 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4162 glLoadMatrixf(matrix);
4163 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4164 break;
4165
4166 case SVGA3D_TRANSFORM_TEXTURE1:
4167 case SVGA3D_TRANSFORM_TEXTURE2:
4168 case SVGA3D_TRANSFORM_TEXTURE3:
4169 case SVGA3D_TRANSFORM_TEXTURE4:
4170 case SVGA3D_TRANSFORM_TEXTURE5:
4171 case SVGA3D_TRANSFORM_TEXTURE6:
4172 case SVGA3D_TRANSFORM_TEXTURE7:
4173 Log(("vmsvga3dSetTransform: unsupported SVGA3D_TRANSFORM_TEXTUREx transform!!\n"));
4174 return VERR_INVALID_PARAMETER;
4175
4176 case SVGA3D_TRANSFORM_WORLD:
4177 /* View * World = Model View */
4178 glMatrixMode(GL_MODELVIEW);
4179 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].fValid)
4180 glLoadMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
4181 else
4182 glLoadIdentity();
4183 glMultMatrixf(matrix);
4184 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4185 fModelViewChanged = true;
4186 break;
4187
4188 case SVGA3D_TRANSFORM_WORLD1:
4189 case SVGA3D_TRANSFORM_WORLD2:
4190 case SVGA3D_TRANSFORM_WORLD3:
4191 Log(("vmsvga3dSetTransform: unsupported SVGA3D_TRANSFORM_WORLDx transform!!\n"));
4192 return VERR_INVALID_PARAMETER;
4193
4194 default:
4195 Log(("vmsvga3dSetTransform: unknown type!!\n"));
4196 return VERR_INVALID_PARAMETER;
4197 }
4198
4199 /* Apparently we need to reset the light and clip data after modifying the modelview matrix. */
4200 if (fModelViewChanged)
4201 {
4202 /* Reprogram the clip planes. */
4203 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aClipPlane); j++)
4204 {
4205 if (pContext->state.aClipPlane[j].fValid == true)
4206 vmsvga3dSetClipPlane(pThis, cid, j, pContext->state.aClipPlane[j].plane);
4207 }
4208
4209 /* Reprogram the light data. */
4210 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aLightData); j++)
4211 {
4212 if (pContext->state.aLightData[j].fValidData == true)
4213 vmsvga3dSetLightData(pThis, cid, j, &pContext->state.aLightData[j].data);
4214 }
4215 }
4216
4217 return VINF_SUCCESS;
4218}
4219
4220int vmsvga3dSetZRange(PVGASTATE pThis, uint32_t cid, SVGA3dZRange zRange)
4221{
4222 PVMSVGA3DCONTEXT pContext;
4223 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4224 AssertReturn(pState, VERR_NO_MEMORY);
4225
4226 Log(("vmsvga3dSetZRange cid=%x min=%d max=%d\n", cid, (uint32_t)(zRange.min * 100.0), (uint32_t)(zRange.max * 100.0)));
4227
4228 if ( cid >= pState->cContexts
4229 || pState->paContext[cid].id != cid)
4230 {
4231 Log(("vmsvga3dSetZRange invalid context id!\n"));
4232 return VERR_INVALID_PARAMETER;
4233 }
4234 pContext = &pState->paContext[cid];
4235 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4236
4237 pContext->state.zRange = zRange;
4238 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_ZRANGE;
4239
4240 if (zRange.min < -1.0)
4241 zRange.min = -1.0;
4242 if (zRange.max > 1.0)
4243 zRange.max = 1.0;
4244
4245 glDepthRange((GLdouble)zRange.min, (GLdouble)zRange.max);
4246 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4247 return VINF_SUCCESS;
4248}
4249
4250/**
4251 * Convert SVGA blend op value to its OpenGL equivalent
4252 */
4253static GLenum vmsvga3dBlendOp2GL(uint32_t blendOp)
4254{
4255 switch (blendOp)
4256 {
4257 case SVGA3D_BLENDOP_ZERO:
4258 return GL_ZERO;
4259 case SVGA3D_BLENDOP_ONE:
4260 return GL_ONE;
4261 case SVGA3D_BLENDOP_SRCCOLOR:
4262 return GL_SRC_COLOR;
4263 case SVGA3D_BLENDOP_INVSRCCOLOR:
4264 return GL_ONE_MINUS_SRC_COLOR;
4265 case SVGA3D_BLENDOP_SRCALPHA:
4266 return GL_SRC_ALPHA;
4267 case SVGA3D_BLENDOP_INVSRCALPHA:
4268 return GL_ONE_MINUS_SRC_ALPHA;
4269 case SVGA3D_BLENDOP_DESTALPHA:
4270 return GL_DST_ALPHA;
4271 case SVGA3D_BLENDOP_INVDESTALPHA:
4272 return GL_ONE_MINUS_DST_ALPHA;
4273 case SVGA3D_BLENDOP_DESTCOLOR:
4274 return GL_DST_COLOR;
4275 case SVGA3D_BLENDOP_INVDESTCOLOR:
4276 return GL_ONE_MINUS_DST_COLOR;
4277 case SVGA3D_BLENDOP_SRCALPHASAT:
4278 return GL_SRC_ALPHA_SATURATE;
4279 case SVGA3D_BLENDOP_BLENDFACTOR:
4280 return GL_CONSTANT_ALPHA; /* @todo correct?? */
4281 case SVGA3D_BLENDOP_INVBLENDFACTOR:
4282 return GL_ONE_MINUS_CONSTANT_ALPHA; /* @todo correct?? */
4283 default:
4284 AssertFailed();
4285 return GL_ONE;
4286 }
4287}
4288
4289static GLenum vmsvga3dBlendEquation2GL(uint32_t blendEq)
4290{
4291 switch (blendEq)
4292 {
4293 case SVGA3D_BLENDEQ_ADD:
4294 return GL_FUNC_ADD;
4295 case SVGA3D_BLENDEQ_SUBTRACT:
4296 return GL_FUNC_SUBTRACT;
4297 case SVGA3D_BLENDEQ_REVSUBTRACT:
4298 return GL_FUNC_REVERSE_SUBTRACT;
4299 case SVGA3D_BLENDEQ_MINIMUM:
4300 return GL_MIN;
4301 case SVGA3D_BLENDEQ_MAXIMUM:
4302 return GL_MAX;
4303 default:
4304 AssertFailed();
4305 return GL_FUNC_ADD;
4306 }
4307}
4308
4309static GLenum vmsvgaCmpFunc2GL(uint32_t cmpFunc)
4310{
4311 switch (cmpFunc)
4312 {
4313 case SVGA3D_CMP_NEVER:
4314 return GL_NEVER;
4315 case SVGA3D_CMP_LESS:
4316 return GL_LESS;
4317 case SVGA3D_CMP_EQUAL:
4318 return GL_EQUAL;
4319 case SVGA3D_CMP_LESSEQUAL:
4320 return GL_LEQUAL;
4321 case SVGA3D_CMP_GREATER:
4322 return GL_GREATER;
4323 case SVGA3D_CMP_NOTEQUAL:
4324 return GL_NOTEQUAL;
4325 case SVGA3D_CMP_GREATEREQUAL:
4326 return GL_GEQUAL;
4327 case SVGA3D_CMP_ALWAYS:
4328 return GL_ALWAYS;
4329 default:
4330 AssertFailed();
4331 return GL_LESS;
4332 }
4333}
4334
4335static GLenum vmsvgaStencipOp2GL(uint32_t stencilOp)
4336{
4337 switch (stencilOp)
4338 {
4339 case SVGA3D_STENCILOP_KEEP:
4340 return GL_KEEP;
4341 case SVGA3D_STENCILOP_ZERO:
4342 return GL_ZERO;
4343 case SVGA3D_STENCILOP_REPLACE:
4344 return GL_REPLACE;
4345 case SVGA3D_STENCILOP_INCRSAT:
4346 return GL_INCR_WRAP;
4347 case SVGA3D_STENCILOP_DECRSAT:
4348 return GL_DECR_WRAP;
4349 case SVGA3D_STENCILOP_INVERT:
4350 return GL_INVERT;
4351 case SVGA3D_STENCILOP_INCR:
4352 return GL_INCR;
4353 case SVGA3D_STENCILOP_DECR:
4354 return GL_DECR;
4355 default:
4356 AssertFailed();
4357 return GL_KEEP;
4358 }
4359}
4360
4361int vmsvga3dSetRenderState(PVGASTATE pThis, uint32_t cid, uint32_t cRenderStates, SVGA3dRenderState *pRenderState)
4362{
4363 uint32_t val;
4364 PVMSVGA3DCONTEXT pContext;
4365 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
4366 AssertReturn(pState, VERR_NO_MEMORY);
4367
4368 Log(("vmsvga3dSetRenderState cid=%x cRenderStates=%d\n", cid, cRenderStates));
4369
4370 if ( cid >= pState->cContexts
4371 || pState->paContext[cid].id != cid)
4372 {
4373 Log(("vmsvga3dSetRenderState invalid context id!\n"));
4374 return VERR_INVALID_PARAMETER;
4375 }
4376 pContext = &pState->paContext[cid];
4377 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4378
4379 for (unsigned i = 0; i < cRenderStates; i++)
4380 {
4381 GLenum enableCap = ~0U;
4382 Log(("vmsvga3dSetRenderState: cid=%d state=%s (%d) val=%x\n", cid, vmsvga3dGetRenderStateName(pRenderState[i].state), pRenderState[i].state, pRenderState[i].uintValue));
4383 /* Save the render state for vm state saving. */
4384 if (pRenderState[i].state < SVGA3D_RS_MAX)
4385 pContext->state.aRenderState[pRenderState[i].state] = pRenderState[i];
4386
4387 switch (pRenderState[i].state)
4388 {
4389 case SVGA3D_RS_ZENABLE: /* SVGA3dBool */
4390 enableCap = GL_DEPTH_TEST;
4391 val = pRenderState[i].uintValue;
4392 break;
4393
4394 case SVGA3D_RS_ZWRITEENABLE: /* SVGA3dBool */
4395 glDepthMask(!!pRenderState[i].uintValue);
4396 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4397 break;
4398
4399 case SVGA3D_RS_ALPHATESTENABLE: /* SVGA3dBool */
4400 enableCap = GL_ALPHA_TEST;
4401 val = pRenderState[i].uintValue;
4402 break;
4403
4404 case SVGA3D_RS_DITHERENABLE: /* SVGA3dBool */
4405 enableCap = GL_DITHER;
4406 val = pRenderState[i].uintValue;
4407 break;
4408
4409 case SVGA3D_RS_FOGENABLE: /* SVGA3dBool */
4410 enableCap = GL_FOG;
4411 val = pRenderState[i].uintValue;
4412 break;
4413
4414 case SVGA3D_RS_SPECULARENABLE: /* SVGA3dBool */
4415 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
4416 break;
4417
4418 case SVGA3D_RS_LIGHTINGENABLE: /* SVGA3dBool */
4419 enableCap = GL_LIGHTING;
4420 val = pRenderState[i].uintValue;
4421 break;
4422
4423 case SVGA3D_RS_NORMALIZENORMALS: /* SVGA3dBool */
4424 /* not applicable */
4425 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
4426 break;
4427
4428 case SVGA3D_RS_POINTSPRITEENABLE: /* SVGA3dBool */
4429 enableCap = GL_POINT_SPRITE_ARB;
4430 val = pRenderState[i].uintValue;
4431 break;
4432
4433 case SVGA3D_RS_POINTSIZE: /* float */
4434 /* @todo we need to apply scaling for point sizes below the min or above the max; see Wine) */
4435 if (pRenderState[i].floatValue < pState->caps.flPointSize[0])
4436 pRenderState[i].floatValue = pState->caps.flPointSize[0];
4437 if (pRenderState[i].floatValue > pState->caps.flPointSize[1])
4438 pRenderState[i].floatValue = pState->caps.flPointSize[1];
4439
4440 glPointSize(pRenderState[i].floatValue);
4441 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4442 Log(("SVGA3D_RS_POINTSIZE: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
4443 break;
4444
4445 case SVGA3D_RS_POINTSIZEMIN: /* float */
4446 pState->ext.glPointParameterf(GL_POINT_SIZE_MIN, pRenderState[i].floatValue);
4447 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4448 Log(("SVGA3D_RS_POINTSIZEMIN: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
4449 break;
4450
4451 case SVGA3D_RS_POINTSIZEMAX: /* float */
4452 pState->ext.glPointParameterf(GL_POINT_SIZE_MAX, pRenderState[i].floatValue);
4453 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4454 Log(("SVGA3D_RS_POINTSIZEMAX: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
4455 break;
4456
4457 case SVGA3D_RS_POINTSCALEENABLE: /* SVGA3dBool */
4458 case SVGA3D_RS_POINTSCALE_A: /* float */
4459 case SVGA3D_RS_POINTSCALE_B: /* float */
4460 case SVGA3D_RS_POINTSCALE_C: /* float */
4461 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
4462 break;
4463
4464 case SVGA3D_RS_AMBIENT: /* SVGA3dColor */
4465 {
4466 GLfloat color[4]; /* red, green, blue, alpha */
4467
4468 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &color[0], &color[1], &color[2], &color[3]);
4469
4470 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, color);
4471 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4472 break;
4473 }
4474
4475 case SVGA3D_RS_CLIPPLANEENABLE: /* SVGA3dClipPlanes */
4476 {
4477 AssertCompile(SVGA3D_CLIPPLANE_MAX == (1 << 5));
4478 for (uint32_t j = 0; j <= 5; j++)
4479 {
4480 if (pRenderState[i].uintValue & RT_BIT(j))
4481 glEnable(GL_CLIP_PLANE0 + j);
4482 else
4483 glDisable(GL_CLIP_PLANE0 + j);
4484 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4485 }
4486 break;
4487 }
4488
4489 case SVGA3D_RS_FOGCOLOR: /* SVGA3dColor */
4490 {
4491 GLfloat color[4]; /* red, green, blue, alpha */
4492
4493 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &color[0], &color[1], &color[2], &color[3]);
4494
4495 glFogfv(GL_FOG_COLOR, color);
4496 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4497 break;
4498 }
4499
4500 case SVGA3D_RS_FOGSTART: /* float */
4501 glFogf(GL_FOG_START, pRenderState[i].floatValue);
4502 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4503 break;
4504
4505 case SVGA3D_RS_FOGEND: /* float */
4506 glFogf(GL_FOG_END, pRenderState[i].floatValue);
4507 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4508 break;
4509
4510 case SVGA3D_RS_FOGDENSITY: /* float */
4511 glFogf(GL_FOG_DENSITY, pRenderState[i].floatValue);
4512 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4513 break;
4514
4515 case SVGA3D_RS_RANGEFOGENABLE: /* SVGA3dBool */
4516 glFogi(GL_FOG_COORD_SRC, (pRenderState[i].uintValue) ? GL_FOG_COORD : GL_FRAGMENT_DEPTH);
4517 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4518 break;
4519
4520 case SVGA3D_RS_FOGMODE: /* SVGA3dFogMode */
4521 {
4522 SVGA3dFogMode mode;
4523 mode.uintValue = pRenderState[i].uintValue;
4524
4525 enableCap = GL_FOG_MODE;
4526 switch (mode.s.function)
4527 {
4528 case SVGA3D_FOGFUNC_EXP:
4529 val = GL_EXP;
4530 break;
4531 case SVGA3D_FOGFUNC_EXP2:
4532 val = GL_EXP2;
4533 break;
4534 case SVGA3D_FOGFUNC_LINEAR:
4535 val = GL_LINEAR;
4536 break;
4537 default:
4538 AssertMsgFailedReturn(("Unexpected fog function %d\n", mode.s.function), VERR_INTERNAL_ERROR);
4539 break;
4540 }
4541
4542 /* @todo how to switch between vertex and pixel fog modes??? */
4543 Assert(mode.s.type == SVGA3D_FOGTYPE_PIXEL);
4544#if 0
4545 /* The fog type determines the render state. */
4546 switch (mode.s.type)
4547 {
4548 case SVGA3D_FOGTYPE_VERTEX:
4549 renderState = D3DRS_FOGVERTEXMODE;
4550 break;
4551 case SVGA3D_FOGTYPE_PIXEL:
4552 renderState = D3DRS_FOGTABLEMODE;
4553 break;
4554 default:
4555 AssertMsgFailedReturn(("Unexpected fog type %d\n", mode.s.type), VERR_INTERNAL_ERROR);
4556 break;
4557 }
4558#endif
4559
4560 /* Set the fog base to depth or range. */
4561 switch (mode.s.base)
4562 {
4563 case SVGA3D_FOGBASE_DEPTHBASED:
4564 glFogi(GL_FOG_COORD_SRC, GL_FRAGMENT_DEPTH);
4565 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4566 break;
4567 case SVGA3D_FOGBASE_RANGEBASED:
4568 glFogi(GL_FOG_COORD_SRC, GL_FOG_COORD);
4569 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4570 break;
4571 default:
4572 /* ignore */
4573 AssertMsgFailed(("Unexpected fog base %d\n", mode.s.base));
4574 break;
4575 }
4576 break;
4577 }
4578
4579 case SVGA3D_RS_FILLMODE: /* SVGA3dFillMode */
4580 {
4581 SVGA3dFillMode mode;
4582
4583 mode.uintValue = pRenderState[i].uintValue;
4584
4585 switch (mode.s.mode)
4586 {
4587 case SVGA3D_FILLMODE_POINT:
4588 val = GL_POINT;
4589 break;
4590 case SVGA3D_FILLMODE_LINE:
4591 val = GL_LINE;
4592 break;
4593 case SVGA3D_FILLMODE_FILL:
4594 val = GL_FILL;
4595 break;
4596 default:
4597 AssertMsgFailedReturn(("Unexpected fill mode %d\n", mode.s.mode), VERR_INTERNAL_ERROR);
4598 break;
4599 }
4600 /* @note only front and back faces */
4601 Assert(mode.s.face == SVGA3D_FACE_FRONT_BACK);
4602 glPolygonMode(GL_FRONT_AND_BACK, val);
4603 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4604 break;
4605 }
4606
4607 case SVGA3D_RS_SHADEMODE: /* SVGA3dShadeMode */
4608 switch (pRenderState[i].uintValue)
4609 {
4610 case SVGA3D_SHADEMODE_FLAT:
4611 val = GL_FLAT;
4612 break;
4613
4614 case SVGA3D_SHADEMODE_SMOOTH:
4615 val = GL_SMOOTH;
4616 break;
4617
4618 default:
4619 AssertMsgFailedReturn(("Unexpected shade mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
4620 break;
4621 }
4622
4623 glShadeModel(val);
4624 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4625 break;
4626
4627 case SVGA3D_RS_LINEPATTERN: /* SVGA3dLinePattern */
4628 /* No longer supported by d3d; mesagl comments suggest not all backends support it */
4629 /* @todo */
4630 Log(("WARNING: SVGA3D_RS_LINEPATTERN %x not supported!!\n", pRenderState[i].uintValue));
4631 /*
4632 renderState = D3DRS_LINEPATTERN;
4633 val = pRenderState[i].uintValue;
4634 */
4635 break;
4636
4637 case SVGA3D_RS_LINEAA: /* SVGA3dBool */
4638 enableCap = GL_LINE_SMOOTH;
4639 val = pRenderState[i].uintValue;
4640 break;
4641
4642 case SVGA3D_RS_LINEWIDTH: /* float */
4643 glLineWidth(pRenderState[i].floatValue);
4644 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4645 break;
4646
4647 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
4648 {
4649 /* Refresh the blending state based on the new enable setting. */
4650 SVGA3dRenderState renderstate[2];
4651
4652 renderstate[0].state = SVGA3D_RS_SRCBLEND;
4653 renderstate[0].uintValue = pContext->state.aRenderState[SVGA3D_RS_SRCBLEND].uintValue;
4654 renderstate[1].state = SVGA3D_RS_BLENDEQUATION;
4655 renderstate[1].uintValue = pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATION].uintValue;
4656
4657 int rc = vmsvga3dSetRenderState(pThis, cid, 2, renderstate);
4658 AssertRCReturn(rc, rc);
4659
4660 if (pContext->state.aRenderState[SVGA3D_RS_BLENDENABLE].uintValue != 0)
4661 continue; /* ignore if blend is already enabled */
4662 /* no break */
4663 }
4664
4665 case SVGA3D_RS_BLENDENABLE: /* SVGA3dBool */
4666 enableCap = GL_BLEND;
4667 val = pRenderState[i].uintValue;
4668 break;
4669
4670 case SVGA3D_RS_SRCBLENDALPHA: /* SVGA3dBlendOp */
4671 case SVGA3D_RS_DSTBLENDALPHA: /* SVGA3dBlendOp */
4672 case SVGA3D_RS_SRCBLEND: /* SVGA3dBlendOp */
4673 case SVGA3D_RS_DSTBLEND: /* SVGA3dBlendOp */
4674 {
4675 GLint srcRGB, srcAlpha, dstRGB, dstAlpha;
4676 GLint blendop = vmsvga3dBlendOp2GL(pRenderState[i].uintValue);
4677
4678 glGetIntegerv(GL_BLEND_SRC_RGB, &srcRGB);
4679 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4680 glGetIntegerv(GL_BLEND_DST_RGB, &dstRGB);
4681 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4682 glGetIntegerv(GL_BLEND_DST_ALPHA, &dstAlpha);
4683 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4684 glGetIntegerv(GL_BLEND_SRC_ALPHA, &srcAlpha);
4685 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4686
4687 switch (pRenderState[i].state)
4688 {
4689 case SVGA3D_RS_SRCBLEND:
4690 srcRGB = blendop;
4691 break;
4692 case SVGA3D_RS_DSTBLEND:
4693 dstRGB = blendop;
4694 break;
4695 case SVGA3D_RS_SRCBLENDALPHA:
4696 srcAlpha = blendop;
4697 break;
4698 case SVGA3D_RS_DSTBLENDALPHA:
4699 dstAlpha = blendop;
4700 break;
4701 default:
4702 /* not possible; shut up gcc */
4703 AssertFailed();
4704 break;
4705 }
4706
4707 if (pContext->state.aRenderState[SVGA3D_RS_SEPARATEALPHABLENDENABLE].uintValue != 0)
4708 pState->ext.glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
4709 else
4710 glBlendFunc(srcRGB, dstRGB);
4711 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4712 break;
4713 }
4714
4715 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation */
4716 case SVGA3D_RS_BLENDEQUATION: /* SVGA3dBlendEquation */
4717 if (pContext->state.aRenderState[SVGA3D_RS_SEPARATEALPHABLENDENABLE].uintValue != 0)
4718 pState->ext.glBlendEquationSeparate(vmsvga3dBlendEquation2GL(pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATION].uintValue),
4719 vmsvga3dBlendEquation2GL(pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATIONALPHA].uintValue));
4720 else
4721 {
4722#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x102
4723 glBlendEquation(vmsvga3dBlendEquation2GL(pRenderState[i].uintValue));
4724#else
4725 pState->ext.glBlendEquation(vmsvga3dBlendEquation2GL(pRenderState[i].uintValue));
4726#endif
4727 }
4728 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4729 break;
4730
4731 case SVGA3D_RS_BLENDCOLOR: /* SVGA3dColor */
4732 {
4733 GLfloat red, green, blue, alpha;
4734
4735 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &red, &green, &blue, &alpha);
4736
4737#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x102
4738 glBlendColor(red, green, blue, alpha);
4739#else
4740 pState->ext.glBlendColor(red, green, blue, alpha);
4741#endif
4742 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4743 break;
4744 }
4745
4746 case SVGA3D_RS_CULLMODE: /* SVGA3dFace */
4747 {
4748 GLenum mode = GL_BACK; /* default for OpenGL */
4749
4750 switch (pRenderState[i].uintValue)
4751 {
4752 case SVGA3D_FACE_NONE:
4753 break;
4754 case SVGA3D_FACE_FRONT:
4755 mode = GL_FRONT;
4756 break;
4757 case SVGA3D_FACE_BACK:
4758 mode = GL_BACK;
4759 break;
4760 case SVGA3D_FACE_FRONT_BACK:
4761 mode = GL_FRONT_AND_BACK;
4762 break;
4763 default:
4764 AssertMsgFailedReturn(("Unexpected cull mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
4765 break;
4766 }
4767 enableCap = GL_CULL_FACE;
4768 if (pRenderState[i].uintValue != SVGA3D_FACE_NONE)
4769 {
4770 glCullFace(mode);
4771 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4772 val = 1;
4773 }
4774 else
4775 val = 0;
4776 break;
4777 }
4778
4779 case SVGA3D_RS_ZFUNC: /* SVGA3dCmpFunc */
4780 glDepthFunc(vmsvgaCmpFunc2GL(pRenderState[i].uintValue));
4781 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4782 break;
4783
4784 case SVGA3D_RS_ALPHAFUNC: /* SVGA3dCmpFunc */
4785 {
4786 GLclampf ref;
4787
4788 glGetFloatv(GL_ALPHA_TEST_REF, &ref);
4789 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4790 glAlphaFunc(vmsvgaCmpFunc2GL(pRenderState[i].uintValue), ref);
4791 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4792 break;
4793 }
4794
4795 case SVGA3D_RS_ALPHAREF: /* float (0.0 .. 1.0) */
4796 {
4797 GLint func;
4798
4799 glGetIntegerv(GL_ALPHA_TEST_FUNC, &func);
4800 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4801 glAlphaFunc(func, pRenderState[i].floatValue);
4802 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4803 break;
4804 }
4805
4806 case SVGA3D_RS_STENCILENABLE: /* SVGA3dBool */
4807 enableCap = GL_STENCIL_TEST;
4808 val = pRenderState[i].uintValue;
4809 break;
4810
4811 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc */
4812 case SVGA3D_RS_STENCILREF: /* uint32_t */
4813 case SVGA3D_RS_STENCILMASK: /* uint32_t */
4814 {
4815 GLint func, ref;
4816 GLuint mask;
4817
4818 glGetIntegerv(GL_STENCIL_FUNC, &func);
4819 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4820 glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)&mask);
4821 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4822 glGetIntegerv(GL_STENCIL_REF, &ref);
4823 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4824
4825 switch (pRenderState[i].state)
4826 {
4827 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc */
4828 func = vmsvgaCmpFunc2GL(pRenderState[i].uintValue);
4829 break;
4830
4831 case SVGA3D_RS_STENCILREF: /* uint32_t */
4832 ref = pRenderState[i].uintValue;
4833 break;
4834
4835 case SVGA3D_RS_STENCILMASK: /* uint32_t */
4836 mask = pRenderState[i].uintValue;
4837 break;
4838
4839 default:
4840 /* not possible; shut up gcc */
4841 AssertFailed();
4842 break;
4843 }
4844
4845 glStencilFunc(func, ref, mask);
4846 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4847 break;
4848 }
4849
4850 case SVGA3D_RS_STENCILWRITEMASK: /* uint32_t */
4851 glStencilMask(pRenderState[i].uintValue);
4852 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4853 break;
4854
4855 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp */
4856 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp */
4857 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp */
4858 {
4859 GLint sfail, dpfail, dppass;
4860 GLenum stencilop = vmsvgaStencipOp2GL(pRenderState[i].uintValue);
4861
4862 glGetIntegerv(GL_STENCIL_FAIL, &sfail);
4863 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4864 glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, &dpfail);
4865 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4866 glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, &dppass);
4867 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4868
4869 switch (pRenderState[i].state)
4870 {
4871 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp */
4872 sfail = stencilop;
4873 break;
4874 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp */
4875 dpfail = stencilop;
4876 break;
4877 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp */
4878 dppass = stencilop;
4879 break;
4880 default:
4881 /* not possible; shut up gcc */
4882 AssertFailed();
4883 break;
4884 }
4885 glStencilOp(sfail, dpfail, dppass);
4886 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4887 break;
4888 }
4889
4890 case SVGA3D_RS_STENCILENABLE2SIDED: /* SVGA3dBool */
4891 /* @note GL_EXT_stencil_two_side required! */
4892 if (pState->ext.fEXT_stencil_two_side)
4893 {
4894 enableCap = GL_STENCIL_TEST_TWO_SIDE_EXT;
4895 val = pRenderState[i].uintValue;
4896 }
4897 else
4898 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_STENCILENABLE2SIDED\n"));
4899 break;
4900
4901 case SVGA3D_RS_CCWSTENCILFUNC: /* SVGA3dCmpFunc */
4902 {
4903 /* @todo SVGA3D_RS_STENCILFAIL/ZFAIL/PASS for front & back faces
4904 * SVGA3D_RS_CCWSTENCILFAIL/ZFAIL/PASS for back faces ??
4905 */
4906 GLint ref;
4907 GLuint mask;
4908
4909 glGetIntegerv(GL_STENCIL_BACK_VALUE_MASK, (GLint *)&mask);
4910 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4911 glGetIntegerv(GL_STENCIL_BACK_REF, &ref);
4912 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4913
4914 pState->ext.glStencilFuncSeparate(GL_BACK, vmsvgaCmpFunc2GL(pRenderState[i].uintValue), ref, mask);
4915 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4916 break;
4917 }
4918
4919 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp */
4920 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp */
4921 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp */
4922 {
4923 /* @todo SVGA3D_RS_STENCILFAIL/ZFAIL/PASS for front & back faces
4924 * SVGA3D_RS_CCWSTENCILFAIL/ZFAIL/PASS for back faces ??
4925 */
4926 GLint sfail, dpfail, dppass;
4927 GLenum stencilop = vmsvgaStencipOp2GL(pRenderState[i].uintValue);
4928
4929 glGetIntegerv(GL_STENCIL_BACK_FAIL, &sfail);
4930 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4931 glGetIntegerv(GL_STENCIL_BACK_PASS_DEPTH_FAIL, &dpfail);
4932 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4933 glGetIntegerv(GL_STENCIL_BACK_PASS_DEPTH_PASS, &dppass);
4934 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4935
4936 switch (pRenderState[i].state)
4937 {
4938 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp */
4939 sfail = stencilop;
4940 break;
4941 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp */
4942 dpfail = stencilop;
4943 break;
4944 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp */
4945 dppass = stencilop;
4946 break;
4947 default:
4948 /* not possible; shut up gcc */
4949 AssertFailed();
4950 break;
4951 }
4952 pState->ext.glStencilOpSeparate(GL_BACK, sfail, dpfail, dppass);
4953 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4954 break;
4955 }
4956
4957 case SVGA3D_RS_ZBIAS: /* float */
4958 /* @todo unknown meaning; depth bias is not identical
4959 renderState = D3DRS_DEPTHBIAS;
4960 val = pRenderState[i].uintValue;
4961 */
4962 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_ZBIAS\n"));
4963 break;
4964
4965 case SVGA3D_RS_DEPTHBIAS: /* float */
4966 {
4967 GLfloat factor;
4968
4969 /* @todo not sure if the d3d & ogl definitions are identical. */
4970
4971 /* Do not change the factor part. */
4972 glGetFloatv(GL_POLYGON_OFFSET_FACTOR, &factor);
4973 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4974
4975 glPolygonOffset(factor, pRenderState[i].floatValue);
4976 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4977 break;
4978 }
4979
4980 case SVGA3D_RS_SLOPESCALEDEPTHBIAS: /* float */
4981 {
4982 GLfloat units;
4983
4984 /* @todo not sure if the d3d & ogl definitions are identical. */
4985
4986 /* Do not change the factor part. */
4987 glGetFloatv(GL_POLYGON_OFFSET_UNITS, &units);
4988 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4989
4990 glPolygonOffset(pRenderState[i].floatValue, units);
4991 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4992 break;
4993 }
4994
4995 case SVGA3D_RS_COLORWRITEENABLE: /* SVGA3dColorMask */
4996 {
4997 GLboolean red, green, blue, alpha;
4998 SVGA3dColorMask mask;
4999
5000 mask.uintValue = pRenderState[i].uintValue;
5001
5002 red = mask.s.red;
5003 green = mask.s.green;
5004 blue = mask.s.blue;
5005 alpha = mask.s.alpha;
5006
5007 glColorMask(red, green, blue, alpha);
5008 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5009 break;
5010 }
5011
5012 case SVGA3D_RS_COLORWRITEENABLE1: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5013 case SVGA3D_RS_COLORWRITEENABLE2: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5014 case SVGA3D_RS_COLORWRITEENABLE3: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5015 Log(("vmsvga3dSetRenderState: WARNING SVGA3D_RS_COLORWRITEENABLEx not supported!!\n"));
5016 break;
5017
5018 case SVGA3D_RS_SCISSORTESTENABLE: /* SVGA3dBool */
5019 enableCap = GL_SCISSOR_TEST;
5020 val = pRenderState[i].uintValue;
5021 break;
5022
5023#if 0
5024 case SVGA3D_RS_DIFFUSEMATERIALSOURCE: /* SVGA3dVertexMaterial */
5025 AssertCompile(D3DMCS_COLOR2 == SVGA3D_VERTEXMATERIAL_SPECULAR);
5026 renderState = D3DRS_DIFFUSEMATERIALSOURCE;
5027 val = pRenderState[i].uintValue;
5028 break;
5029
5030 case SVGA3D_RS_SPECULARMATERIALSOURCE: /* SVGA3dVertexMaterial */
5031 renderState = D3DRS_SPECULARMATERIALSOURCE;
5032 val = pRenderState[i].uintValue;
5033 break;
5034
5035 case SVGA3D_RS_AMBIENTMATERIALSOURCE: /* SVGA3dVertexMaterial */
5036 renderState = D3DRS_AMBIENTMATERIALSOURCE;
5037 val = pRenderState[i].uintValue;
5038 break;
5039
5040 case SVGA3D_RS_EMISSIVEMATERIALSOURCE: /* SVGA3dVertexMaterial */
5041 renderState = D3DRS_EMISSIVEMATERIALSOURCE;
5042 val = pRenderState[i].uintValue;
5043 break;
5044#endif
5045
5046 case SVGA3D_RS_WRAP3: /* SVGA3dWrapFlags */
5047 case SVGA3D_RS_WRAP4: /* SVGA3dWrapFlags */
5048 case SVGA3D_RS_WRAP5: /* SVGA3dWrapFlags */
5049 case SVGA3D_RS_WRAP6: /* SVGA3dWrapFlags */
5050 case SVGA3D_RS_WRAP7: /* SVGA3dWrapFlags */
5051 case SVGA3D_RS_WRAP8: /* SVGA3dWrapFlags */
5052 case SVGA3D_RS_WRAP9: /* SVGA3dWrapFlags */
5053 case SVGA3D_RS_WRAP10: /* SVGA3dWrapFlags */
5054 case SVGA3D_RS_WRAP11: /* SVGA3dWrapFlags */
5055 case SVGA3D_RS_WRAP12: /* SVGA3dWrapFlags */
5056 case SVGA3D_RS_WRAP13: /* SVGA3dWrapFlags */
5057 case SVGA3D_RS_WRAP14: /* SVGA3dWrapFlags */
5058 case SVGA3D_RS_WRAP15: /* SVGA3dWrapFlags */
5059 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_WRAPx (x >= 3)\n"));
5060 break;
5061
5062 case SVGA3D_RS_LASTPIXEL: /* SVGA3dBool */
5063 case SVGA3D_RS_TWEENFACTOR: /* float */
5064 case SVGA3D_RS_INDEXEDVERTEXBLENDENABLE: /* SVGA3dBool */
5065 case SVGA3D_RS_VERTEXBLEND: /* SVGA3dVertexBlendFlags */
5066 Log(("vmsvga3dSetRenderState: WARNING not applicable!!\n"));
5067 break;
5068
5069 case SVGA3D_RS_MULTISAMPLEANTIALIAS: /* SVGA3dBool */
5070 enableCap = GL_MULTISAMPLE;
5071 val = pRenderState[i].uintValue;
5072 break;
5073
5074 case SVGA3D_RS_MULTISAMPLEMASK: /* uint32_t */
5075 case SVGA3D_RS_ANTIALIASEDLINEENABLE: /* SVGA3dBool */
5076 Log(("vmsvga3dSetRenderState: WARNING not applicable??!!\n"));
5077 break;
5078
5079 case SVGA3D_RS_COORDINATETYPE: /* SVGA3dCoordinateType */
5080 Assert(pRenderState[i].uintValue == SVGA3D_COORDINATE_LEFTHANDED);
5081 /* @todo setup a view matrix to scale the world space by -1 in the z-direction for right handed coordinates. */
5082 /*
5083 renderState = D3DRS_COORDINATETYPE;
5084 val = pRenderState[i].uintValue;
5085 */
5086 break;
5087
5088 case SVGA3D_RS_FRONTWINDING: /* SVGA3dFrontWinding */
5089 Assert(pRenderState[i].uintValue == SVGA3D_FRONTWINDING_CW);
5090 /* Invert the selected mode because of y-inversion (?) */
5091 glFrontFace((pRenderState[i].uintValue != SVGA3D_FRONTWINDING_CW) ? GL_CW : GL_CCW);
5092 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5093 break;
5094
5095 case SVGA3D_RS_OUTPUTGAMMA: /* float */
5096 //AssertFailed();
5097 /*
5098 D3DRS_SRGBWRITEENABLE ??
5099 renderState = D3DRS_OUTPUTGAMMA;
5100 val = pRenderState[i].uintValue;
5101 */
5102 break;
5103
5104#if 0
5105
5106 case SVGA3D_RS_VERTEXMATERIALENABLE: /* SVGA3dBool */
5107 //AssertFailed();
5108 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE; /* correct?? */
5109 val = pRenderState[i].uintValue;
5110 break;
5111
5112 case SVGA3D_RS_TEXTUREFACTOR: /* SVGA3dColor */
5113 renderState = D3DRS_TEXTUREFACTOR;
5114 val = pRenderState[i].uintValue;
5115 break;
5116
5117 case SVGA3D_RS_LOCALVIEWER: /* SVGA3dBool */
5118 renderState = D3DRS_LOCALVIEWER;
5119 val = pRenderState[i].uintValue;
5120 break;
5121
5122 case SVGA3D_RS_ZVISIBLE: /* SVGA3dBool */
5123 AssertFailed();
5124 /*
5125 renderState = D3DRS_ZVISIBLE;
5126 val = pRenderState[i].uintValue;
5127 */
5128 break;
5129
5130 case SVGA3D_RS_CLIPPING: /* SVGA3dBool */
5131 renderState = D3DRS_CLIPPING;
5132 val = pRenderState[i].uintValue;
5133 break;
5134
5135 case SVGA3D_RS_WRAP0: /* SVGA3dWrapFlags */
5136 glTexParameter GL_TEXTURE_WRAP_S
5137 Assert(SVGA3D_WRAPCOORD_3 == D3DWRAPCOORD_3);
5138 renderState = D3DRS_WRAP0;
5139 val = pRenderState[i].uintValue;
5140 break;
5141
5142 case SVGA3D_RS_WRAP1: /* SVGA3dWrapFlags */
5143 glTexParameter GL_TEXTURE_WRAP_T
5144 renderState = D3DRS_WRAP1;
5145 val = pRenderState[i].uintValue;
5146 break;
5147
5148 case SVGA3D_RS_WRAP2: /* SVGA3dWrapFlags */
5149 glTexParameter GL_TEXTURE_WRAP_R
5150 renderState = D3DRS_WRAP2;
5151 val = pRenderState[i].uintValue;
5152 break;
5153
5154
5155 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
5156 renderState = D3DRS_SEPARATEALPHABLENDENABLE;
5157 val = pRenderState[i].uintValue;
5158 break;
5159
5160
5161 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation */
5162 renderState = D3DRS_BLENDOPALPHA;
5163 val = pRenderState[i].uintValue;
5164 break;
5165
5166 case SVGA3D_RS_TRANSPARENCYANTIALIAS: /* SVGA3dTransparencyAntialiasType */
5167 AssertFailed();
5168 /*
5169 renderState = D3DRS_TRANSPARENCYANTIALIAS;
5170 val = pRenderState[i].uintValue;
5171 */
5172 break;
5173
5174#endif
5175 default:
5176 AssertFailed();
5177 break;
5178 }
5179
5180 if (enableCap != ~0U)
5181 {
5182 if (val)
5183 glEnable(enableCap);
5184 else
5185 glDisable(enableCap);
5186 }
5187 }
5188
5189 return VINF_SUCCESS;
5190}
5191
5192int vmsvga3dSetRenderTarget(PVGASTATE pThis, uint32_t cid, SVGA3dRenderTargetType type, SVGA3dSurfaceImageId target)
5193{
5194 PVMSVGA3DCONTEXT pContext;
5195 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
5196 PVMSVGA3DSURFACE pRenderTarget;
5197
5198 AssertReturn(pState, VERR_NO_MEMORY);
5199 AssertReturn(type < SVGA3D_RT_MAX, VERR_INVALID_PARAMETER);
5200 AssertReturn(target.face == 0, VERR_INVALID_PARAMETER);
5201 AssertReturn(target.mipmap == 0, VERR_INVALID_PARAMETER);
5202
5203 Log(("vmsvga3dSetRenderTarget cid=%x type=%x surface id=%x\n", cid, type, target.sid));
5204
5205 if ( cid >= pState->cContexts
5206 || pState->paContext[cid].id != cid)
5207 {
5208 Log(("vmsvga3dSetRenderTarget invalid context id!\n"));
5209 return VERR_INVALID_PARAMETER;
5210 }
5211 pContext = &pState->paContext[cid];
5212 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5213
5214 /* Save for vm state save/restore. */
5215 pContext->state.aRenderTargets[type] = target.sid;
5216
5217 if (target.sid == SVGA3D_INVALID_ID)
5218 {
5219 /* Disable render target. */
5220 switch (type)
5221 {
5222 case SVGA3D_RT_DEPTH:
5223 case SVGA3D_RT_STENCIL:
5224 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER, (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
5225 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5226 break;
5227
5228 case SVGA3D_RT_COLOR0:
5229 case SVGA3D_RT_COLOR1:
5230 case SVGA3D_RT_COLOR2:
5231 case SVGA3D_RT_COLOR3:
5232 case SVGA3D_RT_COLOR4:
5233 case SVGA3D_RT_COLOR5:
5234 case SVGA3D_RT_COLOR6:
5235 case SVGA3D_RT_COLOR7:
5236 pContext->sidRenderTarget = SVGA3D_INVALID_ID;
5237 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0, 0, 0, 0);
5238 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5239 break;
5240
5241 default:
5242 AssertFailedReturn(VERR_INVALID_PARAMETER);
5243 }
5244 return VINF_SUCCESS;
5245 }
5246
5247 AssertReturn(target.sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
5248 AssertReturn(target.sid < pState->cSurfaces && pState->paSurface[target.sid].id == target.sid, VERR_INVALID_PARAMETER);
5249 pRenderTarget = &pState->paSurface[target.sid];
5250
5251 switch (type)
5252 {
5253 case SVGA3D_RT_DEPTH:
5254 case SVGA3D_RT_STENCIL:
5255 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
5256 {
5257 Log(("vmsvga3dSetRenderTarget: create renderbuffer to be used as render target; surface id=%x type=%d format=%d\n", target.sid, pRenderTarget->flags, pRenderTarget->internalFormatGL));
5258 pState->ext.glGenRenderbuffers(1, &pRenderTarget->oglId.renderbuffer);
5259 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5260
5261 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5262 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5263
5264 pState->ext.glRenderbufferStorage(GL_RENDERBUFFER,
5265 pRenderTarget->internalFormatGL,
5266 pRenderTarget->pMipmapLevels[0].size.width,
5267 pRenderTarget->pMipmapLevels[0].size.height);
5268 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5269
5270 LogFlow(("vmsvga3dSetRenderTarget: sid=%#x idAssociatedContext %#x -> %#x\n", pRenderTarget->id, pRenderTarget->idAssociatedContext, cid));
5271 pRenderTarget->idAssociatedContext = cid;
5272 }
5273 else
5274 {
5275 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5276 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5277 }
5278 Assert(pRenderTarget->idAssociatedContext == cid);
5279 Assert(!pRenderTarget->fDirty);
5280 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
5281
5282 pRenderTarget->flags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
5283
5284 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
5285 (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT,
5286 GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5287 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5288 break;
5289
5290 case SVGA3D_RT_COLOR0:
5291 case SVGA3D_RT_COLOR1:
5292 case SVGA3D_RT_COLOR2:
5293 case SVGA3D_RT_COLOR3:
5294 case SVGA3D_RT_COLOR4:
5295 case SVGA3D_RT_COLOR5:
5296 case SVGA3D_RT_COLOR6:
5297 case SVGA3D_RT_COLOR7:
5298 {
5299 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
5300 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
5301 {
5302 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));
5303 int rc = vmsvga3dCreateTexture(pState, pContext, cid, pRenderTarget);
5304 AssertRCReturn(rc, rc);
5305 }
5306
5307 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
5308 Assert(!pRenderTarget->fDirty);
5309
5310 pRenderTarget->flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
5311
5312 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0, GL_TEXTURE_2D, pRenderTarget->oglId.texture, target.mipmap);
5313 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5314
5315 pContext->sidRenderTarget = target.sid;
5316
5317#ifdef DEBUG
5318 GLenum status = pState->ext.glCheckFramebufferStatus(GL_FRAMEBUFFER);
5319 if (status != GL_FRAMEBUFFER_COMPLETE)
5320 Log(("vmsvga3dSetRenderTarget: WARNING: glCheckFramebufferStatus returned %x\n", status));
5321#endif
5322 /* @todo use glDrawBuffers too? */
5323 break;
5324 }
5325
5326 default:
5327 AssertFailedReturn(VERR_INVALID_PARAMETER);
5328 }
5329
5330 return VINF_SUCCESS;
5331}
5332
5333#if 0
5334/**
5335 * Convert SVGA texture combiner value to its D3D equivalent
5336 */
5337static DWORD vmsvga3dTextureCombiner2D3D(uint32_t value)
5338{
5339 switch (value)
5340 {
5341 case SVGA3D_TC_DISABLE:
5342 return D3DTOP_DISABLE;
5343 case SVGA3D_TC_SELECTARG1:
5344 return D3DTOP_SELECTARG1;
5345 case SVGA3D_TC_SELECTARG2:
5346 return D3DTOP_SELECTARG2;
5347 case SVGA3D_TC_MODULATE:
5348 return D3DTOP_MODULATE;
5349 case SVGA3D_TC_ADD:
5350 return D3DTOP_ADD;
5351 case SVGA3D_TC_ADDSIGNED:
5352 return D3DTOP_ADDSIGNED;
5353 case SVGA3D_TC_SUBTRACT:
5354 return D3DTOP_SUBTRACT;
5355 case SVGA3D_TC_BLENDTEXTUREALPHA:
5356 return D3DTOP_BLENDTEXTUREALPHA;
5357 case SVGA3D_TC_BLENDDIFFUSEALPHA:
5358 return D3DTOP_BLENDDIFFUSEALPHA;
5359 case SVGA3D_TC_BLENDCURRENTALPHA:
5360 return D3DTOP_BLENDCURRENTALPHA;
5361 case SVGA3D_TC_BLENDFACTORALPHA:
5362 return D3DTOP_BLENDFACTORALPHA;
5363 case SVGA3D_TC_MODULATE2X:
5364 return D3DTOP_MODULATE2X;
5365 case SVGA3D_TC_MODULATE4X:
5366 return D3DTOP_MODULATE4X;
5367 case SVGA3D_TC_DSDT:
5368 AssertFailed(); /* @todo ??? */
5369 return D3DTOP_DISABLE;
5370 case SVGA3D_TC_DOTPRODUCT3:
5371 return D3DTOP_DOTPRODUCT3;
5372 case SVGA3D_TC_BLENDTEXTUREALPHAPM:
5373 return D3DTOP_BLENDTEXTUREALPHAPM;
5374 case SVGA3D_TC_ADDSIGNED2X:
5375 return D3DTOP_ADDSIGNED2X;
5376 case SVGA3D_TC_ADDSMOOTH:
5377 return D3DTOP_ADDSMOOTH;
5378 case SVGA3D_TC_PREMODULATE:
5379 return D3DTOP_PREMODULATE;
5380 case SVGA3D_TC_MODULATEALPHA_ADDCOLOR:
5381 return D3DTOP_MODULATEALPHA_ADDCOLOR;
5382 case SVGA3D_TC_MODULATECOLOR_ADDALPHA:
5383 return D3DTOP_MODULATECOLOR_ADDALPHA;
5384 case SVGA3D_TC_MODULATEINVALPHA_ADDCOLOR:
5385 return D3DTOP_MODULATEINVALPHA_ADDCOLOR;
5386 case SVGA3D_TC_MODULATEINVCOLOR_ADDALPHA:
5387 return D3DTOP_MODULATEINVCOLOR_ADDALPHA;
5388 case SVGA3D_TC_BUMPENVMAPLUMINANCE:
5389 return D3DTOP_BUMPENVMAPLUMINANCE;
5390 case SVGA3D_TC_MULTIPLYADD:
5391 return D3DTOP_MULTIPLYADD;
5392 case SVGA3D_TC_LERP:
5393 return D3DTOP_LERP;
5394 default:
5395 AssertFailed();
5396 return D3DTOP_DISABLE;
5397 }
5398}
5399
5400/**
5401 * Convert SVGA texture arg data value to its D3D equivalent
5402 */
5403static DWORD vmsvga3dTextureArgData2D3D(uint32_t value)
5404{
5405 switch (value)
5406 {
5407 case SVGA3D_TA_CONSTANT:
5408 return D3DTA_CONSTANT;
5409 case SVGA3D_TA_PREVIOUS:
5410 return D3DTA_CURRENT; /* current = previous */
5411 case SVGA3D_TA_DIFFUSE:
5412 return D3DTA_DIFFUSE;
5413 case SVGA3D_TA_TEXTURE:
5414 return D3DTA_TEXTURE;
5415 case SVGA3D_TA_SPECULAR:
5416 return D3DTA_SPECULAR;
5417 default:
5418 AssertFailed();
5419 return 0;
5420 }
5421}
5422
5423/**
5424 * Convert SVGA texture transform flag value to its D3D equivalent
5425 */
5426static DWORD vmsvga3dTextTransformFlags2D3D(uint32_t value)
5427{
5428 switch (value)
5429 {
5430 case SVGA3D_TEX_TRANSFORM_OFF:
5431 return D3DTTFF_DISABLE;
5432 case SVGA3D_TEX_TRANSFORM_S:
5433 return D3DTTFF_COUNT1; /* @todo correct? */
5434 case SVGA3D_TEX_TRANSFORM_T:
5435 return D3DTTFF_COUNT2; /* @todo correct? */
5436 case SVGA3D_TEX_TRANSFORM_R:
5437 return D3DTTFF_COUNT3; /* @todo correct? */
5438 case SVGA3D_TEX_TRANSFORM_Q:
5439 return D3DTTFF_COUNT4; /* @todo correct? */
5440 case SVGA3D_TEX_PROJECTED:
5441 return D3DTTFF_PROJECTED;
5442 default:
5443 AssertFailed();
5444 return 0;
5445 }
5446}
5447#endif
5448
5449static GLenum vmsvga3dTextureAddress2OGL(SVGA3dTextureAddress value)
5450{
5451 switch (value)
5452 {
5453 case SVGA3D_TEX_ADDRESS_WRAP:
5454 return GL_REPEAT;
5455 case SVGA3D_TEX_ADDRESS_MIRROR:
5456 return GL_MIRRORED_REPEAT;
5457 case SVGA3D_TEX_ADDRESS_CLAMP:
5458 return GL_CLAMP_TO_EDGE;
5459 case SVGA3D_TEX_ADDRESS_BORDER:
5460 return GL_CLAMP_TO_BORDER;
5461 case SVGA3D_TEX_ADDRESS_MIRRORONCE:
5462 AssertFailed();
5463 return GL_CLAMP_TO_EDGE_SGIS; /* @todo correct? */
5464
5465 case SVGA3D_TEX_ADDRESS_EDGE:
5466 case SVGA3D_TEX_ADDRESS_INVALID:
5467 default:
5468 AssertFailed();
5469 return GL_REPEAT; /* default */
5470 }
5471}
5472
5473static GLenum vmsvga3dTextureFilter2OGL(SVGA3dTextureFilter value)
5474{
5475 switch (value)
5476 {
5477 case SVGA3D_TEX_FILTER_NONE:
5478 case SVGA3D_TEX_FILTER_LINEAR:
5479 return GL_LINEAR;
5480 case SVGA3D_TEX_FILTER_NEAREST:
5481 return GL_NEAREST;
5482 case SVGA3D_TEX_FILTER_ANISOTROPIC:
5483 /* @todo */
5484 case SVGA3D_TEX_FILTER_FLATCUBIC: // Deprecated, not implemented
5485 case SVGA3D_TEX_FILTER_GAUSSIANCUBIC: // Deprecated, not implemented
5486 case SVGA3D_TEX_FILTER_PYRAMIDALQUAD: // Not currently implemented
5487 case SVGA3D_TEX_FILTER_GAUSSIANQUAD: // Not currently implemented
5488 default:
5489 AssertFailed();
5490 return GL_LINEAR; /* default */
5491 }
5492}
5493
5494uint32_t vmsvga3dSVGA3dColor2RGBA(SVGA3dColor value)
5495{
5496 /* flip the red and blue bytes */
5497 uint8_t blue = value & 0xff;
5498 uint8_t red = (value >> 16) & 0xff;
5499 return (value & 0xff00ff00) | red | (blue << 16);
5500}
5501
5502int vmsvga3dSetTextureState(PVGASTATE pThis, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
5503{
5504 GLenum val;
5505 GLenum currentStage = ~0L;
5506 PVMSVGA3DCONTEXT pContext;
5507 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
5508 AssertReturn(pState, VERR_NO_MEMORY);
5509
5510 Log(("vmsvga3dSetTextureState %x cTextureState=%d\n", cid, cTextureStates));
5511
5512 if ( cid >= pState->cContexts
5513 || pState->paContext[cid].id != cid)
5514 {
5515 Log(("vmsvga3dSetTextureState invalid context id!\n"));
5516 return VERR_INVALID_PARAMETER;
5517 }
5518 pContext = &pState->paContext[cid];
5519 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5520
5521 for (unsigned i = 0; i < cTextureStates; i++)
5522 {
5523 GLenum textureType = ~0U;
5524 GLenum samplerType = ~0U;
5525
5526 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));
5527 /* Record the texture state for vm state saving. */
5528 if ( pTextureState[i].stage < SVGA3D_MAX_TEXTURE_STAGE
5529 && pTextureState[i].name < SVGA3D_TS_MAX)
5530 {
5531 pContext->state.aTextureState[pTextureState[i].stage][pTextureState[i].name] = pTextureState[i];
5532 }
5533
5534 /* Active the right texture unit for subsequent texture state changes. */
5535 if (pTextureState[i].stage != currentStage)
5536 {
5537 pState->ext.glActiveTexture(GL_TEXTURE0 + pTextureState[i].stage);
5538 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5539 currentStage = pTextureState[i].stage;
5540 }
5541
5542 switch (pTextureState[i].name)
5543 {
5544 case SVGA3D_TS_BUMPENVMAT00: /* float */
5545 case SVGA3D_TS_BUMPENVMAT01: /* float */
5546 case SVGA3D_TS_BUMPENVMAT10: /* float */
5547 case SVGA3D_TS_BUMPENVMAT11: /* float */
5548 case SVGA3D_TS_BUMPENVLSCALE: /* float */
5549 case SVGA3D_TS_BUMPENVLOFFSET: /* float */
5550 Log(("vmsvga3dSetTextureState: bump mapping texture options not supported!!\n"));
5551 break;
5552
5553 case SVGA3D_TS_COLOROP: /* SVGA3dTextureCombiner */
5554 case SVGA3D_TS_COLORARG0: /* SVGA3dTextureArgData */
5555 case SVGA3D_TS_COLORARG1: /* SVGA3dTextureArgData */
5556 case SVGA3D_TS_COLORARG2: /* SVGA3dTextureArgData */
5557 case SVGA3D_TS_ALPHAOP: /* SVGA3dTextureCombiner */
5558 case SVGA3D_TS_ALPHAARG0: /* SVGA3dTextureArgData */
5559 case SVGA3D_TS_ALPHAARG1: /* SVGA3dTextureArgData */
5560 case SVGA3D_TS_ALPHAARG2: /* SVGA3dTextureArgData */
5561 /* @todo; not used by MesaGL */
5562 Log(("vmsvga3dSetTextureState: colorop/alphaop not yet supported!!\n"));
5563 break;
5564#if 0
5565
5566 case SVGA3D_TS_TEXCOORDINDEX: /* uint32_t */
5567 textureType = D3DTSS_TEXCOORDINDEX;
5568 val = pTextureState[i].value;
5569 break;
5570
5571 case SVGA3D_TS_TEXTURETRANSFORMFLAGS: /* SVGA3dTexTransformFlags */
5572 textureType = D3DTSS_TEXTURETRANSFORMFLAGS;
5573 val = vmsvga3dTextTransformFlags2D3D(pTextureState[i].value);
5574 break;
5575#endif
5576
5577 case SVGA3D_TS_BIND_TEXTURE: /* SVGA3dSurfaceId */
5578 if (pTextureState[i].value == SVGA3D_INVALID_ID)
5579 {
5580 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture surface id=%x\n", pTextureState[i].stage, pTextureState[i].value));
5581
5582 pContext->aSidActiveTexture[currentStage] = SVGA3D_INVALID_ID;
5583 /* Unselect the currently associated texture. */
5584 glBindTexture(GL_TEXTURE_2D, 0);
5585 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5586 /* Necessary for the fixed pipeline. */
5587 glDisable(GL_TEXTURE_2D);
5588 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5589 }
5590 else
5591 {
5592 uint32_t sid = pTextureState[i].value;
5593
5594 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
5595 AssertReturn(sid < pState->cSurfaces && pState->paSurface[sid].id == sid, VERR_INVALID_PARAMETER);
5596
5597 PVMSVGA3DSURFACE pSurface = &pState->paSurface[sid];
5598
5599 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));
5600
5601 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
5602 {
5603 Assert(pSurface->idAssociatedContext == SVGA3D_INVALID_ID);
5604 Log(("CreateTexture (%d,%d) level=%d\n", pSurface->pMipmapLevels[0].size.width, pSurface->pMipmapLevels[0].size.height, pSurface->faces[0].numMipLevels));
5605 int rc = vmsvga3dCreateTexture(pState, pContext, cid, pSurface);
5606 AssertRCReturn(rc, rc);
5607 }
5608
5609 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
5610 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5611
5612 /* Necessary for the fixed pipeline. */
5613 glEnable(GL_TEXTURE_2D);
5614 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5615
5616 if (pContext->aSidActiveTexture[currentStage] != sid)
5617 {
5618 /* Recreate the texture state as glBindTexture resets them all (sigh). */
5619 for (uint32_t iStage = 0; iStage < SVGA3D_MAX_TEXTURE_STAGE; iStage++)
5620 {
5621 for (uint32_t j = 0; j < SVGA3D_TS_MAX; j++)
5622 {
5623 SVGA3dTextureState *pTextureStateIter = &pContext->state.aTextureState[iStage][j];
5624
5625 if ( pTextureStateIter->name != SVGA3D_TS_INVALID
5626 && pTextureStateIter->name != SVGA3D_TS_BIND_TEXTURE)
5627 vmsvga3dSetTextureState(pThis, pContext->id, 1, pTextureStateIter);
5628 }
5629 }
5630 }
5631 pContext->aSidActiveTexture[currentStage] = sid;
5632 }
5633 /* Finished; continue with the next one. */
5634 continue;
5635
5636 case SVGA3D_TS_ADDRESSW: /* SVGA3dTextureAddress */
5637 textureType = GL_TEXTURE_WRAP_R; /* R = W */
5638 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
5639 break;
5640
5641 case SVGA3D_TS_ADDRESSU: /* SVGA3dTextureAddress */
5642 textureType = GL_TEXTURE_WRAP_S; /* S = U */
5643 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
5644 break;
5645
5646 case SVGA3D_TS_ADDRESSV: /* SVGA3dTextureAddress */
5647 textureType = GL_TEXTURE_WRAP_T; /* T = V */
5648 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
5649 break;
5650
5651 case SVGA3D_TS_MIPFILTER: /* SVGA3dTextureFilter */
5652 //AssertFailed(); /* @todo */
5653 //samplerType = D3DSAMP_MIPFILTER;
5654 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)pTextureState[i].value);
5655 break;
5656
5657 case SVGA3D_TS_MAGFILTER: /* SVGA3dTextureFilter */
5658 textureType = GL_TEXTURE_MAG_FILTER;
5659 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)pTextureState[i].value);
5660 Assert(val == GL_NEAREST || val == GL_LINEAR);
5661 break;
5662
5663 case SVGA3D_TS_MINFILTER: /* SVGA3dTextureFilter */
5664 textureType = GL_TEXTURE_MIN_FILTER;
5665 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)pTextureState[i].value);
5666 break;
5667
5668 case SVGA3D_TS_BORDERCOLOR: /* SVGA3dColor */
5669 {
5670 GLfloat color[4]; /* red, green, blue, alpha */
5671
5672 vmsvgaColor2GLFloatArray(pTextureState[i].value, &color[0], &color[1], &color[2], &color[3]);
5673
5674 glTexParameterfv(GL_TEXTURE_2D /* @todo flexible type */, GL_TEXTURE_BORDER_COLOR, color); /* Identical; default 0.0 identical too */
5675 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5676 break;
5677 }
5678
5679 case SVGA3D_TS_TEXTURE_LOD_BIAS: /* float */
5680 glTexParameterf(GL_TEXTURE_2D /* @todo flexible type */, GL_TEXTURE_LOD_BIAS, pTextureState[i].value); /* Identical; default 0.0 identical too */
5681 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5682 break;
5683
5684 case SVGA3D_TS_TEXTURE_MIPMAP_LEVEL: /* uint32_t */
5685 textureType = GL_TEXTURE_MAX_LEVEL;
5686 val = pTextureState[i].value; /* Identical?? */
5687 break;
5688
5689#if 0
5690 case SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL: /* uint32_t */
5691 samplerType = D3DSAMP_MAXANISOTROPY;
5692 val = pTextureState[i].value; /* Identical?? */
5693 break;
5694
5695 case SVGA3D_TS_GAMMA: /* float */
5696 samplerType = D3DSAMP_SRGBTEXTURE;
5697 /* Boolean in D3D */
5698 if (pTextureState[i].floatValue == 1.0f)
5699 val = FALSE;
5700 else
5701 val = TRUE;
5702 break;
5703#endif
5704 /* Internal commands, that don't map directly to the SetTextureStageState API. */
5705 case SVGA3D_TS_TEXCOORDGEN: /* SVGA3dTextureCoordGen */
5706 AssertFailed();
5707 break;
5708
5709 default:
5710 //AssertFailed();
5711 break;
5712 }
5713
5714 if (textureType != ~0U)
5715 {
5716 glTexParameteri(GL_TEXTURE_2D /* @todo flexible type */, textureType, val);
5717 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5718 }
5719 }
5720
5721 return VINF_SUCCESS;
5722}
5723
5724int vmsvga3dSetMaterial(PVGASTATE pThis, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
5725{
5726 PVMSVGA3DCONTEXT pContext;
5727 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
5728 AssertReturn(pState, VERR_NO_MEMORY);
5729 GLenum oglFace;
5730
5731 Log(("vmsvga3dSetMaterial cid=%x face %d\n", cid, face));
5732
5733 if ( cid >= pState->cContexts
5734 || pState->paContext[cid].id != cid)
5735 {
5736 Log(("vmsvga3dSetMaterial invalid context id!\n"));
5737 return VERR_INVALID_PARAMETER;
5738 }
5739 pContext = &pState->paContext[cid];
5740 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5741
5742 switch (face)
5743 {
5744 case SVGA3D_FACE_NONE:
5745 case SVGA3D_FACE_FRONT:
5746 oglFace = GL_FRONT;
5747 break;
5748
5749 case SVGA3D_FACE_BACK:
5750 oglFace = GL_BACK;
5751 break;
5752
5753 case SVGA3D_FACE_FRONT_BACK:
5754 oglFace = GL_FRONT_AND_BACK;
5755 break;
5756
5757 default:
5758 AssertFailedReturn(VERR_INVALID_PARAMETER);
5759 }
5760
5761 /* Save for vm state save/restore. */
5762 pContext->state.aMaterial[face].fValid = true;
5763 pContext->state.aMaterial[face].material = *pMaterial;
5764 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_MATERIAL;
5765
5766 glMaterialfv(oglFace, GL_DIFFUSE, pMaterial->diffuse);
5767 glMaterialfv(oglFace, GL_AMBIENT, pMaterial->ambient);
5768 glMaterialfv(oglFace, GL_SPECULAR, pMaterial->specular);
5769 glMaterialfv(oglFace, GL_EMISSION, pMaterial->emissive);
5770 glMaterialfv(oglFace, GL_SHININESS, &pMaterial->shininess);
5771 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5772
5773 return VINF_SUCCESS;
5774}
5775
5776/* @todo Move into separate library as we are using logic from Wine here. */
5777int vmsvga3dSetLightData(PVGASTATE pThis, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
5778{
5779 PVMSVGA3DCONTEXT pContext;
5780 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
5781 AssertReturn(pState, VERR_NO_MEMORY);
5782 float QuadAttenuation;
5783
5784 Log(("vmsvga3dSetLightData cid=%x index=%d type=%d\n", cid, index, pData->type));
5785
5786 if ( cid >= pState->cContexts
5787 || pState->paContext[cid].id != cid)
5788 {
5789 Log(("vmsvga3dSetLightData invalid context id!\n"));
5790 return VERR_INVALID_PARAMETER;
5791 }
5792 pContext = &pState->paContext[cid];
5793 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5794
5795 /* Store for vm state save/restore */
5796 if (index < SVGA3D_MAX_LIGHTS)
5797 {
5798 pContext->state.aLightData[index].fValidData = true;
5799 pContext->state.aLightData[index].data = *pData;
5800 }
5801 else
5802 AssertFailed();
5803
5804 if ( pData->attenuation0 < 0.0f
5805 || pData->attenuation1 < 0.0f
5806 || pData->attenuation2 < 0.0f)
5807 {
5808 Log(("vmsvga3dSetLightData: invalid negative attenuation values!!\n"));
5809 return VINF_SUCCESS; /* ignore; could crash the GL driver */
5810 }
5811
5812 /* Light settings are affected by the model view in OpenGL, the View transform in direct3d */
5813 glMatrixMode(GL_MODELVIEW);
5814 glPushMatrix();
5815 glLoadMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
5816
5817 glLightfv(GL_LIGHT0 + index, GL_DIFFUSE, pData->diffuse);
5818 glLightfv(GL_LIGHT0 + index, GL_SPECULAR, pData->specular);
5819 glLightfv(GL_LIGHT0 + index, GL_AMBIENT, pData->ambient);
5820
5821 if (pData->range * pData->range >= FLT_MIN)
5822 QuadAttenuation = 1.4f / (pData->range * pData->range);
5823 else
5824 QuadAttenuation = 0.0f;
5825
5826 switch (pData->type)
5827 {
5828 case SVGA3D_LIGHTTYPE_POINT:
5829 {
5830 GLfloat position[4];
5831
5832 position[0] = pData->position[0];
5833 position[1] = pData->position[1];
5834 position[2] = pData->position[2];
5835 position[3] = 1.0f;
5836
5837 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
5838 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5839
5840 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
5841 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5842
5843 /* Attenuation - Are these right? guessing... */
5844 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
5845 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5846
5847 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
5848 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5849
5850 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
5851 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5852
5853 /* @todo range */
5854 break;
5855 }
5856
5857 case SVGA3D_LIGHTTYPE_SPOT1:
5858 {
5859 GLfloat exponent;
5860 GLfloat position[4];
5861 const GLfloat pi = 4.0f * atanf(1.0f);
5862
5863 position[0] = pData->position[0];
5864 position[1] = pData->position[1];
5865 position[2] = pData->position[2];
5866 position[3] = 1.0f;
5867
5868 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
5869 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5870
5871 position[0] = pData->direction[0];
5872 position[1] = pData->direction[1];
5873 position[2] = pData->direction[2];
5874 position[3] = 1.0f;
5875
5876 glLightfv(GL_LIGHT0 + index, GL_SPOT_DIRECTION, position);
5877 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5878
5879 /*
5880 * opengl-ish and d3d-ish spot lights use too different models for the
5881 * light "intensity" as a function of the angle towards the main light direction,
5882 * so we only can approximate very roughly.
5883 * however spot lights are rather rarely used in games (if ever used at all).
5884 * furthermore if still used, probably nobody pays attention to such details.
5885 */
5886 if (pData->falloff == 0)
5887 {
5888 /* Falloff = 0 is easy, because d3d's and opengl's spot light equations have the
5889 * falloff resp. exponent parameter as an exponent, so the spot light lighting
5890 * will always be 1.0 for both of them, and we don't have to care for the
5891 * rest of the rather complex calculation
5892 */
5893 exponent = 0.0f;
5894 }
5895 else
5896 {
5897 float rho = pData->theta + (pData->phi - pData->theta) / (2 * pData->falloff);
5898 if (rho < 0.0001f)
5899 rho = 0.0001f;
5900 exponent = -0.3f/log(cos(rho/2));
5901 }
5902 if (exponent > 128.0f)
5903 exponent = 128.0f;
5904
5905 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, exponent);
5906 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5907
5908 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, pData->phi * 90.0 / pi);
5909 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5910
5911 /* Attenuation - Are these right? guessing... */
5912 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
5913 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5914
5915 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
5916 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5917
5918 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
5919 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5920
5921 /* @todo range */
5922 break;
5923 }
5924
5925 case SVGA3D_LIGHTTYPE_DIRECTIONAL:
5926 {
5927 GLfloat position[4];
5928
5929 position[0] = -pData->direction[0];
5930 position[1] = -pData->direction[1];
5931 position[2] = -pData->direction[2];
5932 position[3] = 0.0f;
5933
5934 glLightfv(GL_LIGHT0 + index, GL_POSITION, position); /* Note gl uses w position of 0 for direction! */
5935 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5936
5937 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
5938 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5939
5940 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, 0.0f);
5941 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5942 break;
5943 }
5944
5945 case SVGA3D_LIGHTTYPE_SPOT2:
5946 default:
5947 Log(("Unsupported light type!!\n"));
5948 return VERR_INVALID_PARAMETER;
5949 }
5950
5951 /* Restore the modelview matrix */
5952 glPopMatrix();
5953
5954 return VINF_SUCCESS;
5955}
5956
5957int vmsvga3dSetLightEnabled(PVGASTATE pThis, uint32_t cid, uint32_t index, uint32_t enabled)
5958{
5959 PVMSVGA3DCONTEXT pContext;
5960 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
5961 AssertReturn(pState, VERR_NO_MEMORY);
5962
5963 Log(("vmsvga3dSetLightEnabled cid=%x %d -> %d\n", cid, index, enabled));
5964
5965 if ( cid >= pState->cContexts
5966 || pState->paContext[cid].id != cid)
5967 {
5968 Log(("vmsvga3dSetLightEnabled invalid context id!\n"));
5969 return VERR_INVALID_PARAMETER;
5970 }
5971 pContext = &pState->paContext[cid];
5972 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5973
5974 /* Store for vm state save/restore */
5975 if (index < SVGA3D_MAX_LIGHTS)
5976 pContext->state.aLightData[index].fEnabled = !!enabled;
5977 else
5978 AssertFailed();
5979
5980 if (enabled)
5981 {
5982 /* Load the default settings if none have been set yet. */
5983 if (!pContext->state.aLightData[index].fValidData)
5984 vmsvga3dSetLightData(pThis, cid, index, (SVGA3dLightData *)&vmsvga3d_default_light);
5985 glEnable(GL_LIGHT0 + index);
5986 }
5987 else
5988 glDisable(GL_LIGHT0 + index);
5989
5990 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5991 return VINF_SUCCESS;
5992}
5993
5994int vmsvga3dSetViewPort(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
5995{
5996 PVMSVGA3DCONTEXT pContext;
5997 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
5998 AssertReturn(pState, VERR_NO_MEMORY);
5999
6000 Log(("vmsvga3dSetViewPort cid=%x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
6001
6002 if ( cid >= pState->cContexts
6003 || pState->paContext[cid].id != cid)
6004 {
6005 Log(("vmsvga3dSetViewPort invalid context id!\n"));
6006 return VERR_INVALID_PARAMETER;
6007 }
6008 pContext = &pState->paContext[cid];
6009 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6010
6011 /* Save for vm state save/restore. */
6012 pContext->state.RectViewPort = *pRect;
6013 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VIEWPORT;
6014
6015 /* @todo y-inversion for partial viewport coordinates? */
6016 glViewport(pRect->x, pRect->y, pRect->w, pRect->h);
6017 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6018
6019 /* Reset the projection matrix as that relies on the viewport setting. */
6020 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].fValid == true)
6021 {
6022 vmsvga3dSetTransform(pThis, cid, SVGA3D_TRANSFORM_PROJECTION, pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].matrix);
6023 }
6024 else
6025 {
6026 float matrix[16];
6027
6028 /* identity matrix if no matrix set. */
6029 memset(matrix, 0, sizeof(matrix));
6030 matrix[0] = 1.0;
6031 matrix[5] = 1.0;
6032 matrix[10] = 1.0;
6033 matrix[15] = 1.0;
6034 vmsvga3dSetTransform(pThis, cid, SVGA3D_TRANSFORM_PROJECTION, matrix);
6035 }
6036
6037 return VINF_SUCCESS;
6038}
6039
6040int vmsvga3dSetClipPlane(PVGASTATE pThis, uint32_t cid, uint32_t index, float plane[4])
6041{
6042 PVMSVGA3DCONTEXT pContext;
6043 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6044 AssertReturn(pState, VERR_NO_MEMORY);
6045 double oglPlane[4];
6046
6047 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)));
6048 AssertReturn(index < SVGA3D_CLIPPLANE_MAX, VERR_INVALID_PARAMETER);
6049
6050 if ( cid >= pState->cContexts
6051 || pState->paContext[cid].id != cid)
6052 {
6053 Log(("vmsvga3dSetClipPlane invalid context id!\n"));
6054 return VERR_INVALID_PARAMETER;
6055 }
6056 pContext = &pState->paContext[cid];
6057 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6058
6059 /* Store for vm state save/restore. */
6060 pContext->state.aClipPlane[index].fValid = true;
6061 memcpy(pContext->state.aClipPlane[index].plane, plane, sizeof(pContext->state.aClipPlane[index].plane));
6062
6063 /** @todo clip plane affected by model view in OpenGL & view in D3D + vertex shader -> not transformed (see Wine; state.c clipplane) */
6064 oglPlane[0] = (double)plane[0];
6065 oglPlane[1] = (double)plane[1];
6066 oglPlane[2] = (double)plane[2];
6067 oglPlane[3] = (double)plane[3];
6068
6069 glClipPlane(GL_CLIP_PLANE0 + index, oglPlane);
6070 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6071
6072 return VINF_SUCCESS;
6073}
6074
6075int vmsvga3dSetScissorRect(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
6076{
6077 PVMSVGA3DCONTEXT pContext;
6078 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6079 AssertReturn(pState, VERR_NO_MEMORY);
6080
6081 Log(("vmsvga3dSetScissorRect cid=%x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
6082
6083 if ( cid >= pState->cContexts
6084 || pState->paContext[cid].id != cid)
6085 {
6086 Log(("vmsvga3dSetScissorRect invalid context id!\n"));
6087 return VERR_INVALID_PARAMETER;
6088 }
6089 pContext = &pState->paContext[cid];
6090 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6091
6092 /* Store for vm state save/restore. */
6093 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_SCISSORRECT;
6094 pContext->state.RectScissor = *pRect;
6095
6096 glScissor(pRect->x, pRect->y, pRect->w, pRect->h);
6097 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6098
6099 return VINF_SUCCESS;
6100}
6101
6102static void vmsvgaColor2GLFloatArray(uint32_t color, GLfloat *pRed, GLfloat *pGreen, GLfloat *pBlue, GLfloat *pAlpha)
6103{
6104 /* Convert byte color components to float (0-1.0) */
6105 *pAlpha = (GLfloat)(color >> 24) / 255.0;
6106 *pRed = (GLfloat)((color >> 16) & 0xff) / 255.0;
6107 *pGreen = (GLfloat)((color >> 8) & 0xff) / 255.0;
6108 *pBlue = (GLfloat)(color & 0xff) / 255.0;
6109}
6110
6111int vmsvga3dCommandClear(PVGASTATE pThis, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth, uint32_t stencil,
6112 uint32_t cRects, SVGA3dRect *pRect)
6113{
6114 GLbitfield mask = 0;
6115 PVMSVGA3DCONTEXT pContext;
6116 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6117 AssertReturn(pState, VERR_NO_MEMORY);
6118 GLboolean fDepthWriteEnabled = GL_FALSE;
6119
6120 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));
6121
6122 if ( cid >= pState->cContexts
6123 || pState->paContext[cid].id != cid)
6124 {
6125 Log(("vmsvga3dCommandClear invalid context id!\n"));
6126 return VERR_INVALID_PARAMETER;
6127 }
6128 pContext = &pState->paContext[cid];
6129 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6130
6131 if (clearFlag & SVGA3D_CLEAR_COLOR)
6132 {
6133 GLfloat red, green, blue, alpha;
6134
6135 vmsvgaColor2GLFloatArray(color, &red, &green, &blue, &alpha);
6136
6137 /* Set the color clear value. */
6138 glClearColor(red, green, blue, alpha);
6139
6140 mask |= GL_COLOR_BUFFER_BIT;
6141 }
6142 if (clearFlag & SVGA3D_CLEAR_STENCIL)
6143 {
6144 /* @todo possibly the same problem as with glDepthMask */
6145 glClearStencil(stencil);
6146 mask |= GL_STENCIL_BUFFER_BIT;
6147 }
6148 if (clearFlag & SVGA3D_CLEAR_DEPTH)
6149 {
6150 glClearDepth((GLdouble)depth);
6151 mask |= GL_DEPTH_BUFFER_BIT;
6152
6153 /* glClear will not clear the depth buffer if writing is disabled. */
6154 glGetBooleanv(GL_DEPTH_WRITEMASK, &fDepthWriteEnabled);
6155 if (fDepthWriteEnabled == GL_FALSE)
6156 glDepthMask(GL_TRUE);
6157 }
6158
6159 if (cRects)
6160 {
6161 /* Save the current scissor test bit and scissor box. */
6162 glPushAttrib(GL_SCISSOR_BIT);
6163 glEnable(GL_SCISSOR_TEST);
6164 for (unsigned i=0; i < cRects; i++)
6165 {
6166 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));
6167 glScissor(pRect[i].x, pRect[i].y, pRect[i].w, pRect[i].h);
6168 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6169 glClear(mask);
6170 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6171 }
6172 /* Restore the old scissor test bit and box */
6173 glPopAttrib();
6174 }
6175 else
6176 {
6177 glClear(mask);
6178 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6179 }
6180
6181 /* Restore depth write state. */
6182 if ( (clearFlag & SVGA3D_CLEAR_DEPTH)
6183 && fDepthWriteEnabled == GL_FALSE)
6184 glDepthMask(GL_FALSE);
6185
6186 return VINF_SUCCESS;
6187}
6188
6189/* Convert VMWare vertex declaration to its OpenGL equivalent. */
6190int vmsvga3dVertexDecl2OGL(SVGA3dVertexArrayIdentity &identity, GLint &size, GLenum &type, GLboolean &normalized)
6191{
6192 normalized = GL_FALSE;
6193 switch (identity.type)
6194 {
6195 case SVGA3D_DECLTYPE_FLOAT1:
6196 size = 1;
6197 type = GL_FLOAT;
6198 break;
6199 case SVGA3D_DECLTYPE_FLOAT2:
6200 size = 2;
6201 type = GL_FLOAT;
6202 break;
6203 case SVGA3D_DECLTYPE_FLOAT3:
6204 size = 3;
6205 type = GL_FLOAT;
6206 break;
6207 case SVGA3D_DECLTYPE_FLOAT4:
6208 size = 4;
6209 type = GL_FLOAT;
6210 break;
6211
6212 case SVGA3D_DECLTYPE_D3DCOLOR:
6213 size = GL_BGRA; /* @note requires GL_ARB_vertex_array_bgra */
6214 type = GL_UNSIGNED_BYTE;
6215 normalized = GL_TRUE; /* glVertexAttribPointer fails otherwise */
6216 break;
6217
6218 case SVGA3D_DECLTYPE_UBYTE4N:
6219 normalized = GL_TRUE;
6220 /* no break */
6221 case SVGA3D_DECLTYPE_UBYTE4:
6222 size = 4;
6223 type = GL_UNSIGNED_BYTE;
6224 break;
6225
6226 case SVGA3D_DECLTYPE_SHORT2N:
6227 normalized = GL_TRUE;
6228 /* no break */
6229 case SVGA3D_DECLTYPE_SHORT2:
6230 size = 2;
6231 type = GL_SHORT;
6232 break;
6233
6234 case SVGA3D_DECLTYPE_SHORT4N:
6235 normalized = GL_TRUE;
6236 /* no break */
6237 case SVGA3D_DECLTYPE_SHORT4:
6238 size = 4;
6239 type = GL_SHORT;
6240 break;
6241
6242 case SVGA3D_DECLTYPE_USHORT4N:
6243 normalized = GL_TRUE;
6244 size = 4;
6245 type = GL_UNSIGNED_SHORT;
6246 break;
6247
6248 case SVGA3D_DECLTYPE_USHORT2N:
6249 normalized = GL_TRUE;
6250 size = 2;
6251 type = GL_UNSIGNED_SHORT;
6252 break;
6253
6254 case SVGA3D_DECLTYPE_UDEC3:
6255 size = 3;
6256 type = GL_UNSIGNED_INT_2_10_10_10_REV; /* @todo correct? */
6257 break;
6258
6259 case SVGA3D_DECLTYPE_DEC3N:
6260 normalized = true;
6261 size = 3;
6262 type = GL_INT_2_10_10_10_REV; /* @todo correct? */
6263 break;
6264
6265 case SVGA3D_DECLTYPE_FLOAT16_2:
6266 size = 2;
6267 type = GL_HALF_FLOAT;
6268 break;
6269 case SVGA3D_DECLTYPE_FLOAT16_4:
6270 size = 4;
6271 type = GL_HALF_FLOAT;
6272 break;
6273 default:
6274 AssertFailedReturn(VERR_INVALID_PARAMETER);
6275 }
6276
6277 //pVertexElement->Method = identity.method;
6278 //pVertexElement->Usage = identity.usage;
6279
6280 return VINF_SUCCESS;
6281}
6282
6283/* Convert VMWare primitive type to its OpenGL equivalent. */
6284/* Calculate the vertex count based on the primitive type and nr of primitives. */
6285int vmsvga3dPrimitiveType2OGL(SVGA3dPrimitiveType PrimitiveType, GLenum *pMode, uint32_t cPrimitiveCount, uint32_t *pcVertices)
6286{
6287 switch (PrimitiveType)
6288 {
6289 case SVGA3D_PRIMITIVE_TRIANGLELIST:
6290 *pMode = GL_TRIANGLES;
6291 *pcVertices = cPrimitiveCount * 3;
6292 break;
6293 case SVGA3D_PRIMITIVE_POINTLIST:
6294 *pMode = GL_POINTS;
6295 *pcVertices = cPrimitiveCount;
6296 break;
6297 case SVGA3D_PRIMITIVE_LINELIST:
6298 *pMode = GL_LINES;
6299 *pcVertices = cPrimitiveCount * 2;
6300 break;
6301 case SVGA3D_PRIMITIVE_LINESTRIP:
6302 *pMode = GL_LINE_STRIP;
6303 *pcVertices = cPrimitiveCount + 1;
6304 break;
6305 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
6306 *pMode = GL_TRIANGLE_STRIP;
6307 *pcVertices = cPrimitiveCount + 2;
6308 break;
6309 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
6310 *pMode = GL_TRIANGLE_FAN;
6311 *pcVertices = cPrimitiveCount + 2;
6312 break;
6313 default:
6314 return VERR_INVALID_PARAMETER;
6315 }
6316 return VINF_SUCCESS;
6317}
6318
6319int vmsvga3dDrawPrimitivesProcessVertexDecls(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t iVertexDeclBase, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl)
6320{
6321 unsigned sidVertex = pVertexDecl[0].array.surfaceId;
6322 PVMSVGA3DSURFACE pVertexSurface;
6323
6324 AssertReturn(sidVertex < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
6325 AssertReturn(sidVertex < pState->cSurfaces && pState->paSurface[sidVertex].id == sidVertex, VERR_INVALID_PARAMETER);
6326
6327 pVertexSurface = &pState->paSurface[sidVertex];
6328 Log(("vmsvga3dDrawPrimitives: vertex surface %x\n", sidVertex));
6329
6330 /* Create and/or bind the vertex buffer. */
6331 if (pVertexSurface->oglId.buffer == OPENGL_INVALID_ID)
6332 {
6333 Log(("vmsvga3dDrawPrimitives: create vertex buffer fDirty=%d size=%x bytes\n", pVertexSurface->fDirty, pVertexSurface->pMipmapLevels[0].cbSurface));
6334 pState->ext.glGenBuffers(1, &pVertexSurface->oglId.buffer);
6335 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6336
6337 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
6338 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6339
6340 Assert(pVertexSurface->fDirty);
6341 /* @todo rethink usage dynamic/static */
6342 pState->ext.glBufferData(GL_ARRAY_BUFFER, pVertexSurface->pMipmapLevels[0].cbSurface, pVertexSurface->pMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
6343 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6344
6345 pVertexSurface->pMipmapLevels[0].fDirty = false;
6346 pVertexSurface->fDirty = false;
6347
6348 pVertexSurface->flags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
6349 }
6350 else
6351 {
6352 Assert(pVertexSurface->fDirty == false);
6353 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
6354 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6355 }
6356 pVertexSurface->idAssociatedContext = pContext->id;
6357 LogFlow(("vmsvga3dDrawPrimitivesProcessVertexDecls: sid=%#x idAssociatedContext %#x -> %#x\n", pVertexSurface->id, pVertexSurface->idAssociatedContext, pContext->id));
6358
6359 /* Setup the vertex declarations. */
6360 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
6361 {
6362 GLint size;
6363 GLenum type;
6364 GLboolean normalized;
6365 GLuint index = iVertexDeclBase + iVertex;
6366
6367 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));
6368
6369 int rc = vmsvga3dVertexDecl2OGL(pVertexDecl[iVertex].identity, size, type, normalized);
6370 AssertRCReturn(rc, rc);
6371
6372 if (pContext->state.shidVertex != SVGA_ID_INVALID)
6373 {
6374 /* Use numbered vertex arrays when shaders are active. */
6375 pState->ext.glEnableVertexAttribArray(index);
6376 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6377 pState->ext.glVertexAttribPointer(index, size, type, normalized, pVertexDecl[iVertex].array.stride,
6378 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6379 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6380 /* case SVGA3D_DECLUSAGE_COLOR: @todo color component order not identical!! test GL_BGRA!! */
6381 }
6382 else
6383 {
6384 /* Use the predefined selection of vertex streams for the fixed pipeline. */
6385 switch (pVertexDecl[iVertex].identity.usage)
6386 {
6387 case SVGA3D_DECLUSAGE_POSITION:
6388 glEnableClientState(GL_VERTEX_ARRAY);
6389 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6390 glVertexPointer(size, type, pVertexDecl[iVertex].array.stride,
6391 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6392 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6393 break;
6394 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
6395 AssertFailed();
6396 break;
6397 case SVGA3D_DECLUSAGE_BLENDINDICES:
6398 AssertFailed();
6399 break;
6400 case SVGA3D_DECLUSAGE_NORMAL:
6401 glEnableClientState(GL_NORMAL_ARRAY);
6402 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6403 glNormalPointer(type, pVertexDecl[iVertex].array.stride,
6404 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6405 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6406 break;
6407 case SVGA3D_DECLUSAGE_PSIZE:
6408 AssertFailed();
6409 break;
6410 case SVGA3D_DECLUSAGE_TEXCOORD:
6411 /* Specify the affected texture unit. */
6412#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
6413 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
6414#else
6415 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
6416#endif
6417 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
6418 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6419 glTexCoordPointer(size, type, pVertexDecl[iVertex].array.stride,
6420 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6421 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6422 break;
6423 case SVGA3D_DECLUSAGE_TANGENT:
6424 AssertFailed();
6425 break;
6426 case SVGA3D_DECLUSAGE_BINORMAL:
6427 AssertFailed();
6428 break;
6429 case SVGA3D_DECLUSAGE_TESSFACTOR:
6430 AssertFailed();
6431 break;
6432 case SVGA3D_DECLUSAGE_POSITIONT:
6433 AssertFailed(); /* see position_transformed in Wine */
6434 break;
6435 case SVGA3D_DECLUSAGE_COLOR: /** @todo color component order not identical!! test GL_BGRA!! */
6436 glEnableClientState(GL_COLOR_ARRAY);
6437 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6438 glColorPointer(size, type, pVertexDecl[iVertex].array.stride,
6439 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6440 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6441 break;
6442 case SVGA3D_DECLUSAGE_FOG:
6443 glEnableClientState(GL_FOG_COORD_ARRAY);
6444 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6445 pState->ext.glFogCoordPointer(type, pVertexDecl[iVertex].array.stride,
6446 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6447 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6448 break;
6449 case SVGA3D_DECLUSAGE_DEPTH:
6450 AssertFailed();
6451 break;
6452 case SVGA3D_DECLUSAGE_SAMPLE:
6453 AssertFailed();
6454 break;
6455 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
6456 }
6457 }
6458
6459#ifdef LOG_ENABLED
6460 if (pVertexDecl[iVertex].array.stride == 0)
6461 Log(("vmsvga3dDrawPrimitives: stride == 0! Can be valid\n"));
6462#endif
6463 }
6464
6465 return VINF_SUCCESS;
6466}
6467
6468int vmsvga3dDrawPrimitivesCleanupVertexDecls(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t iVertexDeclBase, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl)
6469{
6470 /* Setup the vertex declarations. */
6471 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
6472 {
6473 if (pContext->state.shidVertex != SVGA_ID_INVALID)
6474 {
6475 /* Use numbered vertex arrays when shaders are active. */
6476 pState->ext.glDisableVertexAttribArray(iVertexDeclBase + iVertex);
6477 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6478 }
6479 else
6480 {
6481 /* Use the predefined selection of vertex streams for the fixed pipeline. */
6482 switch (pVertexDecl[iVertex].identity.usage)
6483 {
6484 case SVGA3D_DECLUSAGE_POSITION:
6485 glDisableClientState(GL_VERTEX_ARRAY);
6486 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6487 break;
6488 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
6489 break;
6490 case SVGA3D_DECLUSAGE_BLENDINDICES:
6491 break;
6492 case SVGA3D_DECLUSAGE_NORMAL:
6493 glDisableClientState(GL_NORMAL_ARRAY);
6494 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6495 break;
6496 case SVGA3D_DECLUSAGE_PSIZE:
6497 break;
6498 case SVGA3D_DECLUSAGE_TEXCOORD:
6499 /* Specify the affected texture unit. */
6500#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
6501 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
6502#else
6503 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
6504#endif
6505 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
6506 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6507 break;
6508 case SVGA3D_DECLUSAGE_TANGENT:
6509 break;
6510 case SVGA3D_DECLUSAGE_BINORMAL:
6511 break;
6512 case SVGA3D_DECLUSAGE_TESSFACTOR:
6513 break;
6514 case SVGA3D_DECLUSAGE_POSITIONT:
6515 break;
6516 case SVGA3D_DECLUSAGE_COLOR: /* @todo color component order not identical!! */
6517 glDisableClientState(GL_COLOR_ARRAY);
6518 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6519 break;
6520 case SVGA3D_DECLUSAGE_FOG:
6521 glDisableClientState(GL_FOG_COORD_ARRAY);
6522 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6523 break;
6524 case SVGA3D_DECLUSAGE_DEPTH:
6525 break;
6526 case SVGA3D_DECLUSAGE_SAMPLE:
6527 break;
6528 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
6529 }
6530 }
6531 }
6532 /* Unbind the vertex buffer after usage. */
6533 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0);
6534 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6535 return VINF_SUCCESS;
6536}
6537
6538int vmsvga3dDrawPrimitives(PVGASTATE pThis, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl, uint32_t numRanges, SVGA3dPrimitiveRange *pRange, uint32_t cVertexDivisor, SVGA3dVertexDivisor *pVertexDivisor)
6539{
6540 PVMSVGA3DCONTEXT pContext;
6541 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6542 AssertReturn(pState, VERR_INTERNAL_ERROR);
6543 int rc = VERR_NOT_IMPLEMENTED;
6544 uint32_t iCurrentVertex;
6545
6546 Log(("vmsvga3dDrawPrimitives cid=%x numVertexDecls=%d numRanges=%d, cVertexDivisor=%d\n", cid, numVertexDecls, numRanges, cVertexDivisor));
6547
6548 AssertReturn(numVertexDecls && numVertexDecls <= SVGA3D_MAX_VERTEX_ARRAYS, VERR_INVALID_PARAMETER);
6549 AssertReturn(numRanges && numRanges <= SVGA3D_MAX_DRAW_PRIMITIVE_RANGES, VERR_INVALID_PARAMETER);
6550 AssertReturn(!cVertexDivisor || cVertexDivisor == numVertexDecls, VERR_INVALID_PARAMETER);
6551 /* @todo */
6552 Assert(!cVertexDivisor);
6553
6554 if ( cid >= pState->cContexts
6555 || pState->paContext[cid].id != cid)
6556 {
6557 Log(("vmsvga3dDrawPrimitives invalid context id!\n"));
6558 return VERR_INVALID_PARAMETER;
6559 }
6560 pContext = &pState->paContext[cid];
6561 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6562
6563 /* Flush any shader changes. */
6564 if (pContext->pShaderContext)
6565 {
6566 uint32_t rtHeight = 0;
6567
6568 if (pContext->sidRenderTarget != SVGA_ID_INVALID)
6569 {
6570 PVMSVGA3DSURFACE pRenderTarget = &pState->paSurface[pContext->sidRenderTarget];
6571 rtHeight = pRenderTarget->pMipmapLevels[0].size.height;
6572 }
6573
6574 ShaderUpdateState(pContext->pShaderContext, rtHeight);
6575 }
6576
6577 /* Process all vertex declarations. Each vertex buffer is represented by one stream. */
6578 iCurrentVertex = 0;
6579 while (iCurrentVertex < numVertexDecls)
6580 {
6581 uint32_t sidVertex = SVGA_ID_INVALID;
6582 uint32_t iVertex;
6583
6584 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
6585 {
6586 if ( sidVertex != SVGA_ID_INVALID
6587 && pVertexDecl[iVertex].array.surfaceId != sidVertex
6588 )
6589 break;
6590 sidVertex = pVertexDecl[iVertex].array.surfaceId;
6591 }
6592
6593 rc = vmsvga3dDrawPrimitivesProcessVertexDecls(pState, pContext, iCurrentVertex, iVertex - iCurrentVertex, &pVertexDecl[iCurrentVertex]);
6594 AssertRCReturn(rc, rc);
6595
6596 iCurrentVertex = iVertex;
6597 }
6598
6599 /* Now draw the primitives. */
6600 for (unsigned iPrimitive = 0; iPrimitive < numRanges; iPrimitive++)
6601 {
6602 GLenum modeDraw;
6603 unsigned sidIndex = pRange[iPrimitive].indexArray.surfaceId;
6604 PVMSVGA3DSURFACE pIndexSurface = NULL;
6605 unsigned cVertices;
6606
6607 Log(("Primitive %d: type %s\n", iPrimitive, vmsvga3dPrimitiveType2String(pRange[iPrimitive].primType)));
6608 rc = vmsvga3dPrimitiveType2OGL(pRange[iPrimitive].primType, &modeDraw, pRange[iPrimitive].primitiveCount, &cVertices);
6609 if (RT_FAILURE(rc))
6610 {
6611 AssertRC(rc);
6612 goto internal_error;
6613 }
6614
6615 if (sidIndex != SVGA3D_INVALID_ID)
6616 {
6617 AssertMsg(pRange[iPrimitive].indexWidth == sizeof(uint32_t) || pRange[iPrimitive].indexWidth == sizeof(uint16_t), ("Unsupported primitive width %d\n", pRange[iPrimitive].indexWidth));
6618
6619 if ( sidIndex >= SVGA3D_MAX_SURFACE_IDS
6620 || sidIndex >= pState->cSurfaces
6621 || pState->paSurface[sidIndex].id != sidIndex)
6622 {
6623 Assert(sidIndex < SVGA3D_MAX_SURFACE_IDS);
6624 Assert(sidIndex < pState->cSurfaces && pState->paSurface[sidIndex].id == sidIndex);
6625 rc = VERR_INVALID_PARAMETER;
6626 goto internal_error;
6627 }
6628 pIndexSurface = &pState->paSurface[sidIndex];
6629 Log(("vmsvga3dDrawPrimitives: index surface %x\n", sidIndex));
6630
6631 if (pIndexSurface->oglId.buffer == OPENGL_INVALID_ID)
6632 {
6633 Log(("vmsvga3dDrawPrimitives: create index buffer fDirty=%d size=%x bytes\n", pIndexSurface->fDirty, pIndexSurface->pMipmapLevels[0].cbSurface));
6634
6635 pState->ext.glGenBuffers(1, &pIndexSurface->oglId.buffer);
6636 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6637
6638 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
6639 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6640
6641 Assert(pIndexSurface->fDirty);
6642
6643 /* @todo rethink usage dynamic/static */
6644 pState->ext.glBufferData(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->pMipmapLevels[0].cbSurface, pIndexSurface->pMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
6645 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6646
6647 pIndexSurface->pMipmapLevels[0].fDirty = false;
6648 pIndexSurface->fDirty = false;
6649
6650 pIndexSurface->flags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
6651 }
6652 else
6653 {
6654 Assert(pIndexSurface->fDirty == false);
6655
6656 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
6657 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6658 }
6659 LogFlow(("vmsvga3dDrawPrimitives: sid=%#x idAssociatedContext %#x -> %#x\n", pIndexSurface->id, pIndexSurface->idAssociatedContext, pContext->id));
6660 pIndexSurface->idAssociatedContext = pContext->id;
6661 }
6662
6663 if (!pIndexSurface)
6664 {
6665 /* Render without an index buffer */
6666 Log(("DrawPrimitive %x cPrimitives=%d cVertices=%d index index bias=%d\n", modeDraw, pRange[iPrimitive].primitiveCount, cVertices, pRange[iPrimitive].indexBias));
6667 glDrawArrays(modeDraw, pRange[iPrimitive].indexBias, cVertices);
6668 }
6669 else
6670 {
6671 Assert(pRange[iPrimitive].indexBias >= 0); /* @todo */
6672 Assert(pRange[iPrimitive].indexWidth == pRange[iPrimitive].indexArray.stride);
6673
6674 /* Render with an index buffer */
6675 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));
6676 if (pRange[iPrimitive].indexBias == 0)
6677 glDrawElements(modeDraw,
6678 cVertices,
6679 (pRange[iPrimitive].indexWidth == sizeof(uint16_t)) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
6680 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset); /* byte offset in indices buffer */
6681 else
6682 pState->ext.glDrawElementsBaseVertex(modeDraw,
6683 cVertices,
6684 (pRange[iPrimitive].indexWidth == sizeof(uint16_t)) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
6685 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
6686 pRange[iPrimitive].indexBias); /* basevertex */
6687
6688 /* Unbind the index buffer after usage. */
6689 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
6690 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6691 }
6692 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6693 }
6694
6695internal_error:
6696
6697 /* Deactivate the vertex declarations. */
6698 iCurrentVertex = 0;
6699 while (iCurrentVertex < numVertexDecls)
6700 {
6701 uint32_t sidVertex = SVGA_ID_INVALID;
6702 uint32_t iVertex;
6703
6704 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
6705 {
6706 if ( sidVertex != SVGA_ID_INVALID
6707 && pVertexDecl[iVertex].array.surfaceId != sidVertex
6708 )
6709 break;
6710 sidVertex = pVertexDecl[iVertex].array.surfaceId;
6711 }
6712
6713 rc = vmsvga3dDrawPrimitivesCleanupVertexDecls(pState, pContext, iCurrentVertex, iVertex - iCurrentVertex, &pVertexDecl[iCurrentVertex]);
6714 AssertRCReturn(rc, rc);
6715
6716 iCurrentVertex = iVertex;
6717 }
6718#ifdef DEBUG
6719 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTexture); i++)
6720 {
6721 if (pContext->aSidActiveTexture[i] != SVGA3D_INVALID_ID)
6722 {
6723 GLint activeTexture = 0;
6724 GLint activeTextureUnit = 0;
6725
6726 glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
6727 pState->ext.glActiveTexture(GL_TEXTURE0 + i);
6728
6729 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
6730 pState->ext.glActiveTexture(activeTextureUnit);
6731
6732 if (pContext->aSidActiveTexture[activeTextureUnit - GL_TEXTURE0] != SVGA3D_INVALID_ID)
6733 {
6734 PVMSVGA3DSURFACE pTexture;
6735 pTexture = &pState->paSurface[pContext->aSidActiveTexture[activeTextureUnit - GL_TEXTURE0]];
6736
6737 AssertMsg(pTexture->oglId.texture == (GLuint)activeTexture, ("%x vs %x unit %d - %d\n", pTexture->oglId.texture, activeTexture, i, activeTextureUnit - GL_TEXTURE0));
6738 }
6739 }
6740 }
6741#endif
6742
6743#ifdef DEBUG_GFX_WINDOW
6744 if (pContext->aSidActiveTexture[0])
6745 {
6746 SVGA3dCopyRect rect;
6747
6748 rect.srcx = rect.srcy = rect.x = rect.y = 0;
6749 rect.w = 800;
6750 rect.h = 600;
6751 vmsvga3dCommandPresent(pThis, pContext->sidRenderTarget, 0, NULL);
6752 }
6753#endif
6754 return rc;
6755}
6756
6757
6758int vmsvga3dShaderDefine(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type, uint32_t cbData, uint32_t *pShaderData)
6759{
6760 PVMSVGA3DCONTEXT pContext;
6761 PVMSVGA3DSHADER pShader;
6762 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6763 AssertReturn(pState, VERR_NO_MEMORY);
6764 int rc;
6765
6766 Log(("vmsvga3dShaderDefine cid=%x shid=%x type=%s cbData=%x\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cbData));
6767 Log3(("shader code:\n%.*Rhxd\n", cbData, pShaderData));
6768
6769 if ( cid >= pState->cContexts
6770 || pState->paContext[cid].id != cid)
6771 {
6772 Log(("vmsvga3dShaderDefine invalid context id!\n"));
6773 return VERR_INVALID_PARAMETER;
6774 }
6775 pContext = &pState->paContext[cid];
6776 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6777
6778 AssertReturn(shid < SVGA3D_MAX_SHADER_IDS, VERR_INVALID_PARAMETER);
6779 if (type == SVGA3D_SHADERTYPE_VS)
6780 {
6781 if (shid >= pContext->cVertexShaders)
6782 {
6783 pContext->paVertexShader = (PVMSVGA3DSHADER)RTMemRealloc(pContext->paVertexShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
6784 AssertReturn(pContext->paVertexShader, VERR_NO_MEMORY);
6785 memset(&pContext->paVertexShader[pContext->cVertexShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cVertexShaders));
6786 for (uint32_t i = pContext->cVertexShaders; i < shid + 1; i++)
6787 pContext->paVertexShader[i].id = SVGA3D_INVALID_ID;
6788 pContext->cVertexShaders = shid + 1;
6789 }
6790 /* If one already exists with this id, then destroy it now. */
6791 if (pContext->paVertexShader[shid].id != SVGA3D_INVALID_ID)
6792 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paVertexShader[shid].type);
6793
6794 pShader = &pContext->paVertexShader[shid];
6795 }
6796 else
6797 {
6798 Assert(type == SVGA3D_SHADERTYPE_PS);
6799 if (shid >= pContext->cPixelShaders)
6800 {
6801 pContext->paPixelShader = (PVMSVGA3DSHADER)RTMemRealloc(pContext->paPixelShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
6802 AssertReturn(pContext->paPixelShader, VERR_NO_MEMORY);
6803 memset(&pContext->paPixelShader[pContext->cPixelShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cPixelShaders));
6804 for (uint32_t i = pContext->cPixelShaders; i < shid + 1; i++)
6805 pContext->paPixelShader[i].id = SVGA3D_INVALID_ID;
6806 pContext->cPixelShaders = shid + 1;
6807 }
6808 /* If one already exists with this id, then destroy it now. */
6809 if (pContext->paPixelShader[shid].id != SVGA3D_INVALID_ID)
6810 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paPixelShader[shid].type);
6811
6812 pShader = &pContext->paPixelShader[shid];
6813 }
6814
6815 memset(pShader, 0, sizeof(*pShader));
6816 pShader->id = shid;
6817 pShader->cid = cid;
6818 pShader->type = type;
6819 pShader->cbData = cbData;
6820 pShader->pShaderProgram = RTMemAllocZ(cbData);
6821 AssertReturn(pShader->pShaderProgram, VERR_NO_MEMORY);
6822 memcpy(pShader->pShaderProgram, pShaderData, cbData);
6823
6824 switch (type)
6825 {
6826 case SVGA3D_SHADERTYPE_VS:
6827 rc = ShaderCreateVertexShader(pContext->pShaderContext, (const uint32_t *)pShaderData, &pShader->u.pVertexShader);
6828 break;
6829
6830 case SVGA3D_SHADERTYPE_PS:
6831 rc = ShaderCreatePixelShader(pContext->pShaderContext, (const uint32_t *)pShaderData, &pShader->u.pPixelShader);
6832 break;
6833
6834 default:
6835 AssertFailedReturn(VERR_INVALID_PARAMETER);
6836 }
6837
6838 return rc;
6839}
6840
6841int vmsvga3dShaderDestroy(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type)
6842{
6843 PVMSVGA3DCONTEXT pContext;
6844 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6845 AssertReturn(pState, VERR_NO_MEMORY);
6846 PVMSVGA3DSHADER pShader = NULL;
6847 int rc;
6848
6849 Log(("vmsvga3dShaderDestroy cid=%x shid=%x type=%s\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL"));
6850
6851 if ( cid >= pState->cContexts
6852 || pState->paContext[cid].id != cid)
6853 {
6854 Log(("vmsvga3dShaderDestroy invalid context id!\n"));
6855 return VERR_INVALID_PARAMETER;
6856 }
6857 pContext = &pState->paContext[cid];
6858 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6859
6860 if (type == SVGA3D_SHADERTYPE_VS)
6861 {
6862 if ( shid < pContext->cVertexShaders
6863 && pContext->paVertexShader[shid].id == shid)
6864 {
6865 pShader = &pContext->paVertexShader[shid];
6866 rc = ShaderDestroyVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
6867 AssertRC(rc);
6868 }
6869 }
6870 else
6871 {
6872 Assert(type == SVGA3D_SHADERTYPE_PS);
6873 if ( shid < pContext->cPixelShaders
6874 && pContext->paPixelShader[shid].id == shid)
6875 {
6876 pShader = &pContext->paPixelShader[shid];
6877 rc = ShaderDestroyPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
6878 AssertRC(rc);
6879 }
6880 }
6881
6882 if (pShader)
6883 {
6884 if (pShader->pShaderProgram)
6885 RTMemFree(pShader->pShaderProgram);
6886 memset(pShader, 0, sizeof(*pShader));
6887 pShader->id = SVGA3D_INVALID_ID;
6888 }
6889 else
6890 AssertFailedReturn(VERR_INVALID_PARAMETER);
6891
6892 return VINF_SUCCESS;
6893}
6894
6895int vmsvga3dShaderSet(PVGASTATE pThis, uint32_t cid, SVGA3dShaderType type, uint32_t shid)
6896{
6897 PVMSVGA3DCONTEXT pContext;
6898 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6899 AssertReturn(pState, VERR_NO_MEMORY);
6900 int rc;
6901
6902 Log(("vmsvga3dShaderSet cid=%x type=%s shid=%d\n", cid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", shid));
6903
6904 if ( cid >= pState->cContexts
6905 || pState->paContext[cid].id != cid)
6906 {
6907 Log(("vmsvga3dShaderSet invalid context id!\n"));
6908 return VERR_INVALID_PARAMETER;
6909 }
6910 pContext = &pState->paContext[cid];
6911 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6912
6913 if (type == SVGA3D_SHADERTYPE_VS)
6914 {
6915 /* Save for vm state save/restore. */
6916 pContext->state.shidVertex = shid;
6917 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VERTEXSHADER;
6918
6919 if ( shid < pContext->cVertexShaders
6920 && pContext->paVertexShader[shid].id == shid)
6921 {
6922 PVMSVGA3DSHADER pShader = &pContext->paVertexShader[shid];
6923 Assert(type == pShader->type);
6924
6925 rc = ShaderSetVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
6926 AssertRCReturn(rc, rc);
6927 }
6928 else
6929 if (shid == SVGA_ID_INVALID)
6930 {
6931 /* Unselect shader. */
6932 rc = ShaderSetVertexShader(pContext->pShaderContext, NULL);
6933 AssertRCReturn(rc, rc);
6934 }
6935 else
6936 AssertFailedReturn(VERR_INVALID_PARAMETER);
6937 }
6938 else
6939 {
6940 /* Save for vm state save/restore. */
6941 pContext->state.shidPixel = shid;
6942 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_PIXELSHADER;
6943
6944 Assert(type == SVGA3D_SHADERTYPE_PS);
6945 if ( shid < pContext->cPixelShaders
6946 && pContext->paPixelShader[shid].id == shid)
6947 {
6948 PVMSVGA3DSHADER pShader = &pContext->paPixelShader[shid];
6949 Assert(type == pShader->type);
6950
6951 rc = ShaderSetPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
6952 AssertRCReturn(rc, rc);
6953 }
6954 else
6955 if (shid == SVGA_ID_INVALID)
6956 {
6957 /* Unselect shader. */
6958 rc = ShaderSetPixelShader(pContext->pShaderContext, NULL);
6959 AssertRCReturn(rc, rc);
6960 }
6961 else
6962 AssertFailedReturn(VERR_INVALID_PARAMETER);
6963 }
6964
6965 return VINF_SUCCESS;
6966}
6967
6968int vmsvga3dShaderSetConst(PVGASTATE pThis, uint32_t cid, uint32_t reg, SVGA3dShaderType type, SVGA3dShaderConstType ctype, uint32_t cRegisters, uint32_t *pValues)
6969{
6970 PVMSVGA3DCONTEXT pContext;
6971 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pThis->svga.p3dState;
6972 AssertReturn(pState, VERR_NO_MEMORY);
6973 int rc;
6974
6975 Log(("vmsvga3dShaderSetConst cid=%x reg=%x type=%s cregs=%d ctype=%x\n", cid, reg, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cRegisters, ctype));
6976
6977 if ( cid >= pState->cContexts
6978 || pState->paContext[cid].id != cid)
6979 {
6980 Log(("vmsvga3dShaderSetConst invalid context id!\n"));
6981 return VERR_INVALID_PARAMETER;
6982 }
6983 pContext = &pState->paContext[cid];
6984 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6985
6986 for (uint32_t i = 0; i < cRegisters; i++)
6987 {
6988#ifdef LOG_ENABLED
6989 switch (ctype)
6990 {
6991 case SVGA3D_CONST_TYPE_FLOAT:
6992 {
6993 float *pValuesF = (float *)pValues;
6994 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)));
6995 break;
6996 }
6997
6998 case SVGA3D_CONST_TYPE_INT:
6999 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]));
7000 break;
7001
7002 case SVGA3D_CONST_TYPE_BOOL:
7003 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]));
7004 break;
7005 }
7006#endif
7007 vmsvga3dSaveShaderConst(pContext, reg + i, type, ctype, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]);
7008 }
7009
7010 switch (type)
7011 {
7012 case SVGA3D_SHADERTYPE_VS:
7013 switch (ctype)
7014 {
7015 case SVGA3D_CONST_TYPE_FLOAT:
7016 rc = ShaderSetVertexShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
7017 break;
7018
7019 case SVGA3D_CONST_TYPE_INT:
7020 rc = ShaderSetVertexShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
7021 break;
7022
7023 case SVGA3D_CONST_TYPE_BOOL:
7024 rc = ShaderSetVertexShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
7025 break;
7026
7027 default:
7028 AssertFailedReturn(VERR_INVALID_PARAMETER);
7029 }
7030 AssertRCReturn(rc, rc);
7031 break;
7032
7033 case SVGA3D_SHADERTYPE_PS:
7034 switch (ctype)
7035 {
7036 case SVGA3D_CONST_TYPE_FLOAT:
7037 rc = ShaderSetPixelShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
7038 break;
7039
7040 case SVGA3D_CONST_TYPE_INT:
7041 rc = ShaderSetPixelShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
7042 break;
7043
7044 case SVGA3D_CONST_TYPE_BOOL:
7045 rc = ShaderSetPixelShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
7046 break;
7047
7048 default:
7049 AssertFailedReturn(VERR_INVALID_PARAMETER);
7050 }
7051 AssertRCReturn(rc, rc);
7052 break;
7053
7054 default:
7055 AssertFailedReturn(VERR_INVALID_PARAMETER);
7056 }
7057
7058 return VINF_SUCCESS;
7059}
7060
7061
7062int vmsvga3dQueryBegin(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type)
7063{
7064 AssertFailed();
7065 return VERR_NOT_IMPLEMENTED;
7066}
7067
7068int vmsvga3dQueryEnd(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type, SVGAGuestPtr guestResult)
7069{
7070 AssertFailed();
7071 return VERR_NOT_IMPLEMENTED;
7072}
7073
7074int vmsvga3dQueryWait(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type, SVGAGuestPtr guestResult)
7075{
7076 AssertFailed();
7077 return VERR_NOT_IMPLEMENTED;
7078}
7079
Note: See TracBrowser for help on using the repository browser.

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