VirtualBox

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

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

DevVGA-SVGA3d-ogl: remember type of created resource; CopySurface/StretchBlt fixes; vmsvga3dBackSurfaceDMACopyBox support for 3D textures; occlusion query functions should set current context.

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