VirtualBox

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

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

Devices/Graphics: suppress noisy logging.

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