VirtualBox

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

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

VMSVGA: Ignore requests to set invalid transform type or render state, bugref:9460

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