VirtualBox

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

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

DevVGA-SVGA3d-ogl.cpp: allow DXT2 and DXT4 formats.

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