VirtualBox

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

Last change on this file since 97622 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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