VirtualBox

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

Last change on this file since 82781 was 82718, checked in by vboxsync, 4 years ago

3D: Check last shader bytecode token, bugref:9613

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

© 2023 Oracle
ContactPrivacy policyTerms of Use