VirtualBox

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

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

DevVGA-SVGA3d-ogl.cpp: formats fixes.

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