VirtualBox

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

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

DevVGA-SVGA: simplified creation of per context device windows; implement SVGA_3D_CMD_PRESENT using SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN.

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