VirtualBox

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

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

Devices/Graphics: fixed the wrong translation of BlendOp in the OpenGL backend

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