VirtualBox

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

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

DevVGA-SVGA3d-ogl.cpp: Z_DF16 is not supported by OpenGL; use texture for depth/stencil render targets.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 250.6 KB
Line 
1/* $Id: DevVGA-SVGA3d-ogl.cpp 75695 2018-11-23 19:06:36Z 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 LogFunc(("sid=%x\n", pSurface->id));
2112
2113 uint32_t const numMipLevels = pSurface->faces[0].numMipLevels;
2114
2115 /* Fugure out what kind of texture we are creating. */
2116 GLenum binding;
2117 GLenum target;
2118 if (pSurface->surfaceFlags & SVGA3D_SURFACE_CUBEMAP)
2119 {
2120 Assert(pSurface->cFaces == 6);
2121
2122 binding = GL_TEXTURE_BINDING_CUBE_MAP;
2123 target = GL_TEXTURE_CUBE_MAP;
2124 }
2125 else
2126 {
2127 if (pSurface->pMipmapLevels[0].mipmapSize.depth > 1)
2128 {
2129 binding = GL_TEXTURE_BINDING_3D;
2130 target = GL_TEXTURE_3D;
2131 }
2132 else
2133 {
2134 Assert(pSurface->cFaces == 1);
2135
2136 binding = GL_TEXTURE_BINDING_2D;
2137 target = GL_TEXTURE_2D;
2138 }
2139 }
2140
2141 /* All textures are created in the SharedCtx. */
2142 uint32_t idPrevCtx = pState->idActiveContext;
2143 pContext = &pState->SharedCtx;
2144 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2145
2146 glGenTextures(1, &pSurface->oglId.texture);
2147 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2148
2149 GLint activeTexture = 0;
2150 glGetIntegerv(binding, &activeTexture);
2151 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2152
2153 /* Must bind texture to the current context in order to change it. */
2154 glBindTexture(target, pSurface->oglId.texture);
2155 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2156
2157 /* Set the unpacking parameters. */
2158 VMSVGAPACKPARAMS SavedParams;
2159 vmsvga3dOglSetUnpackParams(pState, pContext, pSurface, &SavedParams);
2160
2161 /** @todo Set the mip map generation filter settings. */
2162
2163 /* Set the mipmap base and max level parameters. */
2164 glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, 0);
2165 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2166 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, pSurface->faces[0].numMipLevels - 1);
2167 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2168
2169 if (pSurface->fDirty)
2170 LogFunc(("sync dirty texture\n"));
2171
2172 /* Always allocate and initialize all mipmap levels; non-initialized mipmap levels used as render targets cause failures. */
2173 if (target == GL_TEXTURE_3D)
2174 {
2175 for (uint32_t i = 0; i < numMipLevels; ++i)
2176 {
2177 /* Allocate and initialize texture memory. Passing the zero filled pSurfaceData avoids
2178 * exposing random host memory to the guest and helps a with the fedora 21 surface
2179 * corruption issues (launchpad, background, search field, login).
2180 */
2181 PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->pMipmapLevels[i];
2182
2183 LogFunc(("sync dirty 3D texture mipmap level %d (pitch %x) (dirty %d)\n",
2184 i, pMipLevel->cbSurfacePitch, pMipLevel->fDirty));
2185
2186 if ( pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
2187 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
2188 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
2189 {
2190 pState->ext.glCompressedTexImage3D(GL_TEXTURE_3D,
2191 i,
2192 pSurface->internalFormatGL,
2193 pMipLevel->mipmapSize.width,
2194 pMipLevel->mipmapSize.height,
2195 pMipLevel->mipmapSize.depth,
2196 0,
2197 pMipLevel->cbSurface,
2198 pMipLevel->pSurfaceData);
2199 }
2200 else
2201 {
2202 pState->ext.glTexImage3D(GL_TEXTURE_3D,
2203 i,
2204 pSurface->internalFormatGL,
2205 pMipLevel->mipmapSize.width,
2206 pMipLevel->mipmapSize.height,
2207 pMipLevel->mipmapSize.depth,
2208 0, /* border */
2209 pSurface->formatGL,
2210 pSurface->typeGL,
2211 pMipLevel->pSurfaceData);
2212 }
2213 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2214
2215 pMipLevel->fDirty = false;
2216 }
2217 }
2218 else if (target == GL_TEXTURE_CUBE_MAP)
2219 {
2220 for (uint32_t iFace = 0; iFace < 6; ++iFace)
2221 {
2222 GLenum const Face = vmsvga3dCubemapFaceFromIndex(iFace);
2223
2224 for (uint32_t i = 0; i < numMipLevels; ++i)
2225 {
2226 PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->pMipmapLevels[iFace * numMipLevels + i];
2227 Assert(pMipLevel->mipmapSize.width == pMipLevel->mipmapSize.height);
2228 Assert(pMipLevel->mipmapSize.depth == 1);
2229
2230 LogFunc(("sync cube texture face %d mipmap level %d (dirty %d)\n",
2231 iFace, i, pMipLevel->fDirty));
2232
2233 if ( pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
2234 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
2235 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
2236 {
2237 pState->ext.glCompressedTexImage2D(Face,
2238 i,
2239 pSurface->internalFormatGL,
2240 pMipLevel->mipmapSize.width,
2241 pMipLevel->mipmapSize.height,
2242 0,
2243 pMipLevel->cbSurface,
2244 pMipLevel->pSurfaceData);
2245 }
2246 else
2247 {
2248 glTexImage2D(Face,
2249 i,
2250 pSurface->internalFormatGL,
2251 pMipLevel->mipmapSize.width,
2252 pMipLevel->mipmapSize.height,
2253 0,
2254 pSurface->formatGL,
2255 pSurface->typeGL,
2256 pMipLevel->pSurfaceData);
2257 }
2258 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2259
2260 pMipLevel->fDirty = false;
2261 }
2262 }
2263 }
2264 else if (target == GL_TEXTURE_2D)
2265 {
2266 for (uint32_t i = 0; i < numMipLevels; ++i)
2267 {
2268 /* Allocate and initialize texture memory. Passing the zero filled pSurfaceData avoids
2269 * exposing random host memory to the guest and helps a with the fedora 21 surface
2270 * corruption issues (launchpad, background, search field, login).
2271 */
2272 PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->pMipmapLevels[i];
2273 Assert(pMipLevel->mipmapSize.depth == 1);
2274
2275 LogFunc(("sync dirty texture mipmap level %d (pitch %x) (dirty %d)\n",
2276 i, pMipLevel->cbSurfacePitch, pMipLevel->fDirty));
2277
2278 if ( pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
2279 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
2280 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
2281 {
2282 pState->ext.glCompressedTexImage2D(GL_TEXTURE_2D,
2283 i,
2284 pSurface->internalFormatGL,
2285 pMipLevel->mipmapSize.width,
2286 pMipLevel->mipmapSize.height,
2287 0,
2288 pMipLevel->cbSurface,
2289 pMipLevel->pSurfaceData);
2290 }
2291 else
2292 {
2293 glTexImage2D(GL_TEXTURE_2D,
2294 i,
2295 pSurface->internalFormatGL,
2296 pMipLevel->mipmapSize.width,
2297 pMipLevel->mipmapSize.height,
2298 0,
2299 pSurface->formatGL,
2300 pSurface->typeGL,
2301 pMipLevel->pSurfaceData);
2302 }
2303 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2304
2305 pMipLevel->fDirty = false;
2306 }
2307 }
2308
2309 pSurface->fDirty = false;
2310
2311 /* Restore unpacking parameters. */
2312 vmsvga3dOglRestoreUnpackParams(pState, pContext, pSurface, &SavedParams);
2313
2314 /* Restore the old active texture. */
2315 glBindTexture(target, activeTexture);
2316 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2317
2318 pSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_TEXTURE;
2319 pSurface->targetGL = target;
2320 pSurface->bindingGL = binding;
2321
2322 if (idPrevCtx < pState->cContexts && pState->papContexts[idPrevCtx]->id == idPrevCtx)
2323 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pState->papContexts[idPrevCtx]);
2324 return VINF_SUCCESS;
2325}
2326
2327
2328/**
2329 * Backend worker for implementing SVGA_3D_CMD_SURFACE_STRETCHBLT.
2330 *
2331 * @returns VBox status code.
2332 * @param pThis The VGA device instance.
2333 * @param pState The VMSVGA3d state.
2334 * @param pDstSurface The destination host surface.
2335 * @param uDstFace The destination face (valid).
2336 * @param uDstMipmap The destination mipmap level (valid).
2337 * @param pDstBox The destination box.
2338 * @param pSrcSurface The source host surface.
2339 * @param uSrcFace The destination face (valid).
2340 * @param uSrcMipmap The source mimap level (valid).
2341 * @param pSrcBox The source box.
2342 * @param enmMode The strecht blt mode .
2343 * @param pContext The VMSVGA3d context (already current for OGL).
2344 */
2345int vmsvga3dBackSurfaceStretchBlt(PVGASTATE pThis, PVMSVGA3DSTATE pState,
2346 PVMSVGA3DSURFACE pDstSurface, uint32_t uDstFace, uint32_t uDstMipmap, SVGA3dBox const *pDstBox,
2347 PVMSVGA3DSURFACE pSrcSurface, uint32_t uSrcFace, uint32_t uSrcMipmap, SVGA3dBox const *pSrcBox,
2348 SVGA3dStretchBltMode enmMode, PVMSVGA3DCONTEXT pContext)
2349{
2350 RT_NOREF(pThis);
2351
2352 /* Activate the read and draw framebuffer objects. */
2353 pState->ext.glBindFramebuffer(GL_READ_FRAMEBUFFER, pContext->idReadFramebuffer);
2354 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2355 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, pContext->idDrawFramebuffer);
2356 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2357
2358 /* Bind the source and destination objects to the right place. */
2359 GLenum textarget;
2360 if (pSrcSurface->surfaceFlags & SVGA3D_SURFACE_CUBEMAP)
2361 textarget = vmsvga3dCubemapFaceFromIndex(uSrcFace);
2362 else
2363 textarget = GL_TEXTURE_2D;
2364 pState->ext.glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textarget,
2365 pSrcSurface->oglId.texture, uSrcMipmap);
2366 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2367
2368 if (pDstSurface->surfaceFlags & SVGA3D_SURFACE_CUBEMAP)
2369 textarget = vmsvga3dCubemapFaceFromIndex(uDstFace);
2370 else
2371 textarget = GL_TEXTURE_2D;
2372 pState->ext.glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textarget,
2373 pDstSurface->oglId.texture, uDstMipmap);
2374 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2375
2376 Log(("src conv. (%d,%d)(%d,%d); dest conv (%d,%d)(%d,%d)\n",
2377 pSrcBox->x, D3D_TO_OGL_Y_COORD(pSrcSurface, pSrcBox->y + pSrcBox->h),
2378 pSrcBox->x + pSrcBox->w, D3D_TO_OGL_Y_COORD(pSrcSurface, pSrcBox->y),
2379 pDstBox->x, D3D_TO_OGL_Y_COORD(pDstSurface, pDstBox->y + pDstBox->h),
2380 pDstBox->x + pDstBox->w, D3D_TO_OGL_Y_COORD(pDstSurface, pDstBox->y)));
2381
2382 pState->ext.glBlitFramebuffer(pSrcBox->x,
2383 pSrcBox->y,
2384 pSrcBox->x + pSrcBox->w, /* exclusive. */
2385 pSrcBox->y + pSrcBox->h,
2386 pDstBox->x,
2387 pDstBox->y,
2388 pDstBox->x + pDstBox->w, /* exclusive. */
2389 pDstBox->y + pDstBox->h,
2390 GL_COLOR_BUFFER_BIT,
2391 (enmMode == SVGA3D_STRETCH_BLT_POINT) ? GL_NEAREST : GL_LINEAR);
2392 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2393
2394 /* Reset the frame buffer association */
2395 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
2396 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2397
2398 return VINF_SUCCESS;
2399}
2400
2401/**
2402 * Save texture packing parameters and loads those appropriate for the given
2403 * surface.
2404 *
2405 * @param pState The VMSVGA3D state structure.
2406 * @param pContext The active context.
2407 * @param pSurface The surface.
2408 * @param pSave Where to save stuff.
2409 */
2410void vmsvga3dOglSetPackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
2411 PVMSVGAPACKPARAMS pSave)
2412{
2413 RT_NOREF(pState);
2414 /*
2415 * Save (ignore errors, setting the defaults we want and avoids restore).
2416 */
2417 pSave->iAlignment = 1;
2418 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_UNPACK_ALIGNMENT, &pSave->iAlignment), pState, pContext);
2419 pSave->cxRow = 0;
2420 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_UNPACK_ROW_LENGTH, &pSave->cxRow), pState, pContext);
2421
2422#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
2423 pSave->cyImage = 0;
2424 glGetIntegerv(GL_PACK_IMAGE_HEIGHT, &pSave->cyImage);
2425 Assert(pSave->cyImage == 0);
2426
2427 pSave->fSwapBytes = GL_FALSE;
2428 glGetBooleanv(GL_PACK_SWAP_BYTES, &pSave->fSwapBytes);
2429 Assert(pSave->fSwapBytes == GL_FALSE);
2430
2431 pSave->fLsbFirst = GL_FALSE;
2432 glGetBooleanv(GL_PACK_LSB_FIRST, &pSave->fLsbFirst);
2433 Assert(pSave->fLsbFirst == GL_FALSE);
2434
2435 pSave->cSkipRows = 0;
2436 glGetIntegerv(GL_PACK_SKIP_ROWS, &pSave->cSkipRows);
2437 Assert(pSave->cSkipRows == 0);
2438
2439 pSave->cSkipPixels = 0;
2440 glGetIntegerv(GL_PACK_SKIP_PIXELS, &pSave->cSkipPixels);
2441 Assert(pSave->cSkipPixels == 0);
2442
2443 pSave->cSkipImages = 0;
2444 glGetIntegerv(GL_PACK_SKIP_IMAGES, &pSave->cSkipImages);
2445 Assert(pSave->cSkipImages == 0);
2446
2447 VMSVGA3D_CLEAR_GL_ERRORS();
2448#endif
2449
2450 /*
2451 * Setup unpack.
2452 *
2453 * Note! We use 1 as alignment here because we currently don't do any
2454 * aligning of line pitches anywhere.
2455 */
2456 NOREF(pSurface);
2457 if (pSave->iAlignment != 1)
2458 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ALIGNMENT, 1), pState, pContext);
2459 if (pSave->cxRow != 0)
2460 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ROW_LENGTH, 0), pState, pContext);
2461#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
2462 if (pSave->cyImage != 0)
2463 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0), pState, pContext);
2464 if (pSave->fSwapBytes != 0)
2465 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE), pState, pContext);
2466 if (pSave->fLsbFirst != 0)
2467 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE), pState, pContext);
2468 if (pSave->cSkipRows != 0)
2469 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_ROWS, 0), pState, pContext);
2470 if (pSave->cSkipPixels != 0)
2471 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_PIXELS, 0), pState, pContext);
2472 if (pSave->cSkipImages != 0)
2473 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_IMAGES, 0), pState, pContext);
2474#endif
2475}
2476
2477
2478/**
2479 * Restores texture packing parameters.
2480 *
2481 * @param pState The VMSVGA3D state structure.
2482 * @param pContext The active context.
2483 * @param pSurface The surface.
2484 * @param pSave Where stuff was saved.
2485 */
2486void vmsvga3dOglRestorePackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
2487 PCVMSVGAPACKPARAMS pSave)
2488{
2489 RT_NOREF(pState, pSurface);
2490 if (pSave->iAlignment != 1)
2491 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ALIGNMENT, pSave->iAlignment), pState, pContext);
2492 if (pSave->cxRow != 0)
2493 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ROW_LENGTH, pSave->cxRow), pState, pContext);
2494#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
2495 if (pSave->cyImage != 0)
2496 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_IMAGE_HEIGHT, pSave->cyImage), pState, pContext);
2497 if (pSave->fSwapBytes != 0)
2498 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SWAP_BYTES, pSave->fSwapBytes), pState, pContext);
2499 if (pSave->fLsbFirst != 0)
2500 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_LSB_FIRST, pSave->fLsbFirst), pState, pContext);
2501 if (pSave->cSkipRows != 0)
2502 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_ROWS, pSave->cSkipRows), pState, pContext);
2503 if (pSave->cSkipPixels != 0)
2504 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_PIXELS, pSave->cSkipPixels), pState, pContext);
2505 if (pSave->cSkipImages != 0)
2506 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_IMAGES, pSave->cSkipImages), pState, pContext);
2507#endif
2508}
2509
2510
2511/**
2512 * Backend worker for implementing SVGA_3D_CMD_SURFACE_DMA that copies one box.
2513 *
2514 * @returns Failure status code or @a rc.
2515 * @param pThis The VGA device instance data.
2516 * @param pState The VMSVGA3d state.
2517 * @param pSurface The host surface.
2518 * @param pMipLevel Mipmap level. The caller knows it already.
2519 * @param uHostFace The host face (valid).
2520 * @param uHostMipmap The host mipmap level (valid).
2521 * @param GuestPtr The guest pointer.
2522 * @param cbGuestPitch The guest pitch.
2523 * @param transfer The transfer direction.
2524 * @param pBox The box to copy (clipped, valid, except for guest's srcx, srcy, srcz).
2525 * @param pContext The context (for OpenGL).
2526 * @param rc The current rc for all boxes.
2527 * @param iBox The current box number (for Direct 3D).
2528 */
2529int vmsvga3dBackSurfaceDMACopyBox(PVGASTATE pThis, PVMSVGA3DSTATE pState, PVMSVGA3DSURFACE pSurface,
2530 PVMSVGA3DMIPMAPLEVEL pMipLevel, uint32_t uHostFace, uint32_t uHostMipmap,
2531 SVGAGuestPtr GuestPtr, uint32_t cbGuestPitch, SVGA3dTransferType transfer,
2532 SVGA3dCopyBox const *pBox, PVMSVGA3DCONTEXT pContext, int rc, int iBox)
2533{
2534 RT_NOREF(iBox);
2535
2536 switch (pSurface->surfaceFlags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK)
2537 {
2538 case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
2539 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
2540 case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE:
2541 case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2542 case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2543 case SVGA3D_SURFACE_HINT_TEXTURE:
2544 case SVGA3D_SURFACE_HINT_RENDERTARGET:
2545 {
2546 uint32_t cbSurfacePitch;
2547 uint8_t *pDoubleBuffer;
2548 uint32_t offHst;
2549
2550 uint32_t const u32HostBlockX = pBox->x / pSurface->cxBlock;
2551 uint32_t const u32HostBlockY = pBox->y / pSurface->cyBlock;
2552 Assert(u32HostBlockX * pSurface->cxBlock == pBox->x);
2553 Assert(u32HostBlockY * pSurface->cyBlock == pBox->y);
2554
2555 uint32_t const u32GuestBlockX = pBox->srcx / pSurface->cxBlock;
2556 uint32_t const u32GuestBlockY = pBox->srcy / pSurface->cyBlock;
2557 Assert(u32GuestBlockX * pSurface->cxBlock == pBox->srcx);
2558 Assert(u32GuestBlockY * pSurface->cyBlock == pBox->srcy);
2559
2560 uint32_t const cBlocksX = (pBox->w + pSurface->cxBlock - 1) / pSurface->cxBlock;
2561 uint32_t const cBlocksY = (pBox->h + pSurface->cyBlock - 1) / pSurface->cyBlock;
2562 AssertMsgReturn(cBlocksX && cBlocksY, ("Empty box %dx%d\n", pBox->w, pBox->h), VERR_INTERNAL_ERROR);
2563
2564 GLenum texImageTarget;
2565 if (pSurface->targetGL == GL_TEXTURE_CUBE_MAP)
2566 {
2567 texImageTarget = vmsvga3dCubemapFaceFromIndex(uHostFace);
2568 }
2569 else
2570 {
2571 Assert(pSurface->targetGL == GL_TEXTURE_2D);
2572 texImageTarget = GL_TEXTURE_2D;
2573 }
2574
2575 pDoubleBuffer = (uint8_t *)RTMemAlloc(pMipLevel->cbSurface);
2576 AssertReturn(pDoubleBuffer, VERR_NO_MEMORY);
2577
2578 if (transfer == SVGA3D_READ_HOST_VRAM)
2579 {
2580 GLint activeTexture;
2581
2582 /* Must bind texture to the current context in order to read it. */
2583 glGetIntegerv(pSurface->bindingGL, &activeTexture);
2584 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2585
2586 glBindTexture(pSurface->targetGL, pSurface->oglId.texture);
2587 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2588
2589 /* Set row length and alignment of the input data. */
2590 VMSVGAPACKPARAMS SavedParams;
2591 vmsvga3dOglSetPackParams(pState, pContext, pSurface, &SavedParams);
2592
2593 glGetTexImage(texImageTarget,
2594 uHostMipmap,
2595 pSurface->formatGL,
2596 pSurface->typeGL,
2597 pDoubleBuffer);
2598 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2599
2600 vmsvga3dOglRestorePackParams(pState, pContext, pSurface, &SavedParams);
2601
2602 /* Restore the old active texture. */
2603 glBindTexture(pSurface->targetGL, activeTexture);
2604 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2605
2606 offHst = u32HostBlockX * pSurface->cbBlock + u32HostBlockY * pMipLevel->cbSurfacePitch;
2607 cbSurfacePitch = pMipLevel->cbSurfacePitch;
2608 }
2609 else
2610 {
2611 /* The buffer will contain only the copied rectangle. */
2612 offHst = 0;
2613 cbSurfacePitch = cBlocksX * pSurface->cbBlock;
2614 }
2615
2616 uint32_t const offGst = u32GuestBlockX * pSurface->cbBlock + u32GuestBlockY * cbGuestPitch;
2617
2618 rc = vmsvgaGMRTransfer(pThis,
2619 transfer,
2620 pDoubleBuffer,
2621 pMipLevel->cbSurface,
2622 offHst,
2623 cbSurfacePitch,
2624 GuestPtr,
2625 offGst,
2626 cbGuestPitch,
2627 cBlocksX * pSurface->cbBlock,
2628 cBlocksY);
2629 AssertRC(rc);
2630
2631 /* Update the opengl surface data. */
2632 if (transfer == SVGA3D_WRITE_HOST_VRAM)
2633 {
2634 GLint activeTexture = 0;
2635 glGetIntegerv(pSurface->bindingGL, &activeTexture);
2636 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2637
2638 /* Must bind texture to the current context in order to change it. */
2639 glBindTexture(pSurface->targetGL, pSurface->oglId.texture);
2640 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2641
2642 LogFunc(("copy texture mipmap level %d (pitch %x)\n", uHostMipmap, pMipLevel->cbSurfacePitch));
2643
2644 /* Set row length and alignment of the input data. */
2645 VMSVGAPACKPARAMS SavedParams;
2646 vmsvga3dOglSetUnpackParams(pState, pContext, pSurface, &SavedParams); /** @todo do we need to set ROW_LENGTH to w here? */
2647
2648 glTexSubImage2D(texImageTarget,
2649 uHostMipmap,
2650 u32HostBlockX,
2651 u32HostBlockY,
2652 cBlocksX,
2653 cBlocksY,
2654 pSurface->formatGL,
2655 pSurface->typeGL,
2656 pDoubleBuffer);
2657 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2658
2659 /* Restore old values. */
2660 vmsvga3dOglRestoreUnpackParams(pState, pContext, pSurface, &SavedParams);
2661
2662 /* Restore the old active texture. */
2663 glBindTexture(pSurface->targetGL, activeTexture);
2664 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2665 }
2666
2667 Log4(("first line:\n%.*Rhxd\n", cBlocksX * pSurface->cbBlock, pDoubleBuffer));
2668
2669 /* Free the double buffer. */
2670 RTMemFree(pDoubleBuffer);
2671 break;
2672 }
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 1
4423 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
4424 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
4425 {
4426 LogFunc(("create depth texture to be used as render target; surface id=%x type=%d format=%d -> create texture\n",
4427 target.sid, pRenderTarget->surfaceFlags, pRenderTarget->format));
4428 int rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
4429 AssertRCReturn(rc, rc);
4430 }
4431
4432 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
4433 Assert(!pRenderTarget->fDirty);
4434
4435 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
4436
4437 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER,
4438 (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT,
4439 GL_TEXTURE_2D, pRenderTarget->oglId.texture, target.mipmap);
4440 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4441#else
4442 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
4443 {
4444 Log(("vmsvga3dSetRenderTarget: create renderbuffer to be used as render target; surface id=%x type=%d format=%d\n", target.sid, pRenderTarget->surfaceFlags, pRenderTarget->internalFormatGL));
4445 pContext = &pState->SharedCtx;
4446 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4447
4448 pState->ext.glGenRenderbuffers(1, &pRenderTarget->oglId.renderbuffer);
4449 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4450
4451 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
4452 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4453
4454 pState->ext.glRenderbufferStorage(GL_RENDERBUFFER,
4455 pRenderTarget->internalFormatGL,
4456 pRenderTarget->pMipmapLevels[0].mipmapSize.width,
4457 pRenderTarget->pMipmapLevels[0].mipmapSize.height);
4458 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4459
4460 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, OPENGL_INVALID_ID);
4461 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4462
4463 pContext = pState->papContexts[cid];
4464 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4465 pRenderTarget->idWeakContextAssociation = cid;
4466 }
4467
4468 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
4469 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4470 Assert(!pRenderTarget->fDirty);
4471 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
4472
4473 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
4474
4475 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
4476 (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT,
4477 GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
4478 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4479#endif
4480 break;
4481
4482 case SVGA3D_RT_COLOR0:
4483 case SVGA3D_RT_COLOR1:
4484 case SVGA3D_RT_COLOR2:
4485 case SVGA3D_RT_COLOR3:
4486 case SVGA3D_RT_COLOR4:
4487 case SVGA3D_RT_COLOR5:
4488 case SVGA3D_RT_COLOR6:
4489 case SVGA3D_RT_COLOR7:
4490 {
4491 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
4492 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
4493 {
4494 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));
4495 int rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
4496 AssertRCReturn(rc, rc);
4497 }
4498
4499 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
4500 Assert(!pRenderTarget->fDirty);
4501
4502 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
4503
4504 GLenum textarget;
4505 if (pRenderTarget->surfaceFlags & SVGA3D_SURFACE_CUBEMAP)
4506 textarget = vmsvga3dCubemapFaceFromIndex(target.face);
4507 else
4508 textarget = GL_TEXTURE_2D;
4509 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0,
4510 textarget, pRenderTarget->oglId.texture, target.mipmap);
4511 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4512
4513#ifdef DEBUG
4514 GLenum status = pState->ext.glCheckFramebufferStatus(GL_FRAMEBUFFER);
4515 if (status != GL_FRAMEBUFFER_COMPLETE)
4516 Log(("vmsvga3dSetRenderTarget: WARNING: glCheckFramebufferStatus returned %x\n", status));
4517#endif
4518 /** @todo use glDrawBuffers too? */
4519 break;
4520 }
4521
4522 default:
4523 AssertFailedReturn(VERR_INVALID_PARAMETER);
4524 }
4525
4526 return VINF_SUCCESS;
4527}
4528
4529#if 0
4530/**
4531 * Convert SVGA texture combiner value to its D3D equivalent
4532 */
4533static DWORD vmsvga3dTextureCombiner2D3D(uint32_t value)
4534{
4535 switch (value)
4536 {
4537 case SVGA3D_TC_DISABLE:
4538 return D3DTOP_DISABLE;
4539 case SVGA3D_TC_SELECTARG1:
4540 return D3DTOP_SELECTARG1;
4541 case SVGA3D_TC_SELECTARG2:
4542 return D3DTOP_SELECTARG2;
4543 case SVGA3D_TC_MODULATE:
4544 return D3DTOP_MODULATE;
4545 case SVGA3D_TC_ADD:
4546 return D3DTOP_ADD;
4547 case SVGA3D_TC_ADDSIGNED:
4548 return D3DTOP_ADDSIGNED;
4549 case SVGA3D_TC_SUBTRACT:
4550 return D3DTOP_SUBTRACT;
4551 case SVGA3D_TC_BLENDTEXTUREALPHA:
4552 return D3DTOP_BLENDTEXTUREALPHA;
4553 case SVGA3D_TC_BLENDDIFFUSEALPHA:
4554 return D3DTOP_BLENDDIFFUSEALPHA;
4555 case SVGA3D_TC_BLENDCURRENTALPHA:
4556 return D3DTOP_BLENDCURRENTALPHA;
4557 case SVGA3D_TC_BLENDFACTORALPHA:
4558 return D3DTOP_BLENDFACTORALPHA;
4559 case SVGA3D_TC_MODULATE2X:
4560 return D3DTOP_MODULATE2X;
4561 case SVGA3D_TC_MODULATE4X:
4562 return D3DTOP_MODULATE4X;
4563 case SVGA3D_TC_DSDT:
4564 AssertFailed(); /** @todo ??? */
4565 return D3DTOP_DISABLE;
4566 case SVGA3D_TC_DOTPRODUCT3:
4567 return D3DTOP_DOTPRODUCT3;
4568 case SVGA3D_TC_BLENDTEXTUREALPHAPM:
4569 return D3DTOP_BLENDTEXTUREALPHAPM;
4570 case SVGA3D_TC_ADDSIGNED2X:
4571 return D3DTOP_ADDSIGNED2X;
4572 case SVGA3D_TC_ADDSMOOTH:
4573 return D3DTOP_ADDSMOOTH;
4574 case SVGA3D_TC_PREMODULATE:
4575 return D3DTOP_PREMODULATE;
4576 case SVGA3D_TC_MODULATEALPHA_ADDCOLOR:
4577 return D3DTOP_MODULATEALPHA_ADDCOLOR;
4578 case SVGA3D_TC_MODULATECOLOR_ADDALPHA:
4579 return D3DTOP_MODULATECOLOR_ADDALPHA;
4580 case SVGA3D_TC_MODULATEINVALPHA_ADDCOLOR:
4581 return D3DTOP_MODULATEINVALPHA_ADDCOLOR;
4582 case SVGA3D_TC_MODULATEINVCOLOR_ADDALPHA:
4583 return D3DTOP_MODULATEINVCOLOR_ADDALPHA;
4584 case SVGA3D_TC_BUMPENVMAPLUMINANCE:
4585 return D3DTOP_BUMPENVMAPLUMINANCE;
4586 case SVGA3D_TC_MULTIPLYADD:
4587 return D3DTOP_MULTIPLYADD;
4588 case SVGA3D_TC_LERP:
4589 return D3DTOP_LERP;
4590 default:
4591 AssertFailed();
4592 return D3DTOP_DISABLE;
4593 }
4594}
4595
4596/**
4597 * Convert SVGA texture arg data value to its D3D equivalent
4598 */
4599static DWORD vmsvga3dTextureArgData2D3D(uint32_t value)
4600{
4601 switch (value)
4602 {
4603 case SVGA3D_TA_CONSTANT:
4604 return D3DTA_CONSTANT;
4605 case SVGA3D_TA_PREVIOUS:
4606 return D3DTA_CURRENT; /* current = previous */
4607 case SVGA3D_TA_DIFFUSE:
4608 return D3DTA_DIFFUSE;
4609 case SVGA3D_TA_TEXTURE:
4610 return D3DTA_TEXTURE;
4611 case SVGA3D_TA_SPECULAR:
4612 return D3DTA_SPECULAR;
4613 default:
4614 AssertFailed();
4615 return 0;
4616 }
4617}
4618
4619/**
4620 * Convert SVGA texture transform flag value to its D3D equivalent
4621 */
4622static DWORD vmsvga3dTextTransformFlags2D3D(uint32_t value)
4623{
4624 switch (value)
4625 {
4626 case SVGA3D_TEX_TRANSFORM_OFF:
4627 return D3DTTFF_DISABLE;
4628 case SVGA3D_TEX_TRANSFORM_S:
4629 return D3DTTFF_COUNT1; /** @todo correct? */
4630 case SVGA3D_TEX_TRANSFORM_T:
4631 return D3DTTFF_COUNT2; /** @todo correct? */
4632 case SVGA3D_TEX_TRANSFORM_R:
4633 return D3DTTFF_COUNT3; /** @todo correct? */
4634 case SVGA3D_TEX_TRANSFORM_Q:
4635 return D3DTTFF_COUNT4; /** @todo correct? */
4636 case SVGA3D_TEX_PROJECTED:
4637 return D3DTTFF_PROJECTED;
4638 default:
4639 AssertFailed();
4640 return 0;
4641 }
4642}
4643#endif
4644
4645static GLenum vmsvga3dTextureAddress2OGL(SVGA3dTextureAddress value)
4646{
4647 switch (value)
4648 {
4649 case SVGA3D_TEX_ADDRESS_WRAP:
4650 return GL_REPEAT;
4651 case SVGA3D_TEX_ADDRESS_MIRROR:
4652 return GL_MIRRORED_REPEAT;
4653 case SVGA3D_TEX_ADDRESS_CLAMP:
4654 return GL_CLAMP_TO_EDGE;
4655 case SVGA3D_TEX_ADDRESS_BORDER:
4656 return GL_CLAMP_TO_BORDER;
4657 case SVGA3D_TEX_ADDRESS_MIRRORONCE:
4658 AssertFailed();
4659 return GL_CLAMP_TO_EDGE_SGIS; /** @todo correct? */
4660
4661 case SVGA3D_TEX_ADDRESS_EDGE:
4662 case SVGA3D_TEX_ADDRESS_INVALID:
4663 default:
4664 AssertFailed();
4665 return GL_REPEAT; /* default */
4666 }
4667}
4668
4669static GLenum vmsvga3dTextureFilter2OGL(SVGA3dTextureFilter value)
4670{
4671 switch (value)
4672 {
4673 case SVGA3D_TEX_FILTER_NONE:
4674 case SVGA3D_TEX_FILTER_LINEAR:
4675 return GL_LINEAR;
4676 case SVGA3D_TEX_FILTER_NEAREST:
4677 return GL_NEAREST;
4678 case SVGA3D_TEX_FILTER_ANISOTROPIC:
4679 /** @todo */
4680 case SVGA3D_TEX_FILTER_FLATCUBIC: // Deprecated, not implemented
4681 case SVGA3D_TEX_FILTER_GAUSSIANCUBIC: // Deprecated, not implemented
4682 case SVGA3D_TEX_FILTER_PYRAMIDALQUAD: // Not currently implemented
4683 case SVGA3D_TEX_FILTER_GAUSSIANQUAD: // Not currently implemented
4684 default:
4685 AssertFailed();
4686 return GL_LINEAR; /* default */
4687 }
4688}
4689
4690uint32_t vmsvga3dSVGA3dColor2RGBA(SVGA3dColor value)
4691{
4692 /* flip the red and blue bytes */
4693 uint8_t blue = value & 0xff;
4694 uint8_t red = (value >> 16) & 0xff;
4695 return (value & 0xff00ff00) | red | (blue << 16);
4696}
4697
4698int vmsvga3dSetTextureState(PVGASTATE pThis, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
4699{
4700 GLenum val = ~(GLenum)0; /* Shut up MSC. */
4701 GLenum currentStage = ~(GLenum)0;
4702 PVMSVGA3DCONTEXT pContext;
4703 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4704 AssertReturn(pState, VERR_NO_MEMORY);
4705
4706 Log(("vmsvga3dSetTextureState %x cTextureState=%d\n", cid, cTextureStates));
4707
4708 if ( cid >= pState->cContexts
4709 || pState->papContexts[cid]->id != cid)
4710 {
4711 Log(("vmsvga3dSetTextureState invalid context id!\n"));
4712 return VERR_INVALID_PARAMETER;
4713 }
4714 pContext = pState->papContexts[cid];
4715 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4716
4717 /* Which texture is active for the current stage. Needed to use right OpenGL target when setting parameters. */
4718 PVMSVGA3DSURFACE pCurrentTextureSurface = NULL;
4719
4720 for (uint32_t i = 0; i < cTextureStates; ++i)
4721 {
4722 GLenum textureType = ~(GLenum)0;
4723#if 0
4724 GLenum samplerType = ~(GLenum)0;
4725#endif
4726
4727 LogFunc(("cid=%x stage=%d type=%s (%x) val=%x\n",
4728 cid, pTextureState[i].stage, vmsvga3dTextureStateToString(pTextureState[i].name), pTextureState[i].name, pTextureState[i].value));
4729
4730 /* Record the texture state for vm state saving. */
4731 if ( pTextureState[i].stage < RT_ELEMENTS(pContext->state.aTextureStates)
4732 && pTextureState[i].name < RT_ELEMENTS(pContext->state.aTextureStates[0]))
4733 {
4734 pContext->state.aTextureStates[pTextureState[i].stage][pTextureState[i].name] = pTextureState[i];
4735 }
4736
4737 /* Activate the right texture unit for subsequent texture state changes. */
4738 if (pTextureState[i].stage != currentStage || i == 0)
4739 {
4740 /** @todo Is this the appropriate limit for all kinds of textures? It is the
4741 * size of aSidActiveTextures and for binding/unbinding we cannot exceed it. */
4742 if (pTextureState[i].stage < RT_ELEMENTS(pContext->state.aTextureStates))
4743 {
4744 pState->ext.glActiveTexture(GL_TEXTURE0 + pTextureState[i].stage);
4745 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4746 currentStage = pTextureState[i].stage;
4747 }
4748 else
4749 {
4750 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x\n", i, pTextureState[i].stage, pTextureState[i].name));
4751 continue;
4752 }
4753
4754 if (pContext->aSidActiveTextures[currentStage] != SVGA3D_INVALID_ID)
4755 {
4756 int rc = vmsvga3dSurfaceFromSid(pState, pContext->aSidActiveTextures[currentStage], &pCurrentTextureSurface);
4757 AssertRCReturn(rc, rc);
4758 }
4759 else
4760 pCurrentTextureSurface = NULL; /* Make sure that no stale pointer is used. */
4761 }
4762
4763 switch (pTextureState[i].name)
4764 {
4765 case SVGA3D_TS_BUMPENVMAT00: /* float */
4766 case SVGA3D_TS_BUMPENVMAT01: /* float */
4767 case SVGA3D_TS_BUMPENVMAT10: /* float */
4768 case SVGA3D_TS_BUMPENVMAT11: /* float */
4769 case SVGA3D_TS_BUMPENVLSCALE: /* float */
4770 case SVGA3D_TS_BUMPENVLOFFSET: /* float */
4771 Log(("vmsvga3dSetTextureState: bump mapping texture options not supported!!\n"));
4772 break;
4773
4774 case SVGA3D_TS_COLOROP: /* SVGA3dTextureCombiner */
4775 case SVGA3D_TS_COLORARG0: /* SVGA3dTextureArgData */
4776 case SVGA3D_TS_COLORARG1: /* SVGA3dTextureArgData */
4777 case SVGA3D_TS_COLORARG2: /* SVGA3dTextureArgData */
4778 case SVGA3D_TS_ALPHAOP: /* SVGA3dTextureCombiner */
4779 case SVGA3D_TS_ALPHAARG0: /* SVGA3dTextureArgData */
4780 case SVGA3D_TS_ALPHAARG1: /* SVGA3dTextureArgData */
4781 case SVGA3D_TS_ALPHAARG2: /* SVGA3dTextureArgData */
4782 /** @todo not used by MesaGL */
4783 Log(("vmsvga3dSetTextureState: colorop/alphaop not yet supported!!\n"));
4784 break;
4785#if 0
4786
4787 case SVGA3D_TS_TEXCOORDINDEX: /* uint32_t */
4788 textureType = D3DTSS_TEXCOORDINDEX;
4789 val = pTextureState[i].value;
4790 break;
4791
4792 case SVGA3D_TS_TEXTURETRANSFORMFLAGS: /* SVGA3dTexTransformFlags */
4793 textureType = D3DTSS_TEXTURETRANSFORMFLAGS;
4794 val = vmsvga3dTextTransformFlags2D3D(pTextureState[i].value);
4795 break;
4796#endif
4797
4798 case SVGA3D_TS_BIND_TEXTURE: /* SVGA3dSurfaceId */
4799 {
4800 uint32_t const sid = pTextureState[i].value;
4801
4802 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture sid=%x replacing sid=%x\n",
4803 currentStage, sid, pContext->aSidActiveTextures[currentStage]));
4804
4805 /* Only is texture actually changed. */ /// @todo needs testing.
4806 if (pContext->aSidActiveTextures[currentStage] != sid)
4807 {
4808 if (pCurrentTextureSurface)
4809 {
4810 /* Unselect the currently associated texture. */
4811 glBindTexture(pCurrentTextureSurface->targetGL, 0);
4812 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4813
4814 /* Necessary for the fixed pipeline. */
4815 glDisable(pCurrentTextureSurface->targetGL);
4816 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4817
4818 pCurrentTextureSurface = NULL;
4819 }
4820
4821 if (sid == SVGA3D_INVALID_ID)
4822 {
4823 Assert(pCurrentTextureSurface == NULL);
4824 }
4825 else
4826 {
4827 PVMSVGA3DSURFACE pSurface;
4828 int rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
4829 AssertRCReturn(rc, rc);
4830
4831 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture sid=%x (%d,%d) replacing sid=%x\n",
4832 currentStage, sid, pSurface->pMipmapLevels[0].mipmapSize.width,
4833 pSurface->pMipmapLevels[0].mipmapSize.height, pContext->aSidActiveTextures[currentStage]));
4834
4835 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
4836 {
4837 Log(("CreateTexture (%d,%d) levels=%d\n",
4838 pSurface->pMipmapLevels[0].mipmapSize.width, pSurface->pMipmapLevels[0].mipmapSize.height, pSurface->faces[0].numMipLevels));
4839 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pSurface);
4840 AssertRCReturn(rc, rc);
4841 }
4842
4843 glBindTexture(pSurface->targetGL, pSurface->oglId.texture);
4844 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4845
4846 /* Necessary for the fixed pipeline. */
4847 glEnable(pSurface->targetGL);
4848 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4849
4850 /* Remember the currently active texture. */
4851 pCurrentTextureSurface = pSurface;
4852
4853 /* Recreate the texture state as glBindTexture resets them all (sigh). */
4854 for (uint32_t iStage = 0; iStage < RT_ELEMENTS(pContext->state.aTextureStates); iStage++)
4855 {
4856 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aTextureStates[0]); j++)
4857 {
4858 SVGA3dTextureState *pTextureStateIter = &pContext->state.aTextureStates[iStage][j];
4859
4860 if ( pTextureStateIter->name != SVGA3D_TS_INVALID
4861 && pTextureStateIter->name != SVGA3D_TS_BIND_TEXTURE)
4862 vmsvga3dSetTextureState(pThis, pContext->id, 1, pTextureStateIter);
4863 }
4864 }
4865 }
4866
4867 pContext->aSidActiveTextures[currentStage] = sid;
4868 }
4869
4870 /* Finished; continue with the next one. */
4871 continue;
4872 }
4873
4874 case SVGA3D_TS_ADDRESSW: /* SVGA3dTextureAddress */
4875 textureType = GL_TEXTURE_WRAP_R; /* R = W */
4876 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
4877 break;
4878
4879 case SVGA3D_TS_ADDRESSU: /* SVGA3dTextureAddress */
4880 textureType = GL_TEXTURE_WRAP_S; /* S = U */
4881 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
4882 break;
4883
4884 case SVGA3D_TS_ADDRESSV: /* SVGA3dTextureAddress */
4885 textureType = GL_TEXTURE_WRAP_T; /* T = V */
4886 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
4887 break;
4888
4889 case SVGA3D_TS_MIPFILTER: /* SVGA3dTextureFilter */
4890 case SVGA3D_TS_MINFILTER: /* SVGA3dTextureFilter */
4891 {
4892 uint32_t mipFilter = pContext->state.aTextureStates[currentStage][SVGA3D_TS_MIPFILTER].value;
4893 uint32_t minFilter = pContext->state.aTextureStates[currentStage][SVGA3D_TS_MINFILTER].value;
4894
4895 /* If SVGA3D_TS_MIPFILTER is set to NONE, then use SVGA3D_TS_MIPFILTER, otherwise SVGA3D_TS_MIPFILTER enables mipmap minification. */
4896 textureType = GL_TEXTURE_MIN_FILTER;
4897 if (mipFilter != SVGA3D_TEX_FILTER_NONE)
4898 {
4899 if (minFilter == SVGA3D_TEX_FILTER_NEAREST)
4900 {
4901 if (mipFilter == SVGA3D_TEX_FILTER_LINEAR)
4902 val = GL_NEAREST_MIPMAP_LINEAR;
4903 else
4904 val = GL_NEAREST_MIPMAP_NEAREST;
4905 }
4906 else
4907 {
4908 if (mipFilter == SVGA3D_TEX_FILTER_LINEAR)
4909 val = GL_LINEAR_MIPMAP_LINEAR;
4910 else
4911 val = GL_LINEAR_MIPMAP_NEAREST;
4912 }
4913 }
4914 else
4915 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)minFilter);
4916 break;
4917 }
4918
4919 case SVGA3D_TS_MAGFILTER: /* SVGA3dTextureFilter */
4920 textureType = GL_TEXTURE_MAG_FILTER;
4921 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)pTextureState[i].value);
4922 Assert(val == GL_NEAREST || val == GL_LINEAR);
4923 break;
4924
4925 case SVGA3D_TS_BORDERCOLOR: /* SVGA3dColor */
4926 {
4927 GLfloat color[4]; /* red, green, blue, alpha */
4928 vmsvgaColor2GLFloatArray(pTextureState[i].value, &color[0], &color[1], &color[2], &color[3]);
4929
4930 GLenum targetGL;
4931 if (pCurrentTextureSurface)
4932 targetGL = pCurrentTextureSurface->targetGL;
4933 else
4934 {
4935 /* No texture bound, assume 2D. */
4936 targetGL = GL_TEXTURE_2D;
4937 }
4938
4939 glTexParameterfv(targetGL, GL_TEXTURE_BORDER_COLOR, color); /* Identical; default 0.0 identical too */
4940 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4941 break;
4942 }
4943
4944 case SVGA3D_TS_TEXTURE_LOD_BIAS: /* float */
4945 {
4946 GLenum targetGL;
4947 if (pCurrentTextureSurface)
4948 targetGL = pCurrentTextureSurface->targetGL;
4949 else
4950 {
4951 /* No texture bound, assume 2D. */
4952 targetGL = GL_TEXTURE_2D;
4953 }
4954
4955 glTexParameterf(targetGL, GL_TEXTURE_LOD_BIAS, pTextureState[i].floatValue); /* Identical; default 0.0 identical too */
4956 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4957 break;
4958 }
4959
4960 case SVGA3D_TS_TEXTURE_MIPMAP_LEVEL: /* uint32_t */
4961 textureType = GL_TEXTURE_BASE_LEVEL;
4962 val = pTextureState[i].value;
4963 break;
4964
4965#if 0
4966 case SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL: /* uint32_t */
4967 samplerType = D3DSAMP_MAXANISOTROPY;
4968 val = pTextureState[i].value; /* Identical?? */
4969 break;
4970
4971 case SVGA3D_TS_GAMMA: /* float */
4972 samplerType = D3DSAMP_SRGBTEXTURE;
4973 /* Boolean in D3D */
4974 if (pTextureState[i].floatValue == 1.0f)
4975 val = FALSE;
4976 else
4977 val = TRUE;
4978 break;
4979#endif
4980 /* Internal commands, that don't map directly to the SetTextureStageState API. */
4981 case SVGA3D_TS_TEXCOORDGEN: /* SVGA3dTextureCoordGen */
4982 AssertFailed();
4983 break;
4984
4985 default:
4986 //AssertFailed();
4987 break;
4988 }
4989
4990 if (textureType != ~(GLenum)0)
4991 {
4992 GLenum targetGL;
4993 if (pCurrentTextureSurface)
4994 targetGL = pCurrentTextureSurface->targetGL;
4995 else
4996 {
4997 /* No texture bound, assume 2D. */
4998 targetGL = GL_TEXTURE_2D;
4999 }
5000
5001 glTexParameteri(targetGL, textureType, val);
5002 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5003 }
5004 }
5005
5006 return VINF_SUCCESS;
5007}
5008
5009int vmsvga3dSetMaterial(PVGASTATE pThis, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
5010{
5011 PVMSVGA3DCONTEXT pContext;
5012 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5013 AssertReturn(pState, VERR_NO_MEMORY);
5014 GLenum oglFace;
5015
5016 Log(("vmsvga3dSetMaterial cid=%x face %d\n", cid, face));
5017
5018 if ( cid >= pState->cContexts
5019 || pState->papContexts[cid]->id != cid)
5020 {
5021 Log(("vmsvga3dSetMaterial invalid context id!\n"));
5022 return VERR_INVALID_PARAMETER;
5023 }
5024 pContext = pState->papContexts[cid];
5025 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5026
5027 switch (face)
5028 {
5029 case SVGA3D_FACE_NONE:
5030 case SVGA3D_FACE_FRONT:
5031 oglFace = GL_FRONT;
5032 break;
5033
5034 case SVGA3D_FACE_BACK:
5035 oglFace = GL_BACK;
5036 break;
5037
5038 case SVGA3D_FACE_FRONT_BACK:
5039 oglFace = GL_FRONT_AND_BACK;
5040 break;
5041
5042 default:
5043 AssertFailedReturn(VERR_INVALID_PARAMETER);
5044 }
5045
5046 /* Save for vm state save/restore. */
5047 pContext->state.aMaterial[face].fValid = true;
5048 pContext->state.aMaterial[face].material = *pMaterial;
5049 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_MATERIAL;
5050
5051 glMaterialfv(oglFace, GL_DIFFUSE, pMaterial->diffuse);
5052 glMaterialfv(oglFace, GL_AMBIENT, pMaterial->ambient);
5053 glMaterialfv(oglFace, GL_SPECULAR, pMaterial->specular);
5054 glMaterialfv(oglFace, GL_EMISSION, pMaterial->emissive);
5055 glMaterialfv(oglFace, GL_SHININESS, &pMaterial->shininess);
5056 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5057
5058 return VINF_SUCCESS;
5059}
5060
5061/** @todo Move into separate library as we are using logic from Wine here. */
5062int vmsvga3dSetLightData(PVGASTATE pThis, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
5063{
5064 PVMSVGA3DCONTEXT pContext;
5065 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5066 AssertReturn(pState, VERR_NO_MEMORY);
5067 float QuadAttenuation;
5068
5069 Log(("vmsvga3dSetLightData cid=%x index=%d type=%d\n", cid, index, pData->type));
5070
5071 if ( cid >= pState->cContexts
5072 || pState->papContexts[cid]->id != cid)
5073 {
5074 Log(("vmsvga3dSetLightData invalid context id!\n"));
5075 return VERR_INVALID_PARAMETER;
5076 }
5077 pContext = pState->papContexts[cid];
5078 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5079
5080 /* Store for vm state save/restore */
5081 if (index < SVGA3D_MAX_LIGHTS)
5082 {
5083 pContext->state.aLightData[index].fValidData = true;
5084 pContext->state.aLightData[index].data = *pData;
5085 }
5086 else
5087 AssertFailed();
5088
5089 if ( pData->attenuation0 < 0.0f
5090 || pData->attenuation1 < 0.0f
5091 || pData->attenuation2 < 0.0f)
5092 {
5093 Log(("vmsvga3dSetLightData: invalid negative attenuation values!!\n"));
5094 return VINF_SUCCESS; /* ignore; could crash the GL driver */
5095 }
5096
5097 /* Light settings are affected by the model view in OpenGL, the View transform in direct3d */
5098 glMatrixMode(GL_MODELVIEW);
5099 glPushMatrix();
5100 glLoadMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
5101
5102 glLightfv(GL_LIGHT0 + index, GL_DIFFUSE, pData->diffuse);
5103 glLightfv(GL_LIGHT0 + index, GL_SPECULAR, pData->specular);
5104 glLightfv(GL_LIGHT0 + index, GL_AMBIENT, pData->ambient);
5105
5106 if (pData->range * pData->range >= FLT_MIN)
5107 QuadAttenuation = 1.4f / (pData->range * pData->range);
5108 else
5109 QuadAttenuation = 0.0f;
5110
5111 switch (pData->type)
5112 {
5113 case SVGA3D_LIGHTTYPE_POINT:
5114 {
5115 GLfloat position[4];
5116
5117 position[0] = pData->position[0];
5118 position[1] = pData->position[1];
5119 position[2] = pData->position[2];
5120 position[3] = 1.0f;
5121
5122 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
5123 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5124
5125 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
5126 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5127
5128 /* Attenuation - Are these right? guessing... */
5129 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
5130 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5131
5132 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
5133 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5134
5135 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
5136 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5137
5138 /** @todo range */
5139 break;
5140 }
5141
5142 case SVGA3D_LIGHTTYPE_SPOT1:
5143 {
5144 GLfloat exponent;
5145 GLfloat position[4];
5146 const GLfloat pi = 4.0f * atanf(1.0f);
5147
5148 position[0] = pData->position[0];
5149 position[1] = pData->position[1];
5150 position[2] = pData->position[2];
5151 position[3] = 1.0f;
5152
5153 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
5154 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5155
5156 position[0] = pData->direction[0];
5157 position[1] = pData->direction[1];
5158 position[2] = pData->direction[2];
5159 position[3] = 1.0f;
5160
5161 glLightfv(GL_LIGHT0 + index, GL_SPOT_DIRECTION, position);
5162 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5163
5164 /*
5165 * opengl-ish and d3d-ish spot lights use too different models for the
5166 * light "intensity" as a function of the angle towards the main light direction,
5167 * so we only can approximate very roughly.
5168 * however spot lights are rather rarely used in games (if ever used at all).
5169 * furthermore if still used, probably nobody pays attention to such details.
5170 */
5171 if (pData->falloff == 0)
5172 {
5173 /* Falloff = 0 is easy, because d3d's and opengl's spot light equations have the
5174 * falloff resp. exponent parameter as an exponent, so the spot light lighting
5175 * will always be 1.0 for both of them, and we don't have to care for the
5176 * rest of the rather complex calculation
5177 */
5178 exponent = 0.0f;
5179 }
5180 else
5181 {
5182 float rho = pData->theta + (pData->phi - pData->theta) / (2 * pData->falloff);
5183 if (rho < 0.0001f)
5184 rho = 0.0001f;
5185 exponent = -0.3f/log(cos(rho/2));
5186 }
5187 if (exponent > 128.0f)
5188 exponent = 128.0f;
5189
5190 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, exponent);
5191 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5192
5193 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, pData->phi * 90.0 / pi);
5194 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5195
5196 /* Attenuation - Are these right? guessing... */
5197 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
5198 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5199
5200 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
5201 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5202
5203 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
5204 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5205
5206 /** @todo range */
5207 break;
5208 }
5209
5210 case SVGA3D_LIGHTTYPE_DIRECTIONAL:
5211 {
5212 GLfloat position[4];
5213
5214 position[0] = -pData->direction[0];
5215 position[1] = -pData->direction[1];
5216 position[2] = -pData->direction[2];
5217 position[3] = 0.0f;
5218
5219 glLightfv(GL_LIGHT0 + index, GL_POSITION, position); /* Note gl uses w position of 0 for direction! */
5220 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5221
5222 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
5223 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5224
5225 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, 0.0f);
5226 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5227 break;
5228 }
5229
5230 case SVGA3D_LIGHTTYPE_SPOT2:
5231 default:
5232 Log(("Unsupported light type!!\n"));
5233 return VERR_INVALID_PARAMETER;
5234 }
5235
5236 /* Restore the modelview matrix */
5237 glPopMatrix();
5238
5239 return VINF_SUCCESS;
5240}
5241
5242int vmsvga3dSetLightEnabled(PVGASTATE pThis, uint32_t cid, uint32_t index, uint32_t enabled)
5243{
5244 PVMSVGA3DCONTEXT pContext;
5245 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5246 AssertReturn(pState, VERR_NO_MEMORY);
5247
5248 Log(("vmsvga3dSetLightEnabled cid=%x %d -> %d\n", cid, index, enabled));
5249
5250 if ( cid >= pState->cContexts
5251 || pState->papContexts[cid]->id != cid)
5252 {
5253 Log(("vmsvga3dSetLightEnabled invalid context id!\n"));
5254 return VERR_INVALID_PARAMETER;
5255 }
5256 pContext = pState->papContexts[cid];
5257 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5258
5259 /* Store for vm state save/restore */
5260 if (index < SVGA3D_MAX_LIGHTS)
5261 pContext->state.aLightData[index].fEnabled = !!enabled;
5262 else
5263 AssertFailed();
5264
5265 if (enabled)
5266 {
5267 /* Load the default settings if none have been set yet. */
5268 if (!pContext->state.aLightData[index].fValidData)
5269 vmsvga3dSetLightData(pThis, cid, index, (SVGA3dLightData *)&vmsvga3d_default_light);
5270 glEnable(GL_LIGHT0 + index);
5271 }
5272 else
5273 glDisable(GL_LIGHT0 + index);
5274
5275 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5276 return VINF_SUCCESS;
5277}
5278
5279int vmsvga3dSetViewPort(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
5280{
5281 PVMSVGA3DCONTEXT pContext;
5282 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5283 AssertReturn(pState, VERR_NO_MEMORY);
5284
5285 Log(("vmsvga3dSetViewPort cid=%x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
5286
5287 if ( cid >= pState->cContexts
5288 || pState->papContexts[cid]->id != cid)
5289 {
5290 Log(("vmsvga3dSetViewPort invalid context id!\n"));
5291 return VERR_INVALID_PARAMETER;
5292 }
5293 pContext = pState->papContexts[cid];
5294 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5295
5296 /* Save for vm state save/restore. */
5297 pContext->state.RectViewPort = *pRect;
5298 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VIEWPORT;
5299
5300 /** @todo y-inversion for partial viewport coordinates? */
5301 glViewport(pRect->x, pRect->y, pRect->w, pRect->h);
5302 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5303
5304 /* Reset the projection matrix as that relies on the viewport setting. */
5305 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].fValid == true)
5306 {
5307 vmsvga3dSetTransform(pThis, cid, SVGA3D_TRANSFORM_PROJECTION, pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].matrix);
5308 }
5309 else
5310 {
5311 float matrix[16];
5312
5313 /* identity matrix if no matrix set. */
5314 memset(matrix, 0, sizeof(matrix));
5315 matrix[0] = 1.0;
5316 matrix[5] = 1.0;
5317 matrix[10] = 1.0;
5318 matrix[15] = 1.0;
5319 vmsvga3dSetTransform(pThis, cid, SVGA3D_TRANSFORM_PROJECTION, matrix);
5320 }
5321
5322 return VINF_SUCCESS;
5323}
5324
5325int vmsvga3dSetClipPlane(PVGASTATE pThis, uint32_t cid, uint32_t index, float plane[4])
5326{
5327 PVMSVGA3DCONTEXT pContext;
5328 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5329 AssertReturn(pState, VERR_NO_MEMORY);
5330 double oglPlane[4];
5331
5332 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)));
5333 AssertReturn(index < SVGA3D_CLIPPLANE_MAX, VERR_INVALID_PARAMETER);
5334
5335 if ( cid >= pState->cContexts
5336 || pState->papContexts[cid]->id != cid)
5337 {
5338 Log(("vmsvga3dSetClipPlane invalid context id!\n"));
5339 return VERR_INVALID_PARAMETER;
5340 }
5341 pContext = pState->papContexts[cid];
5342 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5343
5344 /* Store for vm state save/restore. */
5345 pContext->state.aClipPlane[index].fValid = true;
5346 memcpy(pContext->state.aClipPlane[index].plane, plane, sizeof(pContext->state.aClipPlane[index].plane));
5347
5348 /** @todo clip plane affected by model view in OpenGL & view in D3D + vertex shader -> not transformed (see Wine; state.c clipplane) */
5349 oglPlane[0] = (double)plane[0];
5350 oglPlane[1] = (double)plane[1];
5351 oglPlane[2] = (double)plane[2];
5352 oglPlane[3] = (double)plane[3];
5353
5354 glClipPlane(GL_CLIP_PLANE0 + index, oglPlane);
5355 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5356
5357 return VINF_SUCCESS;
5358}
5359
5360int vmsvga3dSetScissorRect(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
5361{
5362 PVMSVGA3DCONTEXT pContext;
5363 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5364 AssertReturn(pState, VERR_NO_MEMORY);
5365
5366 Log(("vmsvga3dSetScissorRect cid=%x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
5367
5368 if ( cid >= pState->cContexts
5369 || pState->papContexts[cid]->id != cid)
5370 {
5371 Log(("vmsvga3dSetScissorRect invalid context id!\n"));
5372 return VERR_INVALID_PARAMETER;
5373 }
5374 pContext = pState->papContexts[cid];
5375 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5376
5377 /* Store for vm state save/restore. */
5378 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_SCISSORRECT;
5379 pContext->state.RectScissor = *pRect;
5380
5381 glScissor(pRect->x, pRect->y, pRect->w, pRect->h);
5382 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5383
5384 return VINF_SUCCESS;
5385}
5386
5387static void vmsvgaColor2GLFloatArray(uint32_t color, GLfloat *pRed, GLfloat *pGreen, GLfloat *pBlue, GLfloat *pAlpha)
5388{
5389 /* Convert byte color components to float (0-1.0) */
5390 *pAlpha = (GLfloat)(color >> 24) / 255.0;
5391 *pRed = (GLfloat)((color >> 16) & 0xff) / 255.0;
5392 *pGreen = (GLfloat)((color >> 8) & 0xff) / 255.0;
5393 *pBlue = (GLfloat)(color & 0xff) / 255.0;
5394}
5395
5396int vmsvga3dCommandClear(PVGASTATE pThis, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth, uint32_t stencil,
5397 uint32_t cRects, SVGA3dRect *pRect)
5398{
5399 GLbitfield mask = 0;
5400 PVMSVGA3DCONTEXT pContext;
5401 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5402 AssertReturn(pState, VERR_NO_MEMORY);
5403 GLboolean fDepthWriteEnabled = GL_FALSE;
5404
5405 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));
5406
5407 if ( cid >= pState->cContexts
5408 || pState->papContexts[cid]->id != cid)
5409 {
5410 Log(("vmsvga3dCommandClear invalid context id!\n"));
5411 return VERR_INVALID_PARAMETER;
5412 }
5413 pContext = pState->papContexts[cid];
5414 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5415
5416 if (clearFlag & SVGA3D_CLEAR_COLOR)
5417 {
5418 GLfloat red, green, blue, alpha;
5419
5420 vmsvgaColor2GLFloatArray(color, &red, &green, &blue, &alpha);
5421
5422 /* Set the color clear value. */
5423 glClearColor(red, green, blue, alpha);
5424
5425 mask |= GL_COLOR_BUFFER_BIT;
5426 }
5427 if (clearFlag & SVGA3D_CLEAR_STENCIL)
5428 {
5429 /** @todo possibly the same problem as with glDepthMask */
5430 glClearStencil(stencil);
5431 mask |= GL_STENCIL_BUFFER_BIT;
5432 }
5433 if (clearFlag & SVGA3D_CLEAR_DEPTH)
5434 {
5435 glClearDepth((GLdouble)depth);
5436 mask |= GL_DEPTH_BUFFER_BIT;
5437
5438 /* glClear will not clear the depth buffer if writing is disabled. */
5439 glGetBooleanv(GL_DEPTH_WRITEMASK, &fDepthWriteEnabled);
5440 if (fDepthWriteEnabled == GL_FALSE)
5441 glDepthMask(GL_TRUE);
5442 }
5443
5444 if (cRects)
5445 {
5446 /* Save the current scissor test bit and scissor box. */
5447 glPushAttrib(GL_SCISSOR_BIT);
5448 glEnable(GL_SCISSOR_TEST);
5449 for (unsigned i=0; i < cRects; i++)
5450 {
5451 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));
5452 glScissor(pRect[i].x, pRect[i].y, pRect[i].w, pRect[i].h);
5453 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5454 glClear(mask);
5455 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5456 }
5457 /* Restore the old scissor test bit and box */
5458 glPopAttrib();
5459 }
5460 else
5461 {
5462 glClear(mask);
5463 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5464 }
5465
5466 /* Restore depth write state. */
5467 if ( (clearFlag & SVGA3D_CLEAR_DEPTH)
5468 && fDepthWriteEnabled == GL_FALSE)
5469 glDepthMask(GL_FALSE);
5470
5471 return VINF_SUCCESS;
5472}
5473
5474/* Convert VMWare vertex declaration to its OpenGL equivalent. */
5475int vmsvga3dVertexDecl2OGL(SVGA3dVertexArrayIdentity &identity, GLint &size, GLenum &type, GLboolean &normalized)
5476{
5477 normalized = GL_FALSE;
5478 switch (identity.type)
5479 {
5480 case SVGA3D_DECLTYPE_FLOAT1:
5481 size = 1;
5482 type = GL_FLOAT;
5483 break;
5484 case SVGA3D_DECLTYPE_FLOAT2:
5485 size = 2;
5486 type = GL_FLOAT;
5487 break;
5488 case SVGA3D_DECLTYPE_FLOAT3:
5489 size = 3;
5490 type = GL_FLOAT;
5491 break;
5492 case SVGA3D_DECLTYPE_FLOAT4:
5493 size = 4;
5494 type = GL_FLOAT;
5495 break;
5496
5497 case SVGA3D_DECLTYPE_D3DCOLOR:
5498 size = GL_BGRA; /* @note requires GL_ARB_vertex_array_bgra */
5499 type = GL_UNSIGNED_BYTE;
5500 normalized = GL_TRUE; /* glVertexAttribPointer fails otherwise */
5501 break;
5502
5503 case SVGA3D_DECLTYPE_UBYTE4N:
5504 normalized = GL_TRUE;
5505 RT_FALL_THRU();
5506 case SVGA3D_DECLTYPE_UBYTE4:
5507 size = 4;
5508 type = GL_UNSIGNED_BYTE;
5509 break;
5510
5511 case SVGA3D_DECLTYPE_SHORT2N:
5512 normalized = GL_TRUE;
5513 RT_FALL_THRU();
5514 case SVGA3D_DECLTYPE_SHORT2:
5515 size = 2;
5516 type = GL_SHORT;
5517 break;
5518
5519 case SVGA3D_DECLTYPE_SHORT4N:
5520 normalized = GL_TRUE;
5521 RT_FALL_THRU();
5522 case SVGA3D_DECLTYPE_SHORT4:
5523 size = 4;
5524 type = GL_SHORT;
5525 break;
5526
5527 case SVGA3D_DECLTYPE_USHORT4N:
5528 normalized = GL_TRUE;
5529 size = 4;
5530 type = GL_UNSIGNED_SHORT;
5531 break;
5532
5533 case SVGA3D_DECLTYPE_USHORT2N:
5534 normalized = GL_TRUE;
5535 size = 2;
5536 type = GL_UNSIGNED_SHORT;
5537 break;
5538
5539 case SVGA3D_DECLTYPE_UDEC3:
5540 size = 3;
5541 type = GL_UNSIGNED_INT_2_10_10_10_REV; /** @todo correct? */
5542 break;
5543
5544 case SVGA3D_DECLTYPE_DEC3N:
5545 normalized = true;
5546 size = 3;
5547 type = GL_INT_2_10_10_10_REV; /** @todo correct? */
5548 break;
5549
5550 case SVGA3D_DECLTYPE_FLOAT16_2:
5551 size = 2;
5552 type = GL_HALF_FLOAT;
5553 break;
5554 case SVGA3D_DECLTYPE_FLOAT16_4:
5555 size = 4;
5556 type = GL_HALF_FLOAT;
5557 break;
5558 default:
5559 AssertFailedReturn(VERR_INVALID_PARAMETER);
5560 }
5561
5562 //pVertexElement->Method = identity.method;
5563 //pVertexElement->Usage = identity.usage;
5564
5565 return VINF_SUCCESS;
5566}
5567
5568/* Convert VMWare primitive type to its OpenGL equivalent. */
5569/* Calculate the vertex count based on the primitive type and nr of primitives. */
5570int vmsvga3dPrimitiveType2OGL(SVGA3dPrimitiveType PrimitiveType, GLenum *pMode, uint32_t cPrimitiveCount, uint32_t *pcVertices)
5571{
5572 switch (PrimitiveType)
5573 {
5574 case SVGA3D_PRIMITIVE_TRIANGLELIST:
5575 *pMode = GL_TRIANGLES;
5576 *pcVertices = cPrimitiveCount * 3;
5577 break;
5578 case SVGA3D_PRIMITIVE_POINTLIST:
5579 *pMode = GL_POINTS;
5580 *pcVertices = cPrimitiveCount;
5581 break;
5582 case SVGA3D_PRIMITIVE_LINELIST:
5583 *pMode = GL_LINES;
5584 *pcVertices = cPrimitiveCount * 2;
5585 break;
5586 case SVGA3D_PRIMITIVE_LINESTRIP:
5587 *pMode = GL_LINE_STRIP;
5588 *pcVertices = cPrimitiveCount + 1;
5589 break;
5590 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
5591 *pMode = GL_TRIANGLE_STRIP;
5592 *pcVertices = cPrimitiveCount + 2;
5593 break;
5594 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
5595 *pMode = GL_TRIANGLE_FAN;
5596 *pcVertices = cPrimitiveCount + 2;
5597 break;
5598 default:
5599 return VERR_INVALID_PARAMETER;
5600 }
5601 return VINF_SUCCESS;
5602}
5603
5604int vmsvga3dResetTransformMatrices(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext)
5605{
5606 int rc;
5607
5608 /* Reset the view matrix (also takes the world matrix into account). */
5609 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].fValid == true)
5610 {
5611 rc = vmsvga3dSetTransform(pThis, pContext->id, SVGA3D_TRANSFORM_VIEW, pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
5612 }
5613 else
5614 {
5615 float matrix[16];
5616
5617 /* identity matrix if no matrix set. */
5618 memset(matrix, 0, sizeof(matrix));
5619 matrix[0] = 1.0;
5620 matrix[5] = 1.0;
5621 matrix[10] = 1.0;
5622 matrix[15] = 1.0;
5623 rc = vmsvga3dSetTransform(pThis, pContext->id, SVGA3D_TRANSFORM_VIEW, matrix);
5624 }
5625
5626 /* Reset the projection matrix. */
5627 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].fValid == true)
5628 {
5629 rc = vmsvga3dSetTransform(pThis, pContext->id, SVGA3D_TRANSFORM_PROJECTION, pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].matrix);
5630 }
5631 else
5632 {
5633 float matrix[16];
5634
5635 /* identity matrix if no matrix set. */
5636 memset(matrix, 0, sizeof(matrix));
5637 matrix[0] = 1.0;
5638 matrix[5] = 1.0;
5639 matrix[10] = 1.0;
5640 matrix[15] = 1.0;
5641 rc = vmsvga3dSetTransform(pThis, pContext->id, SVGA3D_TRANSFORM_PROJECTION, matrix);
5642 }
5643 AssertRC(rc);
5644 return rc;
5645}
5646
5647int vmsvga3dDrawPrimitivesProcessVertexDecls(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext,
5648 uint32_t iVertexDeclBase, uint32_t numVertexDecls,
5649 SVGA3dVertexDecl *pVertexDecl, SVGA3dVertexDivisor const *paVertexDivisors)
5650{
5651 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5652 unsigned sidVertex = pVertexDecl[0].array.surfaceId;
5653 PVMSVGA3DSURFACE pVertexSurface;
5654
5655 AssertReturn(sidVertex < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
5656 AssertReturn(sidVertex < pState->cSurfaces && pState->papSurfaces[sidVertex]->id == sidVertex, VERR_INVALID_PARAMETER);
5657
5658 pVertexSurface = pState->papSurfaces[sidVertex];
5659 Log(("vmsvga3dDrawPrimitives: vertex surface %x\n", sidVertex));
5660
5661 /* Create and/or bind the vertex buffer. */
5662 if (pVertexSurface->oglId.buffer == OPENGL_INVALID_ID)
5663 {
5664 Log(("vmsvga3dDrawPrimitives: create vertex buffer fDirty=%d size=%x bytes\n", pVertexSurface->fDirty, pVertexSurface->pMipmapLevels[0].cbSurface));
5665 PVMSVGA3DCONTEXT pSavedCtx = pContext;
5666 pContext = &pState->SharedCtx;
5667 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5668
5669 pState->ext.glGenBuffers(1, &pVertexSurface->oglId.buffer);
5670 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5671
5672 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
5673 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5674
5675 Assert(pVertexSurface->fDirty);
5676 /** @todo rethink usage dynamic/static */
5677 pState->ext.glBufferData(GL_ARRAY_BUFFER, pVertexSurface->pMipmapLevels[0].cbSurface, pVertexSurface->pMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
5678 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5679
5680 pVertexSurface->pMipmapLevels[0].fDirty = false;
5681 pVertexSurface->fDirty = false;
5682
5683 pVertexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
5684
5685 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, OPENGL_INVALID_ID);
5686 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5687
5688 pContext = pSavedCtx;
5689 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5690 }
5691
5692 Assert(pVertexSurface->fDirty == false);
5693 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
5694 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5695
5696 /* Setup the vertex declarations. */
5697 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
5698 {
5699 GLint size;
5700 GLenum type;
5701 GLboolean normalized;
5702 GLuint index = iVertexDeclBase + iVertex;
5703
5704 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));
5705
5706 int rc = vmsvga3dVertexDecl2OGL(pVertexDecl[iVertex].identity, size, type, normalized);
5707 AssertRCReturn(rc, rc);
5708
5709 if (pContext->state.shidVertex != SVGA_ID_INVALID)
5710 {
5711 /* Use numbered vertex arrays when shaders are active. */
5712 pState->ext.glEnableVertexAttribArray(index);
5713 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5714 pState->ext.glVertexAttribPointer(index, size, type, normalized, pVertexDecl[iVertex].array.stride,
5715 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5716 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5717
5718 GLuint const divisor = paVertexDivisors && paVertexDivisors[index].s.instanceData ? 1 : 0;
5719 pState->ext.glVertexAttribDivisor(index, divisor);
5720 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5721
5722 /** @todo case SVGA3D_DECLUSAGE_COLOR: color component order not identical!! test GL_BGRA!! */
5723 }
5724 else
5725 {
5726 /* Use the predefined selection of vertex streams for the fixed pipeline. */
5727 switch (pVertexDecl[iVertex].identity.usage)
5728 {
5729 case SVGA3D_DECLUSAGE_POSITIONT:
5730 case SVGA3D_DECLUSAGE_POSITION:
5731 {
5732 glEnableClientState(GL_VERTEX_ARRAY);
5733 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5734 glVertexPointer(size, type, pVertexDecl[iVertex].array.stride,
5735 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5736 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5737 break;
5738 }
5739 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
5740 AssertFailed();
5741 break;
5742 case SVGA3D_DECLUSAGE_BLENDINDICES:
5743 AssertFailed();
5744 break;
5745 case SVGA3D_DECLUSAGE_NORMAL:
5746 glEnableClientState(GL_NORMAL_ARRAY);
5747 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5748 glNormalPointer(type, pVertexDecl[iVertex].array.stride,
5749 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5750 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5751 break;
5752 case SVGA3D_DECLUSAGE_PSIZE:
5753 AssertFailed();
5754 break;
5755 case SVGA3D_DECLUSAGE_TEXCOORD:
5756 /* Specify the affected texture unit. */
5757#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
5758 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
5759#else
5760 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
5761#endif
5762 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
5763 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5764 glTexCoordPointer(size, type, pVertexDecl[iVertex].array.stride,
5765 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5766 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5767 break;
5768 case SVGA3D_DECLUSAGE_TANGENT:
5769 AssertFailed();
5770 break;
5771 case SVGA3D_DECLUSAGE_BINORMAL:
5772 AssertFailed();
5773 break;
5774 case SVGA3D_DECLUSAGE_TESSFACTOR:
5775 AssertFailed();
5776 break;
5777 case SVGA3D_DECLUSAGE_COLOR: /** @todo color component order not identical!! test GL_BGRA!! */
5778 glEnableClientState(GL_COLOR_ARRAY);
5779 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5780 glColorPointer(size, type, pVertexDecl[iVertex].array.stride,
5781 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5782 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5783 break;
5784 case SVGA3D_DECLUSAGE_FOG:
5785 glEnableClientState(GL_FOG_COORD_ARRAY);
5786 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5787 pState->ext.glFogCoordPointer(type, pVertexDecl[iVertex].array.stride,
5788 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5789 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5790 break;
5791 case SVGA3D_DECLUSAGE_DEPTH:
5792 AssertFailed();
5793 break;
5794 case SVGA3D_DECLUSAGE_SAMPLE:
5795 AssertFailed();
5796 break;
5797 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
5798 }
5799 }
5800
5801#ifdef LOG_ENABLED
5802 if (pVertexDecl[iVertex].array.stride == 0)
5803 Log(("vmsvga3dDrawPrimitives: stride == 0! Can be valid\n"));
5804#endif
5805 }
5806
5807 return VINF_SUCCESS;
5808}
5809
5810int vmsvga3dDrawPrimitivesCleanupVertexDecls(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext, uint32_t iVertexDeclBase, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl)
5811{
5812 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5813
5814 /* Clean up the vertex declarations. */
5815 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
5816 {
5817 if (pVertexDecl[iVertex].identity.usage == SVGA3D_DECLUSAGE_POSITIONT)
5818 {
5819 /* Reset the transformation matrices in case of a switch back from pretransformed mode. */
5820 Log(("vmsvga3dDrawPrimitivesCleanupVertexDecls: reset world and projection matrices after transformation reset (pre-transformed -> transformed)\n"));
5821 vmsvga3dResetTransformMatrices(pThis, pContext);
5822 }
5823
5824 if (pContext->state.shidVertex != SVGA_ID_INVALID)
5825 {
5826 /* Use numbered vertex arrays when shaders are active. */
5827 pState->ext.glDisableVertexAttribArray(iVertexDeclBase + iVertex);
5828 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5829 }
5830 else
5831 {
5832 /* Use the predefined selection of vertex streams for the fixed pipeline. */
5833 switch (pVertexDecl[iVertex].identity.usage)
5834 {
5835 case SVGA3D_DECLUSAGE_POSITION:
5836 case SVGA3D_DECLUSAGE_POSITIONT:
5837 glDisableClientState(GL_VERTEX_ARRAY);
5838 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5839 break;
5840 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
5841 break;
5842 case SVGA3D_DECLUSAGE_BLENDINDICES:
5843 break;
5844 case SVGA3D_DECLUSAGE_NORMAL:
5845 glDisableClientState(GL_NORMAL_ARRAY);
5846 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5847 break;
5848 case SVGA3D_DECLUSAGE_PSIZE:
5849 break;
5850 case SVGA3D_DECLUSAGE_TEXCOORD:
5851 /* Specify the affected texture unit. */
5852#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
5853 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
5854#else
5855 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
5856#endif
5857 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
5858 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5859 break;
5860 case SVGA3D_DECLUSAGE_TANGENT:
5861 break;
5862 case SVGA3D_DECLUSAGE_BINORMAL:
5863 break;
5864 case SVGA3D_DECLUSAGE_TESSFACTOR:
5865 break;
5866 case SVGA3D_DECLUSAGE_COLOR: /** @todo color component order not identical!! */
5867 glDisableClientState(GL_COLOR_ARRAY);
5868 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5869 break;
5870 case SVGA3D_DECLUSAGE_FOG:
5871 glDisableClientState(GL_FOG_COORD_ARRAY);
5872 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5873 break;
5874 case SVGA3D_DECLUSAGE_DEPTH:
5875 break;
5876 case SVGA3D_DECLUSAGE_SAMPLE:
5877 break;
5878 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
5879 }
5880 }
5881 }
5882 /* Unbind the vertex buffer after usage. */
5883 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0);
5884 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5885 return VINF_SUCCESS;
5886}
5887
5888int vmsvga3dDrawPrimitives(PVGASTATE pThis, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl,
5889 uint32_t numRanges, SVGA3dPrimitiveRange *pRange, uint32_t cVertexDivisor,
5890 SVGA3dVertexDivisor *pVertexDivisor)
5891{
5892 RT_NOREF(pVertexDivisor);
5893 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5894 AssertReturn(pState, VERR_INTERNAL_ERROR);
5895 PVMSVGA3DCONTEXT pContext;
5896 int rc = VERR_NOT_IMPLEMENTED;
5897 uint32_t iCurrentVertex;
5898
5899 Log(("vmsvga3dDrawPrimitives cid=%x numVertexDecls=%d numRanges=%d, cVertexDivisor=%d\n", cid, numVertexDecls, numRanges, cVertexDivisor));
5900
5901 /* Caller already check these, but it cannot hurt to check again... */
5902 AssertReturn(numVertexDecls && numVertexDecls <= SVGA3D_MAX_VERTEX_ARRAYS, VERR_INVALID_PARAMETER);
5903 AssertReturn(numRanges && numRanges <= SVGA3D_MAX_DRAW_PRIMITIVE_RANGES, VERR_INVALID_PARAMETER);
5904 AssertReturn(!cVertexDivisor || cVertexDivisor == numVertexDecls, VERR_INVALID_PARAMETER);
5905
5906 if (!cVertexDivisor)
5907 pVertexDivisor = NULL; /* Be sure. */
5908
5909 if ( cid >= pState->cContexts
5910 || pState->papContexts[cid]->id != cid)
5911 {
5912 Log(("vmsvga3dDrawPrimitives invalid context id!\n"));
5913 return VERR_INVALID_PARAMETER;
5914 }
5915 pContext = pState->papContexts[cid];
5916 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5917
5918 /* Check for pretransformed vertex declarations. */
5919 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
5920 {
5921 switch (pVertexDecl[iVertex].identity.usage)
5922 {
5923 case SVGA3D_DECLUSAGE_POSITIONT:
5924 Log(("ShaderSetPositionTransformed: (%d,%d)\n", pContext->state.RectViewPort.w, pContext->state.RectViewPort.h));
5925 RT_FALL_THRU();
5926 case SVGA3D_DECLUSAGE_POSITION:
5927 ShaderSetPositionTransformed(pContext->pShaderContext, pContext->state.RectViewPort.w,
5928 pContext->state.RectViewPort.h,
5929 pVertexDecl[iVertex].identity.usage == SVGA3D_DECLUSAGE_POSITIONT);
5930 break;
5931 default: /* Shut up MSC. */ break;
5932 }
5933 }
5934
5935 /* Try to figure out if instancing is used.
5936 * Support simple instancing case with one set of indexed data and one set per-instance data.
5937 */
5938 uint32_t cInstances = 0;
5939 for (uint32_t iVertexDivisor = 0; iVertexDivisor < cVertexDivisor; ++iVertexDivisor)
5940 {
5941 if (pVertexDivisor[iVertexDivisor].s.indexedData)
5942 {
5943 if (cInstances == 0)
5944 cInstances = pVertexDivisor[iVertexDivisor].s.count;
5945 else
5946 Assert(cInstances == pVertexDivisor[iVertexDivisor].s.count);
5947 }
5948 else if (pVertexDivisor[iVertexDivisor].s.instanceData)
5949 {
5950 Assert(pVertexDivisor[iVertexDivisor].s.count == 1);
5951 }
5952 }
5953
5954 /* Flush any shader changes; after (!) checking the vertex declarations to deal with pre-transformed vertices. */
5955 if (pContext->pShaderContext)
5956 {
5957 uint32_t rtHeight = 0;
5958
5959 if (pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] != SVGA_ID_INVALID)
5960 {
5961 PVMSVGA3DSURFACE pRenderTarget = pState->papSurfaces[pContext->state.aRenderTargets[SVGA3D_RT_COLOR0]];
5962 rtHeight = pRenderTarget->pMipmapLevels[0].mipmapSize.height;
5963 }
5964
5965 ShaderUpdateState(pContext->pShaderContext, rtHeight);
5966 }
5967
5968 /* Process all vertex declarations. Each vertex buffer is represented by one stream. */
5969 iCurrentVertex = 0;
5970 while (iCurrentVertex < numVertexDecls)
5971 {
5972 uint32_t sidVertex = SVGA_ID_INVALID;
5973 uint32_t iVertex;
5974
5975 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
5976 {
5977 if ( sidVertex != SVGA_ID_INVALID
5978 && pVertexDecl[iVertex].array.surfaceId != sidVertex
5979 )
5980 break;
5981 sidVertex = pVertexDecl[iVertex].array.surfaceId;
5982 }
5983
5984 rc = vmsvga3dDrawPrimitivesProcessVertexDecls(pThis, pContext, iCurrentVertex, iVertex - iCurrentVertex,
5985 &pVertexDecl[iCurrentVertex], pVertexDivisor);
5986 AssertRCReturn(rc, rc);
5987
5988 iCurrentVertex = iVertex;
5989 }
5990
5991 /* Now draw the primitives. */
5992 for (unsigned iPrimitive = 0; iPrimitive < numRanges; iPrimitive++)
5993 {
5994 GLenum modeDraw;
5995 unsigned sidIndex = pRange[iPrimitive].indexArray.surfaceId;
5996 PVMSVGA3DSURFACE pIndexSurface = NULL;
5997 unsigned cVertices;
5998
5999 Log(("Primitive %d: type %s\n", iPrimitive, vmsvga3dPrimitiveType2String(pRange[iPrimitive].primType)));
6000 rc = vmsvga3dPrimitiveType2OGL(pRange[iPrimitive].primType, &modeDraw, pRange[iPrimitive].primitiveCount, &cVertices);
6001 if (RT_FAILURE(rc))
6002 {
6003 AssertRC(rc);
6004 goto internal_error;
6005 }
6006
6007 if (sidIndex != SVGA3D_INVALID_ID)
6008 {
6009 AssertMsg(pRange[iPrimitive].indexWidth == sizeof(uint32_t) || pRange[iPrimitive].indexWidth == sizeof(uint16_t), ("Unsupported primitive width %d\n", pRange[iPrimitive].indexWidth));
6010
6011 if ( sidIndex >= SVGA3D_MAX_SURFACE_IDS
6012 || sidIndex >= pState->cSurfaces
6013 || pState->papSurfaces[sidIndex]->id != sidIndex)
6014 {
6015 Assert(sidIndex < SVGA3D_MAX_SURFACE_IDS);
6016 Assert(sidIndex < pState->cSurfaces && pState->papSurfaces[sidIndex]->id == sidIndex);
6017 rc = VERR_INVALID_PARAMETER;
6018 goto internal_error;
6019 }
6020 pIndexSurface = pState->papSurfaces[sidIndex];
6021 Log(("vmsvga3dDrawPrimitives: index surface %x\n", sidIndex));
6022
6023 if (pIndexSurface->oglId.buffer == OPENGL_INVALID_ID)
6024 {
6025 Log(("vmsvga3dDrawPrimitives: create index buffer fDirty=%d size=%x bytes\n", pIndexSurface->fDirty, pIndexSurface->pMipmapLevels[0].cbSurface));
6026 pContext = &pState->SharedCtx;
6027 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6028
6029 pState->ext.glGenBuffers(1, &pIndexSurface->oglId.buffer);
6030 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6031
6032 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
6033 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6034
6035 Assert(pIndexSurface->fDirty);
6036
6037 /** @todo rethink usage dynamic/static */
6038 pState->ext.glBufferData(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->pMipmapLevels[0].cbSurface, pIndexSurface->pMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
6039 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6040
6041 pIndexSurface->pMipmapLevels[0].fDirty = false;
6042 pIndexSurface->fDirty = false;
6043
6044 pIndexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
6045
6046 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, OPENGL_INVALID_ID);
6047 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6048
6049 pContext = pState->papContexts[cid];
6050 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6051 }
6052 Assert(pIndexSurface->fDirty == false);
6053
6054 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
6055 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6056 }
6057
6058 if (!pIndexSurface)
6059 {
6060 /* Render without an index buffer */
6061 Log(("DrawPrimitive %x cPrimitives=%d cVertices=%d index index bias=%d\n", modeDraw, pRange[iPrimitive].primitiveCount, cVertices, pRange[iPrimitive].indexBias));
6062 if (cInstances == 0)
6063 {
6064 glDrawArrays(modeDraw, pRange[iPrimitive].indexBias, cVertices);
6065 }
6066 else
6067 {
6068 pState->ext.glDrawArraysInstanced(modeDraw, pRange[iPrimitive].indexBias, cVertices, cInstances);
6069 }
6070 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6071 }
6072 else
6073 {
6074 GLenum indexType;
6075
6076 Assert(pRange[iPrimitive].indexBias >= 0); /** @todo indexBias */
6077 Assert(pRange[iPrimitive].indexWidth == pRange[iPrimitive].indexArray.stride);
6078
6079 if (pRange[iPrimitive].indexWidth == sizeof(uint8_t))
6080 {
6081 indexType = GL_UNSIGNED_BYTE;
6082 }
6083 else
6084 if (pRange[iPrimitive].indexWidth == sizeof(uint16_t))
6085 {
6086 indexType = GL_UNSIGNED_SHORT;
6087 }
6088 else
6089 {
6090 Assert(pRange[iPrimitive].indexWidth == sizeof(uint32_t));
6091 indexType = GL_UNSIGNED_INT;
6092 }
6093
6094 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));
6095 if (cInstances == 0)
6096 {
6097 /* Render with an index buffer */
6098 if (pRange[iPrimitive].indexBias == 0)
6099 glDrawElements(modeDraw,
6100 cVertices,
6101 indexType,
6102 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset); /* byte offset in indices buffer */
6103 else
6104 pState->ext.glDrawElementsBaseVertex(modeDraw,
6105 cVertices,
6106 indexType,
6107 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
6108 pRange[iPrimitive].indexBias); /* basevertex */
6109 }
6110 else
6111 {
6112 /* Render with an index buffer */
6113 if (pRange[iPrimitive].indexBias == 0)
6114 pState->ext.glDrawElementsInstanced(modeDraw,
6115 cVertices,
6116 indexType,
6117 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
6118 cInstances);
6119 else
6120 pState->ext.glDrawElementsInstancedBaseVertex(modeDraw,
6121 cVertices,
6122 indexType,
6123 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
6124 cInstances,
6125 pRange[iPrimitive].indexBias); /* basevertex */
6126 }
6127 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6128
6129 /* Unbind the index buffer after usage. */
6130 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
6131 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6132 }
6133 }
6134
6135internal_error:
6136
6137 /* Deactivate the vertex declarations. */
6138 iCurrentVertex = 0;
6139 while (iCurrentVertex < numVertexDecls)
6140 {
6141 uint32_t sidVertex = SVGA_ID_INVALID;
6142 uint32_t iVertex;
6143
6144 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
6145 {
6146 if ( sidVertex != SVGA_ID_INVALID
6147 && pVertexDecl[iVertex].array.surfaceId != sidVertex
6148 )
6149 break;
6150 sidVertex = pVertexDecl[iVertex].array.surfaceId;
6151 }
6152
6153 rc = vmsvga3dDrawPrimitivesCleanupVertexDecls(pThis, pContext, iCurrentVertex, iVertex - iCurrentVertex, &pVertexDecl[iCurrentVertex]);
6154 AssertRCReturn(rc, rc);
6155
6156 iCurrentVertex = iVertex;
6157 }
6158#ifdef DEBUG
6159 /* Check whether 'activeTexture' on texture unit 'i' matches what we expect. */
6160 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTextures); ++i)
6161 {
6162 if (pContext->aSidActiveTextures[i] != SVGA3D_INVALID_ID)
6163 {
6164 PVMSVGA3DSURFACE pTexture;
6165 int rc2 = vmsvga3dSurfaceFromSid(pState, pContext->aSidActiveTextures[i], &pTexture);
6166 AssertContinue(RT_SUCCESS(rc2));
6167
6168 GLint activeTextureUnit = 0;
6169 glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
6170 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6171
6172 pState->ext.glActiveTexture(GL_TEXTURE0 + i);
6173 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6174
6175 GLint activeTexture = 0;
6176 glGetIntegerv(pTexture->bindingGL, &activeTexture);
6177 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6178
6179 pState->ext.glActiveTexture(activeTextureUnit);
6180 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6181
6182 AssertMsg(pTexture->oglId.texture == (GLuint)activeTexture,
6183 ("%x vs %x unit %d (active unit %d) sid=%x\n", pTexture->oglId.texture, activeTexture, i,
6184 activeTextureUnit - GL_TEXTURE0, pContext->aSidActiveTextures[i]));
6185 }
6186 }
6187#endif
6188
6189#if 0
6190 /* Dump render target to a bitmap. */
6191 if (pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] != SVGA3D_INVALID_ID)
6192 {
6193 vmsvga3dUpdateHeapBuffersForSurfaces(pThis, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0]);
6194 PVMSVGA3DSURFACE pSurface;
6195 int rc2 = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pSurface);
6196 if (RT_SUCCESS(rc2))
6197 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmp", "rt", "-post");
6198 }
6199#endif
6200
6201 return rc;
6202}
6203
6204
6205int vmsvga3dShaderDefine(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type, uint32_t cbData, uint32_t *pShaderData)
6206{
6207 PVMSVGA3DCONTEXT pContext;
6208 PVMSVGA3DSHADER pShader;
6209 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6210 AssertReturn(pState, VERR_NO_MEMORY);
6211 int rc;
6212
6213 Log(("vmsvga3dShaderDefine cid=%x shid=%x type=%s cbData=%x\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cbData));
6214 Log3(("shader code:\n%.*Rhxd\n", cbData, pShaderData));
6215
6216 if ( cid >= pState->cContexts
6217 || pState->papContexts[cid]->id != cid)
6218 {
6219 Log(("vmsvga3dShaderDefine invalid context id!\n"));
6220 return VERR_INVALID_PARAMETER;
6221 }
6222 pContext = pState->papContexts[cid];
6223 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6224
6225 AssertReturn(shid < SVGA3D_MAX_SHADER_IDS, VERR_INVALID_PARAMETER);
6226 if (type == SVGA3D_SHADERTYPE_VS)
6227 {
6228 if (shid >= pContext->cVertexShaders)
6229 {
6230 void *pvNew = RTMemRealloc(pContext->paVertexShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
6231 AssertReturn(pvNew, VERR_NO_MEMORY);
6232 pContext->paVertexShader = (PVMSVGA3DSHADER)pvNew;
6233 memset(&pContext->paVertexShader[pContext->cVertexShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cVertexShaders));
6234 for (uint32_t i = pContext->cVertexShaders; i < shid + 1; i++)
6235 pContext->paVertexShader[i].id = SVGA3D_INVALID_ID;
6236 pContext->cVertexShaders = shid + 1;
6237 }
6238 /* If one already exists with this id, then destroy it now. */
6239 if (pContext->paVertexShader[shid].id != SVGA3D_INVALID_ID)
6240 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paVertexShader[shid].type);
6241
6242 pShader = &pContext->paVertexShader[shid];
6243 }
6244 else
6245 {
6246 Assert(type == SVGA3D_SHADERTYPE_PS);
6247 if (shid >= pContext->cPixelShaders)
6248 {
6249 void *pvNew = RTMemRealloc(pContext->paPixelShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
6250 AssertReturn(pvNew, VERR_NO_MEMORY);
6251 pContext->paPixelShader = (PVMSVGA3DSHADER)pvNew;
6252 memset(&pContext->paPixelShader[pContext->cPixelShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cPixelShaders));
6253 for (uint32_t i = pContext->cPixelShaders; i < shid + 1; i++)
6254 pContext->paPixelShader[i].id = SVGA3D_INVALID_ID;
6255 pContext->cPixelShaders = shid + 1;
6256 }
6257 /* If one already exists with this id, then destroy it now. */
6258 if (pContext->paPixelShader[shid].id != SVGA3D_INVALID_ID)
6259 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paPixelShader[shid].type);
6260
6261 pShader = &pContext->paPixelShader[shid];
6262 }
6263
6264 memset(pShader, 0, sizeof(*pShader));
6265 pShader->id = shid;
6266 pShader->cid = cid;
6267 pShader->type = type;
6268 pShader->cbData = cbData;
6269 pShader->pShaderProgram = RTMemAllocZ(cbData);
6270 AssertReturn(pShader->pShaderProgram, VERR_NO_MEMORY);
6271 memcpy(pShader->pShaderProgram, pShaderData, cbData);
6272
6273#ifdef DUMP_SHADER_DISASSEMBLY
6274 LPD3DXBUFFER pDisassembly;
6275 HRESULT hr = D3DXDisassembleShader((const DWORD *)pShaderData, FALSE, NULL, &pDisassembly);
6276 if (hr == D3D_OK)
6277 {
6278 Log(("Shader disassembly:\n%s\n", pDisassembly->GetBufferPointer()));
6279 pDisassembly->Release();
6280 }
6281#endif
6282
6283 switch (type)
6284 {
6285 case SVGA3D_SHADERTYPE_VS:
6286 rc = ShaderCreateVertexShader(pContext->pShaderContext, (const uint32_t *)pShaderData, &pShader->u.pVertexShader);
6287 break;
6288
6289 case SVGA3D_SHADERTYPE_PS:
6290 rc = ShaderCreatePixelShader(pContext->pShaderContext, (const uint32_t *)pShaderData, &pShader->u.pPixelShader);
6291 break;
6292
6293 default:
6294 AssertFailedReturn(VERR_INVALID_PARAMETER);
6295 }
6296 if (rc != VINF_SUCCESS)
6297 {
6298 RTMemFree(pShader->pShaderProgram);
6299 memset(pShader, 0, sizeof(*pShader));
6300 pShader->id = SVGA3D_INVALID_ID;
6301 }
6302
6303 return rc;
6304}
6305
6306int vmsvga3dShaderDestroy(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type)
6307{
6308 PVMSVGA3DCONTEXT pContext;
6309 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6310 AssertReturn(pState, VERR_NO_MEMORY);
6311 PVMSVGA3DSHADER pShader = NULL;
6312 int rc;
6313
6314 Log(("vmsvga3dShaderDestroy cid=%x shid=%x type=%s\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL"));
6315
6316 if ( cid >= pState->cContexts
6317 || pState->papContexts[cid]->id != cid)
6318 {
6319 Log(("vmsvga3dShaderDestroy invalid context id!\n"));
6320 return VERR_INVALID_PARAMETER;
6321 }
6322 pContext = pState->papContexts[cid];
6323 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6324
6325 if (type == SVGA3D_SHADERTYPE_VS)
6326 {
6327 if ( shid < pContext->cVertexShaders
6328 && pContext->paVertexShader[shid].id == shid)
6329 {
6330 pShader = &pContext->paVertexShader[shid];
6331 rc = ShaderDestroyVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
6332 AssertRC(rc);
6333 }
6334 }
6335 else
6336 {
6337 Assert(type == SVGA3D_SHADERTYPE_PS);
6338 if ( shid < pContext->cPixelShaders
6339 && pContext->paPixelShader[shid].id == shid)
6340 {
6341 pShader = &pContext->paPixelShader[shid];
6342 rc = ShaderDestroyPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
6343 AssertRC(rc);
6344 }
6345 }
6346
6347 if (pShader)
6348 {
6349 if (pShader->pShaderProgram)
6350 RTMemFree(pShader->pShaderProgram);
6351 memset(pShader, 0, sizeof(*pShader));
6352 pShader->id = SVGA3D_INVALID_ID;
6353 }
6354 else
6355 AssertFailedReturn(VERR_INVALID_PARAMETER);
6356
6357 return VINF_SUCCESS;
6358}
6359
6360int vmsvga3dShaderSet(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext, uint32_t cid, SVGA3dShaderType type, uint32_t shid)
6361{
6362 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6363 AssertReturn(pState, VERR_NO_MEMORY);
6364 int rc;
6365
6366 Log(("vmsvga3dShaderSet cid=%x type=%s shid=%d\n", cid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", shid));
6367
6368 if ( !pContext
6369 && cid < pState->cContexts
6370 && pState->papContexts[cid]->id == cid)
6371 pContext = pState->papContexts[cid];
6372 else if (!pContext)
6373 {
6374 AssertMsgFailed(("cid=%#x cContexts=%#x\n", cid, pState->cContexts));
6375 Log(("vmsvga3dShaderSet invalid context id!\n"));
6376 return VERR_INVALID_PARAMETER;
6377 }
6378 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6379
6380 if (type == SVGA3D_SHADERTYPE_VS)
6381 {
6382 /* Save for vm state save/restore. */
6383 pContext->state.shidVertex = shid;
6384 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VERTEXSHADER;
6385
6386 if ( shid < pContext->cVertexShaders
6387 && pContext->paVertexShader[shid].id == shid)
6388 {
6389 PVMSVGA3DSHADER pShader = &pContext->paVertexShader[shid];
6390 Assert(type == pShader->type);
6391
6392 rc = ShaderSetVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
6393 AssertRCReturn(rc, rc);
6394 }
6395 else
6396 if (shid == SVGA_ID_INVALID)
6397 {
6398 /* Unselect shader. */
6399 rc = ShaderSetVertexShader(pContext->pShaderContext, NULL);
6400 AssertRCReturn(rc, rc);
6401 }
6402 else
6403 AssertFailedReturn(VERR_INVALID_PARAMETER);
6404 }
6405 else
6406 {
6407 /* Save for vm state save/restore. */
6408 pContext->state.shidPixel = shid;
6409 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_PIXELSHADER;
6410
6411 Assert(type == SVGA3D_SHADERTYPE_PS);
6412 if ( shid < pContext->cPixelShaders
6413 && pContext->paPixelShader[shid].id == shid)
6414 {
6415 PVMSVGA3DSHADER pShader = &pContext->paPixelShader[shid];
6416 Assert(type == pShader->type);
6417
6418 rc = ShaderSetPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
6419 AssertRCReturn(rc, rc);
6420 }
6421 else
6422 if (shid == SVGA_ID_INVALID)
6423 {
6424 /* Unselect shader. */
6425 rc = ShaderSetPixelShader(pContext->pShaderContext, NULL);
6426 AssertRCReturn(rc, rc);
6427 }
6428 else
6429 AssertFailedReturn(VERR_INVALID_PARAMETER);
6430 }
6431
6432 return VINF_SUCCESS;
6433}
6434
6435int vmsvga3dShaderSetConst(PVGASTATE pThis, uint32_t cid, uint32_t reg, SVGA3dShaderType type, SVGA3dShaderConstType ctype, uint32_t cRegisters, uint32_t *pValues)
6436{
6437 PVMSVGA3DCONTEXT pContext;
6438 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6439 AssertReturn(pState, VERR_NO_MEMORY);
6440 int rc;
6441
6442 Log(("vmsvga3dShaderSetConst cid=%x reg=%x type=%s cregs=%d ctype=%x\n", cid, reg, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cRegisters, ctype));
6443
6444 if ( cid >= pState->cContexts
6445 || pState->papContexts[cid]->id != cid)
6446 {
6447 Log(("vmsvga3dShaderSetConst invalid context id!\n"));
6448 return VERR_INVALID_PARAMETER;
6449 }
6450 pContext = pState->papContexts[cid];
6451 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6452
6453 for (uint32_t i = 0; i < cRegisters; i++)
6454 {
6455#ifdef LOG_ENABLED
6456 switch (ctype)
6457 {
6458 case SVGA3D_CONST_TYPE_FLOAT:
6459 {
6460 float *pValuesF = (float *)pValues;
6461 Log(("ConstantF %d: value=%d, %d, %d, %d\n", reg + i, (int)(pValuesF[i*4 + 0] * 100.0f), (int)(pValuesF[i*4 + 1] * 100.0f), (int)(pValuesF[i*4 + 2] * 100.0f), (int)(pValuesF[i*4 + 3] * 100.0f)));
6462 break;
6463 }
6464
6465 case SVGA3D_CONST_TYPE_INT:
6466 Log(("ConstantI %d: value=%d, %d, %d, %d\n", reg + i, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]));
6467 break;
6468
6469 case SVGA3D_CONST_TYPE_BOOL:
6470 Log(("ConstantB %d: value=%d, %d, %d, %d\n", reg + i, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]));
6471 break;
6472 }
6473#endif
6474 vmsvga3dSaveShaderConst(pContext, reg + i, type, ctype, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]);
6475 }
6476
6477 switch (type)
6478 {
6479 case SVGA3D_SHADERTYPE_VS:
6480 switch (ctype)
6481 {
6482 case SVGA3D_CONST_TYPE_FLOAT:
6483 rc = ShaderSetVertexShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
6484 break;
6485
6486 case SVGA3D_CONST_TYPE_INT:
6487 rc = ShaderSetVertexShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
6488 break;
6489
6490 case SVGA3D_CONST_TYPE_BOOL:
6491 rc = ShaderSetVertexShaderConstantB(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 case SVGA3D_SHADERTYPE_PS:
6501 switch (ctype)
6502 {
6503 case SVGA3D_CONST_TYPE_FLOAT:
6504 rc = ShaderSetPixelShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
6505 break;
6506
6507 case SVGA3D_CONST_TYPE_INT:
6508 rc = ShaderSetPixelShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
6509 break;
6510
6511 case SVGA3D_CONST_TYPE_BOOL:
6512 rc = ShaderSetPixelShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
6513 break;
6514
6515 default:
6516 AssertFailedReturn(VERR_INVALID_PARAMETER);
6517 }
6518 AssertRCReturn(rc, rc);
6519 break;
6520
6521 default:
6522 AssertFailedReturn(VERR_INVALID_PARAMETER);
6523 }
6524
6525 return VINF_SUCCESS;
6526}
6527
6528int vmsvga3dOcclusionQueryCreate(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
6529{
6530 AssertReturn(pState->ext.glGenQueries, VERR_NOT_SUPPORTED);
6531 GLuint idQuery = 0;
6532 pState->ext.glGenQueries(1, &idQuery);
6533 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6534 AssertReturn(idQuery, VERR_INTERNAL_ERROR);
6535 pContext->occlusion.idQuery = idQuery;
6536 return VINF_SUCCESS;
6537}
6538
6539int vmsvga3dOcclusionQueryDelete(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
6540{
6541 AssertReturn(pState->ext.glDeleteQueries, VERR_NOT_SUPPORTED);
6542 if (pContext->occlusion.idQuery)
6543 {
6544 pState->ext.glDeleteQueries(1, &pContext->occlusion.idQuery);
6545 }
6546 return VINF_SUCCESS;
6547}
6548
6549int vmsvga3dOcclusionQueryBegin(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
6550{
6551 AssertReturn(pState->ext.glBeginQuery, VERR_NOT_SUPPORTED);
6552 pState->ext.glBeginQuery(GL_ANY_SAMPLES_PASSED, pContext->occlusion.idQuery);
6553 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6554 return VINF_SUCCESS;
6555}
6556
6557int vmsvga3dOcclusionQueryEnd(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
6558{
6559 RT_NOREF(pContext);
6560 AssertReturn(pState->ext.glEndQuery, VERR_NOT_SUPPORTED);
6561 pState->ext.glEndQuery(GL_ANY_SAMPLES_PASSED);
6562 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6563 return VINF_SUCCESS;
6564}
6565
6566int vmsvga3dOcclusionQueryGetData(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t *pu32Pixels)
6567{
6568 AssertReturn(pState->ext.glGetQueryObjectuiv, VERR_NOT_SUPPORTED);
6569 GLuint pixels = 0;
6570 pState->ext.glGetQueryObjectuiv(pContext->occlusion.idQuery, GL_QUERY_RESULT, &pixels);
6571 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6572
6573 *pu32Pixels = (uint32_t)pixels;
6574 return VINF_SUCCESS;
6575}
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