VirtualBox

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

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

DevVGA-SVGA3d-ogl.cpp: a few more formats.

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