VirtualBox

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

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

DevVGA-SVGA3d: dynamic loading of opengl (not enabled)

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