VirtualBox

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

Last change on this file since 95008 was 95008, checked in by vboxsync, 3 years ago

Devices/Graphics: 64 bit surface flags and increased save state version; unordered access views; new commands: bugref:9830

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