VirtualBox

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

Last change on this file since 89121 was 89121, checked in by vboxsync, 4 years ago

Devices/Graphics: Function tables for 3D backends. bugref:9830

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