VirtualBox

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

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

DevVGA-SVGA: further cleanup of context windows creation (Windows host only); removed obsolete debug code.

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