VirtualBox

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

Last change on this file since 82095 was 82095, checked in by vboxsync, 5 years ago

Devices/Graphics: renamed VMSVGA3DSURFACE::pMipmapLevels to paMipmapLevels; use a helper to get PVMSVGA3DMIPMAPLEVEL pointer.

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