VirtualBox

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

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

Devices/Graphics: vmsvga3dQueryCaps for DX backend. bugref:9830

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette