VirtualBox

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

Last change on this file since 85672 was 85370, checked in by vboxsync, 4 years ago

Devices/Graphics,Main,include: Experimental graphics output (build fix). bugref:9695

  • 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 85370 2020-07-17 10:02:57Z 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_BC4_UNORM = 82,
1100 SVGA3D_DEVCAP_SURFACEFMT_BC5_UNORM = 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_BC4_UNORM:
1627 case SVGA3D_DEVCAP_SURFACEFMT_BC5_UNORM:
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_BUMPL8V8U8:
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_BC4_UNORM:
1966 case SVGA3D_BC5_UNORM:
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 AssertCompile(SVGA3D_CLIPPLANE_MAX == (1 << 5));
4482 for (uint32_t j = 0; j <= 5; j++)
4483 {
4484 if (pRenderState[i].uintValue & RT_BIT(j))
4485 glEnable(GL_CLIP_PLANE0 + j);
4486 else
4487 glDisable(GL_CLIP_PLANE0 + j);
4488 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4489 }
4490 break;
4491 }
4492
4493 case SVGA3D_RS_FOGCOLOR: /* SVGA3dColor */
4494 {
4495 GLfloat color[4]; /* red, green, blue, alpha */
4496
4497 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &color[0], &color[1], &color[2], &color[3]);
4498
4499 glFogfv(GL_FOG_COLOR, color);
4500 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4501 break;
4502 }
4503
4504 case SVGA3D_RS_FOGSTART: /* float */
4505 glFogf(GL_FOG_START, pRenderState[i].floatValue);
4506 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4507 break;
4508
4509 case SVGA3D_RS_FOGEND: /* float */
4510 glFogf(GL_FOG_END, pRenderState[i].floatValue);
4511 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4512 break;
4513
4514 case SVGA3D_RS_FOGDENSITY: /* float */
4515 glFogf(GL_FOG_DENSITY, pRenderState[i].floatValue);
4516 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4517 break;
4518
4519 case SVGA3D_RS_RANGEFOGENABLE: /* SVGA3dBool */
4520 glFogi(GL_FOG_COORD_SRC, (pRenderState[i].uintValue) ? GL_FOG_COORD : GL_FRAGMENT_DEPTH);
4521 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4522 break;
4523
4524 case SVGA3D_RS_FOGMODE: /* SVGA3dFogMode */
4525 {
4526 SVGA3dFogMode mode;
4527 mode.uintValue = pRenderState[i].uintValue;
4528
4529 enableCap = GL_FOG_MODE;
4530 switch (mode.s.function)
4531 {
4532 case SVGA3D_FOGFUNC_EXP:
4533 val = GL_EXP;
4534 break;
4535 case SVGA3D_FOGFUNC_EXP2:
4536 val = GL_EXP2;
4537 break;
4538 case SVGA3D_FOGFUNC_LINEAR:
4539 val = GL_LINEAR;
4540 break;
4541 default:
4542 AssertMsgFailedReturn(("Unexpected fog function %d\n", mode.s.function), VERR_INTERNAL_ERROR);
4543 break;
4544 }
4545
4546 /** @todo how to switch between vertex and pixel fog modes??? */
4547 Assert(mode.s.type == SVGA3D_FOGTYPE_PIXEL);
4548#if 0
4549 /* The fog type determines the render state. */
4550 switch (mode.s.type)
4551 {
4552 case SVGA3D_FOGTYPE_VERTEX:
4553 renderState = D3DRS_FOGVERTEXMODE;
4554 break;
4555 case SVGA3D_FOGTYPE_PIXEL:
4556 renderState = D3DRS_FOGTABLEMODE;
4557 break;
4558 default:
4559 AssertMsgFailedReturn(("Unexpected fog type %d\n", mode.s.type), VERR_INTERNAL_ERROR);
4560 break;
4561 }
4562#endif
4563
4564 /* Set the fog base to depth or range. */
4565 switch (mode.s.base)
4566 {
4567 case SVGA3D_FOGBASE_DEPTHBASED:
4568 glFogi(GL_FOG_COORD_SRC, GL_FRAGMENT_DEPTH);
4569 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4570 break;
4571 case SVGA3D_FOGBASE_RANGEBASED:
4572 glFogi(GL_FOG_COORD_SRC, GL_FOG_COORD);
4573 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4574 break;
4575 default:
4576 /* ignore */
4577 AssertMsgFailed(("Unexpected fog base %d\n", mode.s.base));
4578 break;
4579 }
4580 break;
4581 }
4582
4583 case SVGA3D_RS_FILLMODE: /* SVGA3dFillMode */
4584 {
4585 SVGA3dFillMode mode;
4586
4587 mode.uintValue = pRenderState[i].uintValue;
4588
4589 switch (mode.s.mode)
4590 {
4591 case SVGA3D_FILLMODE_POINT:
4592 val = GL_POINT;
4593 break;
4594 case SVGA3D_FILLMODE_LINE:
4595 val = GL_LINE;
4596 break;
4597 case SVGA3D_FILLMODE_FILL:
4598 val = GL_FILL;
4599 break;
4600 default:
4601 AssertMsgFailedReturn(("Unexpected fill mode %d\n", mode.s.mode), VERR_INTERNAL_ERROR);
4602 break;
4603 }
4604 /* Only front and back faces. Also recent Mesa guest drivers initialize the 'face' to zero. */
4605 ASSERT_GUEST(mode.s.face == SVGA3D_FACE_FRONT_BACK || mode.s.face == SVGA3D_FACE_INVALID);
4606 glPolygonMode(GL_FRONT_AND_BACK, val);
4607 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4608 break;
4609 }
4610
4611 case SVGA3D_RS_SHADEMODE: /* SVGA3dShadeMode */
4612 switch (pRenderState[i].uintValue)
4613 {
4614 case SVGA3D_SHADEMODE_FLAT:
4615 val = GL_FLAT;
4616 break;
4617
4618 case SVGA3D_SHADEMODE_SMOOTH:
4619 val = GL_SMOOTH;
4620 break;
4621
4622 default:
4623 AssertMsgFailedReturn(("Unexpected shade mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
4624 break;
4625 }
4626
4627 glShadeModel(val);
4628 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4629 break;
4630
4631 case SVGA3D_RS_LINEPATTERN: /* SVGA3dLinePattern */
4632 /* No longer supported by d3d; mesagl comments suggest not all backends support it */
4633 /** @todo */
4634 Log(("WARNING: SVGA3D_RS_LINEPATTERN %x not supported!!\n", pRenderState[i].uintValue));
4635 /*
4636 renderState = D3DRS_LINEPATTERN;
4637 val = pRenderState[i].uintValue;
4638 */
4639 break;
4640
4641 case SVGA3D_RS_LINEAA: /* SVGA3dBool */
4642 enableCap = GL_LINE_SMOOTH;
4643 val = pRenderState[i].uintValue;
4644 break;
4645
4646 case SVGA3D_RS_LINEWIDTH: /* float */
4647 glLineWidth(pRenderState[i].floatValue);
4648 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4649 break;
4650
4651 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
4652 {
4653 /* Refresh the blending state based on the new enable setting.
4654 * This will take existing states and set them using either glBlend* or glBlend*Separate.
4655 */
4656 static SVGA3dRenderStateName const saRefreshState[] =
4657 {
4658 SVGA3D_RS_SRCBLEND,
4659 SVGA3D_RS_BLENDEQUATION
4660 };
4661 SVGA3dRenderState renderstate[RT_ELEMENTS(saRefreshState)];
4662 for (uint32_t j = 0; j < RT_ELEMENTS(saRefreshState); ++j)
4663 {
4664 renderstate[j].state = saRefreshState[j];
4665 renderstate[j].uintValue = pContext->state.aRenderState[saRefreshState[j]].uintValue;
4666 }
4667
4668 rc = vmsvga3dSetRenderState(pThisCC, cid, 2, renderstate);
4669 AssertRCReturn(rc, rc);
4670
4671 if (pContext->state.aRenderState[SVGA3D_RS_BLENDENABLE].uintValue != 0)
4672 continue; /* Ignore if blend is enabled */
4673 /* Apply SVGA3D_RS_SEPARATEALPHABLENDENABLE as SVGA3D_RS_BLENDENABLE */
4674 } RT_FALL_THRU();
4675
4676 case SVGA3D_RS_BLENDENABLE: /* SVGA3dBool */
4677 enableCap = GL_BLEND;
4678 val = pRenderState[i].uintValue;
4679 break;
4680
4681 case SVGA3D_RS_SRCBLENDALPHA: /* SVGA3dBlendOp */
4682 case SVGA3D_RS_DSTBLENDALPHA: /* SVGA3dBlendOp */
4683 case SVGA3D_RS_SRCBLEND: /* SVGA3dBlendOp */
4684 case SVGA3D_RS_DSTBLEND: /* SVGA3dBlendOp */
4685 {
4686 GLint srcRGB, srcAlpha, dstRGB, dstAlpha;
4687 GLint blendop = vmsvga3dBlendOp2GL(pRenderState[i].uintValue);
4688
4689 glGetIntegerv(GL_BLEND_SRC_RGB, &srcRGB);
4690 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4691 glGetIntegerv(GL_BLEND_DST_RGB, &dstRGB);
4692 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4693 glGetIntegerv(GL_BLEND_DST_ALPHA, &dstAlpha);
4694 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4695 glGetIntegerv(GL_BLEND_SRC_ALPHA, &srcAlpha);
4696 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4697
4698 switch (pRenderState[i].state)
4699 {
4700 case SVGA3D_RS_SRCBLEND:
4701 srcRGB = blendop;
4702 break;
4703 case SVGA3D_RS_DSTBLEND:
4704 dstRGB = blendop;
4705 break;
4706 case SVGA3D_RS_SRCBLENDALPHA:
4707 srcAlpha = blendop;
4708 break;
4709 case SVGA3D_RS_DSTBLENDALPHA:
4710 dstAlpha = blendop;
4711 break;
4712 default:
4713 /* not possible; shut up gcc */
4714 AssertFailed();
4715 break;
4716 }
4717
4718 if (pContext->state.aRenderState[SVGA3D_RS_SEPARATEALPHABLENDENABLE].uintValue != 0)
4719 pState->ext.glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
4720 else
4721 glBlendFunc(srcRGB, dstRGB);
4722 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4723 break;
4724 }
4725
4726 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation */
4727 case SVGA3D_RS_BLENDEQUATION: /* SVGA3dBlendEquation */
4728 if (pContext->state.aRenderState[SVGA3D_RS_SEPARATEALPHABLENDENABLE].uintValue != 0)
4729 {
4730 GLenum const modeRGB = vmsvga3dBlendEquation2GL(pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATION].uintValue);
4731 GLenum const modeAlpha = vmsvga3dBlendEquation2GL(pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATIONALPHA].uintValue);
4732 pState->ext.glBlendEquationSeparate(modeRGB, modeAlpha);
4733 }
4734 else
4735 {
4736#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x102
4737 glBlendEquation(vmsvga3dBlendEquation2GL(pRenderState[i].uintValue));
4738#else
4739 pState->ext.glBlendEquation(vmsvga3dBlendEquation2GL(pRenderState[i].uintValue));
4740#endif
4741 }
4742 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4743 break;
4744
4745 case SVGA3D_RS_BLENDCOLOR: /* SVGA3dColor */
4746 {
4747 GLfloat red, green, blue, alpha;
4748
4749 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &red, &green, &blue, &alpha);
4750
4751#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x102
4752 glBlendColor(red, green, blue, alpha);
4753#else
4754 pState->ext.glBlendColor(red, green, blue, alpha);
4755#endif
4756 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4757 break;
4758 }
4759
4760 case SVGA3D_RS_CULLMODE: /* SVGA3dFace */
4761 {
4762 GLenum mode = GL_BACK; /* default for OpenGL */
4763
4764 switch (pRenderState[i].uintValue)
4765 {
4766 case SVGA3D_FACE_NONE:
4767 break;
4768 case SVGA3D_FACE_FRONT:
4769 mode = GL_FRONT;
4770 break;
4771 case SVGA3D_FACE_BACK:
4772 mode = GL_BACK;
4773 break;
4774 case SVGA3D_FACE_FRONT_BACK:
4775 mode = GL_FRONT_AND_BACK;
4776 break;
4777 default:
4778 AssertMsgFailedReturn(("Unexpected cull mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
4779 break;
4780 }
4781 enableCap = GL_CULL_FACE;
4782 if (pRenderState[i].uintValue != SVGA3D_FACE_NONE)
4783 {
4784 glCullFace(mode);
4785 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4786 val = 1;
4787 }
4788 else
4789 val = 0;
4790 break;
4791 }
4792
4793 case SVGA3D_RS_ZFUNC: /* SVGA3dCmpFunc */
4794 glDepthFunc(vmsvgaCmpFunc2GL(pRenderState[i].uintValue));
4795 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4796 break;
4797
4798 case SVGA3D_RS_ALPHAFUNC: /* SVGA3dCmpFunc */
4799 {
4800 GLclampf ref;
4801
4802 glGetFloatv(GL_ALPHA_TEST_REF, &ref);
4803 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4804 glAlphaFunc(vmsvgaCmpFunc2GL(pRenderState[i].uintValue), ref);
4805 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4806 break;
4807 }
4808
4809 case SVGA3D_RS_ALPHAREF: /* float (0.0 .. 1.0) */
4810 {
4811 GLint func;
4812
4813 glGetIntegerv(GL_ALPHA_TEST_FUNC, &func);
4814 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4815 glAlphaFunc(func, pRenderState[i].floatValue);
4816 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4817 break;
4818 }
4819
4820 case SVGA3D_RS_STENCILENABLE2SIDED: /* SVGA3dBool */
4821 {
4822 /* Refresh the stencil state based on the new enable setting.
4823 * This will take existing states and set them using either glStencil or glStencil*Separate.
4824 */
4825 static SVGA3dRenderStateName const saRefreshState[] =
4826 {
4827 SVGA3D_RS_STENCILFUNC,
4828 SVGA3D_RS_STENCILFAIL,
4829 SVGA3D_RS_CCWSTENCILFUNC,
4830 SVGA3D_RS_CCWSTENCILFAIL
4831 };
4832 SVGA3dRenderState renderstate[RT_ELEMENTS(saRefreshState)];
4833 for (uint32_t j = 0; j < RT_ELEMENTS(saRefreshState); ++j)
4834 {
4835 renderstate[j].state = saRefreshState[j];
4836 renderstate[j].uintValue = pContext->state.aRenderState[saRefreshState[j]].uintValue;
4837 }
4838
4839 rc = vmsvga3dSetRenderState(pThisCC, cid, RT_ELEMENTS(renderstate), renderstate);
4840 AssertRCReturn(rc, rc);
4841
4842 if (pContext->state.aRenderState[SVGA3D_RS_STENCILENABLE].uintValue != 0)
4843 continue; /* Ignore if stencil is enabled */
4844 /* Apply SVGA3D_RS_STENCILENABLE2SIDED as SVGA3D_RS_STENCILENABLE. */
4845 } RT_FALL_THRU();
4846
4847 case SVGA3D_RS_STENCILENABLE: /* SVGA3dBool */
4848 enableCap = GL_STENCIL_TEST;
4849 val = pRenderState[i].uintValue;
4850 break;
4851
4852 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc */
4853 case SVGA3D_RS_STENCILREF: /* uint32_t */
4854 case SVGA3D_RS_STENCILMASK: /* uint32_t */
4855 {
4856 GLint func, ref;
4857 GLuint mask;
4858
4859 /* Query current values to have all parameters for glStencilFunc[Separate]. */
4860 glGetIntegerv(GL_STENCIL_FUNC, &func);
4861 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4862 glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)&mask);
4863 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4864 glGetIntegerv(GL_STENCIL_REF, &ref);
4865 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4866
4867 /* Update the changed value. */
4868 switch (pRenderState[i].state)
4869 {
4870 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc */
4871 func = vmsvgaCmpFunc2GL(pRenderState[i].uintValue);
4872 break;
4873
4874 case SVGA3D_RS_STENCILREF: /* uint32_t */
4875 ref = pRenderState[i].uintValue;
4876 break;
4877
4878 case SVGA3D_RS_STENCILMASK: /* uint32_t */
4879 mask = pRenderState[i].uintValue;
4880 break;
4881
4882 default:
4883 /* not possible; shut up gcc */
4884 AssertFailed();
4885 break;
4886 }
4887
4888 if (pContext->state.aRenderState[SVGA3D_RS_STENCILENABLE2SIDED].uintValue != 0)
4889 {
4890 pState->ext.glStencilFuncSeparate(GL_FRONT, func, ref, mask);
4891 }
4892 else
4893 {
4894 glStencilFunc(func, ref, mask);
4895 }
4896 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4897 break;
4898 }
4899
4900 case SVGA3D_RS_STENCILWRITEMASK: /* uint32_t */
4901 glStencilMask(pRenderState[i].uintValue);
4902 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4903 break;
4904
4905 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp */
4906 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp */
4907 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp */
4908 {
4909 GLint sfail, dpfail, dppass;
4910 GLenum const stencilop = vmsvgaStencipOp2GL(pRenderState[i].uintValue);
4911
4912 glGetIntegerv(GL_STENCIL_FAIL, &sfail);
4913 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4914 glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, &dpfail);
4915 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4916 glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, &dppass);
4917 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4918
4919 switch (pRenderState[i].state)
4920 {
4921 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp */
4922 sfail = stencilop;
4923 break;
4924 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp */
4925 dpfail = stencilop;
4926 break;
4927 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp */
4928 dppass = stencilop;
4929 break;
4930 default:
4931 /* not possible; shut up gcc */
4932 AssertFailed();
4933 break;
4934 }
4935 if (pContext->state.aRenderState[SVGA3D_RS_STENCILENABLE2SIDED].uintValue != 0)
4936 {
4937 pState->ext.glStencilOpSeparate(GL_FRONT, sfail, dpfail, dppass);
4938 }
4939 else
4940 {
4941 glStencilOp(sfail, dpfail, dppass);
4942 }
4943 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4944 break;
4945 }
4946
4947 case SVGA3D_RS_CCWSTENCILFUNC: /* SVGA3dCmpFunc */
4948 {
4949 GLint ref;
4950 GLuint mask;
4951 GLint const func = vmsvgaCmpFunc2GL(pRenderState[i].uintValue);
4952
4953 /* GL_STENCIL_VALUE_MASK and GL_STENCIL_REF are the same for both GL_FRONT and GL_BACK. */
4954 glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)&mask);
4955 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4956 glGetIntegerv(GL_STENCIL_REF, &ref);
4957 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4958
4959 pState->ext.glStencilFuncSeparate(GL_BACK, func, ref, mask);
4960 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4961 break;
4962 }
4963
4964 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp */
4965 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp */
4966 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp */
4967 {
4968 GLint sfail, dpfail, dppass;
4969 GLenum const stencilop = vmsvgaStencipOp2GL(pRenderState[i].uintValue);
4970
4971 glGetIntegerv(GL_STENCIL_BACK_FAIL, &sfail);
4972 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4973 glGetIntegerv(GL_STENCIL_BACK_PASS_DEPTH_FAIL, &dpfail);
4974 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4975 glGetIntegerv(GL_STENCIL_BACK_PASS_DEPTH_PASS, &dppass);
4976 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4977
4978 switch (pRenderState[i].state)
4979 {
4980 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp */
4981 sfail = stencilop;
4982 break;
4983 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp */
4984 dpfail = stencilop;
4985 break;
4986 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp */
4987 dppass = stencilop;
4988 break;
4989 default:
4990 /* not possible; shut up gcc */
4991 AssertFailed();
4992 break;
4993 }
4994 pState->ext.glStencilOpSeparate(GL_BACK, sfail, dpfail, dppass);
4995 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4996 break;
4997 }
4998
4999 case SVGA3D_RS_ZBIAS: /* float */
5000 /** @todo unknown meaning; depth bias is not identical
5001 renderState = D3DRS_DEPTHBIAS;
5002 val = pRenderState[i].uintValue;
5003 */
5004 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_ZBIAS\n"));
5005 break;
5006
5007 case SVGA3D_RS_DEPTHBIAS: /* float */
5008 {
5009 GLfloat factor;
5010
5011 /** @todo not sure if the d3d & ogl definitions are identical. */
5012
5013 /* Do not change the factor part. */
5014 glGetFloatv(GL_POLYGON_OFFSET_FACTOR, &factor);
5015 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5016
5017 glPolygonOffset(factor, pRenderState[i].floatValue);
5018 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5019 break;
5020 }
5021
5022 case SVGA3D_RS_SLOPESCALEDEPTHBIAS: /* float */
5023 {
5024 GLfloat units;
5025
5026 /** @todo not sure if the d3d & ogl definitions are identical. */
5027
5028 /* Do not change the factor part. */
5029 glGetFloatv(GL_POLYGON_OFFSET_UNITS, &units);
5030 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5031
5032 glPolygonOffset(pRenderState[i].floatValue, units);
5033 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5034 break;
5035 }
5036
5037 case SVGA3D_RS_COLORWRITEENABLE: /* SVGA3dColorMask */
5038 {
5039 GLboolean red, green, blue, alpha;
5040 SVGA3dColorMask mask;
5041
5042 mask.uintValue = pRenderState[i].uintValue;
5043
5044 red = mask.s.red;
5045 green = mask.s.green;
5046 blue = mask.s.blue;
5047 alpha = mask.s.alpha;
5048
5049 glColorMask(red, green, blue, alpha);
5050 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5051 break;
5052 }
5053
5054 case SVGA3D_RS_COLORWRITEENABLE1: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5055 case SVGA3D_RS_COLORWRITEENABLE2: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5056 case SVGA3D_RS_COLORWRITEENABLE3: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5057 Log(("vmsvga3dSetRenderState: WARNING SVGA3D_RS_COLORWRITEENABLEx not supported!!\n"));
5058 break;
5059
5060 case SVGA3D_RS_SCISSORTESTENABLE: /* SVGA3dBool */
5061 enableCap = GL_SCISSOR_TEST;
5062 val = pRenderState[i].uintValue;
5063 break;
5064
5065#if 0
5066 case SVGA3D_RS_DIFFUSEMATERIALSOURCE: /* SVGA3dVertexMaterial */
5067 AssertCompile(D3DMCS_COLOR2 == SVGA3D_VERTEXMATERIAL_SPECULAR);
5068 renderState = D3DRS_DIFFUSEMATERIALSOURCE;
5069 val = pRenderState[i].uintValue;
5070 break;
5071
5072 case SVGA3D_RS_SPECULARMATERIALSOURCE: /* SVGA3dVertexMaterial */
5073 renderState = D3DRS_SPECULARMATERIALSOURCE;
5074 val = pRenderState[i].uintValue;
5075 break;
5076
5077 case SVGA3D_RS_AMBIENTMATERIALSOURCE: /* SVGA3dVertexMaterial */
5078 renderState = D3DRS_AMBIENTMATERIALSOURCE;
5079 val = pRenderState[i].uintValue;
5080 break;
5081
5082 case SVGA3D_RS_EMISSIVEMATERIALSOURCE: /* SVGA3dVertexMaterial */
5083 renderState = D3DRS_EMISSIVEMATERIALSOURCE;
5084 val = pRenderState[i].uintValue;
5085 break;
5086#endif
5087
5088 case SVGA3D_RS_WRAP3: /* SVGA3dWrapFlags */
5089 case SVGA3D_RS_WRAP4: /* SVGA3dWrapFlags */
5090 case SVGA3D_RS_WRAP5: /* SVGA3dWrapFlags */
5091 case SVGA3D_RS_WRAP6: /* SVGA3dWrapFlags */
5092 case SVGA3D_RS_WRAP7: /* SVGA3dWrapFlags */
5093 case SVGA3D_RS_WRAP8: /* SVGA3dWrapFlags */
5094 case SVGA3D_RS_WRAP9: /* SVGA3dWrapFlags */
5095 case SVGA3D_RS_WRAP10: /* SVGA3dWrapFlags */
5096 case SVGA3D_RS_WRAP11: /* SVGA3dWrapFlags */
5097 case SVGA3D_RS_WRAP12: /* SVGA3dWrapFlags */
5098 case SVGA3D_RS_WRAP13: /* SVGA3dWrapFlags */
5099 case SVGA3D_RS_WRAP14: /* SVGA3dWrapFlags */
5100 case SVGA3D_RS_WRAP15: /* SVGA3dWrapFlags */
5101 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_WRAPx (x >= 3)\n"));
5102 break;
5103
5104 case SVGA3D_RS_LASTPIXEL: /* SVGA3dBool */
5105 case SVGA3D_RS_TWEENFACTOR: /* float */
5106 case SVGA3D_RS_INDEXEDVERTEXBLENDENABLE: /* SVGA3dBool */
5107 case SVGA3D_RS_VERTEXBLEND: /* SVGA3dVertexBlendFlags */
5108 Log(("vmsvga3dSetRenderState: WARNING not applicable!!\n"));
5109 break;
5110
5111 case SVGA3D_RS_MULTISAMPLEANTIALIAS: /* SVGA3dBool */
5112 enableCap = GL_MULTISAMPLE;
5113 val = pRenderState[i].uintValue;
5114 break;
5115
5116 case SVGA3D_RS_MULTISAMPLEMASK: /* uint32_t */
5117 case SVGA3D_RS_ANTIALIASEDLINEENABLE: /* SVGA3dBool */
5118 Log(("vmsvga3dSetRenderState: WARNING not applicable??!!\n"));
5119 break;
5120
5121 case SVGA3D_RS_COORDINATETYPE: /* SVGA3dCoordinateType */
5122 Assert(pRenderState[i].uintValue == SVGA3D_COORDINATE_LEFTHANDED);
5123 /** @todo setup a view matrix to scale the world space by -1 in the z-direction for right handed coordinates. */
5124 /*
5125 renderState = D3DRS_COORDINATETYPE;
5126 val = pRenderState[i].uintValue;
5127 */
5128 break;
5129
5130 case SVGA3D_RS_FRONTWINDING: /* SVGA3dFrontWinding */
5131 Assert(pRenderState[i].uintValue == SVGA3D_FRONTWINDING_CW);
5132 /* Invert the selected mode because of y-inversion (?) */
5133 glFrontFace((pRenderState[i].uintValue != SVGA3D_FRONTWINDING_CW) ? GL_CW : GL_CCW);
5134 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5135 break;
5136
5137 case SVGA3D_RS_OUTPUTGAMMA: /* float */
5138 //AssertFailed();
5139 /*
5140 D3DRS_SRGBWRITEENABLE ??
5141 renderState = D3DRS_OUTPUTGAMMA;
5142 val = pRenderState[i].uintValue;
5143 */
5144 break;
5145
5146#if 0
5147
5148 case SVGA3D_RS_VERTEXMATERIALENABLE: /* SVGA3dBool */
5149 //AssertFailed();
5150 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE; /* correct?? */
5151 val = pRenderState[i].uintValue;
5152 break;
5153
5154 case SVGA3D_RS_TEXTUREFACTOR: /* SVGA3dColor */
5155 renderState = D3DRS_TEXTUREFACTOR;
5156 val = pRenderState[i].uintValue;
5157 break;
5158
5159 case SVGA3D_RS_LOCALVIEWER: /* SVGA3dBool */
5160 renderState = D3DRS_LOCALVIEWER;
5161 val = pRenderState[i].uintValue;
5162 break;
5163
5164 case SVGA3D_RS_ZVISIBLE: /* SVGA3dBool */
5165 AssertFailed();
5166 /*
5167 renderState = D3DRS_ZVISIBLE;
5168 val = pRenderState[i].uintValue;
5169 */
5170 break;
5171
5172 case SVGA3D_RS_CLIPPING: /* SVGA3dBool */
5173 renderState = D3DRS_CLIPPING;
5174 val = pRenderState[i].uintValue;
5175 break;
5176
5177 case SVGA3D_RS_WRAP0: /* SVGA3dWrapFlags */
5178 glTexParameter GL_TEXTURE_WRAP_S
5179 Assert(SVGA3D_WRAPCOORD_3 == D3DWRAPCOORD_3);
5180 renderState = D3DRS_WRAP0;
5181 val = pRenderState[i].uintValue;
5182 break;
5183
5184 case SVGA3D_RS_WRAP1: /* SVGA3dWrapFlags */
5185 glTexParameter GL_TEXTURE_WRAP_T
5186 renderState = D3DRS_WRAP1;
5187 val = pRenderState[i].uintValue;
5188 break;
5189
5190 case SVGA3D_RS_WRAP2: /* SVGA3dWrapFlags */
5191 glTexParameter GL_TEXTURE_WRAP_R
5192 renderState = D3DRS_WRAP2;
5193 val = pRenderState[i].uintValue;
5194 break;
5195
5196
5197 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
5198 renderState = D3DRS_SEPARATEALPHABLENDENABLE;
5199 val = pRenderState[i].uintValue;
5200 break;
5201
5202
5203 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation */
5204 renderState = D3DRS_BLENDOPALPHA;
5205 val = pRenderState[i].uintValue;
5206 break;
5207
5208 case SVGA3D_RS_TRANSPARENCYANTIALIAS: /* SVGA3dTransparencyAntialiasType */
5209 AssertFailed();
5210 /*
5211 renderState = D3DRS_TRANSPARENCYANTIALIAS;
5212 val = pRenderState[i].uintValue;
5213 */
5214 break;
5215
5216#endif
5217 default:
5218 AssertFailed();
5219 break;
5220 }
5221
5222 if (enableCap != ~(GLenum)0)
5223 {
5224 if (val)
5225 glEnable(enableCap);
5226 else
5227 glDisable(enableCap);
5228 }
5229 }
5230
5231 return VINF_SUCCESS;
5232}
5233
5234int vmsvga3dSetRenderTarget(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRenderTargetType type, SVGA3dSurfaceImageId target)
5235{
5236 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5237
5238 AssertReturn(pState, VERR_NO_MEMORY);
5239 AssertReturn((unsigned)type < SVGA3D_RT_MAX, VERR_INVALID_PARAMETER);
5240
5241 LogFunc(("cid=%u type=%x sid=%u\n", cid, type, target.sid));
5242
5243 PVMSVGA3DCONTEXT pContext;
5244 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5245 AssertRCReturn(rc, rc);
5246
5247 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5248
5249 /* Save for vm state save/restore. */
5250 pContext->state.aRenderTargets[type] = target.sid;
5251
5252 if (target.sid == SVGA3D_INVALID_ID)
5253 {
5254 /* Disable render target. */
5255 switch (type)
5256 {
5257 case SVGA3D_RT_DEPTH:
5258 case SVGA3D_RT_STENCIL:
5259 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER, (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
5260 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5261 break;
5262
5263 case SVGA3D_RT_COLOR0:
5264 case SVGA3D_RT_COLOR1:
5265 case SVGA3D_RT_COLOR2:
5266 case SVGA3D_RT_COLOR3:
5267 case SVGA3D_RT_COLOR4:
5268 case SVGA3D_RT_COLOR5:
5269 case SVGA3D_RT_COLOR6:
5270 case SVGA3D_RT_COLOR7:
5271 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0, 0, 0, 0);
5272 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5273 break;
5274
5275 default:
5276 AssertFailedReturn(VERR_INVALID_PARAMETER);
5277 }
5278 return VINF_SUCCESS;
5279 }
5280
5281 PVMSVGA3DSURFACE pRenderTarget;
5282 rc = vmsvga3dSurfaceFromSid(pState, target.sid, &pRenderTarget);
5283 AssertRCReturn(rc, rc);
5284
5285 switch (type)
5286 {
5287 case SVGA3D_RT_DEPTH:
5288 case SVGA3D_RT_STENCIL:
5289#if 1
5290 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
5291 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
5292 {
5293 LogFunc(("create depth texture to be used as render target; surface id=%x type=%d format=%d -> create texture\n",
5294 target.sid, pRenderTarget->surfaceFlags, pRenderTarget->format));
5295 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
5296 AssertRCReturn(rc, rc);
5297 }
5298
5299 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
5300 Assert(!pRenderTarget->fDirty);
5301
5302 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
5303
5304 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER,
5305 (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT,
5306 GL_TEXTURE_2D, pRenderTarget->oglId.texture, target.mipmap);
5307 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5308#else
5309 AssertReturn(target.mipmap == 0, VERR_INVALID_PARAMETER);
5310 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
5311 {
5312 Log(("vmsvga3dSetRenderTarget: create renderbuffer to be used as render target; surface id=%x type=%d format=%d\n", target.sid, pRenderTarget->surfaceFlags, pRenderTarget->internalFormatGL));
5313 pContext = &pState->SharedCtx;
5314 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5315
5316 pState->ext.glGenRenderbuffers(1, &pRenderTarget->oglId.renderbuffer);
5317 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5318 pSurface->enmOGLResType = VMSVGA3D_OGLRESTYPE_RENDERBUFFER;
5319
5320 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5321 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5322
5323 pState->ext.glRenderbufferStorage(GL_RENDERBUFFER,
5324 pRenderTarget->internalFormatGL,
5325 pRenderTarget->paMipmapLevels[0].mipmapSize.width,
5326 pRenderTarget->paMipmapLevels[0].mipmapSize.height);
5327 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5328
5329 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, OPENGL_INVALID_ID);
5330 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5331
5332 pContext = pState->papContexts[cid];
5333 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5334 pRenderTarget->idWeakContextAssociation = cid;
5335 }
5336
5337 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5338 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5339 Assert(!pRenderTarget->fDirty);
5340 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
5341
5342 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
5343
5344 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
5345 (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT,
5346 GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5347 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5348#endif
5349 break;
5350
5351 case SVGA3D_RT_COLOR0:
5352 case SVGA3D_RT_COLOR1:
5353 case SVGA3D_RT_COLOR2:
5354 case SVGA3D_RT_COLOR3:
5355 case SVGA3D_RT_COLOR4:
5356 case SVGA3D_RT_COLOR5:
5357 case SVGA3D_RT_COLOR6:
5358 case SVGA3D_RT_COLOR7:
5359 {
5360 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
5361 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
5362 {
5363 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));
5364 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
5365 AssertRCReturn(rc, rc);
5366 }
5367
5368 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
5369 Assert(!pRenderTarget->fDirty);
5370
5371 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
5372
5373 GLenum textarget;
5374 if (pRenderTarget->surfaceFlags & SVGA3D_SURFACE_CUBEMAP)
5375 textarget = vmsvga3dCubemapFaceFromIndex(target.face);
5376 else
5377 textarget = GL_TEXTURE_2D;
5378 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0,
5379 textarget, pRenderTarget->oglId.texture, target.mipmap);
5380 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5381
5382#ifdef DEBUG
5383 GLenum status = pState->ext.glCheckFramebufferStatus(GL_FRAMEBUFFER);
5384 if (status != GL_FRAMEBUFFER_COMPLETE)
5385 Log(("vmsvga3dSetRenderTarget: WARNING: glCheckFramebufferStatus returned %x\n", status));
5386#endif
5387 /** @todo use glDrawBuffers too? */
5388 break;
5389 }
5390
5391 default:
5392 AssertFailedReturn(VERR_INVALID_PARAMETER);
5393 }
5394
5395 return VINF_SUCCESS;
5396}
5397
5398#if 0
5399/**
5400 * Convert SVGA texture combiner value to its D3D equivalent
5401 */
5402static DWORD vmsvga3dTextureCombiner2D3D(uint32_t value)
5403{
5404 switch (value)
5405 {
5406 case SVGA3D_TC_DISABLE:
5407 return D3DTOP_DISABLE;
5408 case SVGA3D_TC_SELECTARG1:
5409 return D3DTOP_SELECTARG1;
5410 case SVGA3D_TC_SELECTARG2:
5411 return D3DTOP_SELECTARG2;
5412 case SVGA3D_TC_MODULATE:
5413 return D3DTOP_MODULATE;
5414 case SVGA3D_TC_ADD:
5415 return D3DTOP_ADD;
5416 case SVGA3D_TC_ADDSIGNED:
5417 return D3DTOP_ADDSIGNED;
5418 case SVGA3D_TC_SUBTRACT:
5419 return D3DTOP_SUBTRACT;
5420 case SVGA3D_TC_BLENDTEXTUREALPHA:
5421 return D3DTOP_BLENDTEXTUREALPHA;
5422 case SVGA3D_TC_BLENDDIFFUSEALPHA:
5423 return D3DTOP_BLENDDIFFUSEALPHA;
5424 case SVGA3D_TC_BLENDCURRENTALPHA:
5425 return D3DTOP_BLENDCURRENTALPHA;
5426 case SVGA3D_TC_BLENDFACTORALPHA:
5427 return D3DTOP_BLENDFACTORALPHA;
5428 case SVGA3D_TC_MODULATE2X:
5429 return D3DTOP_MODULATE2X;
5430 case SVGA3D_TC_MODULATE4X:
5431 return D3DTOP_MODULATE4X;
5432 case SVGA3D_TC_DSDT:
5433 AssertFailed(); /** @todo ??? */
5434 return D3DTOP_DISABLE;
5435 case SVGA3D_TC_DOTPRODUCT3:
5436 return D3DTOP_DOTPRODUCT3;
5437 case SVGA3D_TC_BLENDTEXTUREALPHAPM:
5438 return D3DTOP_BLENDTEXTUREALPHAPM;
5439 case SVGA3D_TC_ADDSIGNED2X:
5440 return D3DTOP_ADDSIGNED2X;
5441 case SVGA3D_TC_ADDSMOOTH:
5442 return D3DTOP_ADDSMOOTH;
5443 case SVGA3D_TC_PREMODULATE:
5444 return D3DTOP_PREMODULATE;
5445 case SVGA3D_TC_MODULATEALPHA_ADDCOLOR:
5446 return D3DTOP_MODULATEALPHA_ADDCOLOR;
5447 case SVGA3D_TC_MODULATECOLOR_ADDALPHA:
5448 return D3DTOP_MODULATECOLOR_ADDALPHA;
5449 case SVGA3D_TC_MODULATEINVALPHA_ADDCOLOR:
5450 return D3DTOP_MODULATEINVALPHA_ADDCOLOR;
5451 case SVGA3D_TC_MODULATEINVCOLOR_ADDALPHA:
5452 return D3DTOP_MODULATEINVCOLOR_ADDALPHA;
5453 case SVGA3D_TC_BUMPENVMAPLUMINANCE:
5454 return D3DTOP_BUMPENVMAPLUMINANCE;
5455 case SVGA3D_TC_MULTIPLYADD:
5456 return D3DTOP_MULTIPLYADD;
5457 case SVGA3D_TC_LERP:
5458 return D3DTOP_LERP;
5459 default:
5460 AssertFailed();
5461 return D3DTOP_DISABLE;
5462 }
5463}
5464
5465/**
5466 * Convert SVGA texture arg data value to its D3D equivalent
5467 */
5468static DWORD vmsvga3dTextureArgData2D3D(uint32_t value)
5469{
5470 switch (value)
5471 {
5472 case SVGA3D_TA_CONSTANT:
5473 return D3DTA_CONSTANT;
5474 case SVGA3D_TA_PREVIOUS:
5475 return D3DTA_CURRENT; /* current = previous */
5476 case SVGA3D_TA_DIFFUSE:
5477 return D3DTA_DIFFUSE;
5478 case SVGA3D_TA_TEXTURE:
5479 return D3DTA_TEXTURE;
5480 case SVGA3D_TA_SPECULAR:
5481 return D3DTA_SPECULAR;
5482 default:
5483 AssertFailed();
5484 return 0;
5485 }
5486}
5487
5488/**
5489 * Convert SVGA texture transform flag value to its D3D equivalent
5490 */
5491static DWORD vmsvga3dTextTransformFlags2D3D(uint32_t value)
5492{
5493 switch (value)
5494 {
5495 case SVGA3D_TEX_TRANSFORM_OFF:
5496 return D3DTTFF_DISABLE;
5497 case SVGA3D_TEX_TRANSFORM_S:
5498 return D3DTTFF_COUNT1; /** @todo correct? */
5499 case SVGA3D_TEX_TRANSFORM_T:
5500 return D3DTTFF_COUNT2; /** @todo correct? */
5501 case SVGA3D_TEX_TRANSFORM_R:
5502 return D3DTTFF_COUNT3; /** @todo correct? */
5503 case SVGA3D_TEX_TRANSFORM_Q:
5504 return D3DTTFF_COUNT4; /** @todo correct? */
5505 case SVGA3D_TEX_PROJECTED:
5506 return D3DTTFF_PROJECTED;
5507 default:
5508 AssertFailed();
5509 return 0;
5510 }
5511}
5512#endif
5513
5514static GLenum vmsvga3dTextureAddress2OGL(SVGA3dTextureAddress value)
5515{
5516 switch (value)
5517 {
5518 case SVGA3D_TEX_ADDRESS_WRAP:
5519 return GL_REPEAT;
5520 case SVGA3D_TEX_ADDRESS_MIRROR:
5521 return GL_MIRRORED_REPEAT;
5522 case SVGA3D_TEX_ADDRESS_CLAMP:
5523 return GL_CLAMP_TO_EDGE;
5524 case SVGA3D_TEX_ADDRESS_BORDER:
5525 return GL_CLAMP_TO_BORDER;
5526 case SVGA3D_TEX_ADDRESS_MIRRORONCE:
5527 AssertFailed();
5528 return GL_CLAMP_TO_EDGE_SGIS; /** @todo correct? */
5529
5530 case SVGA3D_TEX_ADDRESS_EDGE:
5531 case SVGA3D_TEX_ADDRESS_INVALID:
5532 default:
5533 AssertFailed();
5534 return GL_REPEAT; /* default */
5535 }
5536}
5537
5538static GLenum vmsvga3dTextureFilter2OGL(SVGA3dTextureFilter value)
5539{
5540 switch (value)
5541 {
5542 case SVGA3D_TEX_FILTER_NONE:
5543 case SVGA3D_TEX_FILTER_LINEAR:
5544 case SVGA3D_TEX_FILTER_ANISOTROPIC: /* Anisotropic filtering is controlled by SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL */
5545 return GL_LINEAR;
5546 case SVGA3D_TEX_FILTER_NEAREST:
5547 return GL_NEAREST;
5548 case SVGA3D_TEX_FILTER_FLATCUBIC: // Deprecated, not implemented
5549 case SVGA3D_TEX_FILTER_GAUSSIANCUBIC: // Deprecated, not implemented
5550 case SVGA3D_TEX_FILTER_PYRAMIDALQUAD: // Not currently implemented
5551 case SVGA3D_TEX_FILTER_GAUSSIANQUAD: // Not currently implemented
5552 default:
5553 AssertFailed();
5554 return GL_LINEAR; /* default */
5555 }
5556}
5557
5558uint32_t vmsvga3dSVGA3dColor2RGBA(SVGA3dColor value)
5559{
5560 /* flip the red and blue bytes */
5561 uint8_t blue = value & 0xff;
5562 uint8_t red = (value >> 16) & 0xff;
5563 return (value & 0xff00ff00) | red | (blue << 16);
5564}
5565
5566int vmsvga3dSetTextureState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
5567{
5568 GLenum val = ~(GLenum)0; /* Shut up MSC. */
5569 GLenum currentStage = ~(GLenum)0;
5570 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5571 AssertReturn(pState, VERR_NO_MEMORY);
5572
5573 Log(("vmsvga3dSetTextureState %x cTextureState=%d\n", cid, cTextureStates));
5574
5575 PVMSVGA3DCONTEXT pContext;
5576 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5577 AssertRCReturn(rc, rc);
5578
5579 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5580
5581 /* Which texture is active for the current stage. Needed to use right OpenGL target when setting parameters. */
5582 PVMSVGA3DSURFACE pCurrentTextureSurface = NULL;
5583
5584 for (uint32_t i = 0; i < cTextureStates; ++i)
5585 {
5586 GLenum textureType = ~(GLenum)0;
5587#if 0
5588 GLenum samplerType = ~(GLenum)0;
5589#endif
5590
5591 LogFunc(("cid=%u stage=%d type=%s (%x) val=%x\n",
5592 cid, pTextureState[i].stage, vmsvga3dTextureStateToString(pTextureState[i].name), pTextureState[i].name, pTextureState[i].value));
5593
5594 /* Record the texture state for vm state saving. */
5595 if ( pTextureState[i].stage < RT_ELEMENTS(pContext->state.aTextureStates)
5596 && (unsigned)pTextureState[i].name < RT_ELEMENTS(pContext->state.aTextureStates[0]))
5597 {
5598 pContext->state.aTextureStates[pTextureState[i].stage][pTextureState[i].name] = pTextureState[i];
5599 }
5600
5601 /* Activate the right texture unit for subsequent texture state changes. */
5602 if (pTextureState[i].stage != currentStage || i == 0)
5603 {
5604 /** @todo Is this the appropriate limit for all kinds of textures? It is the
5605 * size of aSidActiveTextures and for binding/unbinding we cannot exceed it. */
5606 if (pTextureState[i].stage < RT_ELEMENTS(pContext->state.aTextureStates))
5607 {
5608 pState->ext.glActiveTexture(GL_TEXTURE0 + pTextureState[i].stage);
5609 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5610 currentStage = pTextureState[i].stage;
5611 }
5612 else
5613 {
5614 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x\n", i, pTextureState[i].stage, pTextureState[i].name));
5615 continue;
5616 }
5617
5618 if (pContext->aSidActiveTextures[currentStage] != SVGA3D_INVALID_ID)
5619 {
5620 rc = vmsvga3dSurfaceFromSid(pState, pContext->aSidActiveTextures[currentStage], &pCurrentTextureSurface);
5621 AssertRCReturn(rc, rc);
5622 }
5623 else
5624 pCurrentTextureSurface = NULL; /* Make sure that no stale pointer is used. */
5625 }
5626
5627 switch (pTextureState[i].name)
5628 {
5629 case SVGA3D_TS_BUMPENVMAT00: /* float */
5630 case SVGA3D_TS_BUMPENVMAT01: /* float */
5631 case SVGA3D_TS_BUMPENVMAT10: /* float */
5632 case SVGA3D_TS_BUMPENVMAT11: /* float */
5633 case SVGA3D_TS_BUMPENVLSCALE: /* float */
5634 case SVGA3D_TS_BUMPENVLOFFSET: /* float */
5635 Log(("vmsvga3dSetTextureState: bump mapping texture options not supported!!\n"));
5636 break;
5637
5638 case SVGA3D_TS_COLOROP: /* SVGA3dTextureCombiner */
5639 case SVGA3D_TS_COLORARG0: /* SVGA3dTextureArgData */
5640 case SVGA3D_TS_COLORARG1: /* SVGA3dTextureArgData */
5641 case SVGA3D_TS_COLORARG2: /* SVGA3dTextureArgData */
5642 case SVGA3D_TS_ALPHAOP: /* SVGA3dTextureCombiner */
5643 case SVGA3D_TS_ALPHAARG0: /* SVGA3dTextureArgData */
5644 case SVGA3D_TS_ALPHAARG1: /* SVGA3dTextureArgData */
5645 case SVGA3D_TS_ALPHAARG2: /* SVGA3dTextureArgData */
5646 /** @todo not used by MesaGL */
5647 Log(("vmsvga3dSetTextureState: colorop/alphaop not yet supported!!\n"));
5648 break;
5649#if 0
5650
5651 case SVGA3D_TS_TEXCOORDINDEX: /* uint32_t */
5652 textureType = D3DTSS_TEXCOORDINDEX;
5653 val = pTextureState[i].value;
5654 break;
5655
5656 case SVGA3D_TS_TEXTURETRANSFORMFLAGS: /* SVGA3dTexTransformFlags */
5657 textureType = D3DTSS_TEXTURETRANSFORMFLAGS;
5658 val = vmsvga3dTextTransformFlags2D3D(pTextureState[i].value);
5659 break;
5660#endif
5661
5662 case SVGA3D_TS_BIND_TEXTURE: /* SVGA3dSurfaceId */
5663 {
5664 uint32_t const sid = pTextureState[i].value;
5665
5666 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture sid=%u replacing sid=%u\n",
5667 currentStage, sid, pContext->aSidActiveTextures[currentStage]));
5668
5669 /* Only if texture actually changed. */ /// @todo needs testing.
5670 if (pContext->aSidActiveTextures[currentStage] != sid)
5671 {
5672 if (pCurrentTextureSurface)
5673 {
5674 /* Unselect the currently associated texture. */
5675 glBindTexture(pCurrentTextureSurface->targetGL, 0);
5676 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5677
5678 if (currentStage < 8)
5679 {
5680 /* Necessary for the fixed pipeline. */
5681 glDisable(pCurrentTextureSurface->targetGL);
5682 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5683 }
5684
5685 pCurrentTextureSurface = NULL;
5686 }
5687
5688 if (sid == SVGA3D_INVALID_ID)
5689 {
5690 Assert(pCurrentTextureSurface == NULL);
5691 }
5692 else
5693 {
5694 PVMSVGA3DSURFACE pSurface;
5695 rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
5696 AssertRCReturn(rc, rc);
5697
5698 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture sid=%u (%d,%d) replacing sid=%u\n",
5699 currentStage, sid, pSurface->paMipmapLevels[0].mipmapSize.width,
5700 pSurface->paMipmapLevels[0].mipmapSize.height, pContext->aSidActiveTextures[currentStage]));
5701
5702 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
5703 {
5704 Log(("CreateTexture (%d,%d) levels=%d\n",
5705 pSurface->paMipmapLevels[0].mipmapSize.width, pSurface->paMipmapLevels[0].mipmapSize.height, pSurface->faces[0].numMipLevels));
5706 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pSurface);
5707 AssertRCReturn(rc, rc);
5708 }
5709
5710 glBindTexture(pSurface->targetGL, pSurface->oglId.texture);
5711 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5712
5713 if (currentStage < 8)
5714 {
5715 /* Necessary for the fixed pipeline. */
5716 glEnable(pSurface->targetGL);
5717 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5718 }
5719
5720 /* Remember the currently active texture. */
5721 pCurrentTextureSurface = pSurface;
5722
5723 /* Recreate the texture state as glBindTexture resets them all (sigh). */
5724 for (uint32_t iStage = 0; iStage < RT_ELEMENTS(pContext->state.aTextureStates); iStage++)
5725 {
5726 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aTextureStates[0]); j++)
5727 {
5728 SVGA3dTextureState *pTextureStateIter = &pContext->state.aTextureStates[iStage][j];
5729
5730 if ( pTextureStateIter->name != SVGA3D_TS_INVALID
5731 && pTextureStateIter->name != SVGA3D_TS_BIND_TEXTURE)
5732 vmsvga3dSetTextureState(pThisCC, pContext->id, 1, pTextureStateIter);
5733 }
5734 }
5735 }
5736
5737 pContext->aSidActiveTextures[currentStage] = sid;
5738 }
5739
5740 /* Finished; continue with the next one. */
5741 continue;
5742 }
5743
5744 case SVGA3D_TS_ADDRESSW: /* SVGA3dTextureAddress */
5745 textureType = GL_TEXTURE_WRAP_R; /* R = W */
5746 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
5747 break;
5748
5749 case SVGA3D_TS_ADDRESSU: /* SVGA3dTextureAddress */
5750 textureType = GL_TEXTURE_WRAP_S; /* S = U */
5751 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
5752 break;
5753
5754 case SVGA3D_TS_ADDRESSV: /* SVGA3dTextureAddress */
5755 textureType = GL_TEXTURE_WRAP_T; /* T = V */
5756 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
5757 break;
5758
5759 case SVGA3D_TS_MIPFILTER: /* SVGA3dTextureFilter */
5760 case SVGA3D_TS_MINFILTER: /* SVGA3dTextureFilter */
5761 {
5762 uint32_t mipFilter = pContext->state.aTextureStates[currentStage][SVGA3D_TS_MIPFILTER].value;
5763 uint32_t minFilter = pContext->state.aTextureStates[currentStage][SVGA3D_TS_MINFILTER].value;
5764
5765 /* If SVGA3D_TS_MIPFILTER is set to NONE, then use SVGA3D_TS_MIPFILTER, otherwise SVGA3D_TS_MIPFILTER enables mipmap minification. */
5766 textureType = GL_TEXTURE_MIN_FILTER;
5767 if (mipFilter != SVGA3D_TEX_FILTER_NONE)
5768 {
5769 if (minFilter == SVGA3D_TEX_FILTER_NEAREST)
5770 {
5771 if (mipFilter == SVGA3D_TEX_FILTER_LINEAR)
5772 val = GL_NEAREST_MIPMAP_LINEAR;
5773 else
5774 val = GL_NEAREST_MIPMAP_NEAREST;
5775 }
5776 else
5777 {
5778 if (mipFilter == SVGA3D_TEX_FILTER_LINEAR)
5779 val = GL_LINEAR_MIPMAP_LINEAR;
5780 else
5781 val = GL_LINEAR_MIPMAP_NEAREST;
5782 }
5783 }
5784 else
5785 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)minFilter);
5786 break;
5787 }
5788
5789 case SVGA3D_TS_MAGFILTER: /* SVGA3dTextureFilter */
5790 textureType = GL_TEXTURE_MAG_FILTER;
5791 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)pTextureState[i].value);
5792 Assert(val == GL_NEAREST || val == GL_LINEAR);
5793 break;
5794
5795 case SVGA3D_TS_BORDERCOLOR: /* SVGA3dColor */
5796 {
5797 GLfloat color[4]; /* red, green, blue, alpha */
5798 vmsvgaColor2GLFloatArray(pTextureState[i].value, &color[0], &color[1], &color[2], &color[3]);
5799
5800 GLenum targetGL;
5801 if (pCurrentTextureSurface)
5802 targetGL = pCurrentTextureSurface->targetGL;
5803 else
5804 {
5805 /* No texture bound, assume 2D. */
5806 targetGL = GL_TEXTURE_2D;
5807 }
5808
5809 glTexParameterfv(targetGL, GL_TEXTURE_BORDER_COLOR, color); /* Identical; default 0.0 identical too */
5810 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5811 break;
5812 }
5813
5814 case SVGA3D_TS_TEXTURE_LOD_BIAS: /* float */
5815 {
5816 GLenum targetGL;
5817 if (pCurrentTextureSurface)
5818 targetGL = pCurrentTextureSurface->targetGL;
5819 else
5820 {
5821 /* No texture bound, assume 2D. */
5822 targetGL = GL_TEXTURE_2D;
5823 }
5824
5825 glTexParameterf(targetGL, GL_TEXTURE_LOD_BIAS, pTextureState[i].floatValue); /* Identical; default 0.0 identical too */
5826 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5827 break;
5828 }
5829
5830 case SVGA3D_TS_TEXTURE_MIPMAP_LEVEL: /* uint32_t */
5831 textureType = GL_TEXTURE_BASE_LEVEL;
5832 val = pTextureState[i].value;
5833 break;
5834
5835 case SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL: /* uint32_t */
5836 if (pState->caps.fTextureFilterAnisotropicSupported)
5837 {
5838 textureType = GL_TEXTURE_MAX_ANISOTROPY_EXT;
5839 val = RT_MIN((GLint)pTextureState[i].value, pState->caps.maxTextureAnisotropy);
5840 } /* otherwise ignore. */
5841 break;
5842
5843#if 0
5844 case SVGA3D_TS_GAMMA: /* float */
5845 samplerType = D3DSAMP_SRGBTEXTURE;
5846 /* Boolean in D3D */
5847 if (pTextureState[i].floatValue == 1.0f)
5848 val = FALSE;
5849 else
5850 val = TRUE;
5851 break;
5852#endif
5853 /* Internal commands, that don't map directly to the SetTextureStageState API. */
5854 case SVGA3D_TS_TEXCOORDGEN: /* SVGA3dTextureCoordGen */
5855 AssertFailed();
5856 break;
5857
5858 default:
5859 //AssertFailed();
5860 break;
5861 }
5862
5863 if (textureType != ~(GLenum)0)
5864 {
5865 GLenum targetGL;
5866 if (pCurrentTextureSurface)
5867 targetGL = pCurrentTextureSurface->targetGL;
5868 else
5869 {
5870 /* No texture bound, assume 2D. */
5871 targetGL = GL_TEXTURE_2D;
5872 }
5873
5874 switch (pTextureState[i].name)
5875 {
5876 case SVGA3D_TS_MINFILTER:
5877 case SVGA3D_TS_MAGFILTER:
5878 {
5879 if (pState->caps.fTextureFilterAnisotropicSupported)
5880 {
5881 uint32_t const anisotropyLevel = (SVGA3dTextureFilter)pTextureState[i].value == SVGA3D_TEX_FILTER_ANISOTROPIC
5882 ? RT_MAX(1, pContext->state.aTextureStates[currentStage][SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL].value)
5883 : 1;
5884 glTexParameteri(targetGL, GL_TEXTURE_MAX_ANISOTROPY_EXT, RT_MIN((GLint)anisotropyLevel, pState->caps.maxTextureAnisotropy));
5885 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5886 }
5887 } break;
5888
5889 default: break;
5890 }
5891
5892 glTexParameteri(targetGL, textureType, val);
5893 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5894 }
5895 }
5896
5897 return VINF_SUCCESS;
5898}
5899
5900int vmsvga3dSetMaterial(PVGASTATECC pThisCC, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
5901{
5902 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5903 AssertReturn(pState, VERR_NO_MEMORY);
5904
5905 LogFunc(("cid=%u face %d\n", cid, face));
5906
5907 PVMSVGA3DCONTEXT pContext;
5908 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5909 AssertRCReturn(rc, rc);
5910
5911 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5912
5913 GLenum oglFace;
5914 switch (face)
5915 {
5916 case SVGA3D_FACE_NONE:
5917 case SVGA3D_FACE_FRONT:
5918 oglFace = GL_FRONT;
5919 break;
5920
5921 case SVGA3D_FACE_BACK:
5922 oglFace = GL_BACK;
5923 break;
5924
5925 case SVGA3D_FACE_FRONT_BACK:
5926 oglFace = GL_FRONT_AND_BACK;
5927 break;
5928
5929 default:
5930 AssertFailedReturn(VERR_INVALID_PARAMETER);
5931 }
5932
5933 /* Save for vm state save/restore. */
5934 pContext->state.aMaterial[face].fValid = true;
5935 pContext->state.aMaterial[face].material = *pMaterial;
5936 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_MATERIAL;
5937
5938 glMaterialfv(oglFace, GL_DIFFUSE, pMaterial->diffuse);
5939 glMaterialfv(oglFace, GL_AMBIENT, pMaterial->ambient);
5940 glMaterialfv(oglFace, GL_SPECULAR, pMaterial->specular);
5941 glMaterialfv(oglFace, GL_EMISSION, pMaterial->emissive);
5942 glMaterialfv(oglFace, GL_SHININESS, &pMaterial->shininess);
5943 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5944
5945 return VINF_SUCCESS;
5946}
5947
5948/** @todo Move into separate library as we are using logic from Wine here. */
5949int vmsvga3dSetLightData(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
5950{
5951 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5952 AssertReturn(pState, VERR_NO_MEMORY);
5953
5954 LogFunc(("vmsvga3dSetLightData cid=%u index=%d type=%d\n", cid, index, pData->type));
5955 ASSERT_GUEST_RETURN(index < SVGA3D_MAX_LIGHTS, VERR_INVALID_PARAMETER);
5956
5957 PVMSVGA3DCONTEXT pContext;
5958 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5959 AssertRCReturn(rc, rc);
5960
5961 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5962
5963 /* Store for vm state save/restore */
5964 pContext->state.aLightData[index].fValidData = true;
5965 pContext->state.aLightData[index].data = *pData;
5966
5967 if ( pData->attenuation0 < 0.0f
5968 || pData->attenuation1 < 0.0f
5969 || pData->attenuation2 < 0.0f)
5970 {
5971 Log(("vmsvga3dSetLightData: invalid negative attenuation values!!\n"));
5972 return VINF_SUCCESS; /* ignore; could crash the GL driver */
5973 }
5974
5975 /* Light settings are affected by the model view in OpenGL, the View transform in direct3d */
5976 glMatrixMode(GL_MODELVIEW);
5977 glPushMatrix();
5978 glLoadMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
5979
5980 glLightfv(GL_LIGHT0 + index, GL_DIFFUSE, pData->diffuse);
5981 glLightfv(GL_LIGHT0 + index, GL_SPECULAR, pData->specular);
5982 glLightfv(GL_LIGHT0 + index, GL_AMBIENT, pData->ambient);
5983
5984 float QuadAttenuation;
5985 if (pData->range * pData->range >= FLT_MIN)
5986 QuadAttenuation = 1.4f / (pData->range * pData->range);
5987 else
5988 QuadAttenuation = 0.0f;
5989
5990 switch (pData->type)
5991 {
5992 case SVGA3D_LIGHTTYPE_POINT:
5993 {
5994 GLfloat position[4];
5995
5996 position[0] = pData->position[0];
5997 position[1] = pData->position[1];
5998 position[2] = pData->position[2];
5999 position[3] = 1.0f;
6000
6001 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
6002 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6003
6004 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
6005 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6006
6007 /* Attenuation - Are these right? guessing... */
6008 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
6009 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6010
6011 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
6012 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6013
6014 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
6015 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6016
6017 /** @todo range */
6018 break;
6019 }
6020
6021 case SVGA3D_LIGHTTYPE_SPOT1:
6022 {
6023 GLfloat exponent;
6024 GLfloat position[4];
6025 const GLfloat pi = 4.0f * atanf(1.0f);
6026
6027 position[0] = pData->position[0];
6028 position[1] = pData->position[1];
6029 position[2] = pData->position[2];
6030 position[3] = 1.0f;
6031
6032 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
6033 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6034
6035 position[0] = pData->direction[0];
6036 position[1] = pData->direction[1];
6037 position[2] = pData->direction[2];
6038 position[3] = 1.0f;
6039
6040 glLightfv(GL_LIGHT0 + index, GL_SPOT_DIRECTION, position);
6041 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6042
6043 /*
6044 * opengl-ish and d3d-ish spot lights use too different models for the
6045 * light "intensity" as a function of the angle towards the main light direction,
6046 * so we only can approximate very roughly.
6047 * however spot lights are rather rarely used in games (if ever used at all).
6048 * furthermore if still used, probably nobody pays attention to such details.
6049 */
6050 if (pData->falloff == 0)
6051 {
6052 /* Falloff = 0 is easy, because d3d's and opengl's spot light equations have the
6053 * falloff resp. exponent parameter as an exponent, so the spot light lighting
6054 * will always be 1.0 for both of them, and we don't have to care for the
6055 * rest of the rather complex calculation
6056 */
6057 exponent = 0.0f;
6058 }
6059 else
6060 {
6061 float rho = pData->theta + (pData->phi - pData->theta) / (2 * pData->falloff);
6062 if (rho < 0.0001f)
6063 rho = 0.0001f;
6064 exponent = -0.3f/log(cos(rho/2));
6065 }
6066 if (exponent > 128.0f)
6067 exponent = 128.0f;
6068
6069 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, exponent);
6070 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6071
6072 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, pData->phi * 90.0 / pi);
6073 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6074
6075 /* Attenuation - Are these right? guessing... */
6076 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
6077 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6078
6079 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
6080 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6081
6082 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
6083 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6084
6085 /** @todo range */
6086 break;
6087 }
6088
6089 case SVGA3D_LIGHTTYPE_DIRECTIONAL:
6090 {
6091 GLfloat position[4];
6092
6093 position[0] = -pData->direction[0];
6094 position[1] = -pData->direction[1];
6095 position[2] = -pData->direction[2];
6096 position[3] = 0.0f;
6097
6098 glLightfv(GL_LIGHT0 + index, GL_POSITION, position); /* Note gl uses w position of 0 for direction! */
6099 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6100
6101 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
6102 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6103
6104 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, 0.0f);
6105 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6106 break;
6107 }
6108
6109 case SVGA3D_LIGHTTYPE_SPOT2:
6110 default:
6111 Log(("Unsupported light type!!\n"));
6112 rc = VERR_INVALID_PARAMETER;
6113 break;
6114 }
6115
6116 /* Restore the modelview matrix */
6117 glPopMatrix();
6118
6119 return rc;
6120}
6121
6122int vmsvga3dSetLightEnabled(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, uint32_t enabled)
6123{
6124 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6125 AssertReturn(pState, VERR_NO_MEMORY);
6126
6127 LogFunc(("cid=%u %d -> %d\n", cid, index, enabled));
6128
6129 PVMSVGA3DCONTEXT pContext;
6130 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
6131 AssertRCReturn(rc, rc);
6132
6133 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6134
6135 /* Store for vm state save/restore */
6136 if (index < SVGA3D_MAX_LIGHTS)
6137 pContext->state.aLightData[index].fEnabled = !!enabled;
6138 else
6139 AssertFailed();
6140
6141 if (enabled)
6142 {
6143 if (index < SVGA3D_MAX_LIGHTS)
6144 {
6145 /* Load the default settings if none have been set yet. */
6146 if (!pContext->state.aLightData[index].fValidData)
6147 vmsvga3dSetLightData(pThisCC, cid, index, (SVGA3dLightData *)&vmsvga3d_default_light);
6148 }
6149 glEnable(GL_LIGHT0 + index);
6150 }
6151 else
6152 glDisable(GL_LIGHT0 + index);
6153
6154 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6155 return VINF_SUCCESS;
6156}
6157
6158int vmsvga3dSetViewPort(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect)
6159{
6160 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6161 AssertReturn(pState, VERR_NO_MEMORY);
6162
6163 Log(("vmsvga3dSetViewPort cid=%u (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
6164
6165 PVMSVGA3DCONTEXT pContext;
6166 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
6167 AssertRCReturn(rc, rc);
6168
6169 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6170
6171 /* Save for vm state save/restore. */
6172 pContext->state.RectViewPort = *pRect;
6173 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VIEWPORT;
6174
6175 /** @todo y-inversion for partial viewport coordinates? */
6176 glViewport(pRect->x, pRect->y, pRect->w, pRect->h);
6177 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6178
6179 /* Reset the projection matrix as that relies on the viewport setting. */
6180 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].fValid == true)
6181 vmsvga3dSetTransform(pThisCC, cid, SVGA3D_TRANSFORM_PROJECTION,
6182 pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].matrix);
6183 else
6184 {
6185 float matrix[16];
6186
6187 /* identity matrix if no matrix set. */
6188 memset(matrix, 0, sizeof(matrix));
6189 matrix[0] = 1.0;
6190 matrix[5] = 1.0;
6191 matrix[10] = 1.0;
6192 matrix[15] = 1.0;
6193 vmsvga3dSetTransform(pThisCC, cid, SVGA3D_TRANSFORM_PROJECTION, matrix);
6194 }
6195
6196 return VINF_SUCCESS;
6197}
6198
6199int vmsvga3dSetClipPlane(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, float plane[4])
6200{
6201 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6202 AssertReturn(pState, VERR_NO_MEMORY);
6203 double oglPlane[4];
6204
6205 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)));
6206 AssertReturn(index < SVGA3D_CLIPPLANE_MAX, VERR_INVALID_PARAMETER);
6207
6208 PVMSVGA3DCONTEXT pContext;
6209 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
6210 AssertRCReturn(rc, rc);
6211
6212 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6213
6214 /* Store for vm state save/restore. */
6215 pContext->state.aClipPlane[index].fValid = true;
6216 memcpy(pContext->state.aClipPlane[index].plane, plane, sizeof(pContext->state.aClipPlane[index].plane));
6217
6218 /** @todo clip plane affected by model view in OpenGL & view in D3D + vertex shader -> not transformed (see Wine; state.c clipplane) */
6219 oglPlane[0] = (double)plane[0];
6220 oglPlane[1] = (double)plane[1];
6221 oglPlane[2] = (double)plane[2];
6222 oglPlane[3] = (double)plane[3];
6223
6224 glClipPlane(GL_CLIP_PLANE0 + index, oglPlane);
6225 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6226
6227 return VINF_SUCCESS;
6228}
6229
6230int vmsvga3dSetScissorRect(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect)
6231{
6232 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6233 AssertReturn(pState, VERR_NO_MEMORY);
6234
6235 Log(("vmsvga3dSetScissorRect cid=%u (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
6236
6237 PVMSVGA3DCONTEXT pContext;
6238 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
6239 AssertRCReturn(rc, rc);
6240
6241 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6242
6243 /* Store for vm state save/restore. */
6244 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_SCISSORRECT;
6245 pContext->state.RectScissor = *pRect;
6246
6247 glScissor(pRect->x, pRect->y, pRect->w, pRect->h);
6248 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6249
6250 return VINF_SUCCESS;
6251}
6252
6253static void vmsvgaColor2GLFloatArray(uint32_t color, GLfloat *pRed, GLfloat *pGreen, GLfloat *pBlue, GLfloat *pAlpha)
6254{
6255 /* Convert byte color components to float (0-1.0) */
6256 *pAlpha = (GLfloat)(color >> 24) / 255.0;
6257 *pRed = (GLfloat)((color >> 16) & 0xff) / 255.0;
6258 *pGreen = (GLfloat)((color >> 8) & 0xff) / 255.0;
6259 *pBlue = (GLfloat)(color & 0xff) / 255.0;
6260}
6261
6262int vmsvga3dCommandClear(PVGASTATECC pThisCC, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth, uint32_t stencil,
6263 uint32_t cRects, SVGA3dRect *pRect)
6264{
6265 GLbitfield mask = 0;
6266 GLbitfield restoreMask = 0;
6267 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6268 AssertReturn(pState, VERR_NO_MEMORY);
6269 GLboolean fDepthWriteEnabled = GL_FALSE;
6270 GLboolean afColorWriteEnabled[4] = { GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE };
6271
6272 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));
6273
6274 PVMSVGA3DCONTEXT pContext;
6275 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
6276 AssertRCReturn(rc, rc);
6277
6278 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6279
6280 if (clearFlag & SVGA3D_CLEAR_COLOR)
6281 {
6282 GLfloat red, green, blue, alpha;
6283 vmsvgaColor2GLFloatArray(color, &red, &green, &blue, &alpha);
6284
6285 /* Set the color clear value. */
6286 glClearColor(red, green, blue, alpha);
6287 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6288
6289 mask |= GL_COLOR_BUFFER_BIT;
6290
6291 /* glClear will not clear the color buffer if writing is disabled. */
6292 glGetBooleanv(GL_COLOR_WRITEMASK, afColorWriteEnabled);
6293 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6294 if ( afColorWriteEnabled[0] == GL_FALSE
6295 || afColorWriteEnabled[1] == GL_FALSE
6296 || afColorWriteEnabled[2] == GL_FALSE
6297 || afColorWriteEnabled[3] == GL_FALSE)
6298 {
6299 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
6300 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6301
6302 restoreMask |= GL_COLOR_BUFFER_BIT;
6303 }
6304
6305 }
6306
6307 if (clearFlag & SVGA3D_CLEAR_STENCIL)
6308 {
6309 /** @todo possibly the same problem as with glDepthMask */
6310 glClearStencil(stencil);
6311 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6312
6313 mask |= GL_STENCIL_BUFFER_BIT;
6314 }
6315
6316 if (clearFlag & SVGA3D_CLEAR_DEPTH)
6317 {
6318 glClearDepth((GLdouble)depth);
6319 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6320
6321 mask |= GL_DEPTH_BUFFER_BIT;
6322
6323 /* glClear will not clear the depth buffer if writing is disabled. */
6324 glGetBooleanv(GL_DEPTH_WRITEMASK, &fDepthWriteEnabled);
6325 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6326 if (fDepthWriteEnabled == GL_FALSE)
6327 {
6328 glDepthMask(GL_TRUE);
6329 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6330
6331 restoreMask |= GL_DEPTH_BUFFER_BIT;
6332 }
6333 }
6334
6335 /* Save the current scissor test bit and scissor box. */
6336 glPushAttrib(GL_SCISSOR_BIT);
6337 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6338
6339 if (cRects)
6340 {
6341 glEnable(GL_SCISSOR_TEST);
6342 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6343
6344 for (uint32_t i = 0; i < cRects; ++i)
6345 {
6346 LogFunc(("rect [%d] %d,%d %dx%d)\n", i, pRect[i].x, pRect[i].y, pRect[i].w, pRect[i].h));
6347 glScissor(pRect[i].x, pRect[i].y, pRect[i].w, pRect[i].h);
6348 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6349
6350 glClear(mask);
6351 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6352 }
6353 }
6354 else
6355 {
6356 glDisable(GL_SCISSOR_TEST);
6357 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6358
6359 glClear(mask);
6360 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6361 }
6362
6363 /* Restore the old scissor test bit and box */
6364 glPopAttrib();
6365 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6366
6367 /* Restore the write states. */
6368 if (restoreMask & GL_COLOR_BUFFER_BIT)
6369 {
6370 glColorMask(afColorWriteEnabled[0],
6371 afColorWriteEnabled[1],
6372 afColorWriteEnabled[2],
6373 afColorWriteEnabled[3]);
6374 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6375 }
6376
6377 if (restoreMask & GL_DEPTH_BUFFER_BIT)
6378 {
6379 glDepthMask(fDepthWriteEnabled);
6380 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6381 }
6382
6383 return VINF_SUCCESS;
6384}
6385
6386/* Convert VMWare vertex declaration to its OpenGL equivalent. */
6387int vmsvga3dVertexDecl2OGL(SVGA3dVertexArrayIdentity &identity, GLint &size, GLenum &type, GLboolean &normalized, uint32_t &cbAttrib)
6388{
6389 normalized = GL_FALSE;
6390 switch (identity.type)
6391 {
6392 case SVGA3D_DECLTYPE_FLOAT1:
6393 size = 1;
6394 type = GL_FLOAT;
6395 cbAttrib = sizeof(float);
6396 break;
6397 case SVGA3D_DECLTYPE_FLOAT2:
6398 size = 2;
6399 type = GL_FLOAT;
6400 cbAttrib = 2 * sizeof(float);
6401 break;
6402 case SVGA3D_DECLTYPE_FLOAT3:
6403 size = 3;
6404 type = GL_FLOAT;
6405 cbAttrib = 3 * sizeof(float);
6406 break;
6407 case SVGA3D_DECLTYPE_FLOAT4:
6408 size = 4;
6409 type = GL_FLOAT;
6410 cbAttrib = 4 * sizeof(float);
6411 break;
6412
6413 case SVGA3D_DECLTYPE_D3DCOLOR:
6414 size = GL_BGRA; /* @note requires GL_ARB_vertex_array_bgra */
6415 type = GL_UNSIGNED_BYTE;
6416 normalized = GL_TRUE; /* glVertexAttribPointer fails otherwise */
6417 cbAttrib = sizeof(uint32_t);
6418 break;
6419
6420 case SVGA3D_DECLTYPE_UBYTE4N:
6421 normalized = GL_TRUE;
6422 RT_FALL_THRU();
6423 case SVGA3D_DECLTYPE_UBYTE4:
6424 size = 4;
6425 type = GL_UNSIGNED_BYTE;
6426 cbAttrib = sizeof(uint32_t);
6427 break;
6428
6429 case SVGA3D_DECLTYPE_SHORT2N:
6430 normalized = GL_TRUE;
6431 RT_FALL_THRU();
6432 case SVGA3D_DECLTYPE_SHORT2:
6433 size = 2;
6434 type = GL_SHORT;
6435 cbAttrib = 2 * sizeof(uint16_t);
6436 break;
6437
6438 case SVGA3D_DECLTYPE_SHORT4N:
6439 normalized = GL_TRUE;
6440 RT_FALL_THRU();
6441 case SVGA3D_DECLTYPE_SHORT4:
6442 size = 4;
6443 type = GL_SHORT;
6444 cbAttrib = 4 * sizeof(uint16_t);
6445 break;
6446
6447 case SVGA3D_DECLTYPE_USHORT4N:
6448 normalized = GL_TRUE;
6449 size = 4;
6450 type = GL_UNSIGNED_SHORT;
6451 cbAttrib = 4 * sizeof(uint16_t);
6452 break;
6453
6454 case SVGA3D_DECLTYPE_USHORT2N:
6455 normalized = GL_TRUE;
6456 size = 2;
6457 type = GL_UNSIGNED_SHORT;
6458 cbAttrib = 2 * sizeof(uint16_t);
6459 break;
6460
6461 case SVGA3D_DECLTYPE_UDEC3:
6462 size = 3;
6463 type = GL_UNSIGNED_INT_2_10_10_10_REV; /** @todo correct? */
6464 cbAttrib = sizeof(uint32_t);
6465 break;
6466
6467 case SVGA3D_DECLTYPE_DEC3N:
6468 normalized = true;
6469 size = 3;
6470 type = GL_INT_2_10_10_10_REV; /** @todo correct? */
6471 cbAttrib = sizeof(uint32_t);
6472 break;
6473
6474 case SVGA3D_DECLTYPE_FLOAT16_2:
6475 size = 2;
6476 type = GL_HALF_FLOAT;
6477 cbAttrib = 2 * sizeof(uint16_t);
6478 break;
6479 case SVGA3D_DECLTYPE_FLOAT16_4:
6480 size = 4;
6481 type = GL_HALF_FLOAT;
6482 cbAttrib = 4 * sizeof(uint16_t);
6483 break;
6484 default:
6485 AssertFailedReturn(VERR_INVALID_PARAMETER);
6486 }
6487
6488 //pVertexElement->Method = identity.method;
6489 //pVertexElement->Usage = identity.usage;
6490
6491 return VINF_SUCCESS;
6492}
6493
6494static float vmsvga3dFloat16To32(uint16_t f16)
6495{
6496 /* From Wiki */
6497#ifndef INFINITY
6498 static uint32_t const sBitsINFINITY = UINT32_C(0x7f800000);
6499 #define INFINITY (*(float const *)&sBitsINFINITY)
6500#endif
6501#ifndef NAN
6502 static uint32_t const sBitsNAN = UINT32_C(0x7fc00000);
6503 #define NAN (*(float const *)&sBitsNAN)
6504#endif
6505
6506 uint16_t const s = (f16 >> UINT16_C(15)) & UINT16_C(0x1);
6507 uint16_t const e = (f16 >> UINT16_C(10)) & UINT16_C(0x1f);
6508 uint16_t const m = (f16 ) & UINT16_C(0x3ff);
6509
6510 float result = s ? 1.0f : -1.0f;
6511 if (e == 0)
6512 {
6513 if (m == 0)
6514 result *= 0.0f; /* zero, -0 */
6515 else
6516 result *= (float)m / 1024.0f / 16384.0f; /* subnormal numbers: sign * 2^-14 * 0.m */
6517 }
6518 else if (e == 0x1f)
6519 {
6520 if (m == 0)
6521 result *= INFINITY; /* +-infinity */
6522 else
6523 result = NAN; /* NAN */
6524 }
6525 else
6526 {
6527 result *= powf(2.0f, (float)e - 15.0f) * (1.0f + (float)m / 1024.0f); /* sign * 2^(e-15) * 1.m */
6528 }
6529
6530 return result;
6531}
6532
6533/* Set a vertex attribute according to VMSVGA vertex declaration. */
6534static int vmsvga3dSetVertexAttrib(PVMSVGA3DSTATE pState, GLuint index, SVGA3dVertexArrayIdentity const *pIdentity, GLvoid const *pv)
6535{
6536 switch (pIdentity->type)
6537 {
6538 case SVGA3D_DECLTYPE_FLOAT1:
6539 {
6540 /* "One-component float expanded to (float, 0, 0, 1)." */
6541 GLfloat const *p = (GLfloat *)pv;
6542 GLfloat const v[4] = { p[0], 0.0f, 0.0f, 1.0f };
6543 pState->ext.glVertexAttrib4fv(index, v);
6544 break;
6545 }
6546 case SVGA3D_DECLTYPE_FLOAT2:
6547 {
6548 /* "Two-component float expanded to (float, float, 0, 1)." */
6549 GLfloat const *p = (GLfloat *)pv;
6550 GLfloat const v[4] = { p[0], p[1], 0.0f, 1.0f };
6551 pState->ext.glVertexAttrib4fv(index, v);
6552 break;
6553 }
6554 case SVGA3D_DECLTYPE_FLOAT3:
6555 {
6556 /* "Three-component float expanded to (float, float, float, 1)." */
6557 GLfloat const *p = (GLfloat *)pv;
6558 GLfloat const v[4] = { p[0], p[1], p[2], 1.0f };
6559 pState->ext.glVertexAttrib4fv(index, v);
6560 break;
6561 }
6562 case SVGA3D_DECLTYPE_FLOAT4:
6563 pState->ext.glVertexAttrib4fv(index, (GLfloat const *)pv);
6564 break;
6565 case SVGA3D_DECLTYPE_D3DCOLOR:
6566 /** @todo Need to swap bytes? */
6567 pState->ext.glVertexAttrib4Nubv(index, (GLubyte const *)pv);
6568 break;
6569 case SVGA3D_DECLTYPE_UBYTE4:
6570 pState->ext.glVertexAttrib4ubv(index, (GLubyte const *)pv);
6571 break;
6572 case SVGA3D_DECLTYPE_SHORT2:
6573 {
6574 /* "Two-component, signed short expanded to (value, value, 0, 1)." */
6575 GLshort const *p = (GLshort const *)pv;
6576 GLshort const v[4] = { p[0], p[1], 0, 1 };
6577 pState->ext.glVertexAttrib4sv(index, v);
6578 break;
6579 }
6580 case SVGA3D_DECLTYPE_SHORT4:
6581 pState->ext.glVertexAttrib4sv(index, (GLshort const *)pv);
6582 break;
6583 case SVGA3D_DECLTYPE_UBYTE4N:
6584 pState->ext.glVertexAttrib4Nubv(index, (GLubyte const *)pv);
6585 break;
6586 case SVGA3D_DECLTYPE_SHORT2N:
6587 {
6588 /* "Normalized, two-component, signed short, expanded to (first short/32767.0, second short/32767.0, 0, 1)." */
6589 GLshort const *p = (GLshort const *)pv;
6590 GLshort const v[4] = { p[0], p[1], 0, 1 };
6591 pState->ext.glVertexAttrib4Nsv(index, v);
6592 break;
6593 }
6594 case SVGA3D_DECLTYPE_SHORT4N:
6595 pState->ext.glVertexAttrib4Nsv(index, (GLshort const *)pv);
6596 break;
6597 case SVGA3D_DECLTYPE_USHORT2N:
6598 {
6599 GLushort const *p = (GLushort const *)pv;
6600 GLushort const v[4] = { p[0], p[1], 0, 1 };
6601 pState->ext.glVertexAttrib4Nusv(index, v);
6602 break;
6603 }
6604 case SVGA3D_DECLTYPE_USHORT4N:
6605 pState->ext.glVertexAttrib4Nusv(index, (GLushort const *)pv);
6606 break;
6607 case SVGA3D_DECLTYPE_UDEC3:
6608 {
6609 /** @todo Test */
6610 /* "Three-component, unsigned, 10 10 10 format expanded to (value, value, value, 1)." */
6611 uint32_t const u32 = *(uint32_t *)pv;
6612 GLfloat const v[4] = { (float)(u32 & 0x3ff), (float)((u32 >> 10) & 0x3ff), (float)((u32 >> 20) & 0x3ff), 1.0f };
6613 pState->ext.glVertexAttrib4fv(index, v);
6614 break;
6615 }
6616 case SVGA3D_DECLTYPE_DEC3N:
6617 {
6618 /** @todo Test */
6619 /* "Three-component, signed, 10 10 10 format normalized and expanded to (v[0]/511.0, v[1]/511.0, v[2]/511.0, 1)." */
6620 uint32_t const u32 = *(uint32_t *)pv;
6621 GLfloat const v[4] = { (u32 & 0x3ff) / 511.0f, ((u32 >> 10) & 0x3ff) / 511.0f, ((u32 >> 20) & 0x3ff) / 511.0f, 1.0f };
6622 pState->ext.glVertexAttrib4fv(index, v);
6623 break;
6624 }
6625 case SVGA3D_DECLTYPE_FLOAT16_2:
6626 {
6627 /** @todo Test */
6628 /* "Two-component, 16-bit, floating point expanded to (value, value, 0, 1)." */
6629 uint16_t const *p = (uint16_t *)pv;
6630 GLfloat const v[4] = { vmsvga3dFloat16To32(p[0]), vmsvga3dFloat16To32(p[1]), 0.0f, 1.0f };
6631 pState->ext.glVertexAttrib4fv(index, v);
6632 break;
6633 }
6634 case SVGA3D_DECLTYPE_FLOAT16_4:
6635 {
6636 /** @todo Test */
6637 uint16_t const *p = (uint16_t *)pv;
6638 GLfloat const v[4] = { vmsvga3dFloat16To32(p[0]), vmsvga3dFloat16To32(p[1]),
6639 vmsvga3dFloat16To32(p[2]), vmsvga3dFloat16To32(p[3]) };
6640 pState->ext.glVertexAttrib4fv(index, v);
6641 break;
6642 }
6643 default:
6644 AssertFailedReturn(VERR_INVALID_PARAMETER);
6645 }
6646
6647 return VINF_SUCCESS;
6648}
6649
6650/* Convert VMWare primitive type to its OpenGL equivalent. */
6651/* Calculate the vertex count based on the primitive type and nr of primitives. */
6652int vmsvga3dPrimitiveType2OGL(SVGA3dPrimitiveType PrimitiveType, GLenum *pMode, uint32_t cPrimitiveCount, uint32_t *pcVertices)
6653{
6654 switch (PrimitiveType)
6655 {
6656 case SVGA3D_PRIMITIVE_TRIANGLELIST:
6657 *pMode = GL_TRIANGLES;
6658 *pcVertices = cPrimitiveCount * 3;
6659 break;
6660 case SVGA3D_PRIMITIVE_POINTLIST:
6661 *pMode = GL_POINTS;
6662 *pcVertices = cPrimitiveCount;
6663 break;
6664 case SVGA3D_PRIMITIVE_LINELIST:
6665 *pMode = GL_LINES;
6666 *pcVertices = cPrimitiveCount * 2;
6667 break;
6668 case SVGA3D_PRIMITIVE_LINESTRIP:
6669 *pMode = GL_LINE_STRIP;
6670 *pcVertices = cPrimitiveCount + 1;
6671 break;
6672 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
6673 *pMode = GL_TRIANGLE_STRIP;
6674 *pcVertices = cPrimitiveCount + 2;
6675 break;
6676 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
6677 *pMode = GL_TRIANGLE_FAN;
6678 *pcVertices = cPrimitiveCount + 2;
6679 break;
6680 default:
6681 return VERR_INVALID_PARAMETER;
6682 }
6683 return VINF_SUCCESS;
6684}
6685
6686static int vmsvga3dResetTransformMatrices(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext)
6687{
6688 int rc;
6689
6690 /* Reset the view matrix (also takes the world matrix into account). */
6691 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].fValid == true)
6692 rc = vmsvga3dSetTransform(pThisCC, pContext->id, SVGA3D_TRANSFORM_VIEW,
6693 pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
6694 else
6695 {
6696 float matrix[16];
6697
6698 /* identity matrix if no matrix set. */
6699 memset(matrix, 0, sizeof(matrix));
6700 matrix[0] = 1.0;
6701 matrix[5] = 1.0;
6702 matrix[10] = 1.0;
6703 matrix[15] = 1.0;
6704 rc = vmsvga3dSetTransform(pThisCC, pContext->id, SVGA3D_TRANSFORM_VIEW, matrix);
6705 }
6706
6707 /* Reset the projection matrix. */
6708 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].fValid == true)
6709 {
6710 rc = vmsvga3dSetTransform(pThisCC, pContext->id, SVGA3D_TRANSFORM_PROJECTION, pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].matrix);
6711 }
6712 else
6713 {
6714 float matrix[16];
6715
6716 /* identity matrix if no matrix set. */
6717 memset(matrix, 0, sizeof(matrix));
6718 matrix[0] = 1.0;
6719 matrix[5] = 1.0;
6720 matrix[10] = 1.0;
6721 matrix[15] = 1.0;
6722 rc = vmsvga3dSetTransform(pThisCC, pContext->id, SVGA3D_TRANSFORM_PROJECTION, matrix);
6723 }
6724 AssertRC(rc);
6725 return rc;
6726}
6727
6728static int vmsvga3dDrawPrimitivesProcessVertexDecls(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext,
6729 uint32_t iVertexDeclBase, uint32_t numVertexDecls,
6730 SVGA3dVertexDecl *pVertexDecl,
6731 SVGA3dVertexDivisor const *paVertexDivisors)
6732{
6733 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6734 unsigned const sidVertex = pVertexDecl[0].array.surfaceId;
6735
6736 PVMSVGA3DSURFACE pVertexSurface;
6737 int rc = vmsvga3dSurfaceFromSid(pState, sidVertex, &pVertexSurface);
6738 AssertRCReturn(rc, rc);
6739
6740 Log(("vmsvga3dDrawPrimitives: vertex surface sid=%u\n", sidVertex));
6741
6742 /* Create and/or bind the vertex buffer. */
6743 if (pVertexSurface->oglId.buffer == OPENGL_INVALID_ID)
6744 {
6745 Log(("vmsvga3dDrawPrimitives: create vertex buffer fDirty=%d size=%x bytes\n", pVertexSurface->fDirty, pVertexSurface->paMipmapLevels[0].cbSurface));
6746 PVMSVGA3DCONTEXT pSavedCtx = pContext;
6747 pContext = &pState->SharedCtx;
6748 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6749
6750 pState->ext.glGenBuffers(1, &pVertexSurface->oglId.buffer);
6751 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6752 pVertexSurface->enmOGLResType = VMSVGA3D_OGLRESTYPE_BUFFER;
6753
6754 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
6755 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6756
6757 Assert(pVertexSurface->fDirty);
6758 /** @todo rethink usage dynamic/static */
6759 pState->ext.glBufferData(GL_ARRAY_BUFFER, pVertexSurface->paMipmapLevels[0].cbSurface, pVertexSurface->paMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
6760 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6761
6762 pVertexSurface->paMipmapLevels[0].fDirty = false;
6763 pVertexSurface->fDirty = false;
6764
6765 pVertexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
6766
6767 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, OPENGL_INVALID_ID);
6768 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6769
6770 pContext = pSavedCtx;
6771 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6772 }
6773
6774 Assert(pVertexSurface->fDirty == false);
6775 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
6776 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6777
6778 /* Setup the vertex declarations. */
6779 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
6780 {
6781 GLint size;
6782 GLenum type;
6783 GLboolean normalized;
6784 uint32_t cbAttrib;
6785 GLuint index = iVertexDeclBase + iVertex;
6786
6787 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));
6788
6789 rc = vmsvga3dVertexDecl2OGL(pVertexDecl[iVertex].identity, size, type, normalized, cbAttrib);
6790 AssertRCReturn(rc, rc);
6791
6792 ASSERT_GUEST_RETURN( pVertexSurface->paMipmapLevels[0].cbSurface >= pVertexDecl[iVertex].array.offset
6793 && pVertexSurface->paMipmapLevels[0].cbSurface - pVertexDecl[iVertex].array.offset >= cbAttrib,
6794 VERR_INVALID_PARAMETER);
6795 RT_UNTRUSTED_VALIDATED_FENCE();
6796
6797 if (pContext->state.shidVertex != SVGA_ID_INVALID)
6798 {
6799 /* Use numbered vertex arrays (or attributes) when shaders are active. */
6800 if (pVertexDecl[iVertex].array.stride)
6801 {
6802 pState->ext.glEnableVertexAttribArray(index);
6803 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6804 pState->ext.glVertexAttribPointer(index, size, type, normalized, pVertexDecl[iVertex].array.stride,
6805 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6806 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6807
6808 GLuint divisor = paVertexDivisors && paVertexDivisors[index].s.instanceData ? 1 : 0;
6809 pState->ext.glVertexAttribDivisor(index, divisor);
6810 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6811
6812 /** @todo case SVGA3D_DECLUSAGE_COLOR: color component order not identical!! test GL_BGRA!! */
6813 }
6814 else
6815 {
6816 /*
6817 * D3D and OpenGL have a different meaning of value zero for the vertex array stride:
6818 * - D3D (VMSVGA): "use a zero stride to tell the runtime not to increment the vertex buffer offset."
6819 * - OpenGL: "If stride is 0, the generic vertex attributes are understood to be tightly packed in the array."
6820 * VMSVGA uses the D3D semantics.
6821 *
6822 * Use glVertexAttrib in order to tell OpenGL to reuse the zero stride attributes for each vertex.
6823 */
6824 pState->ext.glDisableVertexAttribArray(index);
6825 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6826
6827 const GLvoid *v = (uint8_t *)pVertexSurface->paMipmapLevels[0].pSurfaceData + pVertexDecl[iVertex].array.offset;
6828 vmsvga3dSetVertexAttrib(pState, index, &pVertexDecl[iVertex].identity, v);
6829 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6830 }
6831 }
6832 else
6833 {
6834 if (pVertexDecl[iVertex].array.stride == 0)
6835 {
6836 /* Zero stride means that the attribute pointer must not be increased.
6837 * See comment about stride in vmsvga3dDrawPrimitives.
6838 */
6839 LogRelMax(8, ("VMSVGA: Warning: zero stride array in fixed function pipeline\n"));
6840 AssertFailed();
6841 }
6842
6843 /* Use the predefined selection of vertex streams for the fixed pipeline. */
6844 switch (pVertexDecl[iVertex].identity.usage)
6845 {
6846 case SVGA3D_DECLUSAGE_POSITIONT:
6847 case SVGA3D_DECLUSAGE_POSITION:
6848 {
6849 glEnableClientState(GL_VERTEX_ARRAY);
6850 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6851 glVertexPointer(size, type, pVertexDecl[iVertex].array.stride,
6852 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6853 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6854 break;
6855 }
6856 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
6857 AssertFailed();
6858 break;
6859 case SVGA3D_DECLUSAGE_BLENDINDICES:
6860 AssertFailed();
6861 break;
6862 case SVGA3D_DECLUSAGE_NORMAL:
6863 glEnableClientState(GL_NORMAL_ARRAY);
6864 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6865 glNormalPointer(type, pVertexDecl[iVertex].array.stride,
6866 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6867 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6868 break;
6869 case SVGA3D_DECLUSAGE_PSIZE:
6870 AssertFailed();
6871 break;
6872 case SVGA3D_DECLUSAGE_TEXCOORD:
6873 /* Specify the affected texture unit. */
6874#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
6875 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
6876#else
6877 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
6878#endif
6879 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
6880 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6881 glTexCoordPointer(size, type, pVertexDecl[iVertex].array.stride,
6882 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6883 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6884 break;
6885 case SVGA3D_DECLUSAGE_TANGENT:
6886 AssertFailed();
6887 break;
6888 case SVGA3D_DECLUSAGE_BINORMAL:
6889 AssertFailed();
6890 break;
6891 case SVGA3D_DECLUSAGE_TESSFACTOR:
6892 AssertFailed();
6893 break;
6894 case SVGA3D_DECLUSAGE_COLOR: /** @todo color component order not identical!! test GL_BGRA!! */
6895 glEnableClientState(GL_COLOR_ARRAY);
6896 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6897 glColorPointer(size, type, pVertexDecl[iVertex].array.stride,
6898 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6899 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6900 break;
6901 case SVGA3D_DECLUSAGE_FOG:
6902 glEnableClientState(GL_FOG_COORD_ARRAY);
6903 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6904 pState->ext.glFogCoordPointer(type, pVertexDecl[iVertex].array.stride,
6905 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6906 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6907 break;
6908 case SVGA3D_DECLUSAGE_DEPTH:
6909 AssertFailed();
6910 break;
6911 case SVGA3D_DECLUSAGE_SAMPLE:
6912 AssertFailed();
6913 break;
6914 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
6915 }
6916 }
6917
6918#ifdef LOG_ENABLED
6919 if (pVertexDecl[iVertex].array.stride == 0)
6920 Log(("vmsvga3dDrawPrimitives: stride == 0! Can be valid\n"));
6921#endif
6922 }
6923
6924 return VINF_SUCCESS;
6925}
6926
6927static int vmsvga3dDrawPrimitivesCleanupVertexDecls(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t iVertexDeclBase,
6928 uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl)
6929{
6930 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6931
6932 /* Clean up the vertex declarations. */
6933 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
6934 {
6935 if (pVertexDecl[iVertex].identity.usage == SVGA3D_DECLUSAGE_POSITIONT)
6936 {
6937 /* Reset the transformation matrices in case of a switch back from pretransformed mode. */
6938 Log(("vmsvga3dDrawPrimitivesCleanupVertexDecls: reset world and projection matrices after transformation reset (pre-transformed -> transformed)\n"));
6939 vmsvga3dResetTransformMatrices(pThisCC, pContext);
6940 }
6941
6942 if (pContext->state.shidVertex != SVGA_ID_INVALID)
6943 {
6944 /* Use numbered vertex arrays when shaders are active. */
6945 pState->ext.glVertexAttribDivisor(iVertexDeclBase + iVertex, 0);
6946 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6947 pState->ext.glDisableVertexAttribArray(iVertexDeclBase + iVertex);
6948 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6949 }
6950 else
6951 {
6952 /* Use the predefined selection of vertex streams for the fixed pipeline. */
6953 switch (pVertexDecl[iVertex].identity.usage)
6954 {
6955 case SVGA3D_DECLUSAGE_POSITION:
6956 case SVGA3D_DECLUSAGE_POSITIONT:
6957 glDisableClientState(GL_VERTEX_ARRAY);
6958 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6959 break;
6960 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
6961 break;
6962 case SVGA3D_DECLUSAGE_BLENDINDICES:
6963 break;
6964 case SVGA3D_DECLUSAGE_NORMAL:
6965 glDisableClientState(GL_NORMAL_ARRAY);
6966 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6967 break;
6968 case SVGA3D_DECLUSAGE_PSIZE:
6969 break;
6970 case SVGA3D_DECLUSAGE_TEXCOORD:
6971 /* Specify the affected texture unit. */
6972#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
6973 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
6974#else
6975 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
6976#endif
6977 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
6978 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6979 break;
6980 case SVGA3D_DECLUSAGE_TANGENT:
6981 break;
6982 case SVGA3D_DECLUSAGE_BINORMAL:
6983 break;
6984 case SVGA3D_DECLUSAGE_TESSFACTOR:
6985 break;
6986 case SVGA3D_DECLUSAGE_COLOR: /** @todo color component order not identical!! */
6987 glDisableClientState(GL_COLOR_ARRAY);
6988 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6989 break;
6990 case SVGA3D_DECLUSAGE_FOG:
6991 glDisableClientState(GL_FOG_COORD_ARRAY);
6992 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6993 break;
6994 case SVGA3D_DECLUSAGE_DEPTH:
6995 break;
6996 case SVGA3D_DECLUSAGE_SAMPLE:
6997 break;
6998 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
6999 }
7000 }
7001 }
7002 /* Unbind the vertex buffer after usage. */
7003 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0);
7004 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7005 return VINF_SUCCESS;
7006}
7007
7008int vmsvga3dDrawPrimitives(PVGASTATECC pThisCC, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl,
7009 uint32_t numRanges, SVGA3dPrimitiveRange *pRange, uint32_t cVertexDivisor,
7010 SVGA3dVertexDivisor *pVertexDivisor)
7011{
7012 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
7013 AssertReturn(pState, VERR_INTERNAL_ERROR);
7014 uint32_t iCurrentVertex;
7015
7016 Log(("vmsvga3dDrawPrimitives cid=%u numVertexDecls=%d numRanges=%d, cVertexDivisor=%d\n", cid, numVertexDecls, numRanges, cVertexDivisor));
7017
7018 /* Caller already check these, but it cannot hurt to check again... */
7019 AssertReturn(numVertexDecls && numVertexDecls <= SVGA3D_MAX_VERTEX_ARRAYS, VERR_INVALID_PARAMETER);
7020 AssertReturn(numRanges && numRanges <= SVGA3D_MAX_DRAW_PRIMITIVE_RANGES, VERR_INVALID_PARAMETER);
7021 AssertReturn(!cVertexDivisor || cVertexDivisor == numVertexDecls, VERR_INVALID_PARAMETER);
7022
7023 if (!cVertexDivisor)
7024 pVertexDivisor = NULL; /* Be sure. */
7025
7026 PVMSVGA3DCONTEXT pContext;
7027 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
7028 AssertRCReturn(rc, rc);
7029
7030 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7031
7032 /* Check for pretransformed vertex declarations. */
7033 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
7034 {
7035 switch (pVertexDecl[iVertex].identity.usage)
7036 {
7037 case SVGA3D_DECLUSAGE_POSITIONT:
7038 Log(("ShaderSetPositionTransformed: (%d,%d)\n", pContext->state.RectViewPort.w, pContext->state.RectViewPort.h));
7039 RT_FALL_THRU();
7040 case SVGA3D_DECLUSAGE_POSITION:
7041 ShaderSetPositionTransformed(pContext->pShaderContext, pContext->state.RectViewPort.w,
7042 pContext->state.RectViewPort.h,
7043 pVertexDecl[iVertex].identity.usage == SVGA3D_DECLUSAGE_POSITIONT);
7044 break;
7045 default: /* Shut up MSC. */ break;
7046 }
7047 }
7048
7049 /* Flush any shader changes; after (!) checking the vertex declarations to deal with pre-transformed vertices. */
7050 if (pContext->pShaderContext)
7051 {
7052 uint32_t rtHeight = 0;
7053
7054 if (pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] != SVGA_ID_INVALID)
7055 {
7056 PVMSVGA3DSURFACE pRenderTarget;
7057 rc = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pRenderTarget);
7058 AssertRCReturn(rc, rc);
7059
7060 rtHeight = pRenderTarget->paMipmapLevels[0].mipmapSize.height;
7061 }
7062
7063 ShaderUpdateState(pContext->pShaderContext, rtHeight);
7064 }
7065
7066 /* Try to figure out if instancing is used.
7067 * Support simple instancing case with one set of indexed data and one set per-instance data.
7068 */
7069 uint32_t cInstances = 0;
7070 for (uint32_t iVertexDivisor = 0; iVertexDivisor < cVertexDivisor; ++iVertexDivisor)
7071 {
7072 if (pVertexDivisor[iVertexDivisor].s.indexedData)
7073 {
7074 if (cInstances == 0)
7075 cInstances = pVertexDivisor[iVertexDivisor].s.count;
7076 else
7077 Assert(cInstances == pVertexDivisor[iVertexDivisor].s.count);
7078 }
7079 else if (pVertexDivisor[iVertexDivisor].s.instanceData)
7080 {
7081 Assert(pVertexDivisor[iVertexDivisor].s.count == 1);
7082 }
7083 }
7084
7085 /* Process all vertex declarations. Each vertex buffer is represented by one stream. */
7086 iCurrentVertex = 0;
7087 while (iCurrentVertex < numVertexDecls)
7088 {
7089 uint32_t sidVertex = SVGA_ID_INVALID;
7090 uint32_t iVertex;
7091
7092 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
7093 {
7094 if ( sidVertex != SVGA_ID_INVALID
7095 && pVertexDecl[iVertex].array.surfaceId != sidVertex
7096 )
7097 break;
7098 sidVertex = pVertexDecl[iVertex].array.surfaceId;
7099 }
7100
7101 rc = vmsvga3dDrawPrimitivesProcessVertexDecls(pThisCC, pContext, iCurrentVertex, iVertex - iCurrentVertex,
7102 &pVertexDecl[iCurrentVertex], pVertexDivisor);
7103 AssertRCReturn(rc, rc);
7104
7105 iCurrentVertex = iVertex;
7106 }
7107
7108 /* Now draw the primitives. */
7109 for (unsigned iPrimitive = 0; iPrimitive < numRanges; iPrimitive++)
7110 {
7111 GLenum modeDraw;
7112 unsigned const sidIndex = pRange[iPrimitive].indexArray.surfaceId;
7113 PVMSVGA3DSURFACE pIndexSurface = NULL;
7114 unsigned cVertices;
7115
7116 Log(("Primitive %d: type %s\n", iPrimitive, vmsvga3dPrimitiveType2String(pRange[iPrimitive].primType)));
7117 rc = vmsvga3dPrimitiveType2OGL(pRange[iPrimitive].primType, &modeDraw, pRange[iPrimitive].primitiveCount, &cVertices);
7118 if (RT_FAILURE(rc))
7119 {
7120 AssertRC(rc);
7121 goto internal_error;
7122 }
7123
7124 if (sidIndex != SVGA3D_INVALID_ID)
7125 {
7126 AssertMsg(pRange[iPrimitive].indexWidth == sizeof(uint32_t) || pRange[iPrimitive].indexWidth == sizeof(uint16_t), ("Unsupported primitive width %d\n", pRange[iPrimitive].indexWidth));
7127
7128 rc = vmsvga3dSurfaceFromSid(pState, sidIndex, &pIndexSurface);
7129 if (RT_FAILURE(rc))
7130 {
7131 AssertRC(rc);
7132 goto internal_error;
7133 }
7134
7135 Log(("vmsvga3dDrawPrimitives: index surface sid=%u\n", sidIndex));
7136
7137 if (pIndexSurface->oglId.buffer == OPENGL_INVALID_ID)
7138 {
7139 Log(("vmsvga3dDrawPrimitives: create index buffer fDirty=%d size=%x bytes\n", pIndexSurface->fDirty, pIndexSurface->paMipmapLevels[0].cbSurface));
7140 pContext = &pState->SharedCtx;
7141 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7142
7143 pState->ext.glGenBuffers(1, &pIndexSurface->oglId.buffer);
7144 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7145 pIndexSurface->enmOGLResType = VMSVGA3D_OGLRESTYPE_BUFFER;
7146
7147 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
7148 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7149
7150 Assert(pIndexSurface->fDirty);
7151
7152 /** @todo rethink usage dynamic/static */
7153 pState->ext.glBufferData(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->paMipmapLevels[0].cbSurface, pIndexSurface->paMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
7154 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7155
7156 pIndexSurface->paMipmapLevels[0].fDirty = false;
7157 pIndexSurface->fDirty = false;
7158
7159 pIndexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
7160
7161 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, OPENGL_INVALID_ID);
7162 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7163
7164 pContext = pState->papContexts[cid];
7165 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7166 }
7167 Assert(pIndexSurface->fDirty == false);
7168
7169 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
7170 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7171 }
7172
7173 if (!pIndexSurface)
7174 {
7175 /* Render without an index buffer */
7176 Log(("DrawPrimitive %d cPrimitives=%d cVertices=%d index index bias=%d cInstances=%d\n", modeDraw, pRange[iPrimitive].primitiveCount, cVertices, pRange[iPrimitive].indexBias, cInstances));
7177 if (cInstances == 0)
7178 {
7179 glDrawArrays(modeDraw, pRange[iPrimitive].indexBias, cVertices);
7180 }
7181 else
7182 {
7183 pState->ext.glDrawArraysInstanced(modeDraw, pRange[iPrimitive].indexBias, cVertices, cInstances);
7184 }
7185 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7186 }
7187 else
7188 {
7189 Assert(pRange[iPrimitive].indexWidth == pRange[iPrimitive].indexArray.stride);
7190
7191 GLenum indexType;
7192 switch (pRange[iPrimitive].indexWidth)
7193 {
7194 case 1: indexType = GL_UNSIGNED_BYTE; break;
7195 case 2: indexType = GL_UNSIGNED_SHORT; break;
7196 default: AssertMsgFailed(("indexWidth %d\n", pRange[iPrimitive].indexWidth));
7197 RT_FALL_THROUGH();
7198 case 4: indexType = GL_UNSIGNED_INT; break;
7199 }
7200
7201 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));
7202 if (cInstances == 0)
7203 {
7204 /* Render with an index buffer */
7205 if (pRange[iPrimitive].indexBias == 0)
7206 glDrawElements(modeDraw,
7207 cVertices,
7208 indexType,
7209 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset); /* byte offset in indices buffer */
7210 else
7211 pState->ext.glDrawElementsBaseVertex(modeDraw,
7212 cVertices,
7213 indexType,
7214 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
7215 pRange[iPrimitive].indexBias); /* basevertex */
7216 }
7217 else
7218 {
7219 /* Render with an index buffer */
7220 if (pRange[iPrimitive].indexBias == 0)
7221 pState->ext.glDrawElementsInstanced(modeDraw,
7222 cVertices,
7223 indexType,
7224 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
7225 cInstances);
7226 else
7227 pState->ext.glDrawElementsInstancedBaseVertex(modeDraw,
7228 cVertices,
7229 indexType,
7230 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
7231 cInstances,
7232 pRange[iPrimitive].indexBias); /* basevertex */
7233 }
7234 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7235
7236 /* Unbind the index buffer after usage. */
7237 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
7238 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7239 }
7240 }
7241
7242internal_error:
7243
7244 /* Deactivate the vertex declarations. */
7245 iCurrentVertex = 0;
7246 while (iCurrentVertex < numVertexDecls)
7247 {
7248 uint32_t sidVertex = SVGA_ID_INVALID;
7249 uint32_t iVertex;
7250
7251 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
7252 {
7253 if ( sidVertex != SVGA_ID_INVALID
7254 && pVertexDecl[iVertex].array.surfaceId != sidVertex
7255 )
7256 break;
7257 sidVertex = pVertexDecl[iVertex].array.surfaceId;
7258 }
7259
7260 rc = vmsvga3dDrawPrimitivesCleanupVertexDecls(pThisCC, pContext, iCurrentVertex,
7261 iVertex - iCurrentVertex, &pVertexDecl[iCurrentVertex]);
7262 AssertRCReturn(rc, rc);
7263
7264 iCurrentVertex = iVertex;
7265 }
7266
7267#ifdef DEBUG
7268 /* Check whether 'activeTexture' on texture unit 'i' matches what we expect. */
7269 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTextures); ++i)
7270 {
7271 if (pContext->aSidActiveTextures[i] != SVGA3D_INVALID_ID)
7272 {
7273 PVMSVGA3DSURFACE pTexture;
7274 int rc2 = vmsvga3dSurfaceFromSid(pState, pContext->aSidActiveTextures[i], &pTexture);
7275 AssertContinue(RT_SUCCESS(rc2));
7276
7277 GLint activeTextureUnit = 0;
7278 glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
7279 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7280
7281 pState->ext.glActiveTexture(GL_TEXTURE0 + i);
7282 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7283
7284 GLint activeTexture = 0;
7285 glGetIntegerv(pTexture->bindingGL, &activeTexture);
7286 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7287
7288 pState->ext.glActiveTexture(activeTextureUnit);
7289 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7290
7291 AssertMsg(pTexture->oglId.texture == (GLuint)activeTexture,
7292 ("%d vs %d unit %d (active unit %d) sid=%u\n", pTexture->oglId.texture, activeTexture, i,
7293 activeTextureUnit - GL_TEXTURE0, pContext->aSidActiveTextures[i]));
7294 }
7295 }
7296#endif
7297
7298#if 0
7299 /* Dump render target to a bitmap. */
7300 if (pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] != SVGA3D_INVALID_ID)
7301 {
7302 vmsvga3dUpdateHeapBuffersForSurfaces(pThisCC, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0]);
7303 PVMSVGA3DSURFACE pSurface;
7304 int rc2 = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pSurface);
7305 if (RT_SUCCESS(rc2))
7306 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmpgl", "rt", "-post");
7307# if 0
7308 /* Stage 0 texture. */
7309 if (pContext->aSidActiveTextures[0] != SVGA3D_INVALID_ID)
7310 {
7311 vmsvga3dUpdateHeapBuffersForSurfaces(pThisCC, pContext->aSidActiveTextures[0]);
7312 rc2 = vmsvga3dSurfaceFromSid(pState, pContext->aSidActiveTextures[0], &pSurface);
7313 if (RT_SUCCESS(rc2))
7314 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmpgl", "rt", "-post-tx");
7315 }
7316# endif
7317 }
7318#endif
7319
7320 return rc;
7321}
7322
7323
7324int vmsvga3dShaderDefine(PVGASTATECC pThisCC, uint32_t cid, uint32_t shid, SVGA3dShaderType type, uint32_t cbData, uint32_t *pShaderData)
7325{
7326 PVMSVGA3DSHADER pShader;
7327 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
7328 AssertReturn(pState, VERR_NO_MEMORY);
7329
7330 Log(("vmsvga3dShaderDefine cid=%u shid=%d type=%s cbData=0x%x\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cbData));
7331
7332 PVMSVGA3DCONTEXT pContext;
7333 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
7334 AssertRCReturn(rc, rc);
7335
7336 AssertReturn(shid < SVGA3D_MAX_SHADER_IDS, VERR_INVALID_PARAMETER);
7337
7338 rc = vmsvga3dShaderParse(cbData, pShaderData);
7339 if (RT_FAILURE(rc))
7340 {
7341 AssertRC(rc);
7342 vmsvga3dShaderLogRel("Failed to parse", type, cbData, pShaderData);
7343 return rc;
7344 }
7345
7346 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7347
7348 if (type == SVGA3D_SHADERTYPE_VS)
7349 {
7350 if (shid >= pContext->cVertexShaders)
7351 {
7352 void *pvNew = RTMemRealloc(pContext->paVertexShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
7353 AssertReturn(pvNew, VERR_NO_MEMORY);
7354 pContext->paVertexShader = (PVMSVGA3DSHADER)pvNew;
7355 memset(&pContext->paVertexShader[pContext->cVertexShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cVertexShaders));
7356 for (uint32_t i = pContext->cVertexShaders; i < shid + 1; i++)
7357 pContext->paVertexShader[i].id = SVGA3D_INVALID_ID;
7358 pContext->cVertexShaders = shid + 1;
7359 }
7360 /* If one already exists with this id, then destroy it now. */
7361 if (pContext->paVertexShader[shid].id != SVGA3D_INVALID_ID)
7362 vmsvga3dShaderDestroy(pThisCC, cid, shid, pContext->paVertexShader[shid].type);
7363
7364 pShader = &pContext->paVertexShader[shid];
7365 }
7366 else
7367 {
7368 Assert(type == SVGA3D_SHADERTYPE_PS);
7369 if (shid >= pContext->cPixelShaders)
7370 {
7371 void *pvNew = RTMemRealloc(pContext->paPixelShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
7372 AssertReturn(pvNew, VERR_NO_MEMORY);
7373 pContext->paPixelShader = (PVMSVGA3DSHADER)pvNew;
7374 memset(&pContext->paPixelShader[pContext->cPixelShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cPixelShaders));
7375 for (uint32_t i = pContext->cPixelShaders; i < shid + 1; i++)
7376 pContext->paPixelShader[i].id = SVGA3D_INVALID_ID;
7377 pContext->cPixelShaders = shid + 1;
7378 }
7379 /* If one already exists with this id, then destroy it now. */
7380 if (pContext->paPixelShader[shid].id != SVGA3D_INVALID_ID)
7381 vmsvga3dShaderDestroy(pThisCC, cid, shid, pContext->paPixelShader[shid].type);
7382
7383 pShader = &pContext->paPixelShader[shid];
7384 }
7385
7386 memset(pShader, 0, sizeof(*pShader));
7387 pShader->id = shid;
7388 pShader->cid = cid;
7389 pShader->type = type;
7390 pShader->cbData = cbData;
7391 pShader->pShaderProgram = RTMemAllocZ(cbData);
7392 AssertReturn(pShader->pShaderProgram, VERR_NO_MEMORY);
7393 memcpy(pShader->pShaderProgram, pShaderData, cbData);
7394
7395#ifdef DUMP_SHADER_DISASSEMBLY
7396 LPD3DXBUFFER pDisassembly;
7397 HRESULT hr = D3DXDisassembleShader((const DWORD *)pShaderData, FALSE, NULL, &pDisassembly);
7398 if (hr == D3D_OK)
7399 {
7400 Log(("Shader disassembly:\n%s\n", pDisassembly->GetBufferPointer()));
7401 pDisassembly->Release();
7402 }
7403#endif
7404
7405 switch (type)
7406 {
7407 case SVGA3D_SHADERTYPE_VS:
7408 rc = ShaderCreateVertexShader(pContext->pShaderContext, (const uint32_t *)pShaderData, cbData, &pShader->u.pVertexShader);
7409 AssertRC(rc);
7410 break;
7411
7412 case SVGA3D_SHADERTYPE_PS:
7413 rc = ShaderCreatePixelShader(pContext->pShaderContext, (const uint32_t *)pShaderData, cbData, &pShader->u.pPixelShader);
7414 AssertRC(rc);
7415 break;
7416
7417 default:
7418 AssertFailedReturn(VERR_INVALID_PARAMETER);
7419 }
7420 if (rc != VINF_SUCCESS)
7421 {
7422 vmsvga3dShaderLogRel("Failed to create", type, cbData, pShaderData);
7423
7424 RTMemFree(pShader->pShaderProgram);
7425 memset(pShader, 0, sizeof(*pShader));
7426 pShader->id = SVGA3D_INVALID_ID;
7427 }
7428
7429 return rc;
7430}
7431
7432int vmsvga3dShaderDestroy(PVGASTATECC pThisCC, uint32_t cid, uint32_t shid, SVGA3dShaderType type)
7433{
7434 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
7435 AssertReturn(pState, VERR_NO_MEMORY);
7436 PVMSVGA3DSHADER pShader = NULL;
7437
7438 Log(("vmsvga3dShaderDestroy cid=%u shid=%d type=%s\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL"));
7439
7440 PVMSVGA3DCONTEXT pContext;
7441 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
7442 AssertRCReturn(rc, rc);
7443
7444 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7445
7446 if (type == SVGA3D_SHADERTYPE_VS)
7447 {
7448 if ( shid < pContext->cVertexShaders
7449 && pContext->paVertexShader[shid].id == shid)
7450 {
7451 pShader = &pContext->paVertexShader[shid];
7452 rc = ShaderDestroyVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
7453 AssertRC(rc);
7454 }
7455 }
7456 else
7457 {
7458 Assert(type == SVGA3D_SHADERTYPE_PS);
7459 if ( shid < pContext->cPixelShaders
7460 && pContext->paPixelShader[shid].id == shid)
7461 {
7462 pShader = &pContext->paPixelShader[shid];
7463 rc = ShaderDestroyPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
7464 AssertRC(rc);
7465 }
7466 }
7467
7468 if (pShader)
7469 {
7470 if (pShader->pShaderProgram)
7471 RTMemFree(pShader->pShaderProgram);
7472 memset(pShader, 0, sizeof(*pShader));
7473 pShader->id = SVGA3D_INVALID_ID;
7474 }
7475 else
7476 AssertFailedReturn(VERR_INVALID_PARAMETER);
7477
7478 return VINF_SUCCESS;
7479}
7480
7481int vmsvga3dShaderSet(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t cid, SVGA3dShaderType type, uint32_t shid)
7482{
7483 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
7484 AssertReturn(pState, VERR_NO_MEMORY);
7485 int rc;
7486
7487 Log(("vmsvga3dShaderSet cid=%u type=%s shid=%d\n", cid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", shid));
7488
7489 if (!pContext)
7490 {
7491 rc = vmsvga3dContextFromCid(pState, cid, &pContext);
7492 AssertRCReturn(rc, rc);
7493 }
7494
7495 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7496
7497 if (type == SVGA3D_SHADERTYPE_VS)
7498 {
7499 /* Save for vm state save/restore. */
7500 pContext->state.shidVertex = shid;
7501 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VERTEXSHADER;
7502
7503 if ( shid < pContext->cVertexShaders
7504 && pContext->paVertexShader[shid].id == shid)
7505 {
7506 PVMSVGA3DSHADER pShader = &pContext->paVertexShader[shid];
7507 Assert(type == pShader->type);
7508
7509 rc = ShaderSetVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
7510 AssertRCReturn(rc, rc);
7511 }
7512 else
7513 if (shid == SVGA_ID_INVALID)
7514 {
7515 /* Unselect shader. */
7516 rc = ShaderSetVertexShader(pContext->pShaderContext, NULL);
7517 AssertRCReturn(rc, rc);
7518 }
7519 else
7520 AssertFailedReturn(VERR_INVALID_PARAMETER);
7521 }
7522 else
7523 {
7524 /* Save for vm state save/restore. */
7525 pContext->state.shidPixel = shid;
7526 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_PIXELSHADER;
7527
7528 Assert(type == SVGA3D_SHADERTYPE_PS);
7529 if ( shid < pContext->cPixelShaders
7530 && pContext->paPixelShader[shid].id == shid)
7531 {
7532 PVMSVGA3DSHADER pShader = &pContext->paPixelShader[shid];
7533 Assert(type == pShader->type);
7534
7535 rc = ShaderSetPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
7536 AssertRCReturn(rc, rc);
7537 }
7538 else
7539 if (shid == SVGA_ID_INVALID)
7540 {
7541 /* Unselect shader. */
7542 rc = ShaderSetPixelShader(pContext->pShaderContext, NULL);
7543 AssertRCReturn(rc, rc);
7544 }
7545 else
7546 AssertFailedReturn(VERR_INVALID_PARAMETER);
7547 }
7548
7549 return VINF_SUCCESS;
7550}
7551
7552int vmsvga3dShaderSetConst(PVGASTATECC pThisCC, uint32_t cid, uint32_t reg, SVGA3dShaderType type, SVGA3dShaderConstType ctype, uint32_t cRegisters, uint32_t *pValues)
7553{
7554 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
7555 AssertReturn(pState, VERR_NO_MEMORY);
7556
7557 Log(("vmsvga3dShaderSetConst cid=%u reg=%x type=%s cregs=%d ctype=%x\n", cid, reg, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cRegisters, ctype));
7558
7559 PVMSVGA3DCONTEXT pContext;
7560 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
7561 AssertRCReturn(rc, rc);
7562
7563 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7564
7565 for (uint32_t i = 0; i < cRegisters; i++)
7566 {
7567#ifdef LOG_ENABLED
7568 switch (ctype)
7569 {
7570 case SVGA3D_CONST_TYPE_FLOAT:
7571 {
7572 float *pValuesF = (float *)pValues;
7573 Log(("ConstantF %d: value=" FLOAT_FMT_STR ", " FLOAT_FMT_STR ", " FLOAT_FMT_STR ", " FLOAT_FMT_STR "\n",
7574 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])));
7575 break;
7576 }
7577
7578 case SVGA3D_CONST_TYPE_INT:
7579 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]));
7580 break;
7581
7582 case SVGA3D_CONST_TYPE_BOOL:
7583 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]));
7584 break;
7585 }
7586#endif
7587 vmsvga3dSaveShaderConst(pContext, reg + i, type, ctype, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]);
7588 }
7589
7590 switch (type)
7591 {
7592 case SVGA3D_SHADERTYPE_VS:
7593 switch (ctype)
7594 {
7595 case SVGA3D_CONST_TYPE_FLOAT:
7596 rc = ShaderSetVertexShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
7597 break;
7598
7599 case SVGA3D_CONST_TYPE_INT:
7600 rc = ShaderSetVertexShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
7601 break;
7602
7603 case SVGA3D_CONST_TYPE_BOOL:
7604 rc = ShaderSetVertexShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
7605 break;
7606
7607 default:
7608 AssertFailedReturn(VERR_INVALID_PARAMETER);
7609 }
7610 AssertRCReturn(rc, rc);
7611 break;
7612
7613 case SVGA3D_SHADERTYPE_PS:
7614 switch (ctype)
7615 {
7616 case SVGA3D_CONST_TYPE_FLOAT:
7617 rc = ShaderSetPixelShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
7618 break;
7619
7620 case SVGA3D_CONST_TYPE_INT:
7621 rc = ShaderSetPixelShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
7622 break;
7623
7624 case SVGA3D_CONST_TYPE_BOOL:
7625 rc = ShaderSetPixelShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
7626 break;
7627
7628 default:
7629 AssertFailedReturn(VERR_INVALID_PARAMETER);
7630 }
7631 AssertRCReturn(rc, rc);
7632 break;
7633
7634 default:
7635 AssertFailedReturn(VERR_INVALID_PARAMETER);
7636 }
7637
7638 return VINF_SUCCESS;
7639}
7640
7641int vmsvga3dOcclusionQueryCreate(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
7642{
7643 AssertReturn(pState->ext.glGenQueries, VERR_NOT_SUPPORTED);
7644 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7645
7646 GLuint idQuery = 0;
7647 pState->ext.glGenQueries(1, &idQuery);
7648 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7649 AssertReturn(idQuery, VERR_INTERNAL_ERROR);
7650 pContext->occlusion.idQuery = idQuery;
7651 return VINF_SUCCESS;
7652}
7653
7654int vmsvga3dOcclusionQueryDelete(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
7655{
7656 AssertReturn(pState->ext.glDeleteQueries, VERR_NOT_SUPPORTED);
7657 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7658
7659 if (pContext->occlusion.idQuery)
7660 {
7661 pState->ext.glDeleteQueries(1, &pContext->occlusion.idQuery);
7662 }
7663 return VINF_SUCCESS;
7664}
7665
7666int vmsvga3dOcclusionQueryBegin(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
7667{
7668 AssertReturn(pState->ext.glBeginQuery, VERR_NOT_SUPPORTED);
7669 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7670
7671 pState->ext.glBeginQuery(GL_ANY_SAMPLES_PASSED, pContext->occlusion.idQuery);
7672 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7673 return VINF_SUCCESS;
7674}
7675
7676int vmsvga3dOcclusionQueryEnd(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
7677{
7678 AssertReturn(pState->ext.glEndQuery, VERR_NOT_SUPPORTED);
7679 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7680
7681 pState->ext.glEndQuery(GL_ANY_SAMPLES_PASSED);
7682 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7683 return VINF_SUCCESS;
7684}
7685
7686int vmsvga3dOcclusionQueryGetData(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t *pu32Pixels)
7687{
7688 AssertReturn(pState->ext.glGetQueryObjectuiv, VERR_NOT_SUPPORTED);
7689 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7690
7691 GLuint pixels = 0;
7692 pState->ext.glGetQueryObjectuiv(pContext->occlusion.idQuery, GL_QUERY_RESULT, &pixels);
7693 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7694
7695 *pu32Pixels = (uint32_t)pixels;
7696 return VINF_SUCCESS;
7697}
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