VirtualBox

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

Last change on this file since 86651 was 86576, checked in by vboxsync, 4 years ago

3D: Clarify shader version

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