VirtualBox

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

Last change on this file since 76196 was 75811, checked in by vboxsync, 6 years ago

DevVGA-SVGA3d-ogl.cpp: get rid of the overlay window on X11 host.

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