VirtualBox

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

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

Devices/Graphics: use more vmsvga3d helpers, minor cleanups (build fix)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 254.0 KB
Line 
1/* $Id: DevVGA-SVGA3d-ogl.cpp 78593 2019-05-20 10:20:22Z 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 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4451
4452 AssertReturn(pState, VERR_NO_MEMORY);
4453 AssertReturn((unsigned)type < SVGA3D_RT_MAX, VERR_INVALID_PARAMETER);
4454
4455 LogFunc(("cid=%x type=%x sid=%x\n", cid, type, target.sid));
4456
4457 PVMSVGA3DCONTEXT pContext;
4458 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4459 AssertRCReturn(rc, rc);
4460
4461 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4462
4463 /* Save for vm state save/restore. */
4464 pContext->state.aRenderTargets[type] = target.sid;
4465
4466 if (target.sid == SVGA3D_INVALID_ID)
4467 {
4468 /* Disable render target. */
4469 switch (type)
4470 {
4471 case SVGA3D_RT_DEPTH:
4472 case SVGA3D_RT_STENCIL:
4473 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER, (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
4474 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4475 break;
4476
4477 case SVGA3D_RT_COLOR0:
4478 case SVGA3D_RT_COLOR1:
4479 case SVGA3D_RT_COLOR2:
4480 case SVGA3D_RT_COLOR3:
4481 case SVGA3D_RT_COLOR4:
4482 case SVGA3D_RT_COLOR5:
4483 case SVGA3D_RT_COLOR6:
4484 case SVGA3D_RT_COLOR7:
4485 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0, 0, 0, 0);
4486 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4487 break;
4488
4489 default:
4490 AssertFailedReturn(VERR_INVALID_PARAMETER);
4491 }
4492 return VINF_SUCCESS;
4493 }
4494
4495 PVMSVGA3DSURFACE pRenderTarget;
4496 rc = vmsvga3dSurfaceFromSid(pState, target.sid, &pRenderTarget);
4497 AssertRCReturn(rc, rc);
4498
4499 switch (type)
4500 {
4501 case SVGA3D_RT_DEPTH:
4502 case SVGA3D_RT_STENCIL:
4503#if 1
4504 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
4505 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
4506 {
4507 LogFunc(("create depth texture to be used as render target; surface id=%x type=%d format=%d -> create texture\n",
4508 target.sid, pRenderTarget->surfaceFlags, pRenderTarget->format));
4509 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
4510 AssertRCReturn(rc, rc);
4511 }
4512
4513 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
4514 Assert(!pRenderTarget->fDirty);
4515
4516 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
4517
4518 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER,
4519 (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT,
4520 GL_TEXTURE_2D, pRenderTarget->oglId.texture, target.mipmap);
4521 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4522#else
4523 AssertReturn(target.mipmap == 0, VERR_INVALID_PARAMETER);
4524 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
4525 {
4526 Log(("vmsvga3dSetRenderTarget: create renderbuffer to be used as render target; surface id=%x type=%d format=%d\n", target.sid, pRenderTarget->surfaceFlags, pRenderTarget->internalFormatGL));
4527 pContext = &pState->SharedCtx;
4528 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4529
4530 pState->ext.glGenRenderbuffers(1, &pRenderTarget->oglId.renderbuffer);
4531 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4532 pSurface->enmOGLResType = VMSVGA3D_OGLRESTYPE_RENDERBUFFER;
4533
4534 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
4535 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4536
4537 pState->ext.glRenderbufferStorage(GL_RENDERBUFFER,
4538 pRenderTarget->internalFormatGL,
4539 pRenderTarget->pMipmapLevels[0].mipmapSize.width,
4540 pRenderTarget->pMipmapLevels[0].mipmapSize.height);
4541 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4542
4543 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, OPENGL_INVALID_ID);
4544 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4545
4546 pContext = pState->papContexts[cid];
4547 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4548 pRenderTarget->idWeakContextAssociation = cid;
4549 }
4550
4551 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
4552 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4553 Assert(!pRenderTarget->fDirty);
4554 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
4555
4556 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
4557
4558 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
4559 (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT,
4560 GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
4561 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4562#endif
4563 break;
4564
4565 case SVGA3D_RT_COLOR0:
4566 case SVGA3D_RT_COLOR1:
4567 case SVGA3D_RT_COLOR2:
4568 case SVGA3D_RT_COLOR3:
4569 case SVGA3D_RT_COLOR4:
4570 case SVGA3D_RT_COLOR5:
4571 case SVGA3D_RT_COLOR6:
4572 case SVGA3D_RT_COLOR7:
4573 {
4574 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
4575 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
4576 {
4577 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));
4578 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
4579 AssertRCReturn(rc, rc);
4580 }
4581
4582 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
4583 Assert(!pRenderTarget->fDirty);
4584
4585 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
4586
4587 GLenum textarget;
4588 if (pRenderTarget->surfaceFlags & SVGA3D_SURFACE_CUBEMAP)
4589 textarget = vmsvga3dCubemapFaceFromIndex(target.face);
4590 else
4591 textarget = GL_TEXTURE_2D;
4592 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0,
4593 textarget, pRenderTarget->oglId.texture, target.mipmap);
4594 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4595
4596#ifdef DEBUG
4597 GLenum status = pState->ext.glCheckFramebufferStatus(GL_FRAMEBUFFER);
4598 if (status != GL_FRAMEBUFFER_COMPLETE)
4599 Log(("vmsvga3dSetRenderTarget: WARNING: glCheckFramebufferStatus returned %x\n", status));
4600#endif
4601 /** @todo use glDrawBuffers too? */
4602 break;
4603 }
4604
4605 default:
4606 AssertFailedReturn(VERR_INVALID_PARAMETER);
4607 }
4608
4609 return VINF_SUCCESS;
4610}
4611
4612#if 0
4613/**
4614 * Convert SVGA texture combiner value to its D3D equivalent
4615 */
4616static DWORD vmsvga3dTextureCombiner2D3D(uint32_t value)
4617{
4618 switch (value)
4619 {
4620 case SVGA3D_TC_DISABLE:
4621 return D3DTOP_DISABLE;
4622 case SVGA3D_TC_SELECTARG1:
4623 return D3DTOP_SELECTARG1;
4624 case SVGA3D_TC_SELECTARG2:
4625 return D3DTOP_SELECTARG2;
4626 case SVGA3D_TC_MODULATE:
4627 return D3DTOP_MODULATE;
4628 case SVGA3D_TC_ADD:
4629 return D3DTOP_ADD;
4630 case SVGA3D_TC_ADDSIGNED:
4631 return D3DTOP_ADDSIGNED;
4632 case SVGA3D_TC_SUBTRACT:
4633 return D3DTOP_SUBTRACT;
4634 case SVGA3D_TC_BLENDTEXTUREALPHA:
4635 return D3DTOP_BLENDTEXTUREALPHA;
4636 case SVGA3D_TC_BLENDDIFFUSEALPHA:
4637 return D3DTOP_BLENDDIFFUSEALPHA;
4638 case SVGA3D_TC_BLENDCURRENTALPHA:
4639 return D3DTOP_BLENDCURRENTALPHA;
4640 case SVGA3D_TC_BLENDFACTORALPHA:
4641 return D3DTOP_BLENDFACTORALPHA;
4642 case SVGA3D_TC_MODULATE2X:
4643 return D3DTOP_MODULATE2X;
4644 case SVGA3D_TC_MODULATE4X:
4645 return D3DTOP_MODULATE4X;
4646 case SVGA3D_TC_DSDT:
4647 AssertFailed(); /** @todo ??? */
4648 return D3DTOP_DISABLE;
4649 case SVGA3D_TC_DOTPRODUCT3:
4650 return D3DTOP_DOTPRODUCT3;
4651 case SVGA3D_TC_BLENDTEXTUREALPHAPM:
4652 return D3DTOP_BLENDTEXTUREALPHAPM;
4653 case SVGA3D_TC_ADDSIGNED2X:
4654 return D3DTOP_ADDSIGNED2X;
4655 case SVGA3D_TC_ADDSMOOTH:
4656 return D3DTOP_ADDSMOOTH;
4657 case SVGA3D_TC_PREMODULATE:
4658 return D3DTOP_PREMODULATE;
4659 case SVGA3D_TC_MODULATEALPHA_ADDCOLOR:
4660 return D3DTOP_MODULATEALPHA_ADDCOLOR;
4661 case SVGA3D_TC_MODULATECOLOR_ADDALPHA:
4662 return D3DTOP_MODULATECOLOR_ADDALPHA;
4663 case SVGA3D_TC_MODULATEINVALPHA_ADDCOLOR:
4664 return D3DTOP_MODULATEINVALPHA_ADDCOLOR;
4665 case SVGA3D_TC_MODULATEINVCOLOR_ADDALPHA:
4666 return D3DTOP_MODULATEINVCOLOR_ADDALPHA;
4667 case SVGA3D_TC_BUMPENVMAPLUMINANCE:
4668 return D3DTOP_BUMPENVMAPLUMINANCE;
4669 case SVGA3D_TC_MULTIPLYADD:
4670 return D3DTOP_MULTIPLYADD;
4671 case SVGA3D_TC_LERP:
4672 return D3DTOP_LERP;
4673 default:
4674 AssertFailed();
4675 return D3DTOP_DISABLE;
4676 }
4677}
4678
4679/**
4680 * Convert SVGA texture arg data value to its D3D equivalent
4681 */
4682static DWORD vmsvga3dTextureArgData2D3D(uint32_t value)
4683{
4684 switch (value)
4685 {
4686 case SVGA3D_TA_CONSTANT:
4687 return D3DTA_CONSTANT;
4688 case SVGA3D_TA_PREVIOUS:
4689 return D3DTA_CURRENT; /* current = previous */
4690 case SVGA3D_TA_DIFFUSE:
4691 return D3DTA_DIFFUSE;
4692 case SVGA3D_TA_TEXTURE:
4693 return D3DTA_TEXTURE;
4694 case SVGA3D_TA_SPECULAR:
4695 return D3DTA_SPECULAR;
4696 default:
4697 AssertFailed();
4698 return 0;
4699 }
4700}
4701
4702/**
4703 * Convert SVGA texture transform flag value to its D3D equivalent
4704 */
4705static DWORD vmsvga3dTextTransformFlags2D3D(uint32_t value)
4706{
4707 switch (value)
4708 {
4709 case SVGA3D_TEX_TRANSFORM_OFF:
4710 return D3DTTFF_DISABLE;
4711 case SVGA3D_TEX_TRANSFORM_S:
4712 return D3DTTFF_COUNT1; /** @todo correct? */
4713 case SVGA3D_TEX_TRANSFORM_T:
4714 return D3DTTFF_COUNT2; /** @todo correct? */
4715 case SVGA3D_TEX_TRANSFORM_R:
4716 return D3DTTFF_COUNT3; /** @todo correct? */
4717 case SVGA3D_TEX_TRANSFORM_Q:
4718 return D3DTTFF_COUNT4; /** @todo correct? */
4719 case SVGA3D_TEX_PROJECTED:
4720 return D3DTTFF_PROJECTED;
4721 default:
4722 AssertFailed();
4723 return 0;
4724 }
4725}
4726#endif
4727
4728static GLenum vmsvga3dTextureAddress2OGL(SVGA3dTextureAddress value)
4729{
4730 switch (value)
4731 {
4732 case SVGA3D_TEX_ADDRESS_WRAP:
4733 return GL_REPEAT;
4734 case SVGA3D_TEX_ADDRESS_MIRROR:
4735 return GL_MIRRORED_REPEAT;
4736 case SVGA3D_TEX_ADDRESS_CLAMP:
4737 return GL_CLAMP_TO_EDGE;
4738 case SVGA3D_TEX_ADDRESS_BORDER:
4739 return GL_CLAMP_TO_BORDER;
4740 case SVGA3D_TEX_ADDRESS_MIRRORONCE:
4741 AssertFailed();
4742 return GL_CLAMP_TO_EDGE_SGIS; /** @todo correct? */
4743
4744 case SVGA3D_TEX_ADDRESS_EDGE:
4745 case SVGA3D_TEX_ADDRESS_INVALID:
4746 default:
4747 AssertFailed();
4748 return GL_REPEAT; /* default */
4749 }
4750}
4751
4752static GLenum vmsvga3dTextureFilter2OGL(SVGA3dTextureFilter value)
4753{
4754 switch (value)
4755 {
4756 case SVGA3D_TEX_FILTER_NONE:
4757 case SVGA3D_TEX_FILTER_LINEAR:
4758 return GL_LINEAR;
4759 case SVGA3D_TEX_FILTER_NEAREST:
4760 return GL_NEAREST;
4761 case SVGA3D_TEX_FILTER_ANISOTROPIC:
4762 /** @todo */
4763 case SVGA3D_TEX_FILTER_FLATCUBIC: // Deprecated, not implemented
4764 case SVGA3D_TEX_FILTER_GAUSSIANCUBIC: // Deprecated, not implemented
4765 case SVGA3D_TEX_FILTER_PYRAMIDALQUAD: // Not currently implemented
4766 case SVGA3D_TEX_FILTER_GAUSSIANQUAD: // Not currently implemented
4767 default:
4768 AssertFailed();
4769 return GL_LINEAR; /* default */
4770 }
4771}
4772
4773uint32_t vmsvga3dSVGA3dColor2RGBA(SVGA3dColor value)
4774{
4775 /* flip the red and blue bytes */
4776 uint8_t blue = value & 0xff;
4777 uint8_t red = (value >> 16) & 0xff;
4778 return (value & 0xff00ff00) | red | (blue << 16);
4779}
4780
4781int vmsvga3dSetTextureState(PVGASTATE pThis, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
4782{
4783 GLenum val = ~(GLenum)0; /* Shut up MSC. */
4784 GLenum currentStage = ~(GLenum)0;
4785 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4786 AssertReturn(pState, VERR_NO_MEMORY);
4787
4788 Log(("vmsvga3dSetTextureState %x cTextureState=%d\n", cid, cTextureStates));
4789
4790 PVMSVGA3DCONTEXT pContext;
4791 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4792 AssertRCReturn(rc, rc);
4793
4794 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4795
4796 /* Which texture is active for the current stage. Needed to use right OpenGL target when setting parameters. */
4797 PVMSVGA3DSURFACE pCurrentTextureSurface = NULL;
4798
4799 for (uint32_t i = 0; i < cTextureStates; ++i)
4800 {
4801 GLenum textureType = ~(GLenum)0;
4802#if 0
4803 GLenum samplerType = ~(GLenum)0;
4804#endif
4805
4806 LogFunc(("cid=%x stage=%d type=%s (%x) val=%x\n",
4807 cid, pTextureState[i].stage, vmsvga3dTextureStateToString(pTextureState[i].name), pTextureState[i].name, pTextureState[i].value));
4808
4809 /* Record the texture state for vm state saving. */
4810 if ( pTextureState[i].stage < RT_ELEMENTS(pContext->state.aTextureStates)
4811 && (unsigned)pTextureState[i].name < RT_ELEMENTS(pContext->state.aTextureStates[0]))
4812 {
4813 pContext->state.aTextureStates[pTextureState[i].stage][pTextureState[i].name] = pTextureState[i];
4814 }
4815
4816 /* Activate the right texture unit for subsequent texture state changes. */
4817 if (pTextureState[i].stage != currentStage || i == 0)
4818 {
4819 /** @todo Is this the appropriate limit for all kinds of textures? It is the
4820 * size of aSidActiveTextures and for binding/unbinding we cannot exceed it. */
4821 if (pTextureState[i].stage < RT_ELEMENTS(pContext->state.aTextureStates))
4822 {
4823 pState->ext.glActiveTexture(GL_TEXTURE0 + pTextureState[i].stage);
4824 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4825 currentStage = pTextureState[i].stage;
4826 }
4827 else
4828 {
4829 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x\n", i, pTextureState[i].stage, pTextureState[i].name));
4830 continue;
4831 }
4832
4833 if (pContext->aSidActiveTextures[currentStage] != SVGA3D_INVALID_ID)
4834 {
4835 rc = vmsvga3dSurfaceFromSid(pState, pContext->aSidActiveTextures[currentStage], &pCurrentTextureSurface);
4836 AssertRCReturn(rc, rc);
4837 }
4838 else
4839 pCurrentTextureSurface = NULL; /* Make sure that no stale pointer is used. */
4840 }
4841
4842 switch (pTextureState[i].name)
4843 {
4844 case SVGA3D_TS_BUMPENVMAT00: /* float */
4845 case SVGA3D_TS_BUMPENVMAT01: /* float */
4846 case SVGA3D_TS_BUMPENVMAT10: /* float */
4847 case SVGA3D_TS_BUMPENVMAT11: /* float */
4848 case SVGA3D_TS_BUMPENVLSCALE: /* float */
4849 case SVGA3D_TS_BUMPENVLOFFSET: /* float */
4850 Log(("vmsvga3dSetTextureState: bump mapping texture options not supported!!\n"));
4851 break;
4852
4853 case SVGA3D_TS_COLOROP: /* SVGA3dTextureCombiner */
4854 case SVGA3D_TS_COLORARG0: /* SVGA3dTextureArgData */
4855 case SVGA3D_TS_COLORARG1: /* SVGA3dTextureArgData */
4856 case SVGA3D_TS_COLORARG2: /* SVGA3dTextureArgData */
4857 case SVGA3D_TS_ALPHAOP: /* SVGA3dTextureCombiner */
4858 case SVGA3D_TS_ALPHAARG0: /* SVGA3dTextureArgData */
4859 case SVGA3D_TS_ALPHAARG1: /* SVGA3dTextureArgData */
4860 case SVGA3D_TS_ALPHAARG2: /* SVGA3dTextureArgData */
4861 /** @todo not used by MesaGL */
4862 Log(("vmsvga3dSetTextureState: colorop/alphaop not yet supported!!\n"));
4863 break;
4864#if 0
4865
4866 case SVGA3D_TS_TEXCOORDINDEX: /* uint32_t */
4867 textureType = D3DTSS_TEXCOORDINDEX;
4868 val = pTextureState[i].value;
4869 break;
4870
4871 case SVGA3D_TS_TEXTURETRANSFORMFLAGS: /* SVGA3dTexTransformFlags */
4872 textureType = D3DTSS_TEXTURETRANSFORMFLAGS;
4873 val = vmsvga3dTextTransformFlags2D3D(pTextureState[i].value);
4874 break;
4875#endif
4876
4877 case SVGA3D_TS_BIND_TEXTURE: /* SVGA3dSurfaceId */
4878 {
4879 uint32_t const sid = pTextureState[i].value;
4880
4881 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture sid=%x replacing sid=%x\n",
4882 currentStage, sid, pContext->aSidActiveTextures[currentStage]));
4883
4884 /* Only if texture actually changed. */ /// @todo needs testing.
4885 if (pContext->aSidActiveTextures[currentStage] != sid)
4886 {
4887 if (pCurrentTextureSurface)
4888 {
4889 /* Unselect the currently associated texture. */
4890 glBindTexture(pCurrentTextureSurface->targetGL, 0);
4891 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4892
4893 if (currentStage < 8)
4894 {
4895 /* Necessary for the fixed pipeline. */
4896 glDisable(pCurrentTextureSurface->targetGL);
4897 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4898 }
4899
4900 pCurrentTextureSurface = NULL;
4901 }
4902
4903 if (sid == SVGA3D_INVALID_ID)
4904 {
4905 Assert(pCurrentTextureSurface == NULL);
4906 }
4907 else
4908 {
4909 PVMSVGA3DSURFACE pSurface;
4910 rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
4911 AssertRCReturn(rc, rc);
4912
4913 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture sid=%x (%d,%d) replacing sid=%x\n",
4914 currentStage, sid, pSurface->pMipmapLevels[0].mipmapSize.width,
4915 pSurface->pMipmapLevels[0].mipmapSize.height, pContext->aSidActiveTextures[currentStage]));
4916
4917 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
4918 {
4919 Log(("CreateTexture (%d,%d) levels=%d\n",
4920 pSurface->pMipmapLevels[0].mipmapSize.width, pSurface->pMipmapLevels[0].mipmapSize.height, pSurface->faces[0].numMipLevels));
4921 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pSurface);
4922 AssertRCReturn(rc, rc);
4923 }
4924
4925 glBindTexture(pSurface->targetGL, pSurface->oglId.texture);
4926 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4927
4928 if (currentStage < 8)
4929 {
4930 /* Necessary for the fixed pipeline. */
4931 glEnable(pSurface->targetGL);
4932 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4933 }
4934
4935 /* Remember the currently active texture. */
4936 pCurrentTextureSurface = pSurface;
4937
4938 /* Recreate the texture state as glBindTexture resets them all (sigh). */
4939 for (uint32_t iStage = 0; iStage < RT_ELEMENTS(pContext->state.aTextureStates); iStage++)
4940 {
4941 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aTextureStates[0]); j++)
4942 {
4943 SVGA3dTextureState *pTextureStateIter = &pContext->state.aTextureStates[iStage][j];
4944
4945 if ( pTextureStateIter->name != SVGA3D_TS_INVALID
4946 && pTextureStateIter->name != SVGA3D_TS_BIND_TEXTURE)
4947 vmsvga3dSetTextureState(pThis, pContext->id, 1, pTextureStateIter);
4948 }
4949 }
4950 }
4951
4952 pContext->aSidActiveTextures[currentStage] = sid;
4953 }
4954
4955 /* Finished; continue with the next one. */
4956 continue;
4957 }
4958
4959 case SVGA3D_TS_ADDRESSW: /* SVGA3dTextureAddress */
4960 textureType = GL_TEXTURE_WRAP_R; /* R = W */
4961 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
4962 break;
4963
4964 case SVGA3D_TS_ADDRESSU: /* SVGA3dTextureAddress */
4965 textureType = GL_TEXTURE_WRAP_S; /* S = U */
4966 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
4967 break;
4968
4969 case SVGA3D_TS_ADDRESSV: /* SVGA3dTextureAddress */
4970 textureType = GL_TEXTURE_WRAP_T; /* T = V */
4971 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
4972 break;
4973
4974 case SVGA3D_TS_MIPFILTER: /* SVGA3dTextureFilter */
4975 case SVGA3D_TS_MINFILTER: /* SVGA3dTextureFilter */
4976 {
4977 uint32_t mipFilter = pContext->state.aTextureStates[currentStage][SVGA3D_TS_MIPFILTER].value;
4978 uint32_t minFilter = pContext->state.aTextureStates[currentStage][SVGA3D_TS_MINFILTER].value;
4979
4980 /* If SVGA3D_TS_MIPFILTER is set to NONE, then use SVGA3D_TS_MIPFILTER, otherwise SVGA3D_TS_MIPFILTER enables mipmap minification. */
4981 textureType = GL_TEXTURE_MIN_FILTER;
4982 if (mipFilter != SVGA3D_TEX_FILTER_NONE)
4983 {
4984 if (minFilter == SVGA3D_TEX_FILTER_NEAREST)
4985 {
4986 if (mipFilter == SVGA3D_TEX_FILTER_LINEAR)
4987 val = GL_NEAREST_MIPMAP_LINEAR;
4988 else
4989 val = GL_NEAREST_MIPMAP_NEAREST;
4990 }
4991 else
4992 {
4993 if (mipFilter == SVGA3D_TEX_FILTER_LINEAR)
4994 val = GL_LINEAR_MIPMAP_LINEAR;
4995 else
4996 val = GL_LINEAR_MIPMAP_NEAREST;
4997 }
4998 }
4999 else
5000 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)minFilter);
5001 break;
5002 }
5003
5004 case SVGA3D_TS_MAGFILTER: /* SVGA3dTextureFilter */
5005 textureType = GL_TEXTURE_MAG_FILTER;
5006 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)pTextureState[i].value);
5007 Assert(val == GL_NEAREST || val == GL_LINEAR);
5008 break;
5009
5010 case SVGA3D_TS_BORDERCOLOR: /* SVGA3dColor */
5011 {
5012 GLfloat color[4]; /* red, green, blue, alpha */
5013 vmsvgaColor2GLFloatArray(pTextureState[i].value, &color[0], &color[1], &color[2], &color[3]);
5014
5015 GLenum targetGL;
5016 if (pCurrentTextureSurface)
5017 targetGL = pCurrentTextureSurface->targetGL;
5018 else
5019 {
5020 /* No texture bound, assume 2D. */
5021 targetGL = GL_TEXTURE_2D;
5022 }
5023
5024 glTexParameterfv(targetGL, GL_TEXTURE_BORDER_COLOR, color); /* Identical; default 0.0 identical too */
5025 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5026 break;
5027 }
5028
5029 case SVGA3D_TS_TEXTURE_LOD_BIAS: /* float */
5030 {
5031 GLenum targetGL;
5032 if (pCurrentTextureSurface)
5033 targetGL = pCurrentTextureSurface->targetGL;
5034 else
5035 {
5036 /* No texture bound, assume 2D. */
5037 targetGL = GL_TEXTURE_2D;
5038 }
5039
5040 glTexParameterf(targetGL, GL_TEXTURE_LOD_BIAS, pTextureState[i].floatValue); /* Identical; default 0.0 identical too */
5041 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5042 break;
5043 }
5044
5045 case SVGA3D_TS_TEXTURE_MIPMAP_LEVEL: /* uint32_t */
5046 textureType = GL_TEXTURE_BASE_LEVEL;
5047 val = pTextureState[i].value;
5048 break;
5049
5050#if 0
5051 case SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL: /* uint32_t */
5052 samplerType = D3DSAMP_MAXANISOTROPY;
5053 val = pTextureState[i].value; /* Identical?? */
5054 break;
5055
5056 case SVGA3D_TS_GAMMA: /* float */
5057 samplerType = D3DSAMP_SRGBTEXTURE;
5058 /* Boolean in D3D */
5059 if (pTextureState[i].floatValue == 1.0f)
5060 val = FALSE;
5061 else
5062 val = TRUE;
5063 break;
5064#endif
5065 /* Internal commands, that don't map directly to the SetTextureStageState API. */
5066 case SVGA3D_TS_TEXCOORDGEN: /* SVGA3dTextureCoordGen */
5067 AssertFailed();
5068 break;
5069
5070 default:
5071 //AssertFailed();
5072 break;
5073 }
5074
5075 if (textureType != ~(GLenum)0)
5076 {
5077 GLenum targetGL;
5078 if (pCurrentTextureSurface)
5079 targetGL = pCurrentTextureSurface->targetGL;
5080 else
5081 {
5082 /* No texture bound, assume 2D. */
5083 targetGL = GL_TEXTURE_2D;
5084 }
5085
5086 glTexParameteri(targetGL, textureType, val);
5087 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5088 }
5089 }
5090
5091 return VINF_SUCCESS;
5092}
5093
5094int vmsvga3dSetMaterial(PVGASTATE pThis, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
5095{
5096 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5097 AssertReturn(pState, VERR_NO_MEMORY);
5098
5099 LogFunc(("cid=%x face %d\n", cid, face));
5100
5101 PVMSVGA3DCONTEXT pContext;
5102 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5103 AssertRCReturn(rc, rc);
5104
5105 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5106
5107 GLenum oglFace;
5108 switch (face)
5109 {
5110 case SVGA3D_FACE_NONE:
5111 case SVGA3D_FACE_FRONT:
5112 oglFace = GL_FRONT;
5113 break;
5114
5115 case SVGA3D_FACE_BACK:
5116 oglFace = GL_BACK;
5117 break;
5118
5119 case SVGA3D_FACE_FRONT_BACK:
5120 oglFace = GL_FRONT_AND_BACK;
5121 break;
5122
5123 default:
5124 AssertFailedReturn(VERR_INVALID_PARAMETER);
5125 }
5126
5127 /* Save for vm state save/restore. */
5128 pContext->state.aMaterial[face].fValid = true;
5129 pContext->state.aMaterial[face].material = *pMaterial;
5130 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_MATERIAL;
5131
5132 glMaterialfv(oglFace, GL_DIFFUSE, pMaterial->diffuse);
5133 glMaterialfv(oglFace, GL_AMBIENT, pMaterial->ambient);
5134 glMaterialfv(oglFace, GL_SPECULAR, pMaterial->specular);
5135 glMaterialfv(oglFace, GL_EMISSION, pMaterial->emissive);
5136 glMaterialfv(oglFace, GL_SHININESS, &pMaterial->shininess);
5137 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5138
5139 return VINF_SUCCESS;
5140}
5141
5142/** @todo Move into separate library as we are using logic from Wine here. */
5143int vmsvga3dSetLightData(PVGASTATE pThis, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
5144{
5145 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5146 AssertReturn(pState, VERR_NO_MEMORY);
5147
5148 LogFunc(("vmsvga3dSetLightData cid=%x index=%d type=%d\n", cid, index, pData->type));
5149
5150 PVMSVGA3DCONTEXT pContext;
5151 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5152 AssertRCReturn(rc, rc);
5153
5154 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5155
5156 /* Store for vm state save/restore */
5157 if (index < SVGA3D_MAX_LIGHTS)
5158 {
5159 pContext->state.aLightData[index].fValidData = true;
5160 pContext->state.aLightData[index].data = *pData;
5161 }
5162 else
5163 AssertFailed();
5164
5165 if ( pData->attenuation0 < 0.0f
5166 || pData->attenuation1 < 0.0f
5167 || pData->attenuation2 < 0.0f)
5168 {
5169 Log(("vmsvga3dSetLightData: invalid negative attenuation values!!\n"));
5170 return VINF_SUCCESS; /* ignore; could crash the GL driver */
5171 }
5172
5173 /* Light settings are affected by the model view in OpenGL, the View transform in direct3d */
5174 glMatrixMode(GL_MODELVIEW);
5175 glPushMatrix();
5176 glLoadMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
5177
5178 glLightfv(GL_LIGHT0 + index, GL_DIFFUSE, pData->diffuse);
5179 glLightfv(GL_LIGHT0 + index, GL_SPECULAR, pData->specular);
5180 glLightfv(GL_LIGHT0 + index, GL_AMBIENT, pData->ambient);
5181
5182 float QuadAttenuation;
5183 if (pData->range * pData->range >= FLT_MIN)
5184 QuadAttenuation = 1.4f / (pData->range * pData->range);
5185 else
5186 QuadAttenuation = 0.0f;
5187
5188 switch (pData->type)
5189 {
5190 case SVGA3D_LIGHTTYPE_POINT:
5191 {
5192 GLfloat position[4];
5193
5194 position[0] = pData->position[0];
5195 position[1] = pData->position[1];
5196 position[2] = pData->position[2];
5197 position[3] = 1.0f;
5198
5199 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
5200 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5201
5202 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
5203 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5204
5205 /* Attenuation - Are these right? guessing... */
5206 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
5207 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5208
5209 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
5210 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5211
5212 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
5213 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5214
5215 /** @todo range */
5216 break;
5217 }
5218
5219 case SVGA3D_LIGHTTYPE_SPOT1:
5220 {
5221 GLfloat exponent;
5222 GLfloat position[4];
5223 const GLfloat pi = 4.0f * atanf(1.0f);
5224
5225 position[0] = pData->position[0];
5226 position[1] = pData->position[1];
5227 position[2] = pData->position[2];
5228 position[3] = 1.0f;
5229
5230 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
5231 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5232
5233 position[0] = pData->direction[0];
5234 position[1] = pData->direction[1];
5235 position[2] = pData->direction[2];
5236 position[3] = 1.0f;
5237
5238 glLightfv(GL_LIGHT0 + index, GL_SPOT_DIRECTION, position);
5239 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5240
5241 /*
5242 * opengl-ish and d3d-ish spot lights use too different models for the
5243 * light "intensity" as a function of the angle towards the main light direction,
5244 * so we only can approximate very roughly.
5245 * however spot lights are rather rarely used in games (if ever used at all).
5246 * furthermore if still used, probably nobody pays attention to such details.
5247 */
5248 if (pData->falloff == 0)
5249 {
5250 /* Falloff = 0 is easy, because d3d's and opengl's spot light equations have the
5251 * falloff resp. exponent parameter as an exponent, so the spot light lighting
5252 * will always be 1.0 for both of them, and we don't have to care for the
5253 * rest of the rather complex calculation
5254 */
5255 exponent = 0.0f;
5256 }
5257 else
5258 {
5259 float rho = pData->theta + (pData->phi - pData->theta) / (2 * pData->falloff);
5260 if (rho < 0.0001f)
5261 rho = 0.0001f;
5262 exponent = -0.3f/log(cos(rho/2));
5263 }
5264 if (exponent > 128.0f)
5265 exponent = 128.0f;
5266
5267 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, exponent);
5268 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5269
5270 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, pData->phi * 90.0 / pi);
5271 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5272
5273 /* Attenuation - Are these right? guessing... */
5274 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
5275 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5276
5277 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
5278 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5279
5280 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
5281 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5282
5283 /** @todo range */
5284 break;
5285 }
5286
5287 case SVGA3D_LIGHTTYPE_DIRECTIONAL:
5288 {
5289 GLfloat position[4];
5290
5291 position[0] = -pData->direction[0];
5292 position[1] = -pData->direction[1];
5293 position[2] = -pData->direction[2];
5294 position[3] = 0.0f;
5295
5296 glLightfv(GL_LIGHT0 + index, GL_POSITION, position); /* Note gl uses w position of 0 for direction! */
5297 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5298
5299 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
5300 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5301
5302 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, 0.0f);
5303 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5304 break;
5305 }
5306
5307 case SVGA3D_LIGHTTYPE_SPOT2:
5308 default:
5309 Log(("Unsupported light type!!\n"));
5310 rc = VERR_INVALID_PARAMETER;
5311 break;
5312 }
5313
5314 /* Restore the modelview matrix */
5315 glPopMatrix();
5316
5317 return rc;
5318}
5319
5320int vmsvga3dSetLightEnabled(PVGASTATE pThis, uint32_t cid, uint32_t index, uint32_t enabled)
5321{
5322 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5323 AssertReturn(pState, VERR_NO_MEMORY);
5324
5325 LogFunc(("cid=%x %d -> %d\n", cid, index, enabled));
5326
5327 PVMSVGA3DCONTEXT pContext;
5328 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5329 AssertRCReturn(rc, rc);
5330
5331 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5332
5333 /* Store for vm state save/restore */
5334 if (index < SVGA3D_MAX_LIGHTS)
5335 pContext->state.aLightData[index].fEnabled = !!enabled;
5336 else
5337 AssertFailed();
5338
5339 if (enabled)
5340 {
5341 if (index < SVGA3D_MAX_LIGHTS)
5342 {
5343 /* Load the default settings if none have been set yet. */
5344 if (!pContext->state.aLightData[index].fValidData)
5345 vmsvga3dSetLightData(pThis, cid, index, (SVGA3dLightData *)&vmsvga3d_default_light);
5346 }
5347 glEnable(GL_LIGHT0 + index);
5348 }
5349 else
5350 glDisable(GL_LIGHT0 + index);
5351
5352 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5353 return VINF_SUCCESS;
5354}
5355
5356int vmsvga3dSetViewPort(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
5357{
5358 PVMSVGA3DCONTEXT pContext;
5359 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5360 AssertReturn(pState, VERR_NO_MEMORY);
5361
5362 Log(("vmsvga3dSetViewPort cid=%x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
5363
5364 if ( cid >= pState->cContexts
5365 || pState->papContexts[cid]->id != cid)
5366 {
5367 Log(("vmsvga3dSetViewPort invalid context id!\n"));
5368 return VERR_INVALID_PARAMETER;
5369 }
5370 pContext = pState->papContexts[cid];
5371 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5372
5373 /* Save for vm state save/restore. */
5374 pContext->state.RectViewPort = *pRect;
5375 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VIEWPORT;
5376
5377 /** @todo y-inversion for partial viewport coordinates? */
5378 glViewport(pRect->x, pRect->y, pRect->w, pRect->h);
5379 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5380
5381 /* Reset the projection matrix as that relies on the viewport setting. */
5382 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].fValid == true)
5383 {
5384 vmsvga3dSetTransform(pThis, cid, SVGA3D_TRANSFORM_PROJECTION, pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].matrix);
5385 }
5386 else
5387 {
5388 float matrix[16];
5389
5390 /* identity matrix if no matrix set. */
5391 memset(matrix, 0, sizeof(matrix));
5392 matrix[0] = 1.0;
5393 matrix[5] = 1.0;
5394 matrix[10] = 1.0;
5395 matrix[15] = 1.0;
5396 vmsvga3dSetTransform(pThis, cid, SVGA3D_TRANSFORM_PROJECTION, matrix);
5397 }
5398
5399 return VINF_SUCCESS;
5400}
5401
5402int vmsvga3dSetClipPlane(PVGASTATE pThis, uint32_t cid, uint32_t index, float plane[4])
5403{
5404 PVMSVGA3DCONTEXT pContext;
5405 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5406 AssertReturn(pState, VERR_NO_MEMORY);
5407 double oglPlane[4];
5408
5409 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)));
5410 AssertReturn(index < SVGA3D_CLIPPLANE_MAX, VERR_INVALID_PARAMETER);
5411
5412 if ( cid >= pState->cContexts
5413 || pState->papContexts[cid]->id != cid)
5414 {
5415 Log(("vmsvga3dSetClipPlane invalid context id!\n"));
5416 return VERR_INVALID_PARAMETER;
5417 }
5418 pContext = pState->papContexts[cid];
5419 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5420
5421 /* Store for vm state save/restore. */
5422 pContext->state.aClipPlane[index].fValid = true;
5423 memcpy(pContext->state.aClipPlane[index].plane, plane, sizeof(pContext->state.aClipPlane[index].plane));
5424
5425 /** @todo clip plane affected by model view in OpenGL & view in D3D + vertex shader -> not transformed (see Wine; state.c clipplane) */
5426 oglPlane[0] = (double)plane[0];
5427 oglPlane[1] = (double)plane[1];
5428 oglPlane[2] = (double)plane[2];
5429 oglPlane[3] = (double)plane[3];
5430
5431 glClipPlane(GL_CLIP_PLANE0 + index, oglPlane);
5432 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5433
5434 return VINF_SUCCESS;
5435}
5436
5437int vmsvga3dSetScissorRect(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
5438{
5439 PVMSVGA3DCONTEXT pContext;
5440 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5441 AssertReturn(pState, VERR_NO_MEMORY);
5442
5443 Log(("vmsvga3dSetScissorRect cid=%x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
5444
5445 if ( cid >= pState->cContexts
5446 || pState->papContexts[cid]->id != cid)
5447 {
5448 Log(("vmsvga3dSetScissorRect invalid context id!\n"));
5449 return VERR_INVALID_PARAMETER;
5450 }
5451 pContext = pState->papContexts[cid];
5452 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5453
5454 /* Store for vm state save/restore. */
5455 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_SCISSORRECT;
5456 pContext->state.RectScissor = *pRect;
5457
5458 glScissor(pRect->x, pRect->y, pRect->w, pRect->h);
5459 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5460
5461 return VINF_SUCCESS;
5462}
5463
5464static void vmsvgaColor2GLFloatArray(uint32_t color, GLfloat *pRed, GLfloat *pGreen, GLfloat *pBlue, GLfloat *pAlpha)
5465{
5466 /* Convert byte color components to float (0-1.0) */
5467 *pAlpha = (GLfloat)(color >> 24) / 255.0;
5468 *pRed = (GLfloat)((color >> 16) & 0xff) / 255.0;
5469 *pGreen = (GLfloat)((color >> 8) & 0xff) / 255.0;
5470 *pBlue = (GLfloat)(color & 0xff) / 255.0;
5471}
5472
5473int vmsvga3dCommandClear(PVGASTATE pThis, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth, uint32_t stencil,
5474 uint32_t cRects, SVGA3dRect *pRect)
5475{
5476 GLbitfield mask = 0;
5477 PVMSVGA3DCONTEXT pContext;
5478 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5479 AssertReturn(pState, VERR_NO_MEMORY);
5480 GLboolean fDepthWriteEnabled = GL_FALSE;
5481
5482 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));
5483
5484 if ( cid >= pState->cContexts
5485 || pState->papContexts[cid]->id != cid)
5486 {
5487 Log(("vmsvga3dCommandClear invalid context id!\n"));
5488 return VERR_INVALID_PARAMETER;
5489 }
5490 pContext = pState->papContexts[cid];
5491 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5492
5493 if (clearFlag & SVGA3D_CLEAR_COLOR)
5494 {
5495 GLfloat red, green, blue, alpha;
5496
5497 vmsvgaColor2GLFloatArray(color, &red, &green, &blue, &alpha);
5498
5499 /* Set the color clear value. */
5500 glClearColor(red, green, blue, alpha);
5501
5502 mask |= GL_COLOR_BUFFER_BIT;
5503 }
5504 if (clearFlag & SVGA3D_CLEAR_STENCIL)
5505 {
5506 /** @todo possibly the same problem as with glDepthMask */
5507 glClearStencil(stencil);
5508 mask |= GL_STENCIL_BUFFER_BIT;
5509 }
5510 if (clearFlag & SVGA3D_CLEAR_DEPTH)
5511 {
5512 glClearDepth((GLdouble)depth);
5513 mask |= GL_DEPTH_BUFFER_BIT;
5514
5515 /* glClear will not clear the depth buffer if writing is disabled. */
5516 glGetBooleanv(GL_DEPTH_WRITEMASK, &fDepthWriteEnabled);
5517 if (fDepthWriteEnabled == GL_FALSE)
5518 glDepthMask(GL_TRUE);
5519 }
5520
5521 if (cRects)
5522 {
5523 /* Save the current scissor test bit and scissor box. */
5524 glPushAttrib(GL_SCISSOR_BIT);
5525 glEnable(GL_SCISSOR_TEST);
5526 for (unsigned i=0; i < cRects; i++)
5527 {
5528 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));
5529 glScissor(pRect[i].x, pRect[i].y, pRect[i].w, pRect[i].h);
5530 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5531 glClear(mask);
5532 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5533 }
5534 /* Restore the old scissor test bit and box */
5535 glPopAttrib();
5536 }
5537 else
5538 {
5539 glClear(mask);
5540 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5541 }
5542
5543 /* Restore depth write state. */
5544 if ( (clearFlag & SVGA3D_CLEAR_DEPTH)
5545 && fDepthWriteEnabled == GL_FALSE)
5546 glDepthMask(GL_FALSE);
5547
5548 return VINF_SUCCESS;
5549}
5550
5551/* Convert VMWare vertex declaration to its OpenGL equivalent. */
5552int vmsvga3dVertexDecl2OGL(SVGA3dVertexArrayIdentity &identity, GLint &size, GLenum &type, GLboolean &normalized)
5553{
5554 normalized = GL_FALSE;
5555 switch (identity.type)
5556 {
5557 case SVGA3D_DECLTYPE_FLOAT1:
5558 size = 1;
5559 type = GL_FLOAT;
5560 break;
5561 case SVGA3D_DECLTYPE_FLOAT2:
5562 size = 2;
5563 type = GL_FLOAT;
5564 break;
5565 case SVGA3D_DECLTYPE_FLOAT3:
5566 size = 3;
5567 type = GL_FLOAT;
5568 break;
5569 case SVGA3D_DECLTYPE_FLOAT4:
5570 size = 4;
5571 type = GL_FLOAT;
5572 break;
5573
5574 case SVGA3D_DECLTYPE_D3DCOLOR:
5575 size = GL_BGRA; /* @note requires GL_ARB_vertex_array_bgra */
5576 type = GL_UNSIGNED_BYTE;
5577 normalized = GL_TRUE; /* glVertexAttribPointer fails otherwise */
5578 break;
5579
5580 case SVGA3D_DECLTYPE_UBYTE4N:
5581 normalized = GL_TRUE;
5582 RT_FALL_THRU();
5583 case SVGA3D_DECLTYPE_UBYTE4:
5584 size = 4;
5585 type = GL_UNSIGNED_BYTE;
5586 break;
5587
5588 case SVGA3D_DECLTYPE_SHORT2N:
5589 normalized = GL_TRUE;
5590 RT_FALL_THRU();
5591 case SVGA3D_DECLTYPE_SHORT2:
5592 size = 2;
5593 type = GL_SHORT;
5594 break;
5595
5596 case SVGA3D_DECLTYPE_SHORT4N:
5597 normalized = GL_TRUE;
5598 RT_FALL_THRU();
5599 case SVGA3D_DECLTYPE_SHORT4:
5600 size = 4;
5601 type = GL_SHORT;
5602 break;
5603
5604 case SVGA3D_DECLTYPE_USHORT4N:
5605 normalized = GL_TRUE;
5606 size = 4;
5607 type = GL_UNSIGNED_SHORT;
5608 break;
5609
5610 case SVGA3D_DECLTYPE_USHORT2N:
5611 normalized = GL_TRUE;
5612 size = 2;
5613 type = GL_UNSIGNED_SHORT;
5614 break;
5615
5616 case SVGA3D_DECLTYPE_UDEC3:
5617 size = 3;
5618 type = GL_UNSIGNED_INT_2_10_10_10_REV; /** @todo correct? */
5619 break;
5620
5621 case SVGA3D_DECLTYPE_DEC3N:
5622 normalized = true;
5623 size = 3;
5624 type = GL_INT_2_10_10_10_REV; /** @todo correct? */
5625 break;
5626
5627 case SVGA3D_DECLTYPE_FLOAT16_2:
5628 size = 2;
5629 type = GL_HALF_FLOAT;
5630 break;
5631 case SVGA3D_DECLTYPE_FLOAT16_4:
5632 size = 4;
5633 type = GL_HALF_FLOAT;
5634 break;
5635 default:
5636 AssertFailedReturn(VERR_INVALID_PARAMETER);
5637 }
5638
5639 //pVertexElement->Method = identity.method;
5640 //pVertexElement->Usage = identity.usage;
5641
5642 return VINF_SUCCESS;
5643}
5644
5645/* Convert VMWare primitive type to its OpenGL equivalent. */
5646/* Calculate the vertex count based on the primitive type and nr of primitives. */
5647int vmsvga3dPrimitiveType2OGL(SVGA3dPrimitiveType PrimitiveType, GLenum *pMode, uint32_t cPrimitiveCount, uint32_t *pcVertices)
5648{
5649 switch (PrimitiveType)
5650 {
5651 case SVGA3D_PRIMITIVE_TRIANGLELIST:
5652 *pMode = GL_TRIANGLES;
5653 *pcVertices = cPrimitiveCount * 3;
5654 break;
5655 case SVGA3D_PRIMITIVE_POINTLIST:
5656 *pMode = GL_POINTS;
5657 *pcVertices = cPrimitiveCount;
5658 break;
5659 case SVGA3D_PRIMITIVE_LINELIST:
5660 *pMode = GL_LINES;
5661 *pcVertices = cPrimitiveCount * 2;
5662 break;
5663 case SVGA3D_PRIMITIVE_LINESTRIP:
5664 *pMode = GL_LINE_STRIP;
5665 *pcVertices = cPrimitiveCount + 1;
5666 break;
5667 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
5668 *pMode = GL_TRIANGLE_STRIP;
5669 *pcVertices = cPrimitiveCount + 2;
5670 break;
5671 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
5672 *pMode = GL_TRIANGLE_FAN;
5673 *pcVertices = cPrimitiveCount + 2;
5674 break;
5675 default:
5676 return VERR_INVALID_PARAMETER;
5677 }
5678 return VINF_SUCCESS;
5679}
5680
5681int vmsvga3dResetTransformMatrices(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext)
5682{
5683 int rc;
5684
5685 /* Reset the view matrix (also takes the world matrix into account). */
5686 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].fValid == true)
5687 {
5688 rc = vmsvga3dSetTransform(pThis, pContext->id, SVGA3D_TRANSFORM_VIEW, pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
5689 }
5690 else
5691 {
5692 float matrix[16];
5693
5694 /* identity matrix if no matrix set. */
5695 memset(matrix, 0, sizeof(matrix));
5696 matrix[0] = 1.0;
5697 matrix[5] = 1.0;
5698 matrix[10] = 1.0;
5699 matrix[15] = 1.0;
5700 rc = vmsvga3dSetTransform(pThis, pContext->id, SVGA3D_TRANSFORM_VIEW, matrix);
5701 }
5702
5703 /* Reset the projection matrix. */
5704 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].fValid == true)
5705 {
5706 rc = vmsvga3dSetTransform(pThis, pContext->id, SVGA3D_TRANSFORM_PROJECTION, pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].matrix);
5707 }
5708 else
5709 {
5710 float matrix[16];
5711
5712 /* identity matrix if no matrix set. */
5713 memset(matrix, 0, sizeof(matrix));
5714 matrix[0] = 1.0;
5715 matrix[5] = 1.0;
5716 matrix[10] = 1.0;
5717 matrix[15] = 1.0;
5718 rc = vmsvga3dSetTransform(pThis, pContext->id, SVGA3D_TRANSFORM_PROJECTION, matrix);
5719 }
5720 AssertRC(rc);
5721 return rc;
5722}
5723
5724int vmsvga3dDrawPrimitivesProcessVertexDecls(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext,
5725 uint32_t iVertexDeclBase, uint32_t numVertexDecls,
5726 SVGA3dVertexDecl *pVertexDecl, SVGA3dVertexDivisor const *paVertexDivisors)
5727{
5728 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5729 unsigned sidVertex = pVertexDecl[0].array.surfaceId;
5730 PVMSVGA3DSURFACE pVertexSurface;
5731
5732 AssertReturn(sidVertex < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
5733 AssertReturn(sidVertex < pState->cSurfaces && pState->papSurfaces[sidVertex]->id == sidVertex, VERR_INVALID_PARAMETER);
5734
5735 pVertexSurface = pState->papSurfaces[sidVertex];
5736 Log(("vmsvga3dDrawPrimitives: vertex surface %x\n", sidVertex));
5737
5738 /* Create and/or bind the vertex buffer. */
5739 if (pVertexSurface->oglId.buffer == OPENGL_INVALID_ID)
5740 {
5741 Log(("vmsvga3dDrawPrimitives: create vertex buffer fDirty=%d size=%x bytes\n", pVertexSurface->fDirty, pVertexSurface->pMipmapLevels[0].cbSurface));
5742 PVMSVGA3DCONTEXT pSavedCtx = pContext;
5743 pContext = &pState->SharedCtx;
5744 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5745
5746 pState->ext.glGenBuffers(1, &pVertexSurface->oglId.buffer);
5747 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5748 pVertexSurface->enmOGLResType = VMSVGA3D_OGLRESTYPE_BUFFER;
5749
5750 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
5751 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5752
5753 Assert(pVertexSurface->fDirty);
5754 /** @todo rethink usage dynamic/static */
5755 pState->ext.glBufferData(GL_ARRAY_BUFFER, pVertexSurface->pMipmapLevels[0].cbSurface, pVertexSurface->pMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
5756 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5757
5758 pVertexSurface->pMipmapLevels[0].fDirty = false;
5759 pVertexSurface->fDirty = false;
5760
5761 pVertexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
5762
5763 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, OPENGL_INVALID_ID);
5764 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5765
5766 pContext = pSavedCtx;
5767 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5768 }
5769
5770 Assert(pVertexSurface->fDirty == false);
5771 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
5772 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5773
5774 /* Setup the vertex declarations. */
5775 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
5776 {
5777 GLint size;
5778 GLenum type;
5779 GLboolean normalized;
5780 GLuint index = iVertexDeclBase + iVertex;
5781
5782 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));
5783
5784 int rc = vmsvga3dVertexDecl2OGL(pVertexDecl[iVertex].identity, size, type, normalized);
5785 AssertRCReturn(rc, rc);
5786
5787 if (pContext->state.shidVertex != SVGA_ID_INVALID)
5788 {
5789 /* Use numbered vertex arrays when shaders are active. */
5790 pState->ext.glEnableVertexAttribArray(index);
5791 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5792 pState->ext.glVertexAttribPointer(index, size, type, normalized, pVertexDecl[iVertex].array.stride,
5793 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5794 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5795
5796 GLuint const divisor = paVertexDivisors && paVertexDivisors[index].s.instanceData ? 1 : 0;
5797 pState->ext.glVertexAttribDivisor(index, divisor);
5798 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5799
5800 /** @todo case SVGA3D_DECLUSAGE_COLOR: color component order not identical!! test GL_BGRA!! */
5801 }
5802 else
5803 {
5804 /* Use the predefined selection of vertex streams for the fixed pipeline. */
5805 switch (pVertexDecl[iVertex].identity.usage)
5806 {
5807 case SVGA3D_DECLUSAGE_POSITIONT:
5808 case SVGA3D_DECLUSAGE_POSITION:
5809 {
5810 glEnableClientState(GL_VERTEX_ARRAY);
5811 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5812 glVertexPointer(size, type, pVertexDecl[iVertex].array.stride,
5813 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5814 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5815 break;
5816 }
5817 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
5818 AssertFailed();
5819 break;
5820 case SVGA3D_DECLUSAGE_BLENDINDICES:
5821 AssertFailed();
5822 break;
5823 case SVGA3D_DECLUSAGE_NORMAL:
5824 glEnableClientState(GL_NORMAL_ARRAY);
5825 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5826 glNormalPointer(type, pVertexDecl[iVertex].array.stride,
5827 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5828 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5829 break;
5830 case SVGA3D_DECLUSAGE_PSIZE:
5831 AssertFailed();
5832 break;
5833 case SVGA3D_DECLUSAGE_TEXCOORD:
5834 /* Specify the affected texture unit. */
5835#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
5836 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
5837#else
5838 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
5839#endif
5840 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
5841 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5842 glTexCoordPointer(size, type, pVertexDecl[iVertex].array.stride,
5843 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5844 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5845 break;
5846 case SVGA3D_DECLUSAGE_TANGENT:
5847 AssertFailed();
5848 break;
5849 case SVGA3D_DECLUSAGE_BINORMAL:
5850 AssertFailed();
5851 break;
5852 case SVGA3D_DECLUSAGE_TESSFACTOR:
5853 AssertFailed();
5854 break;
5855 case SVGA3D_DECLUSAGE_COLOR: /** @todo color component order not identical!! test GL_BGRA!! */
5856 glEnableClientState(GL_COLOR_ARRAY);
5857 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5858 glColorPointer(size, type, pVertexDecl[iVertex].array.stride,
5859 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5860 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5861 break;
5862 case SVGA3D_DECLUSAGE_FOG:
5863 glEnableClientState(GL_FOG_COORD_ARRAY);
5864 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5865 pState->ext.glFogCoordPointer(type, pVertexDecl[iVertex].array.stride,
5866 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5867 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5868 break;
5869 case SVGA3D_DECLUSAGE_DEPTH:
5870 AssertFailed();
5871 break;
5872 case SVGA3D_DECLUSAGE_SAMPLE:
5873 AssertFailed();
5874 break;
5875 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
5876 }
5877 }
5878
5879#ifdef LOG_ENABLED
5880 if (pVertexDecl[iVertex].array.stride == 0)
5881 Log(("vmsvga3dDrawPrimitives: stride == 0! Can be valid\n"));
5882#endif
5883 }
5884
5885 return VINF_SUCCESS;
5886}
5887
5888int vmsvga3dDrawPrimitivesCleanupVertexDecls(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext, uint32_t iVertexDeclBase, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl)
5889{
5890 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5891
5892 /* Clean up the vertex declarations. */
5893 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
5894 {
5895 if (pVertexDecl[iVertex].identity.usage == SVGA3D_DECLUSAGE_POSITIONT)
5896 {
5897 /* Reset the transformation matrices in case of a switch back from pretransformed mode. */
5898 Log(("vmsvga3dDrawPrimitivesCleanupVertexDecls: reset world and projection matrices after transformation reset (pre-transformed -> transformed)\n"));
5899 vmsvga3dResetTransformMatrices(pThis, pContext);
5900 }
5901
5902 if (pContext->state.shidVertex != SVGA_ID_INVALID)
5903 {
5904 /* Use numbered vertex arrays when shaders are active. */
5905 pState->ext.glDisableVertexAttribArray(iVertexDeclBase + iVertex);
5906 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5907 }
5908 else
5909 {
5910 /* Use the predefined selection of vertex streams for the fixed pipeline. */
5911 switch (pVertexDecl[iVertex].identity.usage)
5912 {
5913 case SVGA3D_DECLUSAGE_POSITION:
5914 case SVGA3D_DECLUSAGE_POSITIONT:
5915 glDisableClientState(GL_VERTEX_ARRAY);
5916 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5917 break;
5918 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
5919 break;
5920 case SVGA3D_DECLUSAGE_BLENDINDICES:
5921 break;
5922 case SVGA3D_DECLUSAGE_NORMAL:
5923 glDisableClientState(GL_NORMAL_ARRAY);
5924 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5925 break;
5926 case SVGA3D_DECLUSAGE_PSIZE:
5927 break;
5928 case SVGA3D_DECLUSAGE_TEXCOORD:
5929 /* Specify the affected texture unit. */
5930#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
5931 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
5932#else
5933 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
5934#endif
5935 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
5936 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5937 break;
5938 case SVGA3D_DECLUSAGE_TANGENT:
5939 break;
5940 case SVGA3D_DECLUSAGE_BINORMAL:
5941 break;
5942 case SVGA3D_DECLUSAGE_TESSFACTOR:
5943 break;
5944 case SVGA3D_DECLUSAGE_COLOR: /** @todo color component order not identical!! */
5945 glDisableClientState(GL_COLOR_ARRAY);
5946 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5947 break;
5948 case SVGA3D_DECLUSAGE_FOG:
5949 glDisableClientState(GL_FOG_COORD_ARRAY);
5950 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5951 break;
5952 case SVGA3D_DECLUSAGE_DEPTH:
5953 break;
5954 case SVGA3D_DECLUSAGE_SAMPLE:
5955 break;
5956 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
5957 }
5958 }
5959 }
5960 /* Unbind the vertex buffer after usage. */
5961 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0);
5962 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5963 return VINF_SUCCESS;
5964}
5965
5966int vmsvga3dDrawPrimitives(PVGASTATE pThis, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl,
5967 uint32_t numRanges, SVGA3dPrimitiveRange *pRange, uint32_t cVertexDivisor,
5968 SVGA3dVertexDivisor *pVertexDivisor)
5969{
5970 RT_NOREF(pVertexDivisor);
5971 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5972 AssertReturn(pState, VERR_INTERNAL_ERROR);
5973 PVMSVGA3DCONTEXT pContext;
5974 int rc = VERR_NOT_IMPLEMENTED;
5975 uint32_t iCurrentVertex;
5976
5977 Log(("vmsvga3dDrawPrimitives cid=%x numVertexDecls=%d numRanges=%d, cVertexDivisor=%d\n", cid, numVertexDecls, numRanges, cVertexDivisor));
5978
5979 /* Caller already check these, but it cannot hurt to check again... */
5980 AssertReturn(numVertexDecls && numVertexDecls <= SVGA3D_MAX_VERTEX_ARRAYS, VERR_INVALID_PARAMETER);
5981 AssertReturn(numRanges && numRanges <= SVGA3D_MAX_DRAW_PRIMITIVE_RANGES, VERR_INVALID_PARAMETER);
5982 AssertReturn(!cVertexDivisor || cVertexDivisor == numVertexDecls, VERR_INVALID_PARAMETER);
5983
5984 if (!cVertexDivisor)
5985 pVertexDivisor = NULL; /* Be sure. */
5986
5987 if ( cid >= pState->cContexts
5988 || pState->papContexts[cid]->id != cid)
5989 {
5990 Log(("vmsvga3dDrawPrimitives invalid context id!\n"));
5991 return VERR_INVALID_PARAMETER;
5992 }
5993 pContext = pState->papContexts[cid];
5994 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5995
5996 /* Check for pretransformed vertex declarations. */
5997 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
5998 {
5999 switch (pVertexDecl[iVertex].identity.usage)
6000 {
6001 case SVGA3D_DECLUSAGE_POSITIONT:
6002 Log(("ShaderSetPositionTransformed: (%d,%d)\n", pContext->state.RectViewPort.w, pContext->state.RectViewPort.h));
6003 RT_FALL_THRU();
6004 case SVGA3D_DECLUSAGE_POSITION:
6005 ShaderSetPositionTransformed(pContext->pShaderContext, pContext->state.RectViewPort.w,
6006 pContext->state.RectViewPort.h,
6007 pVertexDecl[iVertex].identity.usage == SVGA3D_DECLUSAGE_POSITIONT);
6008 break;
6009 default: /* Shut up MSC. */ break;
6010 }
6011 }
6012
6013 /* Try to figure out if instancing is used.
6014 * Support simple instancing case with one set of indexed data and one set per-instance data.
6015 */
6016 uint32_t cInstances = 0;
6017 for (uint32_t iVertexDivisor = 0; iVertexDivisor < cVertexDivisor; ++iVertexDivisor)
6018 {
6019 if (pVertexDivisor[iVertexDivisor].s.indexedData)
6020 {
6021 if (cInstances == 0)
6022 cInstances = pVertexDivisor[iVertexDivisor].s.count;
6023 else
6024 Assert(cInstances == pVertexDivisor[iVertexDivisor].s.count);
6025 }
6026 else if (pVertexDivisor[iVertexDivisor].s.instanceData)
6027 {
6028 Assert(pVertexDivisor[iVertexDivisor].s.count == 1);
6029 }
6030 }
6031
6032 /* Flush any shader changes; after (!) checking the vertex declarations to deal with pre-transformed vertices. */
6033 if (pContext->pShaderContext)
6034 {
6035 uint32_t rtHeight = 0;
6036
6037 if (pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] != SVGA_ID_INVALID)
6038 {
6039 PVMSVGA3DSURFACE pRenderTarget = pState->papSurfaces[pContext->state.aRenderTargets[SVGA3D_RT_COLOR0]];
6040 rtHeight = pRenderTarget->pMipmapLevels[0].mipmapSize.height;
6041 }
6042
6043 ShaderUpdateState(pContext->pShaderContext, rtHeight);
6044 }
6045
6046 /* Process all vertex declarations. Each vertex buffer is represented by one stream. */
6047 iCurrentVertex = 0;
6048 while (iCurrentVertex < numVertexDecls)
6049 {
6050 uint32_t sidVertex = SVGA_ID_INVALID;
6051 uint32_t iVertex;
6052
6053 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
6054 {
6055 if ( sidVertex != SVGA_ID_INVALID
6056 && pVertexDecl[iVertex].array.surfaceId != sidVertex
6057 )
6058 break;
6059 sidVertex = pVertexDecl[iVertex].array.surfaceId;
6060 }
6061
6062 rc = vmsvga3dDrawPrimitivesProcessVertexDecls(pThis, pContext, iCurrentVertex, iVertex - iCurrentVertex,
6063 &pVertexDecl[iCurrentVertex], pVertexDivisor);
6064 AssertRCReturn(rc, rc);
6065
6066 iCurrentVertex = iVertex;
6067 }
6068
6069 /* Now draw the primitives. */
6070 for (unsigned iPrimitive = 0; iPrimitive < numRanges; iPrimitive++)
6071 {
6072 GLenum modeDraw;
6073 unsigned sidIndex = pRange[iPrimitive].indexArray.surfaceId;
6074 PVMSVGA3DSURFACE pIndexSurface = NULL;
6075 unsigned cVertices;
6076
6077 Log(("Primitive %d: type %s\n", iPrimitive, vmsvga3dPrimitiveType2String(pRange[iPrimitive].primType)));
6078 rc = vmsvga3dPrimitiveType2OGL(pRange[iPrimitive].primType, &modeDraw, pRange[iPrimitive].primitiveCount, &cVertices);
6079 if (RT_FAILURE(rc))
6080 {
6081 AssertRC(rc);
6082 goto internal_error;
6083 }
6084
6085 if (sidIndex != SVGA3D_INVALID_ID)
6086 {
6087 AssertMsg(pRange[iPrimitive].indexWidth == sizeof(uint32_t) || pRange[iPrimitive].indexWidth == sizeof(uint16_t), ("Unsupported primitive width %d\n", pRange[iPrimitive].indexWidth));
6088
6089 if ( sidIndex >= SVGA3D_MAX_SURFACE_IDS
6090 || sidIndex >= pState->cSurfaces
6091 || pState->papSurfaces[sidIndex]->id != sidIndex)
6092 {
6093 Assert(sidIndex < SVGA3D_MAX_SURFACE_IDS);
6094 Assert(sidIndex < pState->cSurfaces && pState->papSurfaces[sidIndex]->id == sidIndex);
6095 rc = VERR_INVALID_PARAMETER;
6096 goto internal_error;
6097 }
6098 pIndexSurface = pState->papSurfaces[sidIndex];
6099 Log(("vmsvga3dDrawPrimitives: index surface %x\n", sidIndex));
6100
6101 if (pIndexSurface->oglId.buffer == OPENGL_INVALID_ID)
6102 {
6103 Log(("vmsvga3dDrawPrimitives: create index buffer fDirty=%d size=%x bytes\n", pIndexSurface->fDirty, pIndexSurface->pMipmapLevels[0].cbSurface));
6104 pContext = &pState->SharedCtx;
6105 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6106
6107 pState->ext.glGenBuffers(1, &pIndexSurface->oglId.buffer);
6108 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6109 pIndexSurface->enmOGLResType = VMSVGA3D_OGLRESTYPE_BUFFER;
6110
6111 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
6112 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6113
6114 Assert(pIndexSurface->fDirty);
6115
6116 /** @todo rethink usage dynamic/static */
6117 pState->ext.glBufferData(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->pMipmapLevels[0].cbSurface, pIndexSurface->pMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
6118 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6119
6120 pIndexSurface->pMipmapLevels[0].fDirty = false;
6121 pIndexSurface->fDirty = false;
6122
6123 pIndexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
6124
6125 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, OPENGL_INVALID_ID);
6126 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6127
6128 pContext = pState->papContexts[cid];
6129 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6130 }
6131 Assert(pIndexSurface->fDirty == false);
6132
6133 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
6134 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6135 }
6136
6137 if (!pIndexSurface)
6138 {
6139 /* Render without an index buffer */
6140 Log(("DrawPrimitive %x cPrimitives=%d cVertices=%d index index bias=%d\n", modeDraw, pRange[iPrimitive].primitiveCount, cVertices, pRange[iPrimitive].indexBias));
6141 if (cInstances == 0)
6142 {
6143 glDrawArrays(modeDraw, pRange[iPrimitive].indexBias, cVertices);
6144 }
6145 else
6146 {
6147 pState->ext.glDrawArraysInstanced(modeDraw, pRange[iPrimitive].indexBias, cVertices, cInstances);
6148 }
6149 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6150 }
6151 else
6152 {
6153 Assert(pRange[iPrimitive].indexWidth == pRange[iPrimitive].indexArray.stride);
6154
6155 GLenum indexType;
6156 switch (pRange[iPrimitive].indexWidth)
6157 {
6158 case 1: indexType = GL_UNSIGNED_BYTE; break;
6159 case 2: indexType = GL_UNSIGNED_SHORT; break;
6160 default: AssertMsgFailed(("indexWidth %d\n", pRange[iPrimitive].indexWidth));
6161 RT_FALL_THROUGH();
6162 case 4: indexType = GL_UNSIGNED_INT; break;
6163 }
6164
6165 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));
6166 if (cInstances == 0)
6167 {
6168 /* Render with an index buffer */
6169 if (pRange[iPrimitive].indexBias == 0)
6170 glDrawElements(modeDraw,
6171 cVertices,
6172 indexType,
6173 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset); /* byte offset in indices buffer */
6174 else
6175 pState->ext.glDrawElementsBaseVertex(modeDraw,
6176 cVertices,
6177 indexType,
6178 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
6179 pRange[iPrimitive].indexBias); /* basevertex */
6180 }
6181 else
6182 {
6183 /* Render with an index buffer */
6184 if (pRange[iPrimitive].indexBias == 0)
6185 pState->ext.glDrawElementsInstanced(modeDraw,
6186 cVertices,
6187 indexType,
6188 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
6189 cInstances);
6190 else
6191 pState->ext.glDrawElementsInstancedBaseVertex(modeDraw,
6192 cVertices,
6193 indexType,
6194 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
6195 cInstances,
6196 pRange[iPrimitive].indexBias); /* basevertex */
6197 }
6198 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6199
6200 /* Unbind the index buffer after usage. */
6201 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
6202 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6203 }
6204 }
6205
6206internal_error:
6207
6208 /* Deactivate the vertex declarations. */
6209 iCurrentVertex = 0;
6210 while (iCurrentVertex < numVertexDecls)
6211 {
6212 uint32_t sidVertex = SVGA_ID_INVALID;
6213 uint32_t iVertex;
6214
6215 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
6216 {
6217 if ( sidVertex != SVGA_ID_INVALID
6218 && pVertexDecl[iVertex].array.surfaceId != sidVertex
6219 )
6220 break;
6221 sidVertex = pVertexDecl[iVertex].array.surfaceId;
6222 }
6223
6224 rc = vmsvga3dDrawPrimitivesCleanupVertexDecls(pThis, pContext, iCurrentVertex, iVertex - iCurrentVertex, &pVertexDecl[iCurrentVertex]);
6225 AssertRCReturn(rc, rc);
6226
6227 iCurrentVertex = iVertex;
6228 }
6229#ifdef DEBUG
6230 /* Check whether 'activeTexture' on texture unit 'i' matches what we expect. */
6231 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTextures); ++i)
6232 {
6233 if (pContext->aSidActiveTextures[i] != SVGA3D_INVALID_ID)
6234 {
6235 PVMSVGA3DSURFACE pTexture;
6236 int rc2 = vmsvga3dSurfaceFromSid(pState, pContext->aSidActiveTextures[i], &pTexture);
6237 AssertContinue(RT_SUCCESS(rc2));
6238
6239 GLint activeTextureUnit = 0;
6240 glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
6241 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6242
6243 pState->ext.glActiveTexture(GL_TEXTURE0 + i);
6244 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6245
6246 GLint activeTexture = 0;
6247 glGetIntegerv(pTexture->bindingGL, &activeTexture);
6248 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6249
6250 pState->ext.glActiveTexture(activeTextureUnit);
6251 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6252
6253 AssertMsg(pTexture->oglId.texture == (GLuint)activeTexture,
6254 ("%x vs %x unit %d (active unit %d) sid=%x\n", pTexture->oglId.texture, activeTexture, i,
6255 activeTextureUnit - GL_TEXTURE0, pContext->aSidActiveTextures[i]));
6256 }
6257 }
6258#endif
6259
6260#if 0
6261 /* Dump render target to a bitmap. */
6262 if (pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] != SVGA3D_INVALID_ID)
6263 {
6264 vmsvga3dUpdateHeapBuffersForSurfaces(pThis, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0]);
6265 PVMSVGA3DSURFACE pSurface;
6266 int rc2 = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pSurface);
6267 if (RT_SUCCESS(rc2))
6268 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmp", "rt", "-post");
6269 }
6270#endif
6271
6272 return rc;
6273}
6274
6275
6276int vmsvga3dShaderDefine(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type, uint32_t cbData, uint32_t *pShaderData)
6277{
6278 PVMSVGA3DCONTEXT pContext;
6279 PVMSVGA3DSHADER pShader;
6280 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6281 AssertReturn(pState, VERR_NO_MEMORY);
6282 int rc;
6283
6284 Log(("vmsvga3dShaderDefine cid=%x shid=%x type=%s cbData=%x\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cbData));
6285 Log3(("shader code:\n%.*Rhxd\n", cbData, pShaderData));
6286
6287 if ( cid >= pState->cContexts
6288 || pState->papContexts[cid]->id != cid)
6289 {
6290 Log(("vmsvga3dShaderDefine invalid context id!\n"));
6291 return VERR_INVALID_PARAMETER;
6292 }
6293 pContext = pState->papContexts[cid];
6294 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6295
6296 AssertReturn(shid < SVGA3D_MAX_SHADER_IDS, VERR_INVALID_PARAMETER);
6297 if (type == SVGA3D_SHADERTYPE_VS)
6298 {
6299 if (shid >= pContext->cVertexShaders)
6300 {
6301 void *pvNew = RTMemRealloc(pContext->paVertexShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
6302 AssertReturn(pvNew, VERR_NO_MEMORY);
6303 pContext->paVertexShader = (PVMSVGA3DSHADER)pvNew;
6304 memset(&pContext->paVertexShader[pContext->cVertexShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cVertexShaders));
6305 for (uint32_t i = pContext->cVertexShaders; i < shid + 1; i++)
6306 pContext->paVertexShader[i].id = SVGA3D_INVALID_ID;
6307 pContext->cVertexShaders = shid + 1;
6308 }
6309 /* If one already exists with this id, then destroy it now. */
6310 if (pContext->paVertexShader[shid].id != SVGA3D_INVALID_ID)
6311 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paVertexShader[shid].type);
6312
6313 pShader = &pContext->paVertexShader[shid];
6314 }
6315 else
6316 {
6317 Assert(type == SVGA3D_SHADERTYPE_PS);
6318 if (shid >= pContext->cPixelShaders)
6319 {
6320 void *pvNew = RTMemRealloc(pContext->paPixelShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
6321 AssertReturn(pvNew, VERR_NO_MEMORY);
6322 pContext->paPixelShader = (PVMSVGA3DSHADER)pvNew;
6323 memset(&pContext->paPixelShader[pContext->cPixelShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cPixelShaders));
6324 for (uint32_t i = pContext->cPixelShaders; i < shid + 1; i++)
6325 pContext->paPixelShader[i].id = SVGA3D_INVALID_ID;
6326 pContext->cPixelShaders = shid + 1;
6327 }
6328 /* If one already exists with this id, then destroy it now. */
6329 if (pContext->paPixelShader[shid].id != SVGA3D_INVALID_ID)
6330 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paPixelShader[shid].type);
6331
6332 pShader = &pContext->paPixelShader[shid];
6333 }
6334
6335 memset(pShader, 0, sizeof(*pShader));
6336 pShader->id = shid;
6337 pShader->cid = cid;
6338 pShader->type = type;
6339 pShader->cbData = cbData;
6340 pShader->pShaderProgram = RTMemAllocZ(cbData);
6341 AssertReturn(pShader->pShaderProgram, VERR_NO_MEMORY);
6342 memcpy(pShader->pShaderProgram, pShaderData, cbData);
6343
6344#ifdef DUMP_SHADER_DISASSEMBLY
6345 LPD3DXBUFFER pDisassembly;
6346 HRESULT hr = D3DXDisassembleShader((const DWORD *)pShaderData, FALSE, NULL, &pDisassembly);
6347 if (hr == D3D_OK)
6348 {
6349 Log(("Shader disassembly:\n%s\n", pDisassembly->GetBufferPointer()));
6350 pDisassembly->Release();
6351 }
6352#endif
6353
6354 switch (type)
6355 {
6356 case SVGA3D_SHADERTYPE_VS:
6357 rc = ShaderCreateVertexShader(pContext->pShaderContext, (const uint32_t *)pShaderData, &pShader->u.pVertexShader);
6358 break;
6359
6360 case SVGA3D_SHADERTYPE_PS:
6361 rc = ShaderCreatePixelShader(pContext->pShaderContext, (const uint32_t *)pShaderData, &pShader->u.pPixelShader);
6362 break;
6363
6364 default:
6365 AssertFailedReturn(VERR_INVALID_PARAMETER);
6366 }
6367 if (rc != VINF_SUCCESS)
6368 {
6369 RTMemFree(pShader->pShaderProgram);
6370 memset(pShader, 0, sizeof(*pShader));
6371 pShader->id = SVGA3D_INVALID_ID;
6372 }
6373
6374 return rc;
6375}
6376
6377int vmsvga3dShaderDestroy(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type)
6378{
6379 PVMSVGA3DCONTEXT pContext;
6380 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6381 AssertReturn(pState, VERR_NO_MEMORY);
6382 PVMSVGA3DSHADER pShader = NULL;
6383 int rc;
6384
6385 Log(("vmsvga3dShaderDestroy cid=%x shid=%x type=%s\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL"));
6386
6387 if ( cid >= pState->cContexts
6388 || pState->papContexts[cid]->id != cid)
6389 {
6390 Log(("vmsvga3dShaderDestroy invalid context id!\n"));
6391 return VERR_INVALID_PARAMETER;
6392 }
6393 pContext = pState->papContexts[cid];
6394 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6395
6396 if (type == SVGA3D_SHADERTYPE_VS)
6397 {
6398 if ( shid < pContext->cVertexShaders
6399 && pContext->paVertexShader[shid].id == shid)
6400 {
6401 pShader = &pContext->paVertexShader[shid];
6402 rc = ShaderDestroyVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
6403 AssertRC(rc);
6404 }
6405 }
6406 else
6407 {
6408 Assert(type == SVGA3D_SHADERTYPE_PS);
6409 if ( shid < pContext->cPixelShaders
6410 && pContext->paPixelShader[shid].id == shid)
6411 {
6412 pShader = &pContext->paPixelShader[shid];
6413 rc = ShaderDestroyPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
6414 AssertRC(rc);
6415 }
6416 }
6417
6418 if (pShader)
6419 {
6420 if (pShader->pShaderProgram)
6421 RTMemFree(pShader->pShaderProgram);
6422 memset(pShader, 0, sizeof(*pShader));
6423 pShader->id = SVGA3D_INVALID_ID;
6424 }
6425 else
6426 AssertFailedReturn(VERR_INVALID_PARAMETER);
6427
6428 return VINF_SUCCESS;
6429}
6430
6431int vmsvga3dShaderSet(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext, uint32_t cid, SVGA3dShaderType type, uint32_t shid)
6432{
6433 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6434 AssertReturn(pState, VERR_NO_MEMORY);
6435 int rc;
6436
6437 Log(("vmsvga3dShaderSet cid=%x type=%s shid=%d\n", cid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", shid));
6438
6439 if ( !pContext
6440 && cid < pState->cContexts
6441 && pState->papContexts[cid]->id == cid)
6442 pContext = pState->papContexts[cid];
6443 else if (!pContext)
6444 {
6445 AssertMsgFailed(("cid=%#x cContexts=%#x\n", cid, pState->cContexts));
6446 Log(("vmsvga3dShaderSet invalid context id!\n"));
6447 return VERR_INVALID_PARAMETER;
6448 }
6449 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6450
6451 if (type == SVGA3D_SHADERTYPE_VS)
6452 {
6453 /* Save for vm state save/restore. */
6454 pContext->state.shidVertex = shid;
6455 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VERTEXSHADER;
6456
6457 if ( shid < pContext->cVertexShaders
6458 && pContext->paVertexShader[shid].id == shid)
6459 {
6460 PVMSVGA3DSHADER pShader = &pContext->paVertexShader[shid];
6461 Assert(type == pShader->type);
6462
6463 rc = ShaderSetVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
6464 AssertRCReturn(rc, rc);
6465 }
6466 else
6467 if (shid == SVGA_ID_INVALID)
6468 {
6469 /* Unselect shader. */
6470 rc = ShaderSetVertexShader(pContext->pShaderContext, NULL);
6471 AssertRCReturn(rc, rc);
6472 }
6473 else
6474 AssertFailedReturn(VERR_INVALID_PARAMETER);
6475 }
6476 else
6477 {
6478 /* Save for vm state save/restore. */
6479 pContext->state.shidPixel = shid;
6480 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_PIXELSHADER;
6481
6482 Assert(type == SVGA3D_SHADERTYPE_PS);
6483 if ( shid < pContext->cPixelShaders
6484 && pContext->paPixelShader[shid].id == shid)
6485 {
6486 PVMSVGA3DSHADER pShader = &pContext->paPixelShader[shid];
6487 Assert(type == pShader->type);
6488
6489 rc = ShaderSetPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
6490 AssertRCReturn(rc, rc);
6491 }
6492 else
6493 if (shid == SVGA_ID_INVALID)
6494 {
6495 /* Unselect shader. */
6496 rc = ShaderSetPixelShader(pContext->pShaderContext, NULL);
6497 AssertRCReturn(rc, rc);
6498 }
6499 else
6500 AssertFailedReturn(VERR_INVALID_PARAMETER);
6501 }
6502
6503 return VINF_SUCCESS;
6504}
6505
6506int vmsvga3dShaderSetConst(PVGASTATE pThis, uint32_t cid, uint32_t reg, SVGA3dShaderType type, SVGA3dShaderConstType ctype, uint32_t cRegisters, uint32_t *pValues)
6507{
6508 PVMSVGA3DCONTEXT pContext;
6509 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6510 AssertReturn(pState, VERR_NO_MEMORY);
6511 int rc;
6512
6513 Log(("vmsvga3dShaderSetConst cid=%x reg=%x type=%s cregs=%d ctype=%x\n", cid, reg, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cRegisters, ctype));
6514
6515 if ( cid >= pState->cContexts
6516 || pState->papContexts[cid]->id != cid)
6517 {
6518 Log(("vmsvga3dShaderSetConst invalid context id!\n"));
6519 return VERR_INVALID_PARAMETER;
6520 }
6521 pContext = pState->papContexts[cid];
6522 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6523
6524 for (uint32_t i = 0; i < cRegisters; i++)
6525 {
6526#ifdef LOG_ENABLED
6527 switch (ctype)
6528 {
6529 case SVGA3D_CONST_TYPE_FLOAT:
6530 {
6531 float *pValuesF = (float *)pValues;
6532 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)));
6533 break;
6534 }
6535
6536 case SVGA3D_CONST_TYPE_INT:
6537 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]));
6538 break;
6539
6540 case SVGA3D_CONST_TYPE_BOOL:
6541 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]));
6542 break;
6543 }
6544#endif
6545 vmsvga3dSaveShaderConst(pContext, reg + i, type, ctype, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]);
6546 }
6547
6548 switch (type)
6549 {
6550 case SVGA3D_SHADERTYPE_VS:
6551 switch (ctype)
6552 {
6553 case SVGA3D_CONST_TYPE_FLOAT:
6554 rc = ShaderSetVertexShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
6555 break;
6556
6557 case SVGA3D_CONST_TYPE_INT:
6558 rc = ShaderSetVertexShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
6559 break;
6560
6561 case SVGA3D_CONST_TYPE_BOOL:
6562 rc = ShaderSetVertexShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
6563 break;
6564
6565 default:
6566 AssertFailedReturn(VERR_INVALID_PARAMETER);
6567 }
6568 AssertRCReturn(rc, rc);
6569 break;
6570
6571 case SVGA3D_SHADERTYPE_PS:
6572 switch (ctype)
6573 {
6574 case SVGA3D_CONST_TYPE_FLOAT:
6575 rc = ShaderSetPixelShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
6576 break;
6577
6578 case SVGA3D_CONST_TYPE_INT:
6579 rc = ShaderSetPixelShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
6580 break;
6581
6582 case SVGA3D_CONST_TYPE_BOOL:
6583 rc = ShaderSetPixelShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
6584 break;
6585
6586 default:
6587 AssertFailedReturn(VERR_INVALID_PARAMETER);
6588 }
6589 AssertRCReturn(rc, rc);
6590 break;
6591
6592 default:
6593 AssertFailedReturn(VERR_INVALID_PARAMETER);
6594 }
6595
6596 return VINF_SUCCESS;
6597}
6598
6599int vmsvga3dOcclusionQueryCreate(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
6600{
6601 AssertReturn(pState->ext.glGenQueries, VERR_NOT_SUPPORTED);
6602 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6603
6604 GLuint idQuery = 0;
6605 pState->ext.glGenQueries(1, &idQuery);
6606 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6607 AssertReturn(idQuery, VERR_INTERNAL_ERROR);
6608 pContext->occlusion.idQuery = idQuery;
6609 return VINF_SUCCESS;
6610}
6611
6612int vmsvga3dOcclusionQueryDelete(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
6613{
6614 AssertReturn(pState->ext.glDeleteQueries, VERR_NOT_SUPPORTED);
6615 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6616
6617 if (pContext->occlusion.idQuery)
6618 {
6619 pState->ext.glDeleteQueries(1, &pContext->occlusion.idQuery);
6620 }
6621 return VINF_SUCCESS;
6622}
6623
6624int vmsvga3dOcclusionQueryBegin(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
6625{
6626 AssertReturn(pState->ext.glBeginQuery, VERR_NOT_SUPPORTED);
6627 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6628
6629 pState->ext.glBeginQuery(GL_ANY_SAMPLES_PASSED, pContext->occlusion.idQuery);
6630 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6631 return VINF_SUCCESS;
6632}
6633
6634int vmsvga3dOcclusionQueryEnd(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
6635{
6636 AssertReturn(pState->ext.glEndQuery, VERR_NOT_SUPPORTED);
6637 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6638
6639 pState->ext.glEndQuery(GL_ANY_SAMPLES_PASSED);
6640 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6641 return VINF_SUCCESS;
6642}
6643
6644int vmsvga3dOcclusionQueryGetData(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t *pu32Pixels)
6645{
6646 AssertReturn(pState->ext.glGetQueryObjectuiv, VERR_NOT_SUPPORTED);
6647 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6648
6649 GLuint pixels = 0;
6650 pState->ext.glGetQueryObjectuiv(pContext->occlusion.idQuery, GL_QUERY_RESULT, &pixels);
6651 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6652
6653 *pu32Pixels = (uint32_t)pixels;
6654 return VINF_SUCCESS;
6655}
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