VirtualBox

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

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

Devices/Graphics: use new vmsvga headers, stubs for more commands. bugref:9830

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 297.2 KB
Line 
1/* $Id: DevVGA-SVGA3d-ogl.cpp 86905 2020-11-17 23:36:12Z 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_DEAD10: /* 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_DEAD4: /* SVGA3D_DEVCAP_MULTISAMPLE_NONMASKABLESAMPLES */
1521 break;
1522
1523 case SVGA3D_DEVCAP_DEAD5: /* SVGA3D_DEVCAP_MULTISAMPLE_MASKABLESAMPLES */
1524 break;
1525
1526 case SVGA3D_DEVCAP_DEAD7: /* SVGA3D_DEVCAP_ALPHATOCOVERAGE */
1527 break;
1528
1529 case SVGA3D_DEVCAP_DEAD6: /* 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_DEAD10: /* 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_FORMAT_DEAD2: /* Old SVGA3D_AYUV */
1963
1964 case SVGA3D_ATI1:
1965 case SVGA3D_ATI2:
1966 /* Unknown; only in DX10 & 11 */
1967 break;
1968#endif
1969 default:
1970 AssertMsgFailed(("Unsupported format %d\n", format));
1971 break;
1972 }
1973#undef AssertTestFmt
1974}
1975
1976
1977#if 0
1978/**
1979 * Convert SVGA multi sample count value to its D3D equivalent
1980 */
1981D3DMULTISAMPLE_TYPE vmsvga3dMultipeSampleCount2D3D(uint32_t multisampleCount)
1982{
1983 AssertCompile(D3DMULTISAMPLE_2_SAMPLES == 2);
1984 AssertCompile(D3DMULTISAMPLE_16_SAMPLES == 16);
1985
1986 if (multisampleCount > 16)
1987 return D3DMULTISAMPLE_NONE;
1988
1989 /** @todo exact same mapping as d3d? */
1990 return (D3DMULTISAMPLE_TYPE)multisampleCount;
1991}
1992#endif
1993
1994/**
1995 * Destroy backend specific surface bits (part of SVGA_3D_CMD_SURFACE_DESTROY).
1996 *
1997 * @param pState The VMSVGA3d state.
1998 * @param pSurface The surface being destroyed.
1999 */
2000void vmsvga3dBackSurfaceDestroy(PVMSVGA3DSTATE pState, PVMSVGA3DSURFACE pSurface)
2001{
2002 PVMSVGA3DCONTEXT pContext = &pState->SharedCtx;
2003 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2004
2005 switch (pSurface->enmOGLResType)
2006 {
2007 case VMSVGA3D_OGLRESTYPE_BUFFER:
2008 Assert(pSurface->oglId.buffer != OPENGL_INVALID_ID);
2009 pState->ext.glDeleteBuffers(1, &pSurface->oglId.buffer);
2010 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2011 break;
2012
2013 case VMSVGA3D_OGLRESTYPE_TEXTURE:
2014 Assert(pSurface->oglId.texture != OPENGL_INVALID_ID);
2015 glDeleteTextures(1, &pSurface->oglId.texture);
2016 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2017 if (pSurface->fEmulated)
2018 {
2019 if (pSurface->idEmulated)
2020 {
2021 glDeleteTextures(1, &pSurface->idEmulated);
2022 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2023 }
2024 }
2025 else
2026 {
2027 Assert(!pSurface->idEmulated);
2028 }
2029 break;
2030
2031 case VMSVGA3D_OGLRESTYPE_RENDERBUFFER:
2032 Assert(pSurface->oglId.renderbuffer != OPENGL_INVALID_ID);
2033 pState->ext.glDeleteRenderbuffers(1, &pSurface->oglId.renderbuffer);
2034 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2035 break;
2036
2037 default:
2038 AssertMsg(!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface),
2039 ("hint=%#x, type=%d\n",
2040 (pSurface->surfaceFlags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK), pSurface->enmOGLResType));
2041 break;
2042 }
2043}
2044
2045
2046int vmsvga3dSurfaceCopy(PVGASTATECC pThisCC, SVGA3dSurfaceImageId dest, SVGA3dSurfaceImageId src,
2047 uint32_t cCopyBoxes, SVGA3dCopyBox *pBox)
2048{
2049 int rc;
2050
2051 LogFunc(("Copy %d boxes from sid=%u face=%u mipmap=%u to sid=%u face=%u mipmap=%u\n",
2052 cCopyBoxes, src.sid, src.face, src.mipmap, dest.sid, dest.face, dest.mipmap));
2053
2054 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2055 AssertReturn(pState, VERR_INVALID_STATE);
2056
2057 PVMSVGA3DSURFACE pSurfaceSrc;
2058 rc = vmsvga3dSurfaceFromSid(pState, src.sid, &pSurfaceSrc);
2059 AssertRCReturn(rc, rc);
2060
2061 PVMSVGA3DSURFACE pSurfaceDst;
2062 rc = vmsvga3dSurfaceFromSid(pState, dest.sid, &pSurfaceDst);
2063 AssertRCReturn(rc, rc);
2064
2065 if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurfaceSrc))
2066 {
2067 /* The source surface is still in memory. */
2068 PVMSVGA3DMIPMAPLEVEL pMipmapLevelSrc;
2069 rc = vmsvga3dMipmapLevel(pSurfaceSrc, src.face, src.mipmap, &pMipmapLevelSrc);
2070 AssertRCReturn(rc, rc);
2071
2072 PVMSVGA3DMIPMAPLEVEL pMipmapLevelDst;
2073 rc = vmsvga3dMipmapLevel(pSurfaceDst, dest.face, dest.mipmap, &pMipmapLevelDst);
2074 AssertRCReturn(rc, rc);
2075
2076 /* The copy operation is performed on the shared context. */
2077 PVMSVGA3DCONTEXT pContext = &pState->SharedCtx;
2078 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2079
2080 /* Use glTexSubImage to upload the data to the destination texture.
2081 * The latter must be an OpenGL texture.
2082 */
2083 if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurfaceDst))
2084 {
2085 LogFunc(("dest sid=%u type=0x%x format=%d -> create texture\n", dest.sid, pSurfaceDst->surfaceFlags, pSurfaceDst->format));
2086 rc = vmsvga3dBackCreateTexture(pState, pContext, pContext->id, pSurfaceDst);
2087 AssertRCReturn(rc, rc);
2088 }
2089
2090 GLenum target;
2091 if (pSurfaceDst->targetGL == GL_TEXTURE_CUBE_MAP)
2092 target = vmsvga3dCubemapFaceFromIndex(dest.face);
2093 else
2094 {
2095 AssertMsg(pSurfaceDst->targetGL == GL_TEXTURE_2D, ("Test %#x\n", pSurfaceDst->targetGL));
2096 target = pSurfaceDst->targetGL;
2097 }
2098
2099 /* Save the unpacking parameters and set what we need here. */
2100 VMSVGAPACKPARAMS SavedParams;
2101 vmsvga3dOglSetUnpackParams(pState, pContext,
2102 pMipmapLevelSrc->mipmapSize.width,
2103 target == GL_TEXTURE_3D ? pMipmapLevelSrc->mipmapSize.height : 0,
2104 &SavedParams);
2105
2106 glBindTexture(pSurfaceDst->targetGL, pSurfaceDst->oglId.texture);
2107 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2108
2109 for (uint32_t i = 0; i < cCopyBoxes; ++i)
2110 {
2111 SVGA3dCopyBox clipBox = pBox[i];
2112 vmsvgaR3ClipCopyBox(&pMipmapLevelSrc->mipmapSize, &pMipmapLevelDst->mipmapSize, &clipBox);
2113 if ( !clipBox.w
2114 || !clipBox.h
2115 || !clipBox.d)
2116 {
2117 LogFunc(("Skipped empty box.\n"));
2118 continue;
2119 }
2120
2121 LogFunc(("copy box %d,%d,%d %dx%d to %d,%d,%d\n",
2122 clipBox.srcx, clipBox.srcy, clipBox.srcz, clipBox.w, clipBox.h, clipBox.x, clipBox.y, clipBox.z));
2123
2124 uint32_t const u32BlockX = clipBox.srcx / pSurfaceSrc->cxBlock;
2125 uint32_t const u32BlockY = clipBox.srcy / pSurfaceSrc->cyBlock;
2126 uint32_t const u32BlockZ = clipBox.srcz;
2127 Assert(u32BlockX * pSurfaceSrc->cxBlock == clipBox.srcx);
2128 Assert(u32BlockY * pSurfaceSrc->cyBlock == clipBox.srcy);
2129
2130 uint8_t const *pSrcBits = (uint8_t *)pMipmapLevelSrc->pSurfaceData
2131 + pMipmapLevelSrc->cbSurfacePlane * u32BlockZ
2132 + pMipmapLevelSrc->cbSurfacePitch * u32BlockY
2133 + pSurfaceSrc->cbBlock * u32BlockX;
2134
2135 if (target == GL_TEXTURE_3D)
2136 {
2137 if ( pSurfaceDst->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
2138 || pSurfaceDst->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
2139 || pSurfaceDst->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
2140 {
2141 uint32_t const cBlocksX = (clipBox.w + pSurfaceSrc->cxBlock - 1) / pSurfaceSrc->cxBlock;
2142 uint32_t const cBlocksY = (clipBox.h + pSurfaceSrc->cyBlock - 1) / pSurfaceSrc->cyBlock;
2143 uint32_t const imageSize = cBlocksX * cBlocksY * clipBox.d * pSurfaceSrc->cbBlock;
2144 pState->ext.glCompressedTexSubImage3D(target, dest.mipmap,
2145 clipBox.x, clipBox.y, clipBox.z,
2146 clipBox.w, clipBox.h, clipBox.d,
2147 pSurfaceSrc->internalFormatGL, (GLsizei)imageSize, pSrcBits);
2148 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2149 }
2150 else
2151 {
2152 pState->ext.glTexSubImage3D(target, dest.mipmap,
2153 clipBox.x, clipBox.y, clipBox.z,
2154 clipBox.w, clipBox.h, clipBox.d,
2155 pSurfaceSrc->formatGL, pSurfaceSrc->typeGL, pSrcBits);
2156 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2157 }
2158 }
2159 else
2160 {
2161 if ( pSurfaceDst->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
2162 || pSurfaceDst->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
2163 || pSurfaceDst->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
2164 {
2165 uint32_t const cBlocksX = (clipBox.w + pSurfaceSrc->cxBlock - 1) / pSurfaceSrc->cxBlock;
2166 uint32_t const cBlocksY = (clipBox.h + pSurfaceSrc->cyBlock - 1) / pSurfaceSrc->cyBlock;
2167 uint32_t const imageSize = cBlocksX * cBlocksY * pSurfaceSrc->cbBlock;
2168 pState->ext.glCompressedTexSubImage2D(target, dest.mipmap,
2169 clipBox.x, clipBox.y, clipBox.w, clipBox.h,
2170 pSurfaceSrc->internalFormatGL, (GLsizei)imageSize, pSrcBits);
2171 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2172 }
2173 else
2174 {
2175 glTexSubImage2D(target, dest.mipmap,
2176 clipBox.x, clipBox.y, clipBox.w, clipBox.h,
2177 pSurfaceSrc->formatGL, pSurfaceSrc->typeGL, pSrcBits);
2178 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2179 }
2180 }
2181 }
2182
2183 glBindTexture(pSurfaceDst->targetGL, 0);
2184 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2185
2186 vmsvga3dOglRestoreUnpackParams(pState, pContext, &SavedParams);
2187
2188 return VINF_SUCCESS;
2189 }
2190
2191 PVGASTATE pThis = PDMDEVINS_2_DATA(pThisCC->pDevIns, PVGASTATE);
2192 for (uint32_t i = 0; i < cCopyBoxes; i++)
2193 {
2194 SVGA3dBox destBox, srcBox;
2195
2196 srcBox.x = pBox[i].srcx;
2197 srcBox.y = pBox[i].srcy;
2198 srcBox.z = pBox[i].srcz;
2199 srcBox.w = pBox[i].w;
2200 srcBox.h = pBox[i].h;
2201 srcBox.d = pBox[i].d;
2202
2203 destBox.x = pBox[i].x;
2204 destBox.y = pBox[i].y;
2205 destBox.z = pBox[i].z;
2206 destBox.w = pBox[i].w;
2207 destBox.h = pBox[i].h;
2208 destBox.d = pBox[i].d;
2209
2210 /* No stretching is required, therefore use SVGA3D_STRETCH_BLT_POINT which translated to GL_NEAREST. */
2211 rc = vmsvga3dSurfaceStretchBlt(pThis, pThisCC, &dest, &destBox, &src, &srcBox, SVGA3D_STRETCH_BLT_POINT);
2212 AssertRCReturn(rc, rc);
2213 }
2214 return VINF_SUCCESS;
2215}
2216
2217
2218/**
2219 * Saves texture unpacking parameters and loads the specified ones.
2220 *
2221 * @param pState The VMSVGA3D state structure.
2222 * @param pContext The active context.
2223 * @param cxRow The number of pixels in a row. 0 for the entire width.
2224 * @param cyImage The height of the image in pixels. 0 for the entire height.
2225 * @param pSave Where to save stuff.
2226 */
2227void vmsvga3dOglSetUnpackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, GLint cxRow, GLint cyImage,
2228 PVMSVGAPACKPARAMS pSave)
2229{
2230 RT_NOREF(pState);
2231
2232 /*
2233 * Save (ignore errors, setting the defaults we want and avoids restore).
2234 */
2235 pSave->iAlignment = 1;
2236 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_UNPACK_ALIGNMENT, &pSave->iAlignment), pState, pContext);
2237 pSave->cxRow = 0;
2238 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_UNPACK_ROW_LENGTH, &pSave->cxRow), pState, pContext);
2239 pSave->cyImage = 0;
2240 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_UNPACK_IMAGE_HEIGHT, &pSave->cyImage), pState, pContext);
2241
2242#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
2243 pSave->fSwapBytes = GL_FALSE;
2244 glGetBooleanv(GL_UNPACK_SWAP_BYTES, &pSave->fSwapBytes);
2245 Assert(pSave->fSwapBytes == GL_FALSE);
2246
2247 pSave->fLsbFirst = GL_FALSE;
2248 glGetBooleanv(GL_UNPACK_LSB_FIRST, &pSave->fLsbFirst);
2249 Assert(pSave->fLsbFirst == GL_FALSE);
2250
2251 pSave->cSkipRows = 0;
2252 glGetIntegerv(GL_UNPACK_SKIP_ROWS, &pSave->cSkipRows);
2253 Assert(pSave->cSkipRows == 0);
2254
2255 pSave->cSkipPixels = 0;
2256 glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &pSave->cSkipPixels);
2257 Assert(pSave->cSkipPixels == 0);
2258
2259 pSave->cSkipImages = 0;
2260 glGetIntegerv(GL_UNPACK_SKIP_IMAGES, &pSave->cSkipImages);
2261 Assert(pSave->cSkipImages == 0);
2262
2263 VMSVGA3D_CLEAR_GL_ERRORS();
2264#endif
2265
2266 /*
2267 * Setup unpack.
2268 *
2269 * Note! We use 1 as alignment here because we currently don't do any
2270 * aligning of line pitches anywhere.
2271 */
2272 pSave->fChanged = 0;
2273 if (pSave->iAlignment != 1)
2274 {
2275 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_ALIGNMENT, 1), pState, pContext);
2276 pSave->fChanged |= VMSVGAPACKPARAMS_ALIGNMENT;
2277 }
2278 if (pSave->cxRow != cxRow)
2279 {
2280 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, cxRow), pState, pContext);
2281 pSave->fChanged |= VMSVGAPACKPARAMS_ROW_LENGTH;
2282 }
2283 if (pSave->cyImage != cyImage)
2284 {
2285 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, cyImage), pState, pContext);
2286 pSave->fChanged |= VMSVGAPACKPARAMS_IMAGE_HEIGHT;
2287 }
2288#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
2289 if (pSave->fSwapBytes != 0)
2290 {
2291 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE), pState, pContext);
2292 pSave->fChanged |= VMSVGAPACKPARAMS_SWAP_BYTES;
2293 }
2294 if (pSave->fLsbFirst != 0)
2295 {
2296 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE), pState, pContext);
2297 pSave->fChanged |= VMSVGAPACKPARAMS_LSB_FIRST;
2298 }
2299 if (pSave->cSkipRows != 0)
2300 {
2301 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_ROWS, 0), pState, pContext);
2302 pSave->fChanged |= VMSVGAPACKPARAMS_SKIP_ROWS;
2303 }
2304 if (pSave->cSkipPixels != 0)
2305 {
2306 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0), pState, pContext);
2307 pSave->fChanged |= VMSVGAPACKPARAMS_SKIP_PIXELS;
2308 }
2309 if (pSave->cSkipImages != 0)
2310 {
2311 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0), pState, pContext);
2312 pSave->fChanged |= VMSVGAPACKPARAMS_SKIP_IMAGES;
2313 }
2314#endif
2315}
2316
2317
2318/**
2319 * Restores texture unpacking parameters.
2320 *
2321 * @param pState The VMSVGA3D state structure.
2322 * @param pContext The active context.
2323 * @param pSave Where stuff was saved.
2324 */
2325void vmsvga3dOglRestoreUnpackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext,
2326 PCVMSVGAPACKPARAMS pSave)
2327{
2328 RT_NOREF(pState);
2329
2330 if (pSave->fChanged & VMSVGAPACKPARAMS_ALIGNMENT)
2331 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_ALIGNMENT, pSave->iAlignment), pState, pContext);
2332 if (pSave->fChanged & VMSVGAPACKPARAMS_ROW_LENGTH)
2333 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, pSave->cxRow), pState, pContext);
2334 if (pSave->fChanged & VMSVGAPACKPARAMS_IMAGE_HEIGHT)
2335 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, pSave->cyImage), pState, pContext);
2336#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
2337 if (pSave->fChanged & VMSVGAPACKPARAMS_SWAP_BYTES)
2338 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SWAP_BYTES, pSave->fSwapBytes), pState, pContext);
2339 if (pSave->fChanged & VMSVGAPACKPARAMS_LSB_FIRST)
2340 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_LSB_FIRST, pSave->fLsbFirst), pState, pContext);
2341 if (pSave->fChanged & VMSVGAPACKPARAMS_SKIP_ROWS)
2342 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_ROWS, pSave->cSkipRows), pState, pContext);
2343 if (pSave->fChanged & VMSVGAPACKPARAMS_SKIP_PIXELS)
2344 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS, pSave->cSkipPixels), pState, pContext);
2345 if (pSave->fChanged & VMSVGAPACKPARAMS_SKIP_IMAGES)
2346 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_UNPACK_SKIP_IMAGES, pSave->cSkipImages), pState, pContext);
2347#endif
2348}
2349
2350/**
2351 * Create D3D/OpenGL texture object for the specified surface.
2352 *
2353 * Surfaces are created when needed.
2354 *
2355 * @param pState The VMSVGA3d state.
2356 * @param pContext The context.
2357 * @param idAssociatedContext Probably the same as pContext->id.
2358 * @param pSurface The surface to create the texture for.
2359 */
2360int vmsvga3dBackCreateTexture(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t idAssociatedContext,
2361 PVMSVGA3DSURFACE pSurface)
2362{
2363 RT_NOREF(idAssociatedContext);
2364
2365 LogFunc(("sid=%u\n", pSurface->id));
2366
2367 uint32_t const numMipLevels = pSurface->cLevels;
2368
2369 /* Fugure out what kind of texture we are creating. */
2370 GLenum binding;
2371 GLenum target;
2372 if (pSurface->surfaceFlags & SVGA3D_SURFACE_CUBEMAP)
2373 {
2374 Assert(pSurface->cFaces == 6);
2375
2376 binding = GL_TEXTURE_BINDING_CUBE_MAP;
2377 target = GL_TEXTURE_CUBE_MAP;
2378 }
2379 else
2380 {
2381 if (pSurface->paMipmapLevels[0].mipmapSize.depth > 1)
2382 {
2383 binding = GL_TEXTURE_BINDING_3D;
2384 target = GL_TEXTURE_3D;
2385 }
2386 else
2387 {
2388 Assert(pSurface->cFaces == 1);
2389
2390 binding = GL_TEXTURE_BINDING_2D;
2391 target = GL_TEXTURE_2D;
2392 }
2393 }
2394
2395 /* All textures are created in the SharedCtx. */
2396 uint32_t idPrevCtx = pState->idActiveContext;
2397 pContext = &pState->SharedCtx;
2398 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2399
2400 glGenTextures(1, &pSurface->oglId.texture);
2401 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2402 if (pSurface->fEmulated)
2403 {
2404 glGenTextures(1, &pSurface->idEmulated);
2405 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2406 }
2407 pSurface->enmOGLResType = VMSVGA3D_OGLRESTYPE_TEXTURE;
2408
2409 GLint activeTexture = 0;
2410 glGetIntegerv(binding, &activeTexture);
2411 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2412
2413 /* Must bind texture to the current context in order to change it. */
2414 glBindTexture(target, pSurface->oglId.texture);
2415 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2416
2417 /* Set the unpacking parameters. */
2418 VMSVGAPACKPARAMS SavedParams;
2419 vmsvga3dOglSetUnpackParams(pState, pContext, 0, 0, &SavedParams);
2420
2421 /** @todo Set the mip map generation filter settings. */
2422
2423 /* Set the mipmap base and max level parameters. */
2424 glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, 0);
2425 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2426 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, pSurface->cLevels - 1);
2427 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2428
2429 if (pSurface->fDirty)
2430 LogFunc(("sync dirty texture\n"));
2431
2432 /* Always allocate and initialize all mipmap levels; non-initialized mipmap levels used as render targets cause failures. */
2433 if (target == GL_TEXTURE_3D)
2434 {
2435 for (uint32_t i = 0; i < numMipLevels; ++i)
2436 {
2437 /* Allocate and initialize texture memory. Passing the zero filled pSurfaceData avoids
2438 * exposing random host memory to the guest and helps a with the fedora 21 surface
2439 * corruption issues (launchpad, background, search field, login).
2440 */
2441 PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->paMipmapLevels[i];
2442
2443 LogFunc(("sync dirty 3D texture mipmap level %d (pitch %x) (dirty %d)\n",
2444 i, pMipLevel->cbSurfacePitch, pMipLevel->fDirty));
2445
2446 if ( pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
2447 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
2448 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
2449 {
2450 pState->ext.glCompressedTexImage3D(GL_TEXTURE_3D,
2451 i,
2452 pSurface->internalFormatGL,
2453 pMipLevel->mipmapSize.width,
2454 pMipLevel->mipmapSize.height,
2455 pMipLevel->mipmapSize.depth,
2456 0,
2457 pMipLevel->cbSurface,
2458 pMipLevel->pSurfaceData);
2459 }
2460 else
2461 {
2462 pState->ext.glTexImage3D(GL_TEXTURE_3D,
2463 i,
2464 pSurface->internalFormatGL,
2465 pMipLevel->mipmapSize.width,
2466 pMipLevel->mipmapSize.height,
2467 pMipLevel->mipmapSize.depth,
2468 0, /* border */
2469 pSurface->formatGL,
2470 pSurface->typeGL,
2471 pMipLevel->pSurfaceData);
2472 }
2473 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2474
2475 pMipLevel->fDirty = false;
2476 }
2477 }
2478 else if (target == GL_TEXTURE_CUBE_MAP)
2479 {
2480 for (uint32_t iFace = 0; iFace < 6; ++iFace)
2481 {
2482 GLenum const Face = vmsvga3dCubemapFaceFromIndex(iFace);
2483
2484 for (uint32_t i = 0; i < numMipLevels; ++i)
2485 {
2486 PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->paMipmapLevels[iFace * numMipLevels + i];
2487 Assert(pMipLevel->mipmapSize.width == pMipLevel->mipmapSize.height);
2488 Assert(pMipLevel->mipmapSize.depth == 1);
2489
2490 LogFunc(("sync cube texture face %d mipmap level %d (dirty %d)\n",
2491 iFace, i, pMipLevel->fDirty));
2492
2493 if ( pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
2494 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
2495 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
2496 {
2497 pState->ext.glCompressedTexImage2D(Face,
2498 i,
2499 pSurface->internalFormatGL,
2500 pMipLevel->mipmapSize.width,
2501 pMipLevel->mipmapSize.height,
2502 0,
2503 pMipLevel->cbSurface,
2504 pMipLevel->pSurfaceData);
2505 }
2506 else
2507 {
2508 glTexImage2D(Face,
2509 i,
2510 pSurface->internalFormatGL,
2511 pMipLevel->mipmapSize.width,
2512 pMipLevel->mipmapSize.height,
2513 0,
2514 pSurface->formatGL,
2515 pSurface->typeGL,
2516 pMipLevel->pSurfaceData);
2517 }
2518 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2519
2520 pMipLevel->fDirty = false;
2521 }
2522 }
2523 }
2524 else if (target == GL_TEXTURE_2D)
2525 {
2526 for (uint32_t i = 0; i < numMipLevels; ++i)
2527 {
2528 /* Allocate and initialize texture memory. Passing the zero filled pSurfaceData avoids
2529 * exposing random host memory to the guest and helps a with the fedora 21 surface
2530 * corruption issues (launchpad, background, search field, login).
2531 */
2532 PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->paMipmapLevels[i];
2533 Assert(pMipLevel->mipmapSize.depth == 1);
2534
2535 LogFunc(("sync dirty texture mipmap level %d (pitch %x) (dirty %d)\n",
2536 i, pMipLevel->cbSurfacePitch, pMipLevel->fDirty));
2537
2538 if ( pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
2539 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
2540 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
2541 {
2542 pState->ext.glCompressedTexImage2D(GL_TEXTURE_2D,
2543 i,
2544 pSurface->internalFormatGL,
2545 pMipLevel->mipmapSize.width,
2546 pMipLevel->mipmapSize.height,
2547 0,
2548 pMipLevel->cbSurface,
2549 pMipLevel->pSurfaceData);
2550 }
2551 else
2552 {
2553 glTexImage2D(GL_TEXTURE_2D,
2554 i,
2555 pSurface->internalFormatGL,
2556 pMipLevel->mipmapSize.width,
2557 pMipLevel->mipmapSize.height,
2558 0,
2559 pSurface->formatGL,
2560 pSurface->typeGL,
2561 NULL);
2562 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2563
2564 if (pSurface->fEmulated)
2565 {
2566 /* Bind the emulated texture and init it. */
2567 glBindTexture(GL_TEXTURE_2D, pSurface->idEmulated);
2568 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2569
2570 glTexImage2D(GL_TEXTURE_2D,
2571 i,
2572 pSurface->internalFormatGL,
2573 pMipLevel->mipmapSize.width,
2574 pMipLevel->mipmapSize.height,
2575 0,
2576 pSurface->formatGL,
2577 pSurface->typeGL,
2578 NULL);
2579 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2580 }
2581
2582 /* Fetch texture data: either to the actual or to the emulated texture.
2583 * The pSurfaceData buffer may be smaller than the entire texture
2584 * for emulated formats, in which case only part of the texture is synched.
2585 */
2586 uint32_t cBlocksX = pMipLevel->mipmapSize.width / pSurface->cxBlock;
2587 uint32_t cBlocksY = pMipLevel->mipmapSize.height / pSurface->cyBlock;
2588 glTexSubImage2D(GL_TEXTURE_2D,
2589 i,
2590 0,
2591 0,
2592 cBlocksX,
2593 cBlocksY,
2594 pSurface->formatGL,
2595 pSurface->typeGL,
2596 pMipLevel->pSurfaceData);
2597 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2598
2599 if (pSurface->fEmulated)
2600 {
2601 /* Update the actual texture using the format converter. */
2602 FormatConvUpdateTexture(pState, pContext, pSurface, i);
2603
2604 /* Rebind the actual texture. */
2605 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
2606 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2607 }
2608 }
2609 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2610
2611 pMipLevel->fDirty = false;
2612 }
2613 }
2614
2615 pSurface->fDirty = false;
2616
2617 /* Restore unpacking parameters. */
2618 vmsvga3dOglRestoreUnpackParams(pState, pContext, &SavedParams);
2619
2620 /* Restore the old active texture. */
2621 glBindTexture(target, activeTexture);
2622 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2623
2624 pSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_TEXTURE;
2625 pSurface->targetGL = target;
2626 pSurface->bindingGL = binding;
2627
2628 if (idPrevCtx < pState->cContexts && pState->papContexts[idPrevCtx]->id == idPrevCtx)
2629 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pState->papContexts[idPrevCtx]);
2630 return VINF_SUCCESS;
2631}
2632
2633
2634/**
2635 * Backend worker for implementing SVGA_3D_CMD_SURFACE_STRETCHBLT.
2636 *
2637 * @returns VBox status code.
2638 * @param pThis The VGA device instance.
2639 * @param pState The VMSVGA3d state.
2640 * @param pDstSurface The destination host surface.
2641 * @param uDstFace The destination face (valid).
2642 * @param uDstMipmap The destination mipmap level (valid).
2643 * @param pDstBox The destination box.
2644 * @param pSrcSurface The source host surface.
2645 * @param uSrcFace The destination face (valid).
2646 * @param uSrcMipmap The source mimap level (valid).
2647 * @param pSrcBox The source box.
2648 * @param enmMode The strecht blt mode .
2649 * @param pContext The VMSVGA3d context (already current for OGL).
2650 */
2651int vmsvga3dBackSurfaceStretchBlt(PVGASTATE pThis, PVMSVGA3DSTATE pState,
2652 PVMSVGA3DSURFACE pDstSurface, uint32_t uDstFace, uint32_t uDstMipmap, SVGA3dBox const *pDstBox,
2653 PVMSVGA3DSURFACE pSrcSurface, uint32_t uSrcFace, uint32_t uSrcMipmap, SVGA3dBox const *pSrcBox,
2654 SVGA3dStretchBltMode enmMode, PVMSVGA3DCONTEXT pContext)
2655{
2656 RT_NOREF(pThis);
2657
2658 AssertReturn( RT_BOOL(pSrcSurface->surfaceFlags & SVGA3D_SURFACE_HINT_DEPTHSTENCIL)
2659 == RT_BOOL(pDstSurface->surfaceFlags & SVGA3D_SURFACE_HINT_DEPTHSTENCIL), VERR_NOT_IMPLEMENTED);
2660
2661 GLenum glAttachment = GL_COLOR_ATTACHMENT0;
2662 GLbitfield glMask = GL_COLOR_BUFFER_BIT;
2663 if (pDstSurface->surfaceFlags & SVGA3D_SURFACE_HINT_DEPTHSTENCIL)
2664 {
2665 /** @todo Need GL_DEPTH_STENCIL_ATTACHMENT for depth/stencil formats? */
2666 glAttachment = GL_DEPTH_ATTACHMENT;
2667 glMask = GL_DEPTH_BUFFER_BIT;
2668 }
2669
2670 /* Activate the read and draw framebuffer objects. */
2671 pState->ext.glBindFramebuffer(GL_READ_FRAMEBUFFER, pContext->idReadFramebuffer);
2672 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2673 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, pContext->idDrawFramebuffer);
2674 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2675
2676 /* Bind the source and destination objects to the right place. */
2677 GLenum textarget;
2678 if (pSrcSurface->targetGL == GL_TEXTURE_CUBE_MAP)
2679 textarget = vmsvga3dCubemapFaceFromIndex(uSrcFace);
2680 else
2681 {
2682 /// @todo later AssertMsg(pSrcSurface->targetGL == GL_TEXTURE_2D, ("%#x\n", pSrcSurface->targetGL));
2683 textarget = GL_TEXTURE_2D;
2684 }
2685 pState->ext.glFramebufferTexture2D(GL_READ_FRAMEBUFFER, glAttachment, textarget,
2686 pSrcSurface->oglId.texture, uSrcMipmap);
2687 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2688
2689 if (pDstSurface->targetGL == GL_TEXTURE_CUBE_MAP)
2690 textarget = vmsvga3dCubemapFaceFromIndex(uDstFace);
2691 else
2692 {
2693 /// @todo later AssertMsg(pDstSurface->targetGL == GL_TEXTURE_2D, ("%#x\n", pDstSurface->targetGL));
2694 textarget = GL_TEXTURE_2D;
2695 }
2696 pState->ext.glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, glAttachment, textarget,
2697 pDstSurface->oglId.texture, uDstMipmap);
2698 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2699
2700 Log(("src conv. (%d,%d)(%d,%d); dest conv (%d,%d)(%d,%d)\n",
2701 pSrcBox->x, D3D_TO_OGL_Y_COORD(pSrcSurface, pSrcBox->y + pSrcBox->h),
2702 pSrcBox->x + pSrcBox->w, D3D_TO_OGL_Y_COORD(pSrcSurface, pSrcBox->y),
2703 pDstBox->x, D3D_TO_OGL_Y_COORD(pDstSurface, pDstBox->y + pDstBox->h),
2704 pDstBox->x + pDstBox->w, D3D_TO_OGL_Y_COORD(pDstSurface, pDstBox->y)));
2705
2706 pState->ext.glBlitFramebuffer(pSrcBox->x,
2707 pSrcBox->y,
2708 pSrcBox->x + pSrcBox->w, /* exclusive. */
2709 pSrcBox->y + pSrcBox->h,
2710 pDstBox->x,
2711 pDstBox->y,
2712 pDstBox->x + pDstBox->w, /* exclusive. */
2713 pDstBox->y + pDstBox->h,
2714 glMask,
2715 (enmMode == SVGA3D_STRETCH_BLT_POINT) ? GL_NEAREST : GL_LINEAR);
2716 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2717
2718 /* Reset the frame buffer association */
2719 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
2720 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
2721
2722 return VINF_SUCCESS;
2723}
2724
2725/**
2726 * Save texture packing parameters and loads those appropriate for the given
2727 * surface.
2728 *
2729 * @param pState The VMSVGA3D state structure.
2730 * @param pContext The active context.
2731 * @param pSurface The surface.
2732 * @param pSave Where to save stuff.
2733 */
2734void vmsvga3dOglSetPackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
2735 PVMSVGAPACKPARAMS pSave)
2736{
2737 RT_NOREF(pState);
2738 /*
2739 * Save (ignore errors, setting the defaults we want and avoids restore).
2740 */
2741 pSave->iAlignment = 1;
2742 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_PACK_ALIGNMENT, &pSave->iAlignment), pState, pContext);
2743 pSave->cxRow = 0;
2744 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_PACK_ROW_LENGTH, &pSave->cxRow), pState, pContext);
2745
2746#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
2747 pSave->cyImage = 0;
2748 glGetIntegerv(GL_PACK_IMAGE_HEIGHT, &pSave->cyImage);
2749 Assert(pSave->cyImage == 0);
2750
2751 pSave->fSwapBytes = GL_FALSE;
2752 glGetBooleanv(GL_PACK_SWAP_BYTES, &pSave->fSwapBytes);
2753 Assert(pSave->fSwapBytes == GL_FALSE);
2754
2755 pSave->fLsbFirst = GL_FALSE;
2756 glGetBooleanv(GL_PACK_LSB_FIRST, &pSave->fLsbFirst);
2757 Assert(pSave->fLsbFirst == GL_FALSE);
2758
2759 pSave->cSkipRows = 0;
2760 glGetIntegerv(GL_PACK_SKIP_ROWS, &pSave->cSkipRows);
2761 Assert(pSave->cSkipRows == 0);
2762
2763 pSave->cSkipPixels = 0;
2764 glGetIntegerv(GL_PACK_SKIP_PIXELS, &pSave->cSkipPixels);
2765 Assert(pSave->cSkipPixels == 0);
2766
2767 pSave->cSkipImages = 0;
2768 glGetIntegerv(GL_PACK_SKIP_IMAGES, &pSave->cSkipImages);
2769 Assert(pSave->cSkipImages == 0);
2770
2771 VMSVGA3D_CLEAR_GL_ERRORS();
2772#endif
2773
2774 /*
2775 * Setup unpack.
2776 *
2777 * Note! We use 1 as alignment here because we currently don't do any
2778 * aligning of line pitches anywhere.
2779 */
2780 NOREF(pSurface);
2781 if (pSave->iAlignment != 1)
2782 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ALIGNMENT, 1), pState, pContext);
2783 if (pSave->cxRow != 0)
2784 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ROW_LENGTH, 0), pState, pContext);
2785#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
2786 if (pSave->cyImage != 0)
2787 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0), pState, pContext);
2788 if (pSave->fSwapBytes != 0)
2789 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE), pState, pContext);
2790 if (pSave->fLsbFirst != 0)
2791 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE), pState, pContext);
2792 if (pSave->cSkipRows != 0)
2793 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_ROWS, 0), pState, pContext);
2794 if (pSave->cSkipPixels != 0)
2795 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_PIXELS, 0), pState, pContext);
2796 if (pSave->cSkipImages != 0)
2797 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_IMAGES, 0), pState, pContext);
2798#endif
2799}
2800
2801
2802/**
2803 * Restores texture packing parameters.
2804 *
2805 * @param pState The VMSVGA3D state structure.
2806 * @param pContext The active context.
2807 * @param pSurface The surface.
2808 * @param pSave Where stuff was saved.
2809 */
2810void vmsvga3dOglRestorePackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
2811 PCVMSVGAPACKPARAMS pSave)
2812{
2813 RT_NOREF(pState, pSurface);
2814 if (pSave->iAlignment != 1)
2815 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ALIGNMENT, pSave->iAlignment), pState, pContext);
2816 if (pSave->cxRow != 0)
2817 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_ROW_LENGTH, pSave->cxRow), pState, pContext);
2818#ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
2819 if (pSave->cyImage != 0)
2820 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_IMAGE_HEIGHT, pSave->cyImage), pState, pContext);
2821 if (pSave->fSwapBytes != 0)
2822 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SWAP_BYTES, pSave->fSwapBytes), pState, pContext);
2823 if (pSave->fLsbFirst != 0)
2824 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_LSB_FIRST, pSave->fLsbFirst), pState, pContext);
2825 if (pSave->cSkipRows != 0)
2826 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_ROWS, pSave->cSkipRows), pState, pContext);
2827 if (pSave->cSkipPixels != 0)
2828 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_PIXELS, pSave->cSkipPixels), pState, pContext);
2829 if (pSave->cSkipImages != 0)
2830 VMSVGA3D_ASSERT_GL_CALL(glPixelStorei(GL_PACK_SKIP_IMAGES, pSave->cSkipImages), pState, pContext);
2831#endif
2832}
2833
2834
2835/**
2836 * Backend worker for implementing SVGA_3D_CMD_SURFACE_DMA that copies one box.
2837 *
2838 * @returns Failure status code or @a rc.
2839 * @param pThis The shared VGA/VMSVGA instance data.
2840 * @param pThisCC The VGA/VMSVGA state for ring-3.
2841 * @param pState The VMSVGA3d state.
2842 * @param pSurface The host surface.
2843 * @param pMipLevel Mipmap level. The caller knows it already.
2844 * @param uHostFace The host face (valid).
2845 * @param uHostMipmap The host mipmap level (valid).
2846 * @param GuestPtr The guest pointer.
2847 * @param cbGuestPitch The guest pitch.
2848 * @param transfer The transfer direction.
2849 * @param pBox The box to copy (clipped, valid, except for guest's srcx, srcy, srcz).
2850 * @param pContext The context (for OpenGL).
2851 * @param rc The current rc for all boxes.
2852 * @param iBox The current box number (for Direct 3D).
2853 */
2854int vmsvga3dBackSurfaceDMACopyBox(PVGASTATE pThis, PVGASTATECC pThisCC, PVMSVGA3DSTATE pState, PVMSVGA3DSURFACE pSurface,
2855 PVMSVGA3DMIPMAPLEVEL pMipLevel, uint32_t uHostFace, uint32_t uHostMipmap,
2856 SVGAGuestPtr GuestPtr, uint32_t cbGuestPitch, SVGA3dTransferType transfer,
2857 SVGA3dCopyBox const *pBox, PVMSVGA3DCONTEXT pContext, int rc, int iBox)
2858{
2859 RT_NOREF(iBox);
2860
2861 switch (pSurface->enmOGLResType)
2862 {
2863 case VMSVGA3D_OGLRESTYPE_TEXTURE:
2864 {
2865 uint32_t cbSurfacePitch;
2866 uint8_t *pDoubleBuffer;
2867 uint64_t offHst;
2868
2869 uint32_t const u32HostBlockX = pBox->x / pSurface->cxBlock;
2870 uint32_t const u32HostBlockY = pBox->y / pSurface->cyBlock;
2871 uint32_t const u32HostZ = pBox->z;
2872 Assert(u32HostBlockX * pSurface->cxBlock == pBox->x);
2873 Assert(u32HostBlockY * pSurface->cyBlock == pBox->y);
2874
2875 uint32_t const u32GuestBlockX = pBox->srcx / pSurface->cxBlock;
2876 uint32_t const u32GuestBlockY = pBox->srcy / pSurface->cyBlock;
2877 uint32_t const u32GuestZ = pBox->srcz / pSurface->cyBlock;
2878 Assert(u32GuestBlockX * pSurface->cxBlock == pBox->srcx);
2879 Assert(u32GuestBlockY * pSurface->cyBlock == pBox->srcy);
2880
2881 uint32_t const cBlocksX = (pBox->w + pSurface->cxBlock - 1) / pSurface->cxBlock;
2882 uint32_t const cBlocksY = (pBox->h + pSurface->cyBlock - 1) / pSurface->cyBlock;
2883 AssertMsgReturn(cBlocksX && cBlocksY, ("Empty box %dx%d\n", pBox->w, pBox->h), VERR_INTERNAL_ERROR);
2884
2885 GLenum texImageTarget;
2886 if (pSurface->targetGL == GL_TEXTURE_3D)
2887 {
2888 texImageTarget = GL_TEXTURE_3D;
2889 }
2890 else if (pSurface->targetGL == GL_TEXTURE_CUBE_MAP)
2891 {
2892 texImageTarget = vmsvga3dCubemapFaceFromIndex(uHostFace);
2893 }
2894 else
2895 {
2896 AssertMsg(pSurface->targetGL == GL_TEXTURE_2D, ("%#x\n", pSurface->targetGL));
2897 texImageTarget = GL_TEXTURE_2D;
2898 }
2899
2900 /* The buffer must be large enough to hold entire texture in the OpenGL format. */
2901 pDoubleBuffer = (uint8_t *)RTMemAlloc(pSurface->cbBlockGL * pMipLevel->cBlocks);
2902 AssertReturn(pDoubleBuffer, VERR_NO_MEMORY);
2903
2904 if (transfer == SVGA3D_READ_HOST_VRAM)
2905 {
2906 /* Read the entire texture to the double buffer. */
2907 GLint activeTexture;
2908
2909 /* Must bind texture to the current context in order to read it. */
2910 glGetIntegerv(pSurface->bindingGL, &activeTexture);
2911 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2912
2913 glBindTexture(pSurface->targetGL, GLTextureId(pSurface));
2914 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2915
2916 if (pSurface->fEmulated)
2917 {
2918 FormatConvReadTexture(pState, pContext, pSurface, uHostMipmap);
2919 }
2920
2921 /* Set row length and alignment of the input data. */
2922 VMSVGAPACKPARAMS SavedParams;
2923 vmsvga3dOglSetPackParams(pState, pContext, pSurface, &SavedParams);
2924
2925 if ( pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
2926 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
2927 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
2928 {
2929 pState->ext.glGetCompressedTexImage(texImageTarget, uHostMipmap, pDoubleBuffer);
2930 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2931 }
2932 else
2933 {
2934 glGetTexImage(texImageTarget, uHostMipmap, pSurface->formatGL, pSurface->typeGL, pDoubleBuffer);
2935 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2936 }
2937
2938 vmsvga3dOglRestorePackParams(pState, pContext, pSurface, &SavedParams);
2939
2940 /* Restore the old active texture. */
2941 glBindTexture(pSurface->targetGL, activeTexture);
2942 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2943
2944 offHst = u32HostBlockX * pSurface->cbBlock + u32HostBlockY * pMipLevel->cbSurfacePitch + u32HostZ * pMipLevel->cbSurfacePlane;
2945 cbSurfacePitch = pMipLevel->cbSurfacePitch;
2946 }
2947 else
2948 {
2949 /* The buffer will contain only the copied rectangle. */
2950 offHst = 0;
2951 cbSurfacePitch = cBlocksX * pSurface->cbBlock;
2952 }
2953
2954 uint64_t offGst = u32GuestBlockX * pSurface->cbBlock + u32GuestBlockY * cbGuestPitch + u32GuestZ * cbGuestPitch * pMipLevel->mipmapSize.height;
2955
2956 for (uint32_t iPlane = 0; iPlane < pBox->d; ++iPlane)
2957 {
2958 AssertBreak(offHst < UINT32_MAX);
2959 AssertBreak(offGst < UINT32_MAX);
2960
2961 rc = vmsvgaR3GmrTransfer(pThis,
2962 pThisCC,
2963 transfer,
2964 pDoubleBuffer,
2965 pMipLevel->cbSurface,
2966 (uint32_t)offHst,
2967 cbSurfacePitch,
2968 GuestPtr,
2969 (uint32_t)offGst,
2970 cbGuestPitch,
2971 cBlocksX * pSurface->cbBlock,
2972 cBlocksY);
2973 AssertRC(rc);
2974
2975 offHst += pMipLevel->cbSurfacePlane;
2976 offGst += pMipLevel->mipmapSize.height * cbGuestPitch;
2977 }
2978
2979 /* Update the opengl surface data. */
2980 if (transfer == SVGA3D_WRITE_HOST_VRAM)
2981 {
2982 GLint activeTexture = 0;
2983 glGetIntegerv(pSurface->bindingGL, &activeTexture);
2984 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2985
2986 /* Must bind texture to the current context in order to change it. */
2987 glBindTexture(pSurface->targetGL, GLTextureId(pSurface));
2988 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2989
2990 LogFunc(("copy texture mipmap level %d (pitch %x)\n", uHostMipmap, pMipLevel->cbSurfacePitch));
2991
2992 /* Set row length and alignment of the input data. */
2993 /* We do not need to set ROW_LENGTH to w here, because the image in pDoubleBuffer is tightly packed. */
2994 VMSVGAPACKPARAMS SavedParams;
2995 vmsvga3dOglSetUnpackParams(pState, pContext, 0, 0, &SavedParams);
2996
2997 if (texImageTarget == GL_TEXTURE_3D)
2998 {
2999 if ( pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
3000 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
3001 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
3002 {
3003 pState->ext.glCompressedTexSubImage3D(texImageTarget,
3004 uHostMipmap,
3005 pBox->x,
3006 pBox->y,
3007 pBox->z,
3008 pBox->w,
3009 pBox->h,
3010 pBox->d,
3011 pSurface->internalFormatGL,
3012 cbSurfacePitch * cBlocksY * pBox->d,
3013 pDoubleBuffer);
3014 }
3015 else
3016 {
3017 pState->ext.glTexSubImage3D(texImageTarget,
3018 uHostMipmap,
3019 u32HostBlockX,
3020 u32HostBlockY,
3021 pBox->z,
3022 cBlocksX,
3023 cBlocksY,
3024 pBox->d,
3025 pSurface->formatGL,
3026 pSurface->typeGL,
3027 pDoubleBuffer);
3028 }
3029 }
3030 else
3031 {
3032 if ( pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
3033 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
3034 || pSurface->internalFormatGL == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
3035 {
3036 pState->ext.glCompressedTexSubImage2D(texImageTarget,
3037 uHostMipmap,
3038 pBox->x,
3039 pBox->y,
3040 pBox->w,
3041 pBox->h,
3042 pSurface->internalFormatGL,
3043 cbSurfacePitch * cBlocksY,
3044 pDoubleBuffer);
3045 }
3046 else
3047 {
3048 glTexSubImage2D(texImageTarget,
3049 uHostMipmap,
3050 u32HostBlockX,
3051 u32HostBlockY,
3052 cBlocksX,
3053 cBlocksY,
3054 pSurface->formatGL,
3055 pSurface->typeGL,
3056 pDoubleBuffer);
3057 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3058
3059 if (pSurface->fEmulated)
3060 {
3061 /* Convert the texture to the actual texture if necessary */
3062 FormatConvUpdateTexture(pState, pContext, pSurface, uHostMipmap);
3063 }
3064 }
3065 }
3066 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3067
3068 /* Restore old values. */
3069 vmsvga3dOglRestoreUnpackParams(pState, pContext, &SavedParams);
3070
3071 /* Restore the old active texture. */
3072 glBindTexture(pSurface->targetGL, activeTexture);
3073 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
3074 }
3075
3076 Log4(("first line:\n%.*Rhxd\n", cBlocksX * pSurface->cbBlock, pDoubleBuffer));
3077
3078 /* Free the double buffer. */
3079 RTMemFree(pDoubleBuffer);
3080 break;
3081 }
3082
3083 case VMSVGA3D_OGLRESTYPE_BUFFER:
3084 {
3085 /* Buffers are uncompressed. */
3086 AssertReturn(pSurface->cxBlock == 1 && pSurface->cyBlock == 1, VERR_INTERNAL_ERROR);
3087
3088 /* Caller already clipped pBox and buffers are 1-dimensional. */
3089 Assert(pBox->y == 0 && pBox->h == 1 && pBox->z == 0 && pBox->d == 1);
3090
3091 VMSVGA3D_CLEAR_GL_ERRORS();
3092 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pSurface->oglId.buffer);
3093 if (VMSVGA3D_GL_IS_SUCCESS(pContext))
3094 {
3095 GLenum enmGlTransfer = (transfer == SVGA3D_READ_HOST_VRAM) ? GL_READ_ONLY : GL_WRITE_ONLY;
3096 uint8_t *pbData = (uint8_t *)pState->ext.glMapBuffer(GL_ARRAY_BUFFER, enmGlTransfer);
3097 if (RT_LIKELY(pbData != NULL))
3098 {
3099#if defined(VBOX_STRICT) && defined(RT_OS_DARWIN)
3100 GLint cbStrictBufSize;
3101 glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &cbStrictBufSize);
3102 Assert(VMSVGA3D_GL_IS_SUCCESS(pContext));
3103 AssertMsg(cbStrictBufSize >= (int32_t)pMipLevel->cbSurface,
3104 ("cbStrictBufSize=%#x cbSurface=%#x pContext->id=%#x\n", (uint32_t)cbStrictBufSize, pMipLevel->cbSurface, pContext->id));
3105#endif
3106 Log(("Lock %s memory for rectangle (%d,%d)(%d,%d)\n",
3107 (pSurface->surfaceFlags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK) == SVGA3D_SURFACE_HINT_VERTEXBUFFER ? "vertex" :
3108 (pSurface->surfaceFlags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK) == SVGA3D_SURFACE_HINT_INDEXBUFFER ? "index" : "buffer",
3109 pBox->x, pBox->y, pBox->x + pBox->w, pBox->y + pBox->h));
3110
3111 /* The caller already copied the data to the pMipLevel->pSurfaceData buffer, see VMSVGA3DSURFACE_NEEDS_DATA. */
3112 uint32_t const offHst = pBox->x * pSurface->cbBlock;
3113 uint32_t const cbWidth = pBox->w * pSurface->cbBlock;
3114
3115 memcpy(pbData + offHst, (uint8_t *)pMipLevel->pSurfaceData + offHst, cbWidth);
3116
3117 Log4(("Buffer updated at [0x%x;0x%x):\n%.*Rhxd\n", offHst, offHst + cbWidth, cbWidth, (uint8_t *)pbData + offHst));
3118
3119 pState->ext.glUnmapBuffer(GL_ARRAY_BUFFER);
3120 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3121 }
3122 else
3123 VMSVGA3D_GL_GET_AND_COMPLAIN(pState, pContext, ("glMapBuffer(GL_ARRAY_BUFFER, %#x) -> NULL\n", enmGlTransfer));
3124 }
3125 else
3126 VMSVGA3D_GL_COMPLAIN(pState, pContext, ("glBindBuffer(GL_ARRAY_BUFFER, %#x)\n", pSurface->oglId.buffer));
3127 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0);
3128 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3129 break;
3130 }
3131
3132 default:
3133 AssertFailed();
3134 break;
3135 }
3136
3137 return rc;
3138}
3139
3140int vmsvga3dGenerateMipmaps(PVGASTATECC pThisCC, uint32_t sid, SVGA3dTextureFilter filter)
3141{
3142 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3143 PVMSVGA3DSURFACE pSurface;
3144 int rc = VINF_SUCCESS;
3145 PVMSVGA3DCONTEXT pContext;
3146 uint32_t cid;
3147 GLint activeTexture = 0;
3148
3149 AssertReturn(pState, VERR_NO_MEMORY);
3150
3151 rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
3152 AssertRCReturn(rc, rc);
3153
3154 Assert(filter != SVGA3D_TEX_FILTER_FLATCUBIC);
3155 Assert(filter != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
3156 pSurface->autogenFilter = filter;
3157
3158 LogFunc(("sid=%u filter=%d\n", sid, filter));
3159
3160 cid = SVGA3D_INVALID_ID;
3161 pContext = &pState->SharedCtx;
3162 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3163
3164 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
3165 {
3166 /* Unknown surface type; turn it into a texture. */
3167 LogFunc(("unknown src surface id=%x type=%d format=%d -> create texture\n", sid, pSurface->surfaceFlags, pSurface->format));
3168 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pSurface);
3169 AssertRCReturn(rc, rc);
3170 }
3171 else
3172 {
3173 /** @todo new filter */
3174 AssertFailed();
3175 }
3176
3177 glGetIntegerv(pSurface->bindingGL, &activeTexture);
3178 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3179
3180 /* Must bind texture to the current context in order to change it. */
3181 glBindTexture(pSurface->targetGL, pSurface->oglId.texture);
3182 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3183
3184 /* Generate the mip maps. */
3185 pState->ext.glGenerateMipmap(pSurface->targetGL);
3186 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3187
3188 /* Restore the old texture. */
3189 glBindTexture(pSurface->targetGL, activeTexture);
3190 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3191
3192 return VINF_SUCCESS;
3193}
3194
3195
3196#ifdef RT_OS_LINUX
3197/**
3198 * X11 event handling thread.
3199 *
3200 * @returns VINF_SUCCESS (ignored)
3201 * @param hThreadSelf thread handle
3202 * @param pvUser pointer to pState structure
3203 */
3204DECLCALLBACK(int) vmsvga3dXEventThread(RTTHREAD hThreadSelf, void *pvUser)
3205{
3206 RT_NOREF(hThreadSelf);
3207 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pvUser;
3208 while (!pState->bTerminate)
3209 {
3210 while (XPending(pState->display) > 0)
3211 {
3212 XEvent event;
3213 XNextEvent(pState->display, &event);
3214
3215 switch (event.type)
3216 {
3217 default:
3218 break;
3219 }
3220 }
3221 /* sleep for 16ms to not burn too many cycles */
3222 RTThreadSleep(16);
3223 }
3224 return VINF_SUCCESS;
3225}
3226#endif // RT_OS_LINUX
3227
3228
3229/**
3230 * Create a new 3d context
3231 *
3232 * @returns VBox status code.
3233 * @param pThisCC The VGA/VMSVGA state for ring-3.
3234 * @param cid Context id
3235 * @param fFlags VMSVGA3D_DEF_CTX_F_XXX.
3236 */
3237int vmsvga3dContextDefineOgl(PVGASTATECC pThisCC, uint32_t cid, uint32_t fFlags)
3238{
3239 int rc;
3240 PVMSVGA3DCONTEXT pContext;
3241 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3242
3243 AssertReturn(pState, VERR_NO_MEMORY);
3244 AssertReturn( cid < SVGA3D_MAX_CONTEXT_IDS
3245 || (cid == VMSVGA3D_SHARED_CTX_ID && (fFlags & VMSVGA3D_DEF_CTX_F_SHARED_CTX)), VERR_INVALID_PARAMETER);
3246#if !defined(VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE) || !(defined(RT_OS_DARWIN))
3247 AssertReturn(!(fFlags & VMSVGA3D_DEF_CTX_F_OTHER_PROFILE), VERR_INTERNAL_ERROR_3);
3248#endif
3249
3250 Log(("vmsvga3dContextDefine id %x\n", cid));
3251
3252 if (cid == VMSVGA3D_SHARED_CTX_ID)
3253 pContext = &pState->SharedCtx;
3254 else
3255 {
3256 if (cid >= pState->cContexts)
3257 {
3258 /* Grow the array. */
3259 uint32_t cNew = RT_ALIGN(cid + 15, 16);
3260 void *pvNew = RTMemRealloc(pState->papContexts, sizeof(pState->papContexts[0]) * cNew);
3261 AssertReturn(pvNew, VERR_NO_MEMORY);
3262 pState->papContexts = (PVMSVGA3DCONTEXT *)pvNew;
3263 while (pState->cContexts < cNew)
3264 {
3265 pContext = (PVMSVGA3DCONTEXT)RTMemAllocZ(sizeof(*pContext));
3266 AssertReturn(pContext, VERR_NO_MEMORY);
3267 pContext->id = SVGA3D_INVALID_ID;
3268 pState->papContexts[pState->cContexts++] = pContext;
3269 }
3270 }
3271 /* If one already exists with this id, then destroy it now. */
3272 if (pState->papContexts[cid]->id != SVGA3D_INVALID_ID)
3273 vmsvga3dContextDestroy(pThisCC, cid);
3274
3275 pContext = pState->papContexts[cid];
3276 }
3277
3278 /*
3279 * Find or create the shared context if needed (necessary for sharing e.g. textures between contexts).
3280 */
3281 PVMSVGA3DCONTEXT pSharedCtx = NULL;
3282 if (!(fFlags & (VMSVGA3D_DEF_CTX_F_INIT | VMSVGA3D_DEF_CTX_F_SHARED_CTX)))
3283 {
3284 pSharedCtx = &pState->SharedCtx;
3285 if (pSharedCtx->id != VMSVGA3D_SHARED_CTX_ID)
3286 {
3287 rc = vmsvga3dContextDefineOgl(pThisCC, VMSVGA3D_SHARED_CTX_ID, VMSVGA3D_DEF_CTX_F_SHARED_CTX);
3288 AssertLogRelRCReturn(rc, rc);
3289
3290 /* Create resources which use the shared context. */
3291 vmsvga3dOnSharedContextDefine(pState);
3292 }
3293 }
3294
3295 /*
3296 * Initialize the context.
3297 */
3298 memset(pContext, 0, sizeof(*pContext));
3299 pContext->id = cid;
3300 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTextures); i++)
3301 pContext->aSidActiveTextures[i] = SVGA3D_INVALID_ID;
3302
3303 pContext->state.shidVertex = SVGA3D_INVALID_ID;
3304 pContext->state.shidPixel = SVGA3D_INVALID_ID;
3305 pContext->idFramebuffer = OPENGL_INVALID_ID;
3306 pContext->idReadFramebuffer = OPENGL_INVALID_ID;
3307 pContext->idDrawFramebuffer = OPENGL_INVALID_ID;
3308
3309 rc = ShaderContextCreate(&pContext->pShaderContext);
3310 AssertRCReturn(rc, rc);
3311
3312 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); i++)
3313 pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
3314
3315#ifdef RT_OS_WINDOWS
3316 /* Create a context window with minimal 4x4 size. We will never use the swapchain
3317 * to present the rendered image. Rendered images from the guest will be copied to
3318 * the VMSVGA SCREEN object, which can be either an offscreen render target or
3319 * system memory in the guest VRAM.
3320 */
3321 rc = vmsvga3dContextWindowCreate(pState->hInstance, pState->pWindowThread, pState->WndRequestSem, &pContext->hwnd);
3322 AssertRCReturn(rc, rc);
3323
3324 pContext->hdc = GetDC(pContext->hwnd);
3325 AssertMsgReturn(pContext->hdc, ("GetDC %x failed with %d\n", pContext->hwnd, GetLastError()), VERR_INTERNAL_ERROR);
3326
3327 PIXELFORMATDESCRIPTOR pfd = {
3328 sizeof(PIXELFORMATDESCRIPTOR), /* size of this pfd */
3329 1, /* version number */
3330 PFD_DRAW_TO_WINDOW | /* support window */
3331 PFD_SUPPORT_OPENGL, /* support OpenGL */
3332 PFD_TYPE_RGBA, /* RGBA type */
3333 24, /* 24-bit color depth */
3334 0, 0, 0, 0, 0, 0, /* color bits ignored */
3335 8, /* alpha buffer */
3336 0, /* shift bit ignored */
3337 0, /* no accumulation buffer */
3338 0, 0, 0, 0, /* accum bits ignored */
3339 16, /* set depth buffer */
3340 16, /* set stencil buffer */
3341 0, /* no auxiliary buffer */
3342 PFD_MAIN_PLANE, /* main layer */
3343 0, /* reserved */
3344 0, 0, 0 /* layer masks ignored */
3345 };
3346 int pixelFormat;
3347 BOOL ret;
3348
3349 pixelFormat = ChoosePixelFormat(pContext->hdc, &pfd);
3350 /** @todo is this really necessary?? */
3351 pixelFormat = ChoosePixelFormat(pContext->hdc, &pfd);
3352 AssertMsgReturn(pixelFormat != 0, ("ChoosePixelFormat failed with %d\n", GetLastError()), VERR_INTERNAL_ERROR);
3353
3354 ret = SetPixelFormat(pContext->hdc, pixelFormat, &pfd);
3355 AssertMsgReturn(ret == TRUE, ("SetPixelFormat failed with %d\n", GetLastError()), VERR_INTERNAL_ERROR);
3356
3357 pContext->hglrc = wglCreateContext(pContext->hdc);
3358 AssertMsgReturn(pContext->hglrc, ("wglCreateContext %x failed with %d\n", pContext->hdc, GetLastError()), VERR_INTERNAL_ERROR);
3359
3360 if (pSharedCtx)
3361 {
3362 ret = wglShareLists(pSharedCtx->hglrc, pContext->hglrc);
3363 AssertMsg(ret == TRUE, ("wglShareLists(%p, %p) failed with %d\n", pSharedCtx->hglrc, pContext->hglrc, GetLastError()));
3364 }
3365
3366#elif defined(RT_OS_DARWIN)
3367 pContext->fOtherProfile = RT_BOOL(fFlags & VMSVGA3D_DEF_CTX_F_OTHER_PROFILE);
3368
3369 NativeNSOpenGLContextRef pShareContext = pSharedCtx ? pSharedCtx->cocoaContext : NULL;
3370 vmsvga3dCocoaCreateViewAndContext(&pContext->cocoaView, &pContext->cocoaContext,
3371 NULL,
3372 4, 4,
3373 pShareContext, pContext->fOtherProfile);
3374
3375#else
3376 if (pState->display == NULL)
3377 {
3378 /* get an X display and make sure we have glX 1.3 */
3379 pState->display = XOpenDisplay(0);
3380 AssertLogRelMsgReturn(pState->display, ("XOpenDisplay failed"), VERR_INTERNAL_ERROR);
3381 int glxMajor, glxMinor;
3382 Bool ret = glXQueryVersion(pState->display, &glxMajor, &glxMinor);
3383 AssertLogRelMsgReturn(ret && glxMajor == 1 && glxMinor >= 3, ("glX >=1.3 not present"), VERR_INTERNAL_ERROR);
3384 /* start our X event handling thread */
3385 rc = RTThreadCreate(&pState->pWindowThread, vmsvga3dXEventThread, pState, 0, RTTHREADTYPE_GUI, RTTHREADFLAGS_WAITABLE, "VMSVGA3DXEVENT");
3386 AssertLogRelMsgReturn(RT_SUCCESS(rc), ("Async IO Thread creation for 3d window handling failed rc=%Rrc\n", rc), rc);
3387 }
3388
3389 Window defaultRootWindow = XDefaultRootWindow(pState->display);
3390 /* Create a small 4x4 window required for GL context. */
3391 int attrib[] =
3392 {
3393 GLX_RGBA,
3394 GLX_RED_SIZE, 1,
3395 GLX_GREEN_SIZE, 1,
3396 GLX_BLUE_SIZE, 1,
3397 //GLX_ALPHA_SIZE, 1, this flips the bbos screen
3398 GLX_DOUBLEBUFFER,
3399 None
3400 };
3401 XVisualInfo *vi = glXChooseVisual(pState->display, DefaultScreen(pState->display), attrib);
3402 AssertLogRelMsgReturn(vi, ("glXChooseVisual failed"), VERR_INTERNAL_ERROR);
3403 XSetWindowAttributes swa;
3404 swa.colormap = XCreateColormap(pState->display, defaultRootWindow, vi->visual, AllocNone);
3405 AssertLogRelMsgReturn(swa.colormap, ("XCreateColormap failed"), VERR_INTERNAL_ERROR);
3406 swa.border_pixel = 0;
3407 swa.background_pixel = 0;
3408 swa.event_mask = StructureNotifyMask;
3409 unsigned long flags = CWBorderPixel | CWBackPixel | CWColormap | CWEventMask;
3410 pContext->window = XCreateWindow(pState->display, defaultRootWindow,
3411 0, 0, 4, 4,
3412 0, vi->depth, InputOutput,
3413 vi->visual, flags, &swa);
3414 AssertLogRelMsgReturn(pContext->window, ("XCreateWindow failed"), VERR_INTERNAL_ERROR);
3415
3416 /* The window is hidden by default and never mapped, because we only render offscreen and never present to it. */
3417
3418 GLXContext shareContext = pSharedCtx ? pSharedCtx->glxContext : NULL;
3419 pContext->glxContext = glXCreateContext(pState->display, vi, shareContext, GL_TRUE);
3420 AssertLogRelMsgReturn(pContext->glxContext, ("glXCreateContext failed"), VERR_INTERNAL_ERROR);
3421#endif
3422
3423 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3424
3425 /* NULL during the first PowerOn call. */
3426 if (pState->ext.glGenFramebuffers)
3427 {
3428 /* Create a framebuffer object for this context. */
3429 pState->ext.glGenFramebuffers(1, &pContext->idFramebuffer);
3430 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3431
3432 /* Bind the object to the framebuffer target. */
3433 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
3434 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3435
3436 /* Create read and draw framebuffer objects for this context. */
3437 pState->ext.glGenFramebuffers(1, &pContext->idReadFramebuffer);
3438 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3439
3440 pState->ext.glGenFramebuffers(1, &pContext->idDrawFramebuffer);
3441 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3442
3443 }
3444#if 0
3445 /** @todo move to shader lib!!! */
3446 /* Clear the screen */
3447 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3448
3449 glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
3450 glClearIndex(0);
3451 glClearDepth(1);
3452 glClearStencil(0xffff);
3453 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
3454 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
3455 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
3456 if (pState->ext.glProvokingVertex)
3457 pState->ext.glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);
3458 /** @todo move to shader lib!!! */
3459#endif
3460 return VINF_SUCCESS;
3461}
3462
3463#if defined(RT_OS_LINUX)
3464/*
3465 * HW accelerated graphics output.
3466 */
3467
3468/**
3469 * VMSVGA3d screen data.
3470 *
3471 * Allocated on the heap and pointed to by VMSVGASCREENOBJECT::pHwScreen.
3472 */
3473typedef struct VMSVGAHWSCREEN
3474{
3475 /* OpenGL context, which is used for the screen updates. */
3476 GLXContext glxctx;
3477
3478 /* The overlay window. */
3479 Window xwindow;
3480
3481 /* The RGBA texture which hold the screen content. */
3482 GLuint idScreenTexture;
3483
3484 /* Read and draw framebuffer objects for copying a surface to the screen texture. */
3485 GLuint idReadFramebuffer;
3486 GLuint idDrawFramebuffer;
3487} VMSVGAHWSCREEN;
3488
3489/* Send a notification to the UI. */
3490#if 0 /* Unused */
3491static int vmsvga3dDrvNotifyHwScreen(PVGASTATECC pThisCC, VBOX3D_NOTIFY_TYPE enmNotification,
3492 uint32_t idScreen, Pixmap pixmap, void *pvData, size_t cbData)
3493{
3494 uint8_t au8Buffer[128];
3495 AssertLogRelMsgReturn(cbData <= sizeof(au8Buffer) - sizeof(VBOX3DNOTIFY),
3496 ("cbData %zu", cbData),
3497 VERR_INVALID_PARAMETER);
3498
3499 VBOX3DNOTIFY *p = (VBOX3DNOTIFY *)&au8Buffer[0];
3500 p->enmNotification = enmNotification;
3501 p->iDisplay = idScreen;
3502 p->u32Reserved = 0;
3503 p->cbData = cbData + sizeof(uint64_t);
3504 /* au8Data consists of a 64 bit pixmap handle followed by notification specific data. */
3505 AssertCompile(sizeof(pixmap) <= sizeof(uint64_t));
3506 *(uint64_t *)&p->au8Data[0] = (uint64_t)pixmap;
3507 memcpy(&p->au8Data[sizeof(uint64_t)], pvData, cbData);
3508
3509 int rc = pThisCC->pDrv->pfn3DNotifyProcess(pThisCC->pDrv, p);
3510 return rc;
3511}
3512#endif /* Unused */
3513
3514static void vmsvga3dDrvNotifyHwOverlay(PVGASTATECC pThisCC, VBOX3D_NOTIFY_TYPE enmNotification, uint32_t idScreen)
3515{
3516 uint8_t au8Buffer[128];
3517 VBOX3DNOTIFY *p = (VBOX3DNOTIFY *)&au8Buffer[0];
3518 p->enmNotification = enmNotification;
3519 p->iDisplay = idScreen;
3520 p->u32Reserved = 0;
3521 p->cbData = sizeof(uint64_t);
3522 *(uint64_t *)&p->au8Data[0] = 0;
3523
3524 pThisCC->pDrv->pfn3DNotifyProcess(pThisCC->pDrv, p);
3525}
3526
3527/* Get X Window handle of the UI Framebuffer window. */
3528static int vmsvga3dDrvQueryWindow(PVGASTATECC pThisCC, uint32_t idScreen, Window *pWindow)
3529{
3530 uint8_t au8Buffer[128];
3531 VBOX3DNOTIFY *p = (VBOX3DNOTIFY *)&au8Buffer[0];
3532 p->enmNotification = VBOX3D_NOTIFY_TYPE_HW_OVERLAY_GET_ID;
3533 p->iDisplay = idScreen;
3534 p->u32Reserved = 0;
3535 p->cbData = sizeof(uint64_t);
3536 *(uint64_t *)&p->au8Data[0] = 0;
3537
3538 int rc = pThisCC->pDrv->pfn3DNotifyProcess(pThisCC->pDrv, p);
3539 if (RT_SUCCESS(rc))
3540 {
3541 *pWindow = (Window)*(uint64_t *)&p->au8Data[0];
3542 }
3543 return rc;
3544}
3545
3546static int ctxErrorHandler(Display *dpy, XErrorEvent *ev)
3547{
3548 RT_NOREF(dpy);
3549 LogRel4(("VMSVGA: XError %d\n", (int)ev->error_code));
3550 return 0;
3551}
3552
3553/* Create an overlay X window for the HW accelerated screen. */
3554static int vmsvga3dHwScreenCreate(PVMSVGA3DSTATE pState, Window parentWindow, unsigned int cWidth, unsigned int cHeight, VMSVGAHWSCREEN *p)
3555{
3556 int (*oldHandler)(Display*, XErrorEvent*) = XSetErrorHandler(&ctxErrorHandler);
3557
3558 int rc = VINF_SUCCESS;
3559
3560 XWindowAttributes parentAttr;
3561 if (XGetWindowAttributes(pState->display, parentWindow, &parentAttr) == 0)
3562 return VERR_INVALID_PARAMETER;
3563
3564 int const idxParentScreen = XScreenNumberOfScreen(parentAttr.screen);
3565
3566 /*
3567 * Create a new GL context, which will be used for copying to the screen.
3568 */
3569
3570 /* FBConfig attributes for the overlay window. */
3571 static int const aConfigAttribList[] =
3572 {
3573 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, // Must support GLX windows
3574 GLX_DOUBLEBUFFER, False, // Double buffering had a much lower performance.
3575 GLX_RED_SIZE, 8, // True color RGB with 8 bits per channel.
3576 GLX_GREEN_SIZE, 8,
3577 GLX_BLUE_SIZE, 8,
3578 GLX_ALPHA_SIZE, 8,
3579 GLX_STENCIL_SIZE, 0, // No stencil buffer
3580 GLX_DEPTH_SIZE, 0, // No depth buffer
3581 None
3582 };
3583
3584 /* Find a suitable FB config. */
3585 int cConfigs = 0;
3586 GLXFBConfig *paConfigs = glXChooseFBConfig(pState->display, idxParentScreen, aConfigAttribList, &cConfigs);
3587 LogRel4(("VMSVGA: vmsvga3dHwScreenCreate: paConfigs %p cConfigs %d\n", (void *)paConfigs, cConfigs));
3588 if (paConfigs)
3589 {
3590 XVisualInfo *vi = NULL;
3591 int i = 0;
3592 for (; i < cConfigs; ++i)
3593 {
3594 /* Use XFree to free the data returned in the previous iteration of this loop. */
3595 if (vi)
3596 XFree(vi);
3597
3598 vi = glXGetVisualFromFBConfig(pState->display, paConfigs[i]);
3599 if (!vi)
3600 continue;
3601
3602 LogRel4(("VMSVGA: vmsvga3dHwScreenCreate: %p vid %lu screen %d depth %d r %lu g %lu b %lu clrmap %d bitsperrgb %d\n",
3603 (void *)vi->visual, vi->visualid, vi->screen, vi->depth,
3604 vi->red_mask, vi->green_mask, vi->blue_mask, vi->colormap_size, vi->bits_per_rgb));
3605
3606 /* Same screen as the parent window. */
3607 if (vi->screen != idxParentScreen)
3608 continue;
3609
3610 /* Search for 32 bits per pixel. */
3611 if (vi->depth != 32)
3612 continue;
3613
3614 /* 8 bits per color component is enough. */
3615 if (vi->bits_per_rgb != 8)
3616 continue;
3617
3618 /* Render to pixmap. */
3619 int value = 0;
3620 glXGetFBConfigAttrib(pState->display, paConfigs[i], GLX_DRAWABLE_TYPE, &value);
3621 if (!(value & GLX_WINDOW_BIT))
3622 continue;
3623
3624 /* This FB config can be used. */
3625 break;
3626 }
3627
3628 if (i < cConfigs)
3629 {
3630 /* Found a suitable config with index i. */
3631
3632 /* Create an overlay window. */
3633 XSetWindowAttributes swa;
3634 RT_ZERO(swa);
3635
3636 swa.colormap = XCreateColormap(pState->display, parentWindow, vi->visual, AllocNone);
3637 AssertLogRelMsg(swa.colormap, ("XCreateColormap failed"));
3638 swa.border_pixel = 0;
3639 swa.background_pixel = 0;
3640 swa.event_mask = StructureNotifyMask;
3641 swa.override_redirect = 1;
3642 unsigned long const swaAttrs = CWBorderPixel | CWBackPixel | CWColormap | CWEventMask | CWOverrideRedirect;
3643 p->xwindow = XCreateWindow(pState->display, parentWindow,
3644 0, 0, cWidth, cHeight, 0, vi->depth, InputOutput,
3645 vi->visual, swaAttrs, &swa);
3646 LogRel4(("VMSVGA: vmsvga3dHwScreenCreate: p->xwindow %ld\n", p->xwindow));
3647 if (p->xwindow)
3648 {
3649
3650 p->glxctx = glXCreateContext(pState->display, vi, pState->SharedCtx.glxContext, GL_TRUE);
3651 LogRel4(("VMSVGA: vmsvga3dHwScreenCreate: p->glxctx %p\n", (void *)p->glxctx));
3652 if (p->glxctx)
3653 {
3654 XMapWindow(pState->display, p->xwindow);
3655 }
3656 else
3657 {
3658 LogRel4(("VMSVGA: vmsvga3dHwScreenCreate: glXCreateContext failed\n"));
3659 rc = VERR_NOT_SUPPORTED;
3660 }
3661 }
3662 else
3663 {
3664 LogRel4(("VMSVGA: vmsvga3dHwScreenCreate: XCreateWindow failed\n"));
3665 rc = VERR_NOT_SUPPORTED;
3666 }
3667
3668 XSync(pState->display, False);
3669 }
3670 else
3671 {
3672 /* A suitable config is not found. */
3673 LogRel4(("VMSVGA: vmsvga3dHwScreenCreate: no FBConfig\n"));
3674 rc = VERR_NOT_SUPPORTED;
3675 }
3676
3677 if (vi)
3678 XFree(vi);
3679
3680 /* "Use XFree to free the memory returned by glXChooseFBConfig." */
3681 XFree(paConfigs);
3682 }
3683 else
3684 {
3685 /* glXChooseFBConfig failed. */
3686 rc = VERR_NOT_SUPPORTED;
3687 }
3688
3689 XSetErrorHandler(oldHandler);
3690 return rc;
3691}
3692
3693/* Destroy a HW accelerated screen. */
3694static void vmsvga3dHwScreenDestroy(PVMSVGA3DSTATE pState, VMSVGAHWSCREEN *p)
3695{
3696 if (p)
3697 {
3698 LogRel4(("VMSVGA: vmsvga3dHwScreenDestroy: p->xwindow %ld, ctx %p\n", p->xwindow, (void *)p->glxctx));
3699 if (p->glxctx)
3700 {
3701 /* GLX context is changed here, so other code has to set the appropriate context again. */
3702 VMSVGA3D_CLEAR_CURRENT_CONTEXT(pState);
3703
3704 glXMakeCurrent(pState->display, p->xwindow, p->glxctx);
3705
3706 /* Clean up OpenGL. */
3707 if (p->idReadFramebuffer != OPENGL_INVALID_ID)
3708 pState->ext.glDeleteFramebuffers(1, &p->idReadFramebuffer);
3709 if (p->idDrawFramebuffer != OPENGL_INVALID_ID)
3710 pState->ext.glDeleteFramebuffers(1, &p->idDrawFramebuffer);
3711 if (p->idScreenTexture != OPENGL_INVALID_ID)
3712 glDeleteTextures(1, &p->idScreenTexture);
3713
3714 glXMakeCurrent(pState->display, None, NULL);
3715
3716 glXDestroyContext(pState->display, p->glxctx);
3717 }
3718
3719 if (p->xwindow)
3720 XDestroyWindow(pState->display, p->xwindow);
3721
3722 RT_ZERO(*p);
3723 }
3724}
3725
3726#define GLCHECK() \
3727 do { \
3728 int glErr = glGetError(); \
3729 if (glErr != GL_NO_ERROR) LogRel4(("VMSVGA: GL error 0x%x @%d\n", glErr, __LINE__)); \
3730 } while(0)
3731
3732int vmsvga3dBackDefineScreen(PVGASTATE pThis, PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen)
3733{
3734 LogRel4(("VMSVGA: vmsvga3dBackDefineScreen: screen %u\n", pScreen->idScreen));
3735
3736 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3737 AssertReturn(pState, VERR_NOT_SUPPORTED);
3738
3739 if (!pThis->svga.f3DOverlayEnabled)
3740 return VERR_NOT_SUPPORTED;
3741
3742 Assert(pScreen->pHwScreen == NULL);
3743
3744 VMSVGAHWSCREEN *p = (VMSVGAHWSCREEN *)RTMemAllocZ(sizeof(VMSVGAHWSCREEN));
3745 AssertPtrReturn(p, VERR_NO_MEMORY);
3746
3747 /* Query the parent window ID from the UI framebuffer.
3748 * If it is there then
3749 * the device will create a texture for the screen content and an overlay window to present the screen content.
3750 * otherwise
3751 * the device will use the guest VRAM system memory for the screen content.
3752 */
3753 Window parentWindow;
3754 int rc = vmsvga3dDrvQueryWindow(pThisCC, pScreen->idScreen, &parentWindow);
3755 if (RT_SUCCESS(rc))
3756 {
3757 /* Create the hardware accelerated screen. */
3758 rc = vmsvga3dHwScreenCreate(pState, parentWindow, pScreen->cWidth, pScreen->cHeight, p);
3759 if (RT_SUCCESS(rc))
3760 {
3761 /*
3762 * Setup the OpenGL context of the screen. The context will be used to draw on the screen.
3763 */
3764
3765 /* GLX context is changed here, so other code has to set the appropriate context again. */
3766 VMSVGA3D_CLEAR_CURRENT_CONTEXT(pState);
3767
3768 Bool const fSuccess = glXMakeCurrent(pState->display, p->xwindow, p->glxctx);
3769 if (fSuccess)
3770 {
3771 /* Set GL state. */
3772 glClearColor(0, 0, 0, 1);
3773 glEnable(GL_TEXTURE_2D);
3774 glDisable(GL_DEPTH_TEST);
3775 glDisable(GL_CULL_FACE);
3776
3777 /* The RGBA texture which hold the screen content. */
3778 glGenTextures(1, &p->idScreenTexture); GLCHECK();
3779 glBindTexture(GL_TEXTURE_2D, p->idScreenTexture); GLCHECK();
3780 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); GLCHECK();
3781 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); GLCHECK();
3782 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, pScreen->cWidth, pScreen->cHeight, 0,
3783 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL); GLCHECK();
3784
3785 /* Create read and draw framebuffer objects for this screen. */
3786 pState->ext.glGenFramebuffers(1, &p->idReadFramebuffer); GLCHECK();
3787 pState->ext.glGenFramebuffers(1, &p->idDrawFramebuffer); GLCHECK();
3788
3789 /* Work in screen coordinates. */
3790 glMatrixMode(GL_MODELVIEW);
3791 glLoadIdentity();
3792 glOrtho(0, pScreen->cWidth, 0, pScreen->cHeight, -1, 1);
3793 glMatrixMode(GL_PROJECTION);
3794 glLoadIdentity();
3795
3796 /* Clear the texture. */
3797 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, p->idDrawFramebuffer); GLCHECK();
3798 pState->ext.glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
3799 p->idScreenTexture, 0); GLCHECK();
3800
3801 glClear(GL_COLOR_BUFFER_BIT);
3802
3803 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); GLCHECK();
3804
3805 glXMakeCurrent(pState->display, None, NULL);
3806
3807 XSync(pState->display, False);
3808
3809 vmsvga3dDrvNotifyHwOverlay(pThisCC, VBOX3D_NOTIFY_TYPE_HW_OVERLAY_CREATED, pScreen->idScreen);
3810 }
3811 else
3812 {
3813 LogRel4(("VMSVGA: vmsvga3dBackDefineScreen: failed to set current context\n"));
3814 rc = VERR_NOT_SUPPORTED;
3815 }
3816 }
3817 }
3818 else
3819 {
3820 LogRel4(("VMSVGA: vmsvga3dBackDefineScreen: no framebuffer\n"));
3821 }
3822
3823 if (RT_SUCCESS(rc))
3824 {
3825 LogRel(("VMSVGA: Using HW accelerated screen %u\n", pScreen->idScreen));
3826 pScreen->pHwScreen = p;
3827 }
3828 else
3829 {
3830 LogRel4(("VMSVGA: vmsvga3dBackDefineScreen: %Rrc\n", rc));
3831 vmsvga3dHwScreenDestroy(pState, p);
3832 RTMemFree(p);
3833 }
3834
3835 return rc;
3836}
3837
3838int vmsvga3dBackDestroyScreen(PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen)
3839{
3840 LogRel4(("VMSVGA: vmsvga3dBackDestroyScreen: screen %u\n", pScreen->idScreen));
3841
3842 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3843 AssertReturn(pState, VERR_NOT_SUPPORTED);
3844
3845 int (*oldHandler)(Display*, XErrorEvent*) = XSetErrorHandler(&ctxErrorHandler);
3846
3847 VMSVGAHWSCREEN *p = pScreen->pHwScreen;
3848 if (p)
3849 {
3850 pScreen->pHwScreen = NULL;
3851
3852 vmsvga3dDrvNotifyHwOverlay(pThisCC, VBOX3D_NOTIFY_TYPE_HW_OVERLAY_DESTROYED, pScreen->idScreen);
3853
3854 vmsvga3dHwScreenDestroy(pState, p);
3855 RTMemFree(p);
3856 }
3857
3858 XSetErrorHandler(oldHandler);
3859
3860 return VINF_SUCCESS;
3861}
3862
3863/* Blit a surface to the GLX pixmap. */
3864int vmsvga3dBackSurfaceBlitToScreen(PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen,
3865 SVGASignedRect destRect, SVGA3dSurfaceImageId srcImage,
3866 SVGASignedRect srcRect, uint32_t cRects, SVGASignedRect *paRects)
3867{
3868 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3869 AssertReturn(pState, VERR_NOT_SUPPORTED);
3870
3871 VMSVGAHWSCREEN *p = pScreen->pHwScreen;
3872 AssertReturn(p, VERR_NOT_SUPPORTED);
3873
3874 PVMSVGA3DSURFACE pSurface;
3875 int rc = vmsvga3dSurfaceFromSid(pState, srcImage.sid, &pSurface);
3876 AssertRCReturn(rc, rc);
3877
3878 if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
3879 {
3880 LogFunc(("src sid=%u flags=0x%x format=%d -> create texture\n", srcImage.sid, pSurface->surfaceFlags, pSurface->format));
3881 rc = vmsvga3dBackCreateTexture(pState, &pState->SharedCtx, VMSVGA3D_SHARED_CTX_ID, pSurface);
3882 AssertRCReturn(rc, rc);
3883 }
3884
3885 AssertReturn(pSurface->enmOGLResType == VMSVGA3D_OGLRESTYPE_TEXTURE, VERR_NOT_SUPPORTED);
3886
3887 PVMSVGA3DMIPMAPLEVEL pMipLevel;
3888 rc = vmsvga3dMipmapLevel(pSurface, srcImage.face, srcImage.mipmap, &pMipLevel);
3889 AssertRCReturn(rc, rc);
3890
3891 /** @todo Implement. */
3892 RT_NOREF(cRects, paRects);
3893
3894 /* GLX context is changed here, so other code has to set appropriate context again. */
3895 VMSVGA3D_CLEAR_CURRENT_CONTEXT(pState);
3896
3897 int (*oldHandler)(Display*, XErrorEvent*) = XSetErrorHandler(&ctxErrorHandler);
3898
3899 Bool fSuccess = glXMakeCurrent(pState->display, p->xwindow, p->glxctx);
3900 if (fSuccess)
3901 {
3902 /* Activate the read and draw framebuffer objects. */
3903 pState->ext.glBindFramebuffer(GL_READ_FRAMEBUFFER, p->idReadFramebuffer); GLCHECK();
3904 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, p->idDrawFramebuffer); GLCHECK();
3905
3906 /* Bind the source and destination objects to the right place. */
3907 pState->ext.glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
3908 pSurface->oglId.texture, 0); GLCHECK();
3909 pState->ext.glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
3910 p->idScreenTexture, 0); GLCHECK();
3911
3912 pState->ext.glBlitFramebuffer(srcRect.left,
3913 srcRect.top,
3914 srcRect.right,
3915 srcRect.bottom,
3916 destRect.left,
3917 destRect.top,
3918 destRect.right,
3919 destRect.bottom,
3920 GL_COLOR_BUFFER_BIT,
3921 GL_NEAREST); GLCHECK();
3922
3923 /* Reset the frame buffer association */
3924 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, 0); GLCHECK();
3925
3926 /* Update the overlay window. */
3927 glClear(GL_COLOR_BUFFER_BIT);
3928
3929 glBindTexture(GL_TEXTURE_2D, p->idScreenTexture); GLCHECK();
3930
3931 GLint const w = pScreen->cWidth;
3932 GLint const h = pScreen->cHeight;
3933
3934 glBegin(GL_QUADS);
3935 glTexCoord2f(0.0f, 0.0f); glVertex2i(0, h);
3936 glTexCoord2f(0.0f, 1.0f); glVertex2i(0, 0);
3937 glTexCoord2f(1.0f, 1.0f); glVertex2i(w, 0);
3938 glTexCoord2f(1.0f, 0.0f); glVertex2i(w, h);
3939 glEnd(); GLCHECK();
3940
3941 glBindTexture(GL_TEXTURE_2D, 0); GLCHECK();
3942
3943 glXMakeCurrent(pState->display, None, NULL);
3944 }
3945 else
3946 {
3947 LogRel4(("VMSVGA: vmsvga3dBackSurfaceBlitToScreen: screen %u, glXMakeCurrent for pixmap failed\n", pScreen->idScreen));
3948 }
3949
3950 XSetErrorHandler(oldHandler);
3951
3952 return VINF_SUCCESS;
3953}
3954
3955#else /* !RT_OS_LINUX */
3956
3957int vmsvga3dBackDefineScreen(PVGASTATE pThis, PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen)
3958{
3959 RT_NOREF(pThis, pThisCC, pScreen);
3960 return VERR_NOT_IMPLEMENTED;
3961}
3962
3963int vmsvga3dBackDestroyScreen(PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen)
3964{
3965 RT_NOREF(pThisCC, pScreen);
3966 return VERR_NOT_IMPLEMENTED;
3967}
3968
3969int vmsvga3dBackSurfaceBlitToScreen(PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen,
3970 SVGASignedRect destRect, SVGA3dSurfaceImageId srcImage,
3971 SVGASignedRect srcRect, uint32_t cRects, SVGASignedRect *paRects)
3972{
3973 RT_NOREF(pThisCC, pScreen, destRect, srcImage, srcRect, cRects, paRects);
3974 return VERR_NOT_IMPLEMENTED;
3975}
3976#endif
3977
3978/**
3979 * Create a new 3d context
3980 *
3981 * @returns VBox status code.
3982 * @param pThisCC The VGA/VMSVGA state for ring-3.
3983 * @param cid Context id
3984 */
3985int vmsvga3dContextDefine(PVGASTATECC pThisCC, uint32_t cid)
3986{
3987 return vmsvga3dContextDefineOgl(pThisCC, cid, 0/*fFlags*/);
3988}
3989
3990/**
3991 * Destroys a 3d context.
3992 *
3993 * @returns VBox status code.
3994 * @param pThisCC The VGA/VMSVGA state for ring-3.
3995 * @param pContext The context to destroy.
3996 * @param cid Context id
3997 */
3998static int vmsvga3dContextDestroyOgl(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t cid)
3999{
4000 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4001 AssertReturn(pState, VERR_NO_MEMORY);
4002 AssertReturn(pContext, VERR_INVALID_PARAMETER);
4003 AssertReturn(pContext->id == cid, VERR_INVALID_PARAMETER);
4004 Log(("vmsvga3dContextDestroyOgl id %x\n", cid));
4005
4006 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4007
4008 if (pContext->id == VMSVGA3D_SHARED_CTX_ID)
4009 {
4010 /* Delete resources which use the shared context. */
4011 vmsvga3dOnSharedContextDestroy(pState);
4012 }
4013
4014 /* Destroy all leftover pixel shaders. */
4015 for (uint32_t i = 0; i < pContext->cPixelShaders; i++)
4016 {
4017 if (pContext->paPixelShader[i].id != SVGA3D_INVALID_ID)
4018 vmsvga3dShaderDestroy(pThisCC, pContext->paPixelShader[i].cid, pContext->paPixelShader[i].id, pContext->paPixelShader[i].type);
4019 }
4020 if (pContext->paPixelShader)
4021 RTMemFree(pContext->paPixelShader);
4022
4023 /* Destroy all leftover vertex shaders. */
4024 for (uint32_t i = 0; i < pContext->cVertexShaders; i++)
4025 {
4026 if (pContext->paVertexShader[i].id != SVGA3D_INVALID_ID)
4027 vmsvga3dShaderDestroy(pThisCC, pContext->paVertexShader[i].cid, pContext->paVertexShader[i].id, pContext->paVertexShader[i].type);
4028 }
4029 if (pContext->paVertexShader)
4030 RTMemFree(pContext->paVertexShader);
4031
4032 if (pContext->state.paVertexShaderConst)
4033 RTMemFree(pContext->state.paVertexShaderConst);
4034 if (pContext->state.paPixelShaderConst)
4035 RTMemFree(pContext->state.paPixelShaderConst);
4036
4037 if (pContext->pShaderContext)
4038 {
4039 int rc = ShaderContextDestroy(pContext->pShaderContext);
4040 AssertRC(rc);
4041 }
4042
4043 if (pContext->idFramebuffer != OPENGL_INVALID_ID)
4044 {
4045 /* Unbind the object from the framebuffer target. */
4046 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, 0 /* back buffer */);
4047 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4048 pState->ext.glDeleteFramebuffers(1, &pContext->idFramebuffer);
4049 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4050
4051 if (pContext->idReadFramebuffer != OPENGL_INVALID_ID)
4052 {
4053 pState->ext.glDeleteFramebuffers(1, &pContext->idReadFramebuffer);
4054 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4055 }
4056 if (pContext->idDrawFramebuffer != OPENGL_INVALID_ID)
4057 {
4058 pState->ext.glDeleteFramebuffers(1, &pContext->idDrawFramebuffer);
4059 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4060 }
4061 }
4062
4063 vmsvga3dOcclusionQueryDelete(pState, pContext);
4064
4065#ifdef RT_OS_WINDOWS
4066 wglMakeCurrent(pContext->hdc, NULL);
4067 wglDeleteContext(pContext->hglrc);
4068 ReleaseDC(pContext->hwnd, pContext->hdc);
4069
4070 /* Destroy the window we've created. */
4071 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_DESTROYWINDOW, (WPARAM)pContext->hwnd, 0);
4072 AssertRC(rc);
4073#elif defined(RT_OS_DARWIN)
4074 vmsvga3dCocoaDestroyViewAndContext(pContext->cocoaView, pContext->cocoaContext);
4075#elif defined(RT_OS_LINUX)
4076 glXMakeCurrent(pState->display, None, NULL);
4077 glXDestroyContext(pState->display, pContext->glxContext);
4078 XDestroyWindow(pState->display, pContext->window);
4079#endif
4080
4081 memset(pContext, 0, sizeof(*pContext));
4082 pContext->id = SVGA3D_INVALID_ID;
4083
4084 VMSVGA3D_CLEAR_CURRENT_CONTEXT(pState);
4085 return VINF_SUCCESS;
4086}
4087
4088/**
4089 * Destroy an existing 3d context
4090 *
4091 * @returns VBox status code.
4092 * @param pThisCC The VGA/VMSVGA state for ring-3.
4093 * @param cid Context id
4094 */
4095int vmsvga3dContextDestroy(PVGASTATECC pThisCC, uint32_t cid)
4096{
4097 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4098 AssertReturn(pState, VERR_WRONG_ORDER);
4099
4100 /*
4101 * Resolve the context and hand it to the common worker function.
4102 */
4103 if ( cid < pState->cContexts
4104 && pState->papContexts[cid]->id == cid)
4105 return vmsvga3dContextDestroyOgl(pThisCC, pState->papContexts[cid], cid);
4106
4107 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
4108 return VINF_SUCCESS;
4109}
4110
4111/**
4112 * Worker for vmsvga3dChangeMode that resizes a context.
4113 *
4114 * @param pState The VMSVGA3d state.
4115 * @param pContext The context.
4116 */
4117static void vmsvga3dChangeModeOneContext(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
4118{
4119 RT_NOREF(pState, pContext);
4120 /* Do nothing. The window is not used for presenting. */
4121}
4122
4123/* Handle resize */
4124int vmsvga3dChangeMode(PVGASTATECC pThisCC)
4125{
4126 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4127 AssertReturn(pState, VERR_NO_MEMORY);
4128
4129 /* Resize the shared context too. */
4130 if (pState->SharedCtx.id == VMSVGA3D_SHARED_CTX_ID)
4131 vmsvga3dChangeModeOneContext(pState, &pState->SharedCtx);
4132
4133 /* Resize all active contexts. */
4134 for (uint32_t i = 0; i < pState->cContexts; i++)
4135 {
4136 PVMSVGA3DCONTEXT pContext = pState->papContexts[i];
4137 if (pContext->id != SVGA3D_INVALID_ID)
4138 vmsvga3dChangeModeOneContext(pState, pContext);
4139 }
4140
4141 return VINF_SUCCESS;
4142}
4143
4144
4145int vmsvga3dSetTransform(PVGASTATECC pThisCC, uint32_t cid, SVGA3dTransformType type, float matrix[16])
4146{
4147 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4148 AssertReturn(pState, VERR_NO_MEMORY);
4149 bool fModelViewChanged = false;
4150
4151 Log(("vmsvga3dSetTransform cid=%u %s\n", cid, vmsvgaTransformToString(type)));
4152
4153 ASSERT_GUEST_RETURN((unsigned)type < SVGA3D_TRANSFORM_MAX, VERR_INVALID_PARAMETER);
4154
4155 PVMSVGA3DCONTEXT pContext;
4156 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4157 AssertRCReturn(rc, rc);
4158
4159 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4160
4161 /* Save this matrix for vm state save/restore. */
4162 pContext->state.aTransformState[type].fValid = true;
4163 memcpy(pContext->state.aTransformState[type].matrix, matrix, sizeof(pContext->state.aTransformState[type].matrix));
4164 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_TRANSFORM;
4165
4166 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)));
4167 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)));
4168 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)));
4169 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)));
4170
4171 switch (type)
4172 {
4173 case SVGA3D_TRANSFORM_VIEW:
4174 /* View * World = Model View */
4175 glMatrixMode(GL_MODELVIEW);
4176 glLoadMatrixf(matrix);
4177 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_WORLD].fValid)
4178 glMultMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_WORLD].matrix);
4179 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4180 fModelViewChanged = true;
4181 break;
4182
4183 case SVGA3D_TRANSFORM_PROJECTION:
4184 {
4185 rc = ShaderTransformProjection(pContext->state.RectViewPort.w, pContext->state.RectViewPort.h, matrix, false /* fPretransformed */);
4186 AssertRCReturn(rc, rc);
4187 break;
4188 }
4189
4190 case SVGA3D_TRANSFORM_TEXTURE0:
4191 glMatrixMode(GL_TEXTURE);
4192 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4193 glLoadMatrixf(matrix);
4194 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4195 break;
4196
4197 case SVGA3D_TRANSFORM_TEXTURE1:
4198 case SVGA3D_TRANSFORM_TEXTURE2:
4199 case SVGA3D_TRANSFORM_TEXTURE3:
4200 case SVGA3D_TRANSFORM_TEXTURE4:
4201 case SVGA3D_TRANSFORM_TEXTURE5:
4202 case SVGA3D_TRANSFORM_TEXTURE6:
4203 case SVGA3D_TRANSFORM_TEXTURE7:
4204 Log(("vmsvga3dSetTransform: unsupported SVGA3D_TRANSFORM_TEXTUREx transform!!\n"));
4205 return VERR_INVALID_PARAMETER;
4206
4207 case SVGA3D_TRANSFORM_WORLD:
4208 /* View * World = Model View */
4209 glMatrixMode(GL_MODELVIEW);
4210 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].fValid)
4211 glLoadMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
4212 else
4213 glLoadIdentity();
4214 glMultMatrixf(matrix);
4215 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4216 fModelViewChanged = true;
4217 break;
4218
4219 case SVGA3D_TRANSFORM_WORLD1:
4220 case SVGA3D_TRANSFORM_WORLD2:
4221 case SVGA3D_TRANSFORM_WORLD3:
4222 Log(("vmsvga3dSetTransform: unsupported SVGA3D_TRANSFORM_WORLDx transform!!\n"));
4223 return VERR_INVALID_PARAMETER;
4224
4225 default:
4226 Log(("vmsvga3dSetTransform: unknown type!!\n"));
4227 return VERR_INVALID_PARAMETER;
4228 }
4229
4230 /* Apparently we need to reset the light and clip data after modifying the modelview matrix. */
4231 if (fModelViewChanged)
4232 {
4233 /* Reprogram the clip planes. */
4234 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aClipPlane); j++)
4235 {
4236 if (pContext->state.aClipPlane[j].fValid == true)
4237 vmsvga3dSetClipPlane(pThisCC, cid, j, pContext->state.aClipPlane[j].plane);
4238 }
4239
4240 /* Reprogram the light data. */
4241 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aLightData); j++)
4242 {
4243 if (pContext->state.aLightData[j].fValidData == true)
4244 vmsvga3dSetLightData(pThisCC, cid, j, &pContext->state.aLightData[j].data);
4245 }
4246 }
4247
4248 return VINF_SUCCESS;
4249}
4250
4251int vmsvga3dSetZRange(PVGASTATECC pThisCC, uint32_t cid, SVGA3dZRange zRange)
4252{
4253 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4254 AssertReturn(pState, VERR_NO_MEMORY);
4255
4256 Log(("vmsvga3dSetZRange cid=%u min=%d max=%d\n", cid, (uint32_t)(zRange.min * 100.0), (uint32_t)(zRange.max * 100.0)));
4257
4258 PVMSVGA3DCONTEXT pContext;
4259 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4260 AssertRCReturn(rc, rc);
4261
4262 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4263
4264 pContext->state.zRange = zRange;
4265 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_ZRANGE;
4266
4267 if (zRange.min < -1.0)
4268 zRange.min = -1.0;
4269 if (zRange.max > 1.0)
4270 zRange.max = 1.0;
4271
4272 glDepthRange((GLdouble)zRange.min, (GLdouble)zRange.max);
4273 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4274 return VINF_SUCCESS;
4275}
4276
4277/**
4278 * Convert SVGA blend op value to its OpenGL equivalent
4279 */
4280static GLenum vmsvga3dBlendOp2GL(uint32_t blendOp)
4281{
4282 switch (blendOp)
4283 {
4284 case SVGA3D_BLENDOP_ZERO:
4285 return GL_ZERO;
4286 case SVGA3D_BLENDOP_ONE:
4287 return GL_ONE;
4288 case SVGA3D_BLENDOP_SRCCOLOR:
4289 return GL_SRC_COLOR;
4290 case SVGA3D_BLENDOP_INVSRCCOLOR:
4291 return GL_ONE_MINUS_SRC_COLOR;
4292 case SVGA3D_BLENDOP_SRCALPHA:
4293 return GL_SRC_ALPHA;
4294 case SVGA3D_BLENDOP_INVSRCALPHA:
4295 return GL_ONE_MINUS_SRC_ALPHA;
4296 case SVGA3D_BLENDOP_DESTALPHA:
4297 return GL_DST_ALPHA;
4298 case SVGA3D_BLENDOP_INVDESTALPHA:
4299 return GL_ONE_MINUS_DST_ALPHA;
4300 case SVGA3D_BLENDOP_DESTCOLOR:
4301 return GL_DST_COLOR;
4302 case SVGA3D_BLENDOP_INVDESTCOLOR:
4303 return GL_ONE_MINUS_DST_COLOR;
4304 case SVGA3D_BLENDOP_SRCALPHASAT:
4305 return GL_SRC_ALPHA_SATURATE;
4306 case SVGA3D_BLENDOP_BLENDFACTOR:
4307 return GL_CONSTANT_COLOR;
4308 case SVGA3D_BLENDOP_INVBLENDFACTOR:
4309 return GL_ONE_MINUS_CONSTANT_COLOR;
4310 default:
4311 AssertFailed();
4312 return GL_ONE;
4313 }
4314}
4315
4316static GLenum vmsvga3dBlendEquation2GL(uint32_t blendEq)
4317{
4318 switch (blendEq)
4319 {
4320 case SVGA3D_BLENDEQ_ADD:
4321 return GL_FUNC_ADD;
4322 case SVGA3D_BLENDEQ_SUBTRACT:
4323 return GL_FUNC_SUBTRACT;
4324 case SVGA3D_BLENDEQ_REVSUBTRACT:
4325 return GL_FUNC_REVERSE_SUBTRACT;
4326 case SVGA3D_BLENDEQ_MINIMUM:
4327 return GL_MIN;
4328 case SVGA3D_BLENDEQ_MAXIMUM:
4329 return GL_MAX;
4330 default:
4331 /* SVGA3D_BLENDEQ_INVALID means that the render state has not been set, therefore use default. */
4332 AssertMsg(blendEq == SVGA3D_BLENDEQ_INVALID, ("blendEq=%d (%#x)\n", blendEq, blendEq));
4333 return GL_FUNC_ADD;
4334 }
4335}
4336
4337static GLenum vmsvgaCmpFunc2GL(uint32_t cmpFunc)
4338{
4339 switch (cmpFunc)
4340 {
4341 case SVGA3D_CMP_NEVER:
4342 return GL_NEVER;
4343 case SVGA3D_CMP_LESS:
4344 return GL_LESS;
4345 case SVGA3D_CMP_EQUAL:
4346 return GL_EQUAL;
4347 case SVGA3D_CMP_LESSEQUAL:
4348 return GL_LEQUAL;
4349 case SVGA3D_CMP_GREATER:
4350 return GL_GREATER;
4351 case SVGA3D_CMP_NOTEQUAL:
4352 return GL_NOTEQUAL;
4353 case SVGA3D_CMP_GREATEREQUAL:
4354 return GL_GEQUAL;
4355 case SVGA3D_CMP_ALWAYS:
4356 return GL_ALWAYS;
4357 default:
4358 Assert(cmpFunc == SVGA3D_CMP_INVALID);
4359 return GL_LESS;
4360 }
4361}
4362
4363static GLenum vmsvgaStencipOp2GL(uint32_t stencilOp)
4364{
4365 switch (stencilOp)
4366 {
4367 case SVGA3D_STENCILOP_KEEP:
4368 return GL_KEEP;
4369 case SVGA3D_STENCILOP_ZERO:
4370 return GL_ZERO;
4371 case SVGA3D_STENCILOP_REPLACE:
4372 return GL_REPLACE;
4373 case SVGA3D_STENCILOP_INCRSAT:
4374 return GL_INCR_WRAP;
4375 case SVGA3D_STENCILOP_DECRSAT:
4376 return GL_DECR_WRAP;
4377 case SVGA3D_STENCILOP_INVERT:
4378 return GL_INVERT;
4379 case SVGA3D_STENCILOP_INCR:
4380 return GL_INCR;
4381 case SVGA3D_STENCILOP_DECR:
4382 return GL_DECR;
4383 default:
4384 Assert(stencilOp == SVGA3D_STENCILOP_INVALID);
4385 return GL_KEEP;
4386 }
4387}
4388
4389int vmsvga3dSetRenderState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cRenderStates, SVGA3dRenderState *pRenderState)
4390{
4391 uint32_t val = UINT32_MAX; /* Shut up MSC. */
4392 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4393 AssertReturn(pState, VERR_NO_MEMORY);
4394
4395 Log(("vmsvga3dSetRenderState cid=%u cRenderStates=%d\n", cid, cRenderStates));
4396
4397 PVMSVGA3DCONTEXT pContext;
4398 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4399 AssertRCReturn(rc, rc);
4400
4401 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4402
4403 for (unsigned i = 0; i < cRenderStates; i++)
4404 {
4405 GLenum enableCap = ~(GLenum)0;
4406 Log(("vmsvga3dSetRenderState: cid=%u state=%s (%d) val=%x\n", cid, vmsvga3dGetRenderStateName(pRenderState[i].state), pRenderState[i].state, pRenderState[i].uintValue));
4407 /* Save the render state for vm state saving. */
4408 ASSERT_GUEST_RETURN((unsigned)pRenderState[i].state < SVGA3D_RS_MAX, VERR_INVALID_PARAMETER);
4409 pContext->state.aRenderState[pRenderState[i].state] = pRenderState[i];
4410
4411 switch (pRenderState[i].state)
4412 {
4413 case SVGA3D_RS_ZENABLE: /* SVGA3dBool */
4414 enableCap = GL_DEPTH_TEST;
4415 val = pRenderState[i].uintValue;
4416 break;
4417
4418 case SVGA3D_RS_ZWRITEENABLE: /* SVGA3dBool */
4419 glDepthMask(!!pRenderState[i].uintValue);
4420 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4421 break;
4422
4423 case SVGA3D_RS_ALPHATESTENABLE: /* SVGA3dBool */
4424 enableCap = GL_ALPHA_TEST;
4425 val = pRenderState[i].uintValue;
4426 break;
4427
4428 case SVGA3D_RS_DITHERENABLE: /* SVGA3dBool */
4429 enableCap = GL_DITHER;
4430 val = pRenderState[i].uintValue;
4431 break;
4432
4433 case SVGA3D_RS_FOGENABLE: /* SVGA3dBool */
4434 enableCap = GL_FOG;
4435 val = pRenderState[i].uintValue;
4436 break;
4437
4438 case SVGA3D_RS_SPECULARENABLE: /* SVGA3dBool */
4439 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
4440 break;
4441
4442 case SVGA3D_RS_LIGHTINGENABLE: /* SVGA3dBool */
4443 enableCap = GL_LIGHTING;
4444 val = pRenderState[i].uintValue;
4445 break;
4446
4447 case SVGA3D_RS_NORMALIZENORMALS: /* SVGA3dBool */
4448 /* not applicable */
4449 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
4450 break;
4451
4452 case SVGA3D_RS_POINTSPRITEENABLE: /* SVGA3dBool */
4453 enableCap = GL_POINT_SPRITE_ARB;
4454 val = pRenderState[i].uintValue;
4455 break;
4456
4457 case SVGA3D_RS_POINTSIZE: /* float */
4458 /** @todo we need to apply scaling for point sizes below the min or above the max; see Wine) */
4459 if (pRenderState[i].floatValue < pState->caps.flPointSize[0])
4460 pRenderState[i].floatValue = pState->caps.flPointSize[0];
4461 if (pRenderState[i].floatValue > pState->caps.flPointSize[1])
4462 pRenderState[i].floatValue = pState->caps.flPointSize[1];
4463
4464 glPointSize(pRenderState[i].floatValue);
4465 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4466 Log(("SVGA3D_RS_POINTSIZE: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
4467 break;
4468
4469 case SVGA3D_RS_POINTSIZEMIN: /* float */
4470 pState->ext.glPointParameterf(GL_POINT_SIZE_MIN, pRenderState[i].floatValue);
4471 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4472 Log(("SVGA3D_RS_POINTSIZEMIN: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
4473 break;
4474
4475 case SVGA3D_RS_POINTSIZEMAX: /* float */
4476 pState->ext.glPointParameterf(GL_POINT_SIZE_MAX, pRenderState[i].floatValue);
4477 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4478 Log(("SVGA3D_RS_POINTSIZEMAX: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
4479 break;
4480
4481 case SVGA3D_RS_POINTSCALEENABLE: /* SVGA3dBool */
4482 case SVGA3D_RS_POINTSCALE_A: /* float */
4483 case SVGA3D_RS_POINTSCALE_B: /* float */
4484 case SVGA3D_RS_POINTSCALE_C: /* float */
4485 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
4486 break;
4487
4488 case SVGA3D_RS_AMBIENT: /* SVGA3dColor */
4489 {
4490 GLfloat color[4]; /* red, green, blue, alpha */
4491
4492 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &color[0], &color[1], &color[2], &color[3]);
4493
4494 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, color);
4495 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4496 break;
4497 }
4498
4499 case SVGA3D_RS_CLIPPLANEENABLE: /* SVGA3dClipPlanes */
4500 {
4501 for (uint32_t j = 0; j < SVGA3D_NUM_CLIPPLANES; j++)
4502 {
4503 if (pRenderState[i].uintValue & RT_BIT(j))
4504 glEnable(GL_CLIP_PLANE0 + j);
4505 else
4506 glDisable(GL_CLIP_PLANE0 + j);
4507 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4508 }
4509 break;
4510 }
4511
4512 case SVGA3D_RS_FOGCOLOR: /* SVGA3dColor */
4513 {
4514 GLfloat color[4]; /* red, green, blue, alpha */
4515
4516 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &color[0], &color[1], &color[2], &color[3]);
4517
4518 glFogfv(GL_FOG_COLOR, color);
4519 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4520 break;
4521 }
4522
4523 case SVGA3D_RS_FOGSTART: /* float */
4524 glFogf(GL_FOG_START, pRenderState[i].floatValue);
4525 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4526 break;
4527
4528 case SVGA3D_RS_FOGEND: /* float */
4529 glFogf(GL_FOG_END, pRenderState[i].floatValue);
4530 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4531 break;
4532
4533 case SVGA3D_RS_FOGDENSITY: /* float */
4534 glFogf(GL_FOG_DENSITY, pRenderState[i].floatValue);
4535 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4536 break;
4537
4538 case SVGA3D_RS_RANGEFOGENABLE: /* SVGA3dBool */
4539 glFogi(GL_FOG_COORD_SRC, (pRenderState[i].uintValue) ? GL_FOG_COORD : GL_FRAGMENT_DEPTH);
4540 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4541 break;
4542
4543 case SVGA3D_RS_FOGMODE: /* SVGA3dFogMode */
4544 {
4545 SVGA3dFogMode mode;
4546 mode.uintValue = pRenderState[i].uintValue;
4547
4548 enableCap = GL_FOG_MODE;
4549 switch (mode.function)
4550 {
4551 case SVGA3D_FOGFUNC_EXP:
4552 val = GL_EXP;
4553 break;
4554 case SVGA3D_FOGFUNC_EXP2:
4555 val = GL_EXP2;
4556 break;
4557 case SVGA3D_FOGFUNC_LINEAR:
4558 val = GL_LINEAR;
4559 break;
4560 default:
4561 AssertMsgFailedReturn(("Unexpected fog function %d\n", mode.function), VERR_INTERNAL_ERROR);
4562 break;
4563 }
4564
4565 /** @todo how to switch between vertex and pixel fog modes??? */
4566 Assert(mode.type == SVGA3D_FOGTYPE_PIXEL);
4567#if 0
4568 /* The fog type determines the render state. */
4569 switch (mode.type)
4570 {
4571 case SVGA3D_FOGTYPE_VERTEX:
4572 renderState = D3DRS_FOGVERTEXMODE;
4573 break;
4574 case SVGA3D_FOGTYPE_PIXEL:
4575 renderState = D3DRS_FOGTABLEMODE;
4576 break;
4577 default:
4578 AssertMsgFailedReturn(("Unexpected fog type %d\n", mode.type), VERR_INTERNAL_ERROR);
4579 break;
4580 }
4581#endif
4582
4583 /* Set the fog base to depth or range. */
4584 switch (mode.base)
4585 {
4586 case SVGA3D_FOGBASE_DEPTHBASED:
4587 glFogi(GL_FOG_COORD_SRC, GL_FRAGMENT_DEPTH);
4588 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4589 break;
4590 case SVGA3D_FOGBASE_RANGEBASED:
4591 glFogi(GL_FOG_COORD_SRC, GL_FOG_COORD);
4592 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4593 break;
4594 default:
4595 /* ignore */
4596 AssertMsgFailed(("Unexpected fog base %d\n", mode.base));
4597 break;
4598 }
4599 break;
4600 }
4601
4602 case SVGA3D_RS_FILLMODE: /* SVGA3dFillMode */
4603 {
4604 SVGA3dFillMode mode;
4605
4606 mode.uintValue = pRenderState[i].uintValue;
4607
4608 switch (mode.mode)
4609 {
4610 case SVGA3D_FILLMODE_POINT:
4611 val = GL_POINT;
4612 break;
4613 case SVGA3D_FILLMODE_LINE:
4614 val = GL_LINE;
4615 break;
4616 case SVGA3D_FILLMODE_FILL:
4617 val = GL_FILL;
4618 break;
4619 default:
4620 AssertMsgFailedReturn(("Unexpected fill mode %d\n", mode.mode), VERR_INTERNAL_ERROR);
4621 break;
4622 }
4623 /* Only front and back faces. Also recent Mesa guest drivers initialize the 'face' to zero. */
4624 ASSERT_GUEST(mode.face == SVGA3D_FACE_FRONT_BACK || mode.face == SVGA3D_FACE_INVALID);
4625 glPolygonMode(GL_FRONT_AND_BACK, val);
4626 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4627 break;
4628 }
4629
4630 case SVGA3D_RS_SHADEMODE: /* SVGA3dShadeMode */
4631 switch (pRenderState[i].uintValue)
4632 {
4633 case SVGA3D_SHADEMODE_FLAT:
4634 val = GL_FLAT;
4635 break;
4636
4637 case SVGA3D_SHADEMODE_SMOOTH:
4638 val = GL_SMOOTH;
4639 break;
4640
4641 default:
4642 AssertMsgFailedReturn(("Unexpected shade mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
4643 break;
4644 }
4645
4646 glShadeModel(val);
4647 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4648 break;
4649
4650 case SVGA3D_RS_LINEPATTERN: /* SVGA3dLinePattern */
4651 /* No longer supported by d3d; mesagl comments suggest not all backends support it */
4652 /** @todo */
4653 Log(("WARNING: SVGA3D_RS_LINEPATTERN %x not supported!!\n", pRenderState[i].uintValue));
4654 /*
4655 renderState = D3DRS_LINEPATTERN;
4656 val = pRenderState[i].uintValue;
4657 */
4658 break;
4659
4660 case SVGA3D_RS_ANTIALIASEDLINEENABLE: /* SVGA3dBool */
4661 enableCap = GL_LINE_SMOOTH;
4662 val = pRenderState[i].uintValue;
4663 break;
4664
4665 case SVGA3D_RS_LINEWIDTH: /* float */
4666 glLineWidth(pRenderState[i].floatValue);
4667 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4668 break;
4669
4670 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
4671 {
4672 /* Refresh the blending state based on the new enable setting.
4673 * This will take existing states and set them using either glBlend* or glBlend*Separate.
4674 */
4675 static SVGA3dRenderStateName const saRefreshState[] =
4676 {
4677 SVGA3D_RS_SRCBLEND,
4678 SVGA3D_RS_BLENDEQUATION
4679 };
4680 SVGA3dRenderState renderstate[RT_ELEMENTS(saRefreshState)];
4681 for (uint32_t j = 0; j < RT_ELEMENTS(saRefreshState); ++j)
4682 {
4683 renderstate[j].state = saRefreshState[j];
4684 renderstate[j].uintValue = pContext->state.aRenderState[saRefreshState[j]].uintValue;
4685 }
4686
4687 rc = vmsvga3dSetRenderState(pThisCC, cid, 2, renderstate);
4688 AssertRCReturn(rc, rc);
4689
4690 if (pContext->state.aRenderState[SVGA3D_RS_BLENDENABLE].uintValue != 0)
4691 continue; /* Ignore if blend is enabled */
4692 /* Apply SVGA3D_RS_SEPARATEALPHABLENDENABLE as SVGA3D_RS_BLENDENABLE */
4693 } RT_FALL_THRU();
4694
4695 case SVGA3D_RS_BLENDENABLE: /* SVGA3dBool */
4696 enableCap = GL_BLEND;
4697 val = pRenderState[i].uintValue;
4698 break;
4699
4700 case SVGA3D_RS_SRCBLENDALPHA: /* SVGA3dBlendOp */
4701 case SVGA3D_RS_DSTBLENDALPHA: /* SVGA3dBlendOp */
4702 case SVGA3D_RS_SRCBLEND: /* SVGA3dBlendOp */
4703 case SVGA3D_RS_DSTBLEND: /* SVGA3dBlendOp */
4704 {
4705 GLint srcRGB, srcAlpha, dstRGB, dstAlpha;
4706 GLint blendop = vmsvga3dBlendOp2GL(pRenderState[i].uintValue);
4707
4708 glGetIntegerv(GL_BLEND_SRC_RGB, &srcRGB);
4709 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4710 glGetIntegerv(GL_BLEND_DST_RGB, &dstRGB);
4711 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4712 glGetIntegerv(GL_BLEND_DST_ALPHA, &dstAlpha);
4713 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4714 glGetIntegerv(GL_BLEND_SRC_ALPHA, &srcAlpha);
4715 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4716
4717 switch (pRenderState[i].state)
4718 {
4719 case SVGA3D_RS_SRCBLEND:
4720 srcRGB = blendop;
4721 break;
4722 case SVGA3D_RS_DSTBLEND:
4723 dstRGB = blendop;
4724 break;
4725 case SVGA3D_RS_SRCBLENDALPHA:
4726 srcAlpha = blendop;
4727 break;
4728 case SVGA3D_RS_DSTBLENDALPHA:
4729 dstAlpha = blendop;
4730 break;
4731 default:
4732 /* not possible; shut up gcc */
4733 AssertFailed();
4734 break;
4735 }
4736
4737 if (pContext->state.aRenderState[SVGA3D_RS_SEPARATEALPHABLENDENABLE].uintValue != 0)
4738 pState->ext.glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
4739 else
4740 glBlendFunc(srcRGB, dstRGB);
4741 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4742 break;
4743 }
4744
4745 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation */
4746 case SVGA3D_RS_BLENDEQUATION: /* SVGA3dBlendEquation */
4747 if (pContext->state.aRenderState[SVGA3D_RS_SEPARATEALPHABLENDENABLE].uintValue != 0)
4748 {
4749 GLenum const modeRGB = vmsvga3dBlendEquation2GL(pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATION].uintValue);
4750 GLenum const modeAlpha = vmsvga3dBlendEquation2GL(pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATIONALPHA].uintValue);
4751 pState->ext.glBlendEquationSeparate(modeRGB, modeAlpha);
4752 }
4753 else
4754 {
4755#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x102
4756 glBlendEquation(vmsvga3dBlendEquation2GL(pRenderState[i].uintValue));
4757#else
4758 pState->ext.glBlendEquation(vmsvga3dBlendEquation2GL(pRenderState[i].uintValue));
4759#endif
4760 }
4761 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4762 break;
4763
4764 case SVGA3D_RS_BLENDCOLOR: /* SVGA3dColor */
4765 {
4766 GLfloat red, green, blue, alpha;
4767
4768 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &red, &green, &blue, &alpha);
4769
4770#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x102
4771 glBlendColor(red, green, blue, alpha);
4772#else
4773 pState->ext.glBlendColor(red, green, blue, alpha);
4774#endif
4775 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4776 break;
4777 }
4778
4779 case SVGA3D_RS_CULLMODE: /* SVGA3dFace */
4780 {
4781 GLenum mode = GL_BACK; /* default for OpenGL */
4782
4783 switch (pRenderState[i].uintValue)
4784 {
4785 case SVGA3D_FACE_NONE:
4786 break;
4787 case SVGA3D_FACE_FRONT:
4788 mode = GL_FRONT;
4789 break;
4790 case SVGA3D_FACE_BACK:
4791 mode = GL_BACK;
4792 break;
4793 case SVGA3D_FACE_FRONT_BACK:
4794 mode = GL_FRONT_AND_BACK;
4795 break;
4796 default:
4797 AssertMsgFailedReturn(("Unexpected cull mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
4798 break;
4799 }
4800 enableCap = GL_CULL_FACE;
4801 if (pRenderState[i].uintValue != SVGA3D_FACE_NONE)
4802 {
4803 glCullFace(mode);
4804 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4805 val = 1;
4806 }
4807 else
4808 val = 0;
4809 break;
4810 }
4811
4812 case SVGA3D_RS_ZFUNC: /* SVGA3dCmpFunc */
4813 glDepthFunc(vmsvgaCmpFunc2GL(pRenderState[i].uintValue));
4814 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4815 break;
4816
4817 case SVGA3D_RS_ALPHAFUNC: /* SVGA3dCmpFunc */
4818 {
4819 GLclampf ref;
4820
4821 glGetFloatv(GL_ALPHA_TEST_REF, &ref);
4822 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4823 glAlphaFunc(vmsvgaCmpFunc2GL(pRenderState[i].uintValue), ref);
4824 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4825 break;
4826 }
4827
4828 case SVGA3D_RS_ALPHAREF: /* float (0.0 .. 1.0) */
4829 {
4830 GLint func;
4831
4832 glGetIntegerv(GL_ALPHA_TEST_FUNC, &func);
4833 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4834 glAlphaFunc(func, pRenderState[i].floatValue);
4835 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4836 break;
4837 }
4838
4839 case SVGA3D_RS_STENCILENABLE2SIDED: /* SVGA3dBool */
4840 {
4841 /* Refresh the stencil state based on the new enable setting.
4842 * This will take existing states and set them using either glStencil or glStencil*Separate.
4843 */
4844 static SVGA3dRenderStateName const saRefreshState[] =
4845 {
4846 SVGA3D_RS_STENCILFUNC,
4847 SVGA3D_RS_STENCILFAIL,
4848 SVGA3D_RS_CCWSTENCILFUNC,
4849 SVGA3D_RS_CCWSTENCILFAIL
4850 };
4851 SVGA3dRenderState renderstate[RT_ELEMENTS(saRefreshState)];
4852 for (uint32_t j = 0; j < RT_ELEMENTS(saRefreshState); ++j)
4853 {
4854 renderstate[j].state = saRefreshState[j];
4855 renderstate[j].uintValue = pContext->state.aRenderState[saRefreshState[j]].uintValue;
4856 }
4857
4858 rc = vmsvga3dSetRenderState(pThisCC, cid, RT_ELEMENTS(renderstate), renderstate);
4859 AssertRCReturn(rc, rc);
4860
4861 if (pContext->state.aRenderState[SVGA3D_RS_STENCILENABLE].uintValue != 0)
4862 continue; /* Ignore if stencil is enabled */
4863 /* Apply SVGA3D_RS_STENCILENABLE2SIDED as SVGA3D_RS_STENCILENABLE. */
4864 } RT_FALL_THRU();
4865
4866 case SVGA3D_RS_STENCILENABLE: /* SVGA3dBool */
4867 enableCap = GL_STENCIL_TEST;
4868 val = pRenderState[i].uintValue;
4869 break;
4870
4871 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc */
4872 case SVGA3D_RS_STENCILREF: /* uint32_t */
4873 case SVGA3D_RS_STENCILMASK: /* uint32_t */
4874 {
4875 GLint func, ref;
4876 GLuint mask;
4877
4878 /* Query current values to have all parameters for glStencilFunc[Separate]. */
4879 glGetIntegerv(GL_STENCIL_FUNC, &func);
4880 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4881 glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)&mask);
4882 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4883 glGetIntegerv(GL_STENCIL_REF, &ref);
4884 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4885
4886 /* Update the changed value. */
4887 switch (pRenderState[i].state)
4888 {
4889 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc */
4890 func = vmsvgaCmpFunc2GL(pRenderState[i].uintValue);
4891 break;
4892
4893 case SVGA3D_RS_STENCILREF: /* uint32_t */
4894 ref = pRenderState[i].uintValue;
4895 break;
4896
4897 case SVGA3D_RS_STENCILMASK: /* uint32_t */
4898 mask = pRenderState[i].uintValue;
4899 break;
4900
4901 default:
4902 /* not possible; shut up gcc */
4903 AssertFailed();
4904 break;
4905 }
4906
4907 if (pContext->state.aRenderState[SVGA3D_RS_STENCILENABLE2SIDED].uintValue != 0)
4908 {
4909 pState->ext.glStencilFuncSeparate(GL_FRONT, func, ref, mask);
4910 }
4911 else
4912 {
4913 glStencilFunc(func, ref, mask);
4914 }
4915 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4916 break;
4917 }
4918
4919 case SVGA3D_RS_STENCILWRITEMASK: /* uint32_t */
4920 glStencilMask(pRenderState[i].uintValue);
4921 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4922 break;
4923
4924 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp */
4925 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp */
4926 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp */
4927 {
4928 GLint sfail, dpfail, dppass;
4929 GLenum const stencilop = vmsvgaStencipOp2GL(pRenderState[i].uintValue);
4930
4931 glGetIntegerv(GL_STENCIL_FAIL, &sfail);
4932 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4933 glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, &dpfail);
4934 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4935 glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, &dppass);
4936 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4937
4938 switch (pRenderState[i].state)
4939 {
4940 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp */
4941 sfail = stencilop;
4942 break;
4943 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp */
4944 dpfail = stencilop;
4945 break;
4946 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp */
4947 dppass = stencilop;
4948 break;
4949 default:
4950 /* not possible; shut up gcc */
4951 AssertFailed();
4952 break;
4953 }
4954 if (pContext->state.aRenderState[SVGA3D_RS_STENCILENABLE2SIDED].uintValue != 0)
4955 {
4956 pState->ext.glStencilOpSeparate(GL_FRONT, sfail, dpfail, dppass);
4957 }
4958 else
4959 {
4960 glStencilOp(sfail, dpfail, dppass);
4961 }
4962 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4963 break;
4964 }
4965
4966 case SVGA3D_RS_CCWSTENCILFUNC: /* SVGA3dCmpFunc */
4967 {
4968 GLint ref;
4969 GLuint mask;
4970 GLint const func = vmsvgaCmpFunc2GL(pRenderState[i].uintValue);
4971
4972 /* GL_STENCIL_VALUE_MASK and GL_STENCIL_REF are the same for both GL_FRONT and GL_BACK. */
4973 glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)&mask);
4974 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4975 glGetIntegerv(GL_STENCIL_REF, &ref);
4976 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4977
4978 pState->ext.glStencilFuncSeparate(GL_BACK, func, ref, mask);
4979 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4980 break;
4981 }
4982
4983 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp */
4984 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp */
4985 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp */
4986 {
4987 GLint sfail, dpfail, dppass;
4988 GLenum const stencilop = vmsvgaStencipOp2GL(pRenderState[i].uintValue);
4989
4990 glGetIntegerv(GL_STENCIL_BACK_FAIL, &sfail);
4991 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4992 glGetIntegerv(GL_STENCIL_BACK_PASS_DEPTH_FAIL, &dpfail);
4993 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4994 glGetIntegerv(GL_STENCIL_BACK_PASS_DEPTH_PASS, &dppass);
4995 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4996
4997 switch (pRenderState[i].state)
4998 {
4999 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp */
5000 sfail = stencilop;
5001 break;
5002 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp */
5003 dpfail = stencilop;
5004 break;
5005 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp */
5006 dppass = stencilop;
5007 break;
5008 default:
5009 /* not possible; shut up gcc */
5010 AssertFailed();
5011 break;
5012 }
5013 pState->ext.glStencilOpSeparate(GL_BACK, sfail, dpfail, dppass);
5014 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5015 break;
5016 }
5017
5018 case SVGA3D_RS_ZBIAS: /* float */
5019 /** @todo unknown meaning; depth bias is not identical
5020 renderState = D3DRS_DEPTHBIAS;
5021 val = pRenderState[i].uintValue;
5022 */
5023 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_ZBIAS\n"));
5024 break;
5025
5026 case SVGA3D_RS_DEPTHBIAS: /* float */
5027 {
5028 GLfloat factor;
5029
5030 /** @todo not sure if the d3d & ogl definitions are identical. */
5031
5032 /* Do not change the factor part. */
5033 glGetFloatv(GL_POLYGON_OFFSET_FACTOR, &factor);
5034 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5035
5036 glPolygonOffset(factor, pRenderState[i].floatValue);
5037 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5038 break;
5039 }
5040
5041 case SVGA3D_RS_SLOPESCALEDEPTHBIAS: /* float */
5042 {
5043 GLfloat units;
5044
5045 /** @todo not sure if the d3d & ogl definitions are identical. */
5046
5047 /* Do not change the factor part. */
5048 glGetFloatv(GL_POLYGON_OFFSET_UNITS, &units);
5049 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5050
5051 glPolygonOffset(pRenderState[i].floatValue, units);
5052 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5053 break;
5054 }
5055
5056 case SVGA3D_RS_COLORWRITEENABLE: /* SVGA3dColorMask */
5057 {
5058 GLboolean red, green, blue, alpha;
5059 SVGA3dColorMask mask;
5060
5061 mask.uintValue = pRenderState[i].uintValue;
5062
5063 red = mask.red;
5064 green = mask.green;
5065 blue = mask.blue;
5066 alpha = mask.alpha;
5067
5068 glColorMask(red, green, blue, alpha);
5069 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5070 break;
5071 }
5072
5073 case SVGA3D_RS_COLORWRITEENABLE1: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5074 case SVGA3D_RS_COLORWRITEENABLE2: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5075 case SVGA3D_RS_COLORWRITEENABLE3: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
5076 Log(("vmsvga3dSetRenderState: WARNING SVGA3D_RS_COLORWRITEENABLEx not supported!!\n"));
5077 break;
5078
5079 case SVGA3D_RS_SCISSORTESTENABLE: /* SVGA3dBool */
5080 enableCap = GL_SCISSOR_TEST;
5081 val = pRenderState[i].uintValue;
5082 break;
5083
5084#if 0
5085 case SVGA3D_RS_DIFFUSEMATERIALSOURCE: /* SVGA3dVertexMaterial */
5086 AssertCompile(D3DMCS_COLOR2 == SVGA3D_VERTEXMATERIAL_SPECULAR);
5087 renderState = D3DRS_DIFFUSEMATERIALSOURCE;
5088 val = pRenderState[i].uintValue;
5089 break;
5090
5091 case SVGA3D_RS_SPECULARMATERIALSOURCE: /* SVGA3dVertexMaterial */
5092 renderState = D3DRS_SPECULARMATERIALSOURCE;
5093 val = pRenderState[i].uintValue;
5094 break;
5095
5096 case SVGA3D_RS_AMBIENTMATERIALSOURCE: /* SVGA3dVertexMaterial */
5097 renderState = D3DRS_AMBIENTMATERIALSOURCE;
5098 val = pRenderState[i].uintValue;
5099 break;
5100
5101 case SVGA3D_RS_EMISSIVEMATERIALSOURCE: /* SVGA3dVertexMaterial */
5102 renderState = D3DRS_EMISSIVEMATERIALSOURCE;
5103 val = pRenderState[i].uintValue;
5104 break;
5105#endif
5106
5107 case SVGA3D_RS_WRAP3: /* SVGA3dWrapFlags */
5108 case SVGA3D_RS_WRAP4: /* SVGA3dWrapFlags */
5109 case SVGA3D_RS_WRAP5: /* SVGA3dWrapFlags */
5110 case SVGA3D_RS_WRAP6: /* SVGA3dWrapFlags */
5111 case SVGA3D_RS_WRAP7: /* SVGA3dWrapFlags */
5112 case SVGA3D_RS_WRAP8: /* SVGA3dWrapFlags */
5113 case SVGA3D_RS_WRAP9: /* SVGA3dWrapFlags */
5114 case SVGA3D_RS_WRAP10: /* SVGA3dWrapFlags */
5115 case SVGA3D_RS_WRAP11: /* SVGA3dWrapFlags */
5116 case SVGA3D_RS_WRAP12: /* SVGA3dWrapFlags */
5117 case SVGA3D_RS_WRAP13: /* SVGA3dWrapFlags */
5118 case SVGA3D_RS_WRAP14: /* SVGA3dWrapFlags */
5119 case SVGA3D_RS_WRAP15: /* SVGA3dWrapFlags */
5120 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_WRAPx (x >= 3)\n"));
5121 break;
5122
5123 case SVGA3D_RS_LASTPIXEL: /* SVGA3dBool */
5124 case SVGA3D_RS_TWEENFACTOR: /* float */
5125 case SVGA3D_RS_INDEXEDVERTEXBLENDENABLE: /* SVGA3dBool */
5126 case SVGA3D_RS_VERTEXBLEND: /* SVGA3dVertexBlendFlags */
5127 Log(("vmsvga3dSetRenderState: WARNING not applicable!!\n"));
5128 break;
5129
5130 case SVGA3D_RS_MULTISAMPLEANTIALIAS: /* SVGA3dBool */
5131 enableCap = GL_MULTISAMPLE;
5132 val = pRenderState[i].uintValue;
5133 break;
5134
5135 case SVGA3D_RS_MULTISAMPLEMASK: /* uint32_t */
5136 Log(("vmsvga3dSetRenderState: WARNING not applicable??!!\n"));
5137 break;
5138
5139 case SVGA3D_RS_COORDINATETYPE: /* SVGA3dCoordinateType */
5140 Assert(pRenderState[i].uintValue == SVGA3D_COORDINATE_LEFTHANDED);
5141 /** @todo setup a view matrix to scale the world space by -1 in the z-direction for right handed coordinates. */
5142 /*
5143 renderState = D3DRS_COORDINATETYPE;
5144 val = pRenderState[i].uintValue;
5145 */
5146 break;
5147
5148 case SVGA3D_RS_FRONTWINDING: /* SVGA3dFrontWinding */
5149 Assert(pRenderState[i].uintValue == SVGA3D_FRONTWINDING_CW);
5150 /* Invert the selected mode because of y-inversion (?) */
5151 glFrontFace((pRenderState[i].uintValue != SVGA3D_FRONTWINDING_CW) ? GL_CW : GL_CCW);
5152 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5153 break;
5154
5155 case SVGA3D_RS_OUTPUTGAMMA: /* float */
5156 //AssertFailed();
5157 /*
5158 D3DRS_SRGBWRITEENABLE ??
5159 renderState = D3DRS_OUTPUTGAMMA;
5160 val = pRenderState[i].uintValue;
5161 */
5162 break;
5163
5164#if 0
5165
5166 case SVGA3D_RS_VERTEXMATERIALENABLE: /* SVGA3dBool */
5167 //AssertFailed();
5168 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE; /* correct?? */
5169 val = pRenderState[i].uintValue;
5170 break;
5171
5172 case SVGA3D_RS_TEXTUREFACTOR: /* SVGA3dColor */
5173 renderState = D3DRS_TEXTUREFACTOR;
5174 val = pRenderState[i].uintValue;
5175 break;
5176
5177 case SVGA3D_RS_LOCALVIEWER: /* SVGA3dBool */
5178 renderState = D3DRS_LOCALVIEWER;
5179 val = pRenderState[i].uintValue;
5180 break;
5181
5182 case SVGA3D_RS_ZVISIBLE: /* SVGA3dBool */
5183 AssertFailed();
5184 /*
5185 renderState = D3DRS_ZVISIBLE;
5186 val = pRenderState[i].uintValue;
5187 */
5188 break;
5189
5190 case SVGA3D_RS_CLIPPING: /* SVGA3dBool */
5191 renderState = D3DRS_CLIPPING;
5192 val = pRenderState[i].uintValue;
5193 break;
5194
5195 case SVGA3D_RS_WRAP0: /* SVGA3dWrapFlags */
5196 glTexParameter GL_TEXTURE_WRAP_S
5197 Assert(SVGA3D_WRAPCOORD_3 == D3DWRAPCOORD_3);
5198 renderState = D3DRS_WRAP0;
5199 val = pRenderState[i].uintValue;
5200 break;
5201
5202 case SVGA3D_RS_WRAP1: /* SVGA3dWrapFlags */
5203 glTexParameter GL_TEXTURE_WRAP_T
5204 renderState = D3DRS_WRAP1;
5205 val = pRenderState[i].uintValue;
5206 break;
5207
5208 case SVGA3D_RS_WRAP2: /* SVGA3dWrapFlags */
5209 glTexParameter GL_TEXTURE_WRAP_R
5210 renderState = D3DRS_WRAP2;
5211 val = pRenderState[i].uintValue;
5212 break;
5213
5214
5215 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
5216 renderState = D3DRS_SEPARATEALPHABLENDENABLE;
5217 val = pRenderState[i].uintValue;
5218 break;
5219
5220
5221 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation */
5222 renderState = D3DRS_BLENDOPALPHA;
5223 val = pRenderState[i].uintValue;
5224 break;
5225
5226 case SVGA3D_RS_TRANSPARENCYANTIALIAS: /* SVGA3dTransparencyAntialiasType */
5227 AssertFailed();
5228 /*
5229 renderState = D3DRS_TRANSPARENCYANTIALIAS;
5230 val = pRenderState[i].uintValue;
5231 */
5232 break;
5233
5234#endif
5235 default:
5236 AssertFailed();
5237 break;
5238 }
5239
5240 if (enableCap != ~(GLenum)0)
5241 {
5242 if (val)
5243 glEnable(enableCap);
5244 else
5245 glDisable(enableCap);
5246 }
5247 }
5248
5249 return VINF_SUCCESS;
5250}
5251
5252int vmsvga3dSetRenderTarget(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRenderTargetType type, SVGA3dSurfaceImageId target)
5253{
5254 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5255
5256 AssertReturn(pState, VERR_NO_MEMORY);
5257 AssertReturn((unsigned)type < SVGA3D_RT_MAX, VERR_INVALID_PARAMETER);
5258
5259 LogFunc(("cid=%u type=%x sid=%u\n", cid, type, target.sid));
5260
5261 PVMSVGA3DCONTEXT pContext;
5262 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5263 AssertRCReturn(rc, rc);
5264
5265 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5266
5267 /* Save for vm state save/restore. */
5268 pContext->state.aRenderTargets[type] = target.sid;
5269
5270 if (target.sid == SVGA3D_INVALID_ID)
5271 {
5272 /* Disable render target. */
5273 switch (type)
5274 {
5275 case SVGA3D_RT_DEPTH:
5276 case SVGA3D_RT_STENCIL:
5277 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER, (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
5278 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5279 break;
5280
5281 case SVGA3D_RT_COLOR0:
5282 case SVGA3D_RT_COLOR1:
5283 case SVGA3D_RT_COLOR2:
5284 case SVGA3D_RT_COLOR3:
5285 case SVGA3D_RT_COLOR4:
5286 case SVGA3D_RT_COLOR5:
5287 case SVGA3D_RT_COLOR6:
5288 case SVGA3D_RT_COLOR7:
5289 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0, 0, 0, 0);
5290 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5291 break;
5292
5293 default:
5294 AssertFailedReturn(VERR_INVALID_PARAMETER);
5295 }
5296 return VINF_SUCCESS;
5297 }
5298
5299 PVMSVGA3DSURFACE pRenderTarget;
5300 rc = vmsvga3dSurfaceFromSid(pState, target.sid, &pRenderTarget);
5301 AssertRCReturn(rc, rc);
5302
5303 switch (type)
5304 {
5305 case SVGA3D_RT_DEPTH:
5306 case SVGA3D_RT_STENCIL:
5307#if 1
5308 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
5309 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
5310 {
5311 LogFunc(("create depth texture to be used as render target; surface id=%x type=%d format=%d -> create texture\n",
5312 target.sid, pRenderTarget->surfaceFlags, pRenderTarget->format));
5313 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
5314 AssertRCReturn(rc, rc);
5315 }
5316
5317 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
5318 Assert(!pRenderTarget->fDirty);
5319
5320 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
5321
5322 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER,
5323 (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT,
5324 GL_TEXTURE_2D, pRenderTarget->oglId.texture, target.mipmap);
5325 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5326#else
5327 AssertReturn(target.mipmap == 0, VERR_INVALID_PARAMETER);
5328 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
5329 {
5330 Log(("vmsvga3dSetRenderTarget: create renderbuffer to be used as render target; surface id=%x type=%d format=%d\n", target.sid, pRenderTarget->surfaceFlags, pRenderTarget->internalFormatGL));
5331 pContext = &pState->SharedCtx;
5332 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5333
5334 pState->ext.glGenRenderbuffers(1, &pRenderTarget->oglId.renderbuffer);
5335 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5336 pSurface->enmOGLResType = VMSVGA3D_OGLRESTYPE_RENDERBUFFER;
5337
5338 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5339 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5340
5341 pState->ext.glRenderbufferStorage(GL_RENDERBUFFER,
5342 pRenderTarget->internalFormatGL,
5343 pRenderTarget->paMipmapLevels[0].mipmapSize.width,
5344 pRenderTarget->paMipmapLevels[0].mipmapSize.height);
5345 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5346
5347 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, OPENGL_INVALID_ID);
5348 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5349
5350 pContext = pState->papContexts[cid];
5351 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5352 pRenderTarget->idWeakContextAssociation = cid;
5353 }
5354
5355 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5356 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5357 Assert(!pRenderTarget->fDirty);
5358 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
5359
5360 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
5361
5362 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
5363 (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT,
5364 GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
5365 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5366#endif
5367 break;
5368
5369 case SVGA3D_RT_COLOR0:
5370 case SVGA3D_RT_COLOR1:
5371 case SVGA3D_RT_COLOR2:
5372 case SVGA3D_RT_COLOR3:
5373 case SVGA3D_RT_COLOR4:
5374 case SVGA3D_RT_COLOR5:
5375 case SVGA3D_RT_COLOR6:
5376 case SVGA3D_RT_COLOR7:
5377 {
5378 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
5379 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
5380 {
5381 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));
5382 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
5383 AssertRCReturn(rc, rc);
5384 }
5385
5386 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
5387 Assert(!pRenderTarget->fDirty);
5388
5389 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
5390
5391 GLenum textarget;
5392 if (pRenderTarget->surfaceFlags & SVGA3D_SURFACE_CUBEMAP)
5393 textarget = vmsvga3dCubemapFaceFromIndex(target.face);
5394 else
5395 textarget = GL_TEXTURE_2D;
5396 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0,
5397 textarget, pRenderTarget->oglId.texture, target.mipmap);
5398 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5399
5400#ifdef DEBUG
5401 GLenum status = pState->ext.glCheckFramebufferStatus(GL_FRAMEBUFFER);
5402 if (status != GL_FRAMEBUFFER_COMPLETE)
5403 Log(("vmsvga3dSetRenderTarget: WARNING: glCheckFramebufferStatus returned %x\n", status));
5404#endif
5405 /** @todo use glDrawBuffers too? */
5406 break;
5407 }
5408
5409 default:
5410 AssertFailedReturn(VERR_INVALID_PARAMETER);
5411 }
5412
5413 return VINF_SUCCESS;
5414}
5415
5416#if 0
5417/**
5418 * Convert SVGA texture combiner value to its D3D equivalent
5419 */
5420static DWORD vmsvga3dTextureCombiner2D3D(uint32_t value)
5421{
5422 switch (value)
5423 {
5424 case SVGA3D_TC_DISABLE:
5425 return D3DTOP_DISABLE;
5426 case SVGA3D_TC_SELECTARG1:
5427 return D3DTOP_SELECTARG1;
5428 case SVGA3D_TC_SELECTARG2:
5429 return D3DTOP_SELECTARG2;
5430 case SVGA3D_TC_MODULATE:
5431 return D3DTOP_MODULATE;
5432 case SVGA3D_TC_ADD:
5433 return D3DTOP_ADD;
5434 case SVGA3D_TC_ADDSIGNED:
5435 return D3DTOP_ADDSIGNED;
5436 case SVGA3D_TC_SUBTRACT:
5437 return D3DTOP_SUBTRACT;
5438 case SVGA3D_TC_BLENDTEXTUREALPHA:
5439 return D3DTOP_BLENDTEXTUREALPHA;
5440 case SVGA3D_TC_BLENDDIFFUSEALPHA:
5441 return D3DTOP_BLENDDIFFUSEALPHA;
5442 case SVGA3D_TC_BLENDCURRENTALPHA:
5443 return D3DTOP_BLENDCURRENTALPHA;
5444 case SVGA3D_TC_BLENDFACTORALPHA:
5445 return D3DTOP_BLENDFACTORALPHA;
5446 case SVGA3D_TC_MODULATE2X:
5447 return D3DTOP_MODULATE2X;
5448 case SVGA3D_TC_MODULATE4X:
5449 return D3DTOP_MODULATE4X;
5450 case SVGA3D_TC_DSDT:
5451 AssertFailed(); /** @todo ??? */
5452 return D3DTOP_DISABLE;
5453 case SVGA3D_TC_DOTPRODUCT3:
5454 return D3DTOP_DOTPRODUCT3;
5455 case SVGA3D_TC_BLENDTEXTUREALPHAPM:
5456 return D3DTOP_BLENDTEXTUREALPHAPM;
5457 case SVGA3D_TC_ADDSIGNED2X:
5458 return D3DTOP_ADDSIGNED2X;
5459 case SVGA3D_TC_ADDSMOOTH:
5460 return D3DTOP_ADDSMOOTH;
5461 case SVGA3D_TC_PREMODULATE:
5462 return D3DTOP_PREMODULATE;
5463 case SVGA3D_TC_MODULATEALPHA_ADDCOLOR:
5464 return D3DTOP_MODULATEALPHA_ADDCOLOR;
5465 case SVGA3D_TC_MODULATECOLOR_ADDALPHA:
5466 return D3DTOP_MODULATECOLOR_ADDALPHA;
5467 case SVGA3D_TC_MODULATEINVALPHA_ADDCOLOR:
5468 return D3DTOP_MODULATEINVALPHA_ADDCOLOR;
5469 case SVGA3D_TC_MODULATEINVCOLOR_ADDALPHA:
5470 return D3DTOP_MODULATEINVCOLOR_ADDALPHA;
5471 case SVGA3D_TC_BUMPENVMAPLUMINANCE:
5472 return D3DTOP_BUMPENVMAPLUMINANCE;
5473 case SVGA3D_TC_MULTIPLYADD:
5474 return D3DTOP_MULTIPLYADD;
5475 case SVGA3D_TC_LERP:
5476 return D3DTOP_LERP;
5477 default:
5478 AssertFailed();
5479 return D3DTOP_DISABLE;
5480 }
5481}
5482
5483/**
5484 * Convert SVGA texture arg data value to its D3D equivalent
5485 */
5486static DWORD vmsvga3dTextureArgData2D3D(uint32_t value)
5487{
5488 switch (value)
5489 {
5490 case SVGA3D_TA_CONSTANT:
5491 return D3DTA_CONSTANT;
5492 case SVGA3D_TA_PREVIOUS:
5493 return D3DTA_CURRENT; /* current = previous */
5494 case SVGA3D_TA_DIFFUSE:
5495 return D3DTA_DIFFUSE;
5496 case SVGA3D_TA_TEXTURE:
5497 return D3DTA_TEXTURE;
5498 case SVGA3D_TA_SPECULAR:
5499 return D3DTA_SPECULAR;
5500 default:
5501 AssertFailed();
5502 return 0;
5503 }
5504}
5505
5506/**
5507 * Convert SVGA texture transform flag value to its D3D equivalent
5508 */
5509static DWORD vmsvga3dTextTransformFlags2D3D(uint32_t value)
5510{
5511 switch (value)
5512 {
5513 case SVGA3D_TEX_TRANSFORM_OFF:
5514 return D3DTTFF_DISABLE;
5515 case SVGA3D_TEX_TRANSFORM_S:
5516 return D3DTTFF_COUNT1; /** @todo correct? */
5517 case SVGA3D_TEX_TRANSFORM_T:
5518 return D3DTTFF_COUNT2; /** @todo correct? */
5519 case SVGA3D_TEX_TRANSFORM_R:
5520 return D3DTTFF_COUNT3; /** @todo correct? */
5521 case SVGA3D_TEX_TRANSFORM_Q:
5522 return D3DTTFF_COUNT4; /** @todo correct? */
5523 case SVGA3D_TEX_PROJECTED:
5524 return D3DTTFF_PROJECTED;
5525 default:
5526 AssertFailed();
5527 return 0;
5528 }
5529}
5530#endif
5531
5532static GLenum vmsvga3dTextureAddress2OGL(SVGA3dTextureAddress value)
5533{
5534 switch (value)
5535 {
5536 case SVGA3D_TEX_ADDRESS_WRAP:
5537 return GL_REPEAT;
5538 case SVGA3D_TEX_ADDRESS_MIRROR:
5539 return GL_MIRRORED_REPEAT;
5540 case SVGA3D_TEX_ADDRESS_CLAMP:
5541 return GL_CLAMP_TO_EDGE;
5542 case SVGA3D_TEX_ADDRESS_BORDER:
5543 return GL_CLAMP_TO_BORDER;
5544 case SVGA3D_TEX_ADDRESS_MIRRORONCE:
5545 AssertFailed();
5546 return GL_CLAMP_TO_EDGE_SGIS; /** @todo correct? */
5547
5548 case SVGA3D_TEX_ADDRESS_EDGE:
5549 case SVGA3D_TEX_ADDRESS_INVALID:
5550 default:
5551 AssertFailed();
5552 return GL_REPEAT; /* default */
5553 }
5554}
5555
5556static GLenum vmsvga3dTextureFilter2OGL(SVGA3dTextureFilter value)
5557{
5558 switch (value)
5559 {
5560 case SVGA3D_TEX_FILTER_NONE:
5561 case SVGA3D_TEX_FILTER_LINEAR:
5562 case SVGA3D_TEX_FILTER_ANISOTROPIC: /* Anisotropic filtering is controlled by SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL */
5563 return GL_LINEAR;
5564 case SVGA3D_TEX_FILTER_NEAREST:
5565 return GL_NEAREST;
5566 case SVGA3D_TEX_FILTER_FLATCUBIC: // Deprecated, not implemented
5567 case SVGA3D_TEX_FILTER_GAUSSIANCUBIC: // Deprecated, not implemented
5568 case SVGA3D_TEX_FILTER_PYRAMIDALQUAD: // Not currently implemented
5569 case SVGA3D_TEX_FILTER_GAUSSIANQUAD: // Not currently implemented
5570 default:
5571 AssertFailed();
5572 return GL_LINEAR; /* default */
5573 }
5574}
5575
5576uint32_t vmsvga3dSVGA3dColor2RGBA(SVGA3dColor value)
5577{
5578 /* flip the red and blue bytes */
5579 uint8_t blue = value & 0xff;
5580 uint8_t red = (value >> 16) & 0xff;
5581 return (value & 0xff00ff00) | red | (blue << 16);
5582}
5583
5584int vmsvga3dSetTextureState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
5585{
5586 GLenum val = ~(GLenum)0; /* Shut up MSC. */
5587 GLenum currentStage = ~(GLenum)0;
5588 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5589 AssertReturn(pState, VERR_NO_MEMORY);
5590
5591 Log(("vmsvga3dSetTextureState %x cTextureState=%d\n", cid, cTextureStates));
5592
5593 PVMSVGA3DCONTEXT pContext;
5594 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5595 AssertRCReturn(rc, rc);
5596
5597 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5598
5599 /* Which texture is active for the current stage. Needed to use right OpenGL target when setting parameters. */
5600 PVMSVGA3DSURFACE pCurrentTextureSurface = NULL;
5601
5602 for (uint32_t i = 0; i < cTextureStates; ++i)
5603 {
5604 GLenum textureType = ~(GLenum)0;
5605#if 0
5606 GLenum samplerType = ~(GLenum)0;
5607#endif
5608
5609 LogFunc(("cid=%u stage=%d type=%s (%x) val=%x\n",
5610 cid, pTextureState[i].stage, vmsvga3dTextureStateToString(pTextureState[i].name), pTextureState[i].name, pTextureState[i].value));
5611
5612 /* Record the texture state for vm state saving. */
5613 if ( pTextureState[i].stage < RT_ELEMENTS(pContext->state.aTextureStates)
5614 && (unsigned)pTextureState[i].name < RT_ELEMENTS(pContext->state.aTextureStates[0]))
5615 {
5616 pContext->state.aTextureStates[pTextureState[i].stage][pTextureState[i].name] = pTextureState[i];
5617 }
5618
5619 /* Activate the right texture unit for subsequent texture state changes. */
5620 if (pTextureState[i].stage != currentStage || i == 0)
5621 {
5622 /** @todo Is this the appropriate limit for all kinds of textures? It is the
5623 * size of aSidActiveTextures and for binding/unbinding we cannot exceed it. */
5624 if (pTextureState[i].stage < RT_ELEMENTS(pContext->state.aTextureStates))
5625 {
5626 pState->ext.glActiveTexture(GL_TEXTURE0 + pTextureState[i].stage);
5627 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5628 currentStage = pTextureState[i].stage;
5629 }
5630 else
5631 {
5632 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x\n", i, pTextureState[i].stage, pTextureState[i].name));
5633 continue;
5634 }
5635
5636 if (pContext->aSidActiveTextures[currentStage] != SVGA3D_INVALID_ID)
5637 {
5638 rc = vmsvga3dSurfaceFromSid(pState, pContext->aSidActiveTextures[currentStage], &pCurrentTextureSurface);
5639 AssertRCReturn(rc, rc);
5640 }
5641 else
5642 pCurrentTextureSurface = NULL; /* Make sure that no stale pointer is used. */
5643 }
5644
5645 switch (pTextureState[i].name)
5646 {
5647 case SVGA3D_TS_BUMPENVMAT00: /* float */
5648 case SVGA3D_TS_BUMPENVMAT01: /* float */
5649 case SVGA3D_TS_BUMPENVMAT10: /* float */
5650 case SVGA3D_TS_BUMPENVMAT11: /* float */
5651 case SVGA3D_TS_BUMPENVLSCALE: /* float */
5652 case SVGA3D_TS_BUMPENVLOFFSET: /* float */
5653 Log(("vmsvga3dSetTextureState: bump mapping texture options not supported!!\n"));
5654 break;
5655
5656 case SVGA3D_TS_COLOROP: /* SVGA3dTextureCombiner */
5657 case SVGA3D_TS_COLORARG0: /* SVGA3dTextureArgData */
5658 case SVGA3D_TS_COLORARG1: /* SVGA3dTextureArgData */
5659 case SVGA3D_TS_COLORARG2: /* SVGA3dTextureArgData */
5660 case SVGA3D_TS_ALPHAOP: /* SVGA3dTextureCombiner */
5661 case SVGA3D_TS_ALPHAARG0: /* SVGA3dTextureArgData */
5662 case SVGA3D_TS_ALPHAARG1: /* SVGA3dTextureArgData */
5663 case SVGA3D_TS_ALPHAARG2: /* SVGA3dTextureArgData */
5664 /** @todo not used by MesaGL */
5665 Log(("vmsvga3dSetTextureState: colorop/alphaop not yet supported!!\n"));
5666 break;
5667#if 0
5668
5669 case SVGA3D_TS_TEXCOORDINDEX: /* uint32_t */
5670 textureType = D3DTSS_TEXCOORDINDEX;
5671 val = pTextureState[i].value;
5672 break;
5673
5674 case SVGA3D_TS_TEXTURETRANSFORMFLAGS: /* SVGA3dTexTransformFlags */
5675 textureType = D3DTSS_TEXTURETRANSFORMFLAGS;
5676 val = vmsvga3dTextTransformFlags2D3D(pTextureState[i].value);
5677 break;
5678#endif
5679
5680 case SVGA3D_TS_BIND_TEXTURE: /* SVGA3dSurfaceId */
5681 {
5682 uint32_t const sid = pTextureState[i].value;
5683
5684 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture sid=%u replacing sid=%u\n",
5685 currentStage, sid, pContext->aSidActiveTextures[currentStage]));
5686
5687 /* Only if texture actually changed. */ /// @todo needs testing.
5688 if (pContext->aSidActiveTextures[currentStage] != sid)
5689 {
5690 if (pCurrentTextureSurface)
5691 {
5692 /* Unselect the currently associated texture. */
5693 glBindTexture(pCurrentTextureSurface->targetGL, 0);
5694 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5695
5696 if (currentStage < 8)
5697 {
5698 /* Necessary for the fixed pipeline. */
5699 glDisable(pCurrentTextureSurface->targetGL);
5700 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5701 }
5702
5703 pCurrentTextureSurface = NULL;
5704 }
5705
5706 if (sid == SVGA3D_INVALID_ID)
5707 {
5708 Assert(pCurrentTextureSurface == NULL);
5709 }
5710 else
5711 {
5712 PVMSVGA3DSURFACE pSurface;
5713 rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
5714 AssertRCReturn(rc, rc);
5715
5716 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture sid=%u (%d,%d) replacing sid=%u\n",
5717 currentStage, sid, pSurface->paMipmapLevels[0].mipmapSize.width,
5718 pSurface->paMipmapLevels[0].mipmapSize.height, pContext->aSidActiveTextures[currentStage]));
5719
5720 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
5721 {
5722 Log(("CreateTexture (%d,%d) levels=%d\n",
5723 pSurface->paMipmapLevels[0].mipmapSize.width, pSurface->paMipmapLevels[0].mipmapSize.height, pSurface->cLevels));
5724 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pSurface);
5725 AssertRCReturn(rc, rc);
5726 }
5727
5728 glBindTexture(pSurface->targetGL, pSurface->oglId.texture);
5729 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5730
5731 if (currentStage < 8)
5732 {
5733 /* Necessary for the fixed pipeline. */
5734 glEnable(pSurface->targetGL);
5735 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5736 }
5737
5738 /* Remember the currently active texture. */
5739 pCurrentTextureSurface = pSurface;
5740
5741 /* Recreate the texture state as glBindTexture resets them all (sigh). */
5742 for (uint32_t iStage = 0; iStage < RT_ELEMENTS(pContext->state.aTextureStates); iStage++)
5743 {
5744 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aTextureStates[0]); j++)
5745 {
5746 SVGA3dTextureState *pTextureStateIter = &pContext->state.aTextureStates[iStage][j];
5747
5748 if ( pTextureStateIter->name != SVGA3D_TS_INVALID
5749 && pTextureStateIter->name != SVGA3D_TS_BIND_TEXTURE)
5750 vmsvga3dSetTextureState(pThisCC, pContext->id, 1, pTextureStateIter);
5751 }
5752 }
5753 }
5754
5755 pContext->aSidActiveTextures[currentStage] = sid;
5756 }
5757
5758 /* Finished; continue with the next one. */
5759 continue;
5760 }
5761
5762 case SVGA3D_TS_ADDRESSW: /* SVGA3dTextureAddress */
5763 textureType = GL_TEXTURE_WRAP_R; /* R = W */
5764 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
5765 break;
5766
5767 case SVGA3D_TS_ADDRESSU: /* SVGA3dTextureAddress */
5768 textureType = GL_TEXTURE_WRAP_S; /* S = U */
5769 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
5770 break;
5771
5772 case SVGA3D_TS_ADDRESSV: /* SVGA3dTextureAddress */
5773 textureType = GL_TEXTURE_WRAP_T; /* T = V */
5774 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
5775 break;
5776
5777 case SVGA3D_TS_MIPFILTER: /* SVGA3dTextureFilter */
5778 case SVGA3D_TS_MINFILTER: /* SVGA3dTextureFilter */
5779 {
5780 uint32_t mipFilter = pContext->state.aTextureStates[currentStage][SVGA3D_TS_MIPFILTER].value;
5781 uint32_t minFilter = pContext->state.aTextureStates[currentStage][SVGA3D_TS_MINFILTER].value;
5782
5783 /* If SVGA3D_TS_MIPFILTER is set to NONE, then use SVGA3D_TS_MIPFILTER, otherwise SVGA3D_TS_MIPFILTER enables mipmap minification. */
5784 textureType = GL_TEXTURE_MIN_FILTER;
5785 if (mipFilter != SVGA3D_TEX_FILTER_NONE)
5786 {
5787 if (minFilter == SVGA3D_TEX_FILTER_NEAREST)
5788 {
5789 if (mipFilter == SVGA3D_TEX_FILTER_LINEAR)
5790 val = GL_NEAREST_MIPMAP_LINEAR;
5791 else
5792 val = GL_NEAREST_MIPMAP_NEAREST;
5793 }
5794 else
5795 {
5796 if (mipFilter == SVGA3D_TEX_FILTER_LINEAR)
5797 val = GL_LINEAR_MIPMAP_LINEAR;
5798 else
5799 val = GL_LINEAR_MIPMAP_NEAREST;
5800 }
5801 }
5802 else
5803 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)minFilter);
5804 break;
5805 }
5806
5807 case SVGA3D_TS_MAGFILTER: /* SVGA3dTextureFilter */
5808 textureType = GL_TEXTURE_MAG_FILTER;
5809 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)pTextureState[i].value);
5810 Assert(val == GL_NEAREST || val == GL_LINEAR);
5811 break;
5812
5813 case SVGA3D_TS_BORDERCOLOR: /* SVGA3dColor */
5814 {
5815 GLfloat color[4]; /* red, green, blue, alpha */
5816 vmsvgaColor2GLFloatArray(pTextureState[i].value, &color[0], &color[1], &color[2], &color[3]);
5817
5818 GLenum targetGL;
5819 if (pCurrentTextureSurface)
5820 targetGL = pCurrentTextureSurface->targetGL;
5821 else
5822 {
5823 /* No texture bound, assume 2D. */
5824 targetGL = GL_TEXTURE_2D;
5825 }
5826
5827 glTexParameterfv(targetGL, GL_TEXTURE_BORDER_COLOR, color); /* Identical; default 0.0 identical too */
5828 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5829 break;
5830 }
5831
5832 case SVGA3D_TS_TEXTURE_LOD_BIAS: /* float */
5833 {
5834 GLenum targetGL;
5835 if (pCurrentTextureSurface)
5836 targetGL = pCurrentTextureSurface->targetGL;
5837 else
5838 {
5839 /* No texture bound, assume 2D. */
5840 targetGL = GL_TEXTURE_2D;
5841 }
5842
5843 glTexParameterf(targetGL, GL_TEXTURE_LOD_BIAS, pTextureState[i].floatValue); /* Identical; default 0.0 identical too */
5844 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5845 break;
5846 }
5847
5848 case SVGA3D_TS_TEXTURE_MIPMAP_LEVEL: /* uint32_t */
5849 textureType = GL_TEXTURE_BASE_LEVEL;
5850 val = pTextureState[i].value;
5851 break;
5852
5853 case SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL: /* uint32_t */
5854 if (pState->caps.fTextureFilterAnisotropicSupported)
5855 {
5856 textureType = GL_TEXTURE_MAX_ANISOTROPY_EXT;
5857 val = RT_MIN((GLint)pTextureState[i].value, pState->caps.maxTextureAnisotropy);
5858 } /* otherwise ignore. */
5859 break;
5860
5861#if 0
5862 case SVGA3D_TS_GAMMA: /* float */
5863 samplerType = D3DSAMP_SRGBTEXTURE;
5864 /* Boolean in D3D */
5865 if (pTextureState[i].floatValue == 1.0f)
5866 val = FALSE;
5867 else
5868 val = TRUE;
5869 break;
5870#endif
5871 /* Internal commands, that don't map directly to the SetTextureStageState API. */
5872 case SVGA3D_TS_TEXCOORDGEN: /* SVGA3dTextureCoordGen */
5873 AssertFailed();
5874 break;
5875
5876 default:
5877 //AssertFailed();
5878 break;
5879 }
5880
5881 if (textureType != ~(GLenum)0)
5882 {
5883 GLenum targetGL;
5884 if (pCurrentTextureSurface)
5885 targetGL = pCurrentTextureSurface->targetGL;
5886 else
5887 {
5888 /* No texture bound, assume 2D. */
5889 targetGL = GL_TEXTURE_2D;
5890 }
5891
5892 switch (pTextureState[i].name)
5893 {
5894 case SVGA3D_TS_MINFILTER:
5895 case SVGA3D_TS_MAGFILTER:
5896 {
5897 if (pState->caps.fTextureFilterAnisotropicSupported)
5898 {
5899 uint32_t const anisotropyLevel = (SVGA3dTextureFilter)pTextureState[i].value == SVGA3D_TEX_FILTER_ANISOTROPIC
5900 ? RT_MAX(1, pContext->state.aTextureStates[currentStage][SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL].value)
5901 : 1;
5902 glTexParameteri(targetGL, GL_TEXTURE_MAX_ANISOTROPY_EXT, RT_MIN((GLint)anisotropyLevel, pState->caps.maxTextureAnisotropy));
5903 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5904 }
5905 } break;
5906
5907 default: break;
5908 }
5909
5910 glTexParameteri(targetGL, textureType, val);
5911 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5912 }
5913 }
5914
5915 return VINF_SUCCESS;
5916}
5917
5918int vmsvga3dSetMaterial(PVGASTATECC pThisCC, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
5919{
5920 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5921 AssertReturn(pState, VERR_NO_MEMORY);
5922
5923 LogFunc(("cid=%u face %d\n", cid, face));
5924
5925 PVMSVGA3DCONTEXT pContext;
5926 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5927 AssertRCReturn(rc, rc);
5928
5929 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5930
5931 GLenum oglFace;
5932 switch (face)
5933 {
5934 case SVGA3D_FACE_NONE:
5935 case SVGA3D_FACE_FRONT:
5936 oglFace = GL_FRONT;
5937 break;
5938
5939 case SVGA3D_FACE_BACK:
5940 oglFace = GL_BACK;
5941 break;
5942
5943 case SVGA3D_FACE_FRONT_BACK:
5944 oglFace = GL_FRONT_AND_BACK;
5945 break;
5946
5947 default:
5948 AssertFailedReturn(VERR_INVALID_PARAMETER);
5949 }
5950
5951 /* Save for vm state save/restore. */
5952 pContext->state.aMaterial[face].fValid = true;
5953 pContext->state.aMaterial[face].material = *pMaterial;
5954 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_MATERIAL;
5955
5956 glMaterialfv(oglFace, GL_DIFFUSE, pMaterial->diffuse);
5957 glMaterialfv(oglFace, GL_AMBIENT, pMaterial->ambient);
5958 glMaterialfv(oglFace, GL_SPECULAR, pMaterial->specular);
5959 glMaterialfv(oglFace, GL_EMISSION, pMaterial->emissive);
5960 glMaterialfv(oglFace, GL_SHININESS, &pMaterial->shininess);
5961 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5962
5963 return VINF_SUCCESS;
5964}
5965
5966/** @todo Move into separate library as we are using logic from Wine here. */
5967int vmsvga3dSetLightData(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
5968{
5969 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5970 AssertReturn(pState, VERR_NO_MEMORY);
5971
5972 LogFunc(("vmsvga3dSetLightData cid=%u index=%d type=%d\n", cid, index, pData->type));
5973 ASSERT_GUEST_RETURN(index < SVGA3D_MAX_LIGHTS, VERR_INVALID_PARAMETER);
5974
5975 PVMSVGA3DCONTEXT pContext;
5976 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5977 AssertRCReturn(rc, rc);
5978
5979 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5980
5981 /* Store for vm state save/restore */
5982 pContext->state.aLightData[index].fValidData = true;
5983 pContext->state.aLightData[index].data = *pData;
5984
5985 if ( pData->attenuation0 < 0.0f
5986 || pData->attenuation1 < 0.0f
5987 || pData->attenuation2 < 0.0f)
5988 {
5989 Log(("vmsvga3dSetLightData: invalid negative attenuation values!!\n"));
5990 return VINF_SUCCESS; /* ignore; could crash the GL driver */
5991 }
5992
5993 /* Light settings are affected by the model view in OpenGL, the View transform in direct3d */
5994 glMatrixMode(GL_MODELVIEW);
5995 glPushMatrix();
5996 glLoadMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
5997
5998 glLightfv(GL_LIGHT0 + index, GL_DIFFUSE, pData->diffuse);
5999 glLightfv(GL_LIGHT0 + index, GL_SPECULAR, pData->specular);
6000 glLightfv(GL_LIGHT0 + index, GL_AMBIENT, pData->ambient);
6001
6002 float QuadAttenuation;
6003 if (pData->range * pData->range >= FLT_MIN)
6004 QuadAttenuation = 1.4f / (pData->range * pData->range);
6005 else
6006 QuadAttenuation = 0.0f;
6007
6008 switch (pData->type)
6009 {
6010 case SVGA3D_LIGHTTYPE_POINT:
6011 {
6012 GLfloat position[4];
6013
6014 position[0] = pData->position[0];
6015 position[1] = pData->position[1];
6016 position[2] = pData->position[2];
6017 position[3] = 1.0f;
6018
6019 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
6020 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6021
6022 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
6023 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6024
6025 /* Attenuation - Are these right? guessing... */
6026 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
6027 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6028
6029 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
6030 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6031
6032 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
6033 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6034
6035 /** @todo range */
6036 break;
6037 }
6038
6039 case SVGA3D_LIGHTTYPE_SPOT1:
6040 {
6041 GLfloat exponent;
6042 GLfloat position[4];
6043 const GLfloat pi = 4.0f * atanf(1.0f);
6044
6045 position[0] = pData->position[0];
6046 position[1] = pData->position[1];
6047 position[2] = pData->position[2];
6048 position[3] = 1.0f;
6049
6050 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
6051 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6052
6053 position[0] = pData->direction[0];
6054 position[1] = pData->direction[1];
6055 position[2] = pData->direction[2];
6056 position[3] = 1.0f;
6057
6058 glLightfv(GL_LIGHT0 + index, GL_SPOT_DIRECTION, position);
6059 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6060
6061 /*
6062 * opengl-ish and d3d-ish spot lights use too different models for the
6063 * light "intensity" as a function of the angle towards the main light direction,
6064 * so we only can approximate very roughly.
6065 * however spot lights are rather rarely used in games (if ever used at all).
6066 * furthermore if still used, probably nobody pays attention to such details.
6067 */
6068 if (pData->falloff == 0)
6069 {
6070 /* Falloff = 0 is easy, because d3d's and opengl's spot light equations have the
6071 * falloff resp. exponent parameter as an exponent, so the spot light lighting
6072 * will always be 1.0 for both of them, and we don't have to care for the
6073 * rest of the rather complex calculation
6074 */
6075 exponent = 0.0f;
6076 }
6077 else
6078 {
6079 float rho = pData->theta + (pData->phi - pData->theta) / (2 * pData->falloff);
6080 if (rho < 0.0001f)
6081 rho = 0.0001f;
6082 exponent = -0.3f/log(cos(rho/2));
6083 }
6084 if (exponent > 128.0f)
6085 exponent = 128.0f;
6086
6087 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, exponent);
6088 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6089
6090 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, pData->phi * 90.0 / pi);
6091 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6092
6093 /* Attenuation - Are these right? guessing... */
6094 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
6095 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6096
6097 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
6098 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6099
6100 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
6101 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6102
6103 /** @todo range */
6104 break;
6105 }
6106
6107 case SVGA3D_LIGHTTYPE_DIRECTIONAL:
6108 {
6109 GLfloat position[4];
6110
6111 position[0] = -pData->direction[0];
6112 position[1] = -pData->direction[1];
6113 position[2] = -pData->direction[2];
6114 position[3] = 0.0f;
6115
6116 glLightfv(GL_LIGHT0 + index, GL_POSITION, position); /* Note gl uses w position of 0 for direction! */
6117 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6118
6119 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
6120 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6121
6122 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, 0.0f);
6123 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6124 break;
6125 }
6126
6127 case SVGA3D_LIGHTTYPE_SPOT2:
6128 default:
6129 Log(("Unsupported light type!!\n"));
6130 rc = VERR_INVALID_PARAMETER;
6131 break;
6132 }
6133
6134 /* Restore the modelview matrix */
6135 glPopMatrix();
6136
6137 return rc;
6138}
6139
6140int vmsvga3dSetLightEnabled(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, uint32_t enabled)
6141{
6142 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6143 AssertReturn(pState, VERR_NO_MEMORY);
6144
6145 LogFunc(("cid=%u %d -> %d\n", cid, index, enabled));
6146
6147 PVMSVGA3DCONTEXT pContext;
6148 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
6149 AssertRCReturn(rc, rc);
6150
6151 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6152
6153 /* Store for vm state save/restore */
6154 if (index < SVGA3D_MAX_LIGHTS)
6155 pContext->state.aLightData[index].fEnabled = !!enabled;
6156 else
6157 AssertFailed();
6158
6159 if (enabled)
6160 {
6161 if (index < SVGA3D_MAX_LIGHTS)
6162 {
6163 /* Load the default settings if none have been set yet. */
6164 if (!pContext->state.aLightData[index].fValidData)
6165 vmsvga3dSetLightData(pThisCC, cid, index, (SVGA3dLightData *)&vmsvga3d_default_light);
6166 }
6167 glEnable(GL_LIGHT0 + index);
6168 }
6169 else
6170 glDisable(GL_LIGHT0 + index);
6171
6172 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6173 return VINF_SUCCESS;
6174}
6175
6176int vmsvga3dSetViewPort(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect)
6177{
6178 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6179 AssertReturn(pState, VERR_NO_MEMORY);
6180
6181 Log(("vmsvga3dSetViewPort cid=%u (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
6182
6183 PVMSVGA3DCONTEXT pContext;
6184 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
6185 AssertRCReturn(rc, rc);
6186
6187 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6188
6189 /* Save for vm state save/restore. */
6190 pContext->state.RectViewPort = *pRect;
6191 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VIEWPORT;
6192
6193 /** @todo y-inversion for partial viewport coordinates? */
6194 glViewport(pRect->x, pRect->y, pRect->w, pRect->h);
6195 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6196
6197 /* Reset the projection matrix as that relies on the viewport setting. */
6198 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].fValid == true)
6199 vmsvga3dSetTransform(pThisCC, cid, SVGA3D_TRANSFORM_PROJECTION,
6200 pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].matrix);
6201 else
6202 {
6203 float matrix[16];
6204
6205 /* identity matrix if no matrix set. */
6206 memset(matrix, 0, sizeof(matrix));
6207 matrix[0] = 1.0;
6208 matrix[5] = 1.0;
6209 matrix[10] = 1.0;
6210 matrix[15] = 1.0;
6211 vmsvga3dSetTransform(pThisCC, cid, SVGA3D_TRANSFORM_PROJECTION, matrix);
6212 }
6213
6214 return VINF_SUCCESS;
6215}
6216
6217int vmsvga3dSetClipPlane(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, float plane[4])
6218{
6219 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6220 AssertReturn(pState, VERR_NO_MEMORY);
6221 double oglPlane[4];
6222
6223 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)));
6224 AssertReturn(index < SVGA3D_NUM_CLIPPLANES, VERR_INVALID_PARAMETER);
6225
6226 PVMSVGA3DCONTEXT pContext;
6227 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
6228 AssertRCReturn(rc, rc);
6229
6230 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6231
6232 /* Store for vm state save/restore. */
6233 pContext->state.aClipPlane[index].fValid = true;
6234 memcpy(pContext->state.aClipPlane[index].plane, plane, sizeof(pContext->state.aClipPlane[index].plane));
6235
6236 /** @todo clip plane affected by model view in OpenGL & view in D3D + vertex shader -> not transformed (see Wine; state.c clipplane) */
6237 oglPlane[0] = (double)plane[0];
6238 oglPlane[1] = (double)plane[1];
6239 oglPlane[2] = (double)plane[2];
6240 oglPlane[3] = (double)plane[3];
6241
6242 glClipPlane(GL_CLIP_PLANE0 + index, oglPlane);
6243 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6244
6245 return VINF_SUCCESS;
6246}
6247
6248int vmsvga3dSetScissorRect(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect)
6249{
6250 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6251 AssertReturn(pState, VERR_NO_MEMORY);
6252
6253 Log(("vmsvga3dSetScissorRect cid=%u (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
6254
6255 PVMSVGA3DCONTEXT pContext;
6256 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
6257 AssertRCReturn(rc, rc);
6258
6259 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6260
6261 /* Store for vm state save/restore. */
6262 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_SCISSORRECT;
6263 pContext->state.RectScissor = *pRect;
6264
6265 glScissor(pRect->x, pRect->y, pRect->w, pRect->h);
6266 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6267
6268 return VINF_SUCCESS;
6269}
6270
6271static void vmsvgaColor2GLFloatArray(uint32_t color, GLfloat *pRed, GLfloat *pGreen, GLfloat *pBlue, GLfloat *pAlpha)
6272{
6273 /* Convert byte color components to float (0-1.0) */
6274 *pAlpha = (GLfloat)(color >> 24) / 255.0;
6275 *pRed = (GLfloat)((color >> 16) & 0xff) / 255.0;
6276 *pGreen = (GLfloat)((color >> 8) & 0xff) / 255.0;
6277 *pBlue = (GLfloat)(color & 0xff) / 255.0;
6278}
6279
6280int vmsvga3dCommandClear(PVGASTATECC pThisCC, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth, uint32_t stencil,
6281 uint32_t cRects, SVGA3dRect *pRect)
6282{
6283 GLbitfield mask = 0;
6284 GLbitfield restoreMask = 0;
6285 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6286 AssertReturn(pState, VERR_NO_MEMORY);
6287 GLboolean fDepthWriteEnabled = GL_FALSE;
6288 GLboolean afColorWriteEnabled[4] = { GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE };
6289
6290 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));
6291
6292 PVMSVGA3DCONTEXT pContext;
6293 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
6294 AssertRCReturn(rc, rc);
6295
6296 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6297
6298 if (clearFlag & SVGA3D_CLEAR_COLOR)
6299 {
6300 GLfloat red, green, blue, alpha;
6301 vmsvgaColor2GLFloatArray(color, &red, &green, &blue, &alpha);
6302
6303 /* Set the color clear value. */
6304 glClearColor(red, green, blue, alpha);
6305 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6306
6307 mask |= GL_COLOR_BUFFER_BIT;
6308
6309 /* glClear will not clear the color buffer if writing is disabled. */
6310 glGetBooleanv(GL_COLOR_WRITEMASK, afColorWriteEnabled);
6311 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6312 if ( afColorWriteEnabled[0] == GL_FALSE
6313 || afColorWriteEnabled[1] == GL_FALSE
6314 || afColorWriteEnabled[2] == GL_FALSE
6315 || afColorWriteEnabled[3] == GL_FALSE)
6316 {
6317 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
6318 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6319
6320 restoreMask |= GL_COLOR_BUFFER_BIT;
6321 }
6322
6323 }
6324
6325 if (clearFlag & SVGA3D_CLEAR_STENCIL)
6326 {
6327 /** @todo possibly the same problem as with glDepthMask */
6328 glClearStencil(stencil);
6329 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6330
6331 mask |= GL_STENCIL_BUFFER_BIT;
6332 }
6333
6334 if (clearFlag & SVGA3D_CLEAR_DEPTH)
6335 {
6336 glClearDepth((GLdouble)depth);
6337 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6338
6339 mask |= GL_DEPTH_BUFFER_BIT;
6340
6341 /* glClear will not clear the depth buffer if writing is disabled. */
6342 glGetBooleanv(GL_DEPTH_WRITEMASK, &fDepthWriteEnabled);
6343 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6344 if (fDepthWriteEnabled == GL_FALSE)
6345 {
6346 glDepthMask(GL_TRUE);
6347 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6348
6349 restoreMask |= GL_DEPTH_BUFFER_BIT;
6350 }
6351 }
6352
6353 /* Save the current scissor test bit and scissor box. */
6354 glPushAttrib(GL_SCISSOR_BIT);
6355 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6356
6357 if (cRects)
6358 {
6359 glEnable(GL_SCISSOR_TEST);
6360 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6361
6362 for (uint32_t i = 0; i < cRects; ++i)
6363 {
6364 LogFunc(("rect [%d] %d,%d %dx%d)\n", i, pRect[i].x, pRect[i].y, pRect[i].w, pRect[i].h));
6365 glScissor(pRect[i].x, pRect[i].y, pRect[i].w, pRect[i].h);
6366 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6367
6368 glClear(mask);
6369 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6370 }
6371 }
6372 else
6373 {
6374 glDisable(GL_SCISSOR_TEST);
6375 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6376
6377 glClear(mask);
6378 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6379 }
6380
6381 /* Restore the old scissor test bit and box */
6382 glPopAttrib();
6383 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6384
6385 /* Restore the write states. */
6386 if (restoreMask & GL_COLOR_BUFFER_BIT)
6387 {
6388 glColorMask(afColorWriteEnabled[0],
6389 afColorWriteEnabled[1],
6390 afColorWriteEnabled[2],
6391 afColorWriteEnabled[3]);
6392 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6393 }
6394
6395 if (restoreMask & GL_DEPTH_BUFFER_BIT)
6396 {
6397 glDepthMask(fDepthWriteEnabled);
6398 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6399 }
6400
6401 return VINF_SUCCESS;
6402}
6403
6404/* Convert VMWare vertex declaration to its OpenGL equivalent. */
6405int vmsvga3dVertexDecl2OGL(SVGA3dVertexArrayIdentity &identity, GLint &size, GLenum &type, GLboolean &normalized, uint32_t &cbAttrib)
6406{
6407 normalized = GL_FALSE;
6408 switch (identity.type)
6409 {
6410 case SVGA3D_DECLTYPE_FLOAT1:
6411 size = 1;
6412 type = GL_FLOAT;
6413 cbAttrib = sizeof(float);
6414 break;
6415 case SVGA3D_DECLTYPE_FLOAT2:
6416 size = 2;
6417 type = GL_FLOAT;
6418 cbAttrib = 2 * sizeof(float);
6419 break;
6420 case SVGA3D_DECLTYPE_FLOAT3:
6421 size = 3;
6422 type = GL_FLOAT;
6423 cbAttrib = 3 * sizeof(float);
6424 break;
6425 case SVGA3D_DECLTYPE_FLOAT4:
6426 size = 4;
6427 type = GL_FLOAT;
6428 cbAttrib = 4 * sizeof(float);
6429 break;
6430
6431 case SVGA3D_DECLTYPE_D3DCOLOR:
6432 size = GL_BGRA; /* @note requires GL_ARB_vertex_array_bgra */
6433 type = GL_UNSIGNED_BYTE;
6434 normalized = GL_TRUE; /* glVertexAttribPointer fails otherwise */
6435 cbAttrib = sizeof(uint32_t);
6436 break;
6437
6438 case SVGA3D_DECLTYPE_UBYTE4N:
6439 normalized = GL_TRUE;
6440 RT_FALL_THRU();
6441 case SVGA3D_DECLTYPE_UBYTE4:
6442 size = 4;
6443 type = GL_UNSIGNED_BYTE;
6444 cbAttrib = sizeof(uint32_t);
6445 break;
6446
6447 case SVGA3D_DECLTYPE_SHORT2N:
6448 normalized = GL_TRUE;
6449 RT_FALL_THRU();
6450 case SVGA3D_DECLTYPE_SHORT2:
6451 size = 2;
6452 type = GL_SHORT;
6453 cbAttrib = 2 * sizeof(uint16_t);
6454 break;
6455
6456 case SVGA3D_DECLTYPE_SHORT4N:
6457 normalized = GL_TRUE;
6458 RT_FALL_THRU();
6459 case SVGA3D_DECLTYPE_SHORT4:
6460 size = 4;
6461 type = GL_SHORT;
6462 cbAttrib = 4 * sizeof(uint16_t);
6463 break;
6464
6465 case SVGA3D_DECLTYPE_USHORT4N:
6466 normalized = GL_TRUE;
6467 size = 4;
6468 type = GL_UNSIGNED_SHORT;
6469 cbAttrib = 4 * sizeof(uint16_t);
6470 break;
6471
6472 case SVGA3D_DECLTYPE_USHORT2N:
6473 normalized = GL_TRUE;
6474 size = 2;
6475 type = GL_UNSIGNED_SHORT;
6476 cbAttrib = 2 * sizeof(uint16_t);
6477 break;
6478
6479 case SVGA3D_DECLTYPE_UDEC3:
6480 size = 3;
6481 type = GL_UNSIGNED_INT_2_10_10_10_REV; /** @todo correct? */
6482 cbAttrib = sizeof(uint32_t);
6483 break;
6484
6485 case SVGA3D_DECLTYPE_DEC3N:
6486 normalized = true;
6487 size = 3;
6488 type = GL_INT_2_10_10_10_REV; /** @todo correct? */
6489 cbAttrib = sizeof(uint32_t);
6490 break;
6491
6492 case SVGA3D_DECLTYPE_FLOAT16_2:
6493 size = 2;
6494 type = GL_HALF_FLOAT;
6495 cbAttrib = 2 * sizeof(uint16_t);
6496 break;
6497 case SVGA3D_DECLTYPE_FLOAT16_4:
6498 size = 4;
6499 type = GL_HALF_FLOAT;
6500 cbAttrib = 4 * sizeof(uint16_t);
6501 break;
6502 default:
6503 AssertFailedReturn(VERR_INVALID_PARAMETER);
6504 }
6505
6506 //pVertexElement->Method = identity.method;
6507 //pVertexElement->Usage = identity.usage;
6508
6509 return VINF_SUCCESS;
6510}
6511
6512static float vmsvga3dFloat16To32(uint16_t f16)
6513{
6514 /* From Wiki */
6515#ifndef INFINITY
6516 static uint32_t const sBitsINFINITY = UINT32_C(0x7f800000);
6517 #define INFINITY (*(float const *)&sBitsINFINITY)
6518#endif
6519#ifndef NAN
6520 static uint32_t const sBitsNAN = UINT32_C(0x7fc00000);
6521 #define NAN (*(float const *)&sBitsNAN)
6522#endif
6523
6524 uint16_t const s = (f16 >> UINT16_C(15)) & UINT16_C(0x1);
6525 uint16_t const e = (f16 >> UINT16_C(10)) & UINT16_C(0x1f);
6526 uint16_t const m = (f16 ) & UINT16_C(0x3ff);
6527
6528 float result = s ? 1.0f : -1.0f;
6529 if (e == 0)
6530 {
6531 if (m == 0)
6532 result *= 0.0f; /* zero, -0 */
6533 else
6534 result *= (float)m / 1024.0f / 16384.0f; /* subnormal numbers: sign * 2^-14 * 0.m */
6535 }
6536 else if (e == 0x1f)
6537 {
6538 if (m == 0)
6539 result *= INFINITY; /* +-infinity */
6540 else
6541 result = NAN; /* NAN */
6542 }
6543 else
6544 {
6545 result *= powf(2.0f, (float)e - 15.0f) * (1.0f + (float)m / 1024.0f); /* sign * 2^(e-15) * 1.m */
6546 }
6547
6548 return result;
6549}
6550
6551/* Set a vertex attribute according to VMSVGA vertex declaration. */
6552static int vmsvga3dSetVertexAttrib(PVMSVGA3DSTATE pState, GLuint index, SVGA3dVertexArrayIdentity const *pIdentity, GLvoid const *pv)
6553{
6554 switch (pIdentity->type)
6555 {
6556 case SVGA3D_DECLTYPE_FLOAT1:
6557 {
6558 /* "One-component float expanded to (float, 0, 0, 1)." */
6559 GLfloat const *p = (GLfloat *)pv;
6560 GLfloat const v[4] = { p[0], 0.0f, 0.0f, 1.0f };
6561 pState->ext.glVertexAttrib4fv(index, v);
6562 break;
6563 }
6564 case SVGA3D_DECLTYPE_FLOAT2:
6565 {
6566 /* "Two-component float expanded to (float, float, 0, 1)." */
6567 GLfloat const *p = (GLfloat *)pv;
6568 GLfloat const v[4] = { p[0], p[1], 0.0f, 1.0f };
6569 pState->ext.glVertexAttrib4fv(index, v);
6570 break;
6571 }
6572 case SVGA3D_DECLTYPE_FLOAT3:
6573 {
6574 /* "Three-component float expanded to (float, float, float, 1)." */
6575 GLfloat const *p = (GLfloat *)pv;
6576 GLfloat const v[4] = { p[0], p[1], p[2], 1.0f };
6577 pState->ext.glVertexAttrib4fv(index, v);
6578 break;
6579 }
6580 case SVGA3D_DECLTYPE_FLOAT4:
6581 pState->ext.glVertexAttrib4fv(index, (GLfloat const *)pv);
6582 break;
6583 case SVGA3D_DECLTYPE_D3DCOLOR:
6584 /** @todo Need to swap bytes? */
6585 pState->ext.glVertexAttrib4Nubv(index, (GLubyte const *)pv);
6586 break;
6587 case SVGA3D_DECLTYPE_UBYTE4:
6588 pState->ext.glVertexAttrib4ubv(index, (GLubyte const *)pv);
6589 break;
6590 case SVGA3D_DECLTYPE_SHORT2:
6591 {
6592 /* "Two-component, signed short expanded to (value, value, 0, 1)." */
6593 GLshort const *p = (GLshort const *)pv;
6594 GLshort const v[4] = { p[0], p[1], 0, 1 };
6595 pState->ext.glVertexAttrib4sv(index, v);
6596 break;
6597 }
6598 case SVGA3D_DECLTYPE_SHORT4:
6599 pState->ext.glVertexAttrib4sv(index, (GLshort const *)pv);
6600 break;
6601 case SVGA3D_DECLTYPE_UBYTE4N:
6602 pState->ext.glVertexAttrib4Nubv(index, (GLubyte const *)pv);
6603 break;
6604 case SVGA3D_DECLTYPE_SHORT2N:
6605 {
6606 /* "Normalized, two-component, signed short, expanded to (first short/32767.0, second short/32767.0, 0, 1)." */
6607 GLshort const *p = (GLshort const *)pv;
6608 GLshort const v[4] = { p[0], p[1], 0, 1 };
6609 pState->ext.glVertexAttrib4Nsv(index, v);
6610 break;
6611 }
6612 case SVGA3D_DECLTYPE_SHORT4N:
6613 pState->ext.glVertexAttrib4Nsv(index, (GLshort const *)pv);
6614 break;
6615 case SVGA3D_DECLTYPE_USHORT2N:
6616 {
6617 GLushort const *p = (GLushort const *)pv;
6618 GLushort const v[4] = { p[0], p[1], 0, 1 };
6619 pState->ext.glVertexAttrib4Nusv(index, v);
6620 break;
6621 }
6622 case SVGA3D_DECLTYPE_USHORT4N:
6623 pState->ext.glVertexAttrib4Nusv(index, (GLushort const *)pv);
6624 break;
6625 case SVGA3D_DECLTYPE_UDEC3:
6626 {
6627 /** @todo Test */
6628 /* "Three-component, unsigned, 10 10 10 format expanded to (value, value, value, 1)." */
6629 uint32_t const u32 = *(uint32_t *)pv;
6630 GLfloat const v[4] = { (float)(u32 & 0x3ff), (float)((u32 >> 10) & 0x3ff), (float)((u32 >> 20) & 0x3ff), 1.0f };
6631 pState->ext.glVertexAttrib4fv(index, v);
6632 break;
6633 }
6634 case SVGA3D_DECLTYPE_DEC3N:
6635 {
6636 /** @todo Test */
6637 /* "Three-component, signed, 10 10 10 format normalized and expanded to (v[0]/511.0, v[1]/511.0, v[2]/511.0, 1)." */
6638 uint32_t const u32 = *(uint32_t *)pv;
6639 GLfloat const v[4] = { (u32 & 0x3ff) / 511.0f, ((u32 >> 10) & 0x3ff) / 511.0f, ((u32 >> 20) & 0x3ff) / 511.0f, 1.0f };
6640 pState->ext.glVertexAttrib4fv(index, v);
6641 break;
6642 }
6643 case SVGA3D_DECLTYPE_FLOAT16_2:
6644 {
6645 /** @todo Test */
6646 /* "Two-component, 16-bit, floating point expanded to (value, value, 0, 1)." */
6647 uint16_t const *p = (uint16_t *)pv;
6648 GLfloat const v[4] = { vmsvga3dFloat16To32(p[0]), vmsvga3dFloat16To32(p[1]), 0.0f, 1.0f };
6649 pState->ext.glVertexAttrib4fv(index, v);
6650 break;
6651 }
6652 case SVGA3D_DECLTYPE_FLOAT16_4:
6653 {
6654 /** @todo Test */
6655 uint16_t const *p = (uint16_t *)pv;
6656 GLfloat const v[4] = { vmsvga3dFloat16To32(p[0]), vmsvga3dFloat16To32(p[1]),
6657 vmsvga3dFloat16To32(p[2]), vmsvga3dFloat16To32(p[3]) };
6658 pState->ext.glVertexAttrib4fv(index, v);
6659 break;
6660 }
6661 default:
6662 AssertFailedReturn(VERR_INVALID_PARAMETER);
6663 }
6664
6665 return VINF_SUCCESS;
6666}
6667
6668/* Convert VMWare primitive type to its OpenGL equivalent. */
6669/* Calculate the vertex count based on the primitive type and nr of primitives. */
6670int vmsvga3dPrimitiveType2OGL(SVGA3dPrimitiveType PrimitiveType, GLenum *pMode, uint32_t cPrimitiveCount, uint32_t *pcVertices)
6671{
6672 switch (PrimitiveType)
6673 {
6674 case SVGA3D_PRIMITIVE_TRIANGLELIST:
6675 *pMode = GL_TRIANGLES;
6676 *pcVertices = cPrimitiveCount * 3;
6677 break;
6678 case SVGA3D_PRIMITIVE_POINTLIST:
6679 *pMode = GL_POINTS;
6680 *pcVertices = cPrimitiveCount;
6681 break;
6682 case SVGA3D_PRIMITIVE_LINELIST:
6683 *pMode = GL_LINES;
6684 *pcVertices = cPrimitiveCount * 2;
6685 break;
6686 case SVGA3D_PRIMITIVE_LINESTRIP:
6687 *pMode = GL_LINE_STRIP;
6688 *pcVertices = cPrimitiveCount + 1;
6689 break;
6690 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
6691 *pMode = GL_TRIANGLE_STRIP;
6692 *pcVertices = cPrimitiveCount + 2;
6693 break;
6694 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
6695 *pMode = GL_TRIANGLE_FAN;
6696 *pcVertices = cPrimitiveCount + 2;
6697 break;
6698 default:
6699 return VERR_INVALID_PARAMETER;
6700 }
6701 return VINF_SUCCESS;
6702}
6703
6704static int vmsvga3dResetTransformMatrices(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext)
6705{
6706 int rc;
6707
6708 /* Reset the view matrix (also takes the world matrix into account). */
6709 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].fValid == true)
6710 rc = vmsvga3dSetTransform(pThisCC, pContext->id, SVGA3D_TRANSFORM_VIEW,
6711 pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
6712 else
6713 {
6714 float matrix[16];
6715
6716 /* identity matrix if no matrix set. */
6717 memset(matrix, 0, sizeof(matrix));
6718 matrix[0] = 1.0;
6719 matrix[5] = 1.0;
6720 matrix[10] = 1.0;
6721 matrix[15] = 1.0;
6722 rc = vmsvga3dSetTransform(pThisCC, pContext->id, SVGA3D_TRANSFORM_VIEW, matrix);
6723 }
6724
6725 /* Reset the projection matrix. */
6726 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].fValid == true)
6727 {
6728 rc = vmsvga3dSetTransform(pThisCC, pContext->id, SVGA3D_TRANSFORM_PROJECTION, pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].matrix);
6729 }
6730 else
6731 {
6732 float matrix[16];
6733
6734 /* identity matrix if no matrix set. */
6735 memset(matrix, 0, sizeof(matrix));
6736 matrix[0] = 1.0;
6737 matrix[5] = 1.0;
6738 matrix[10] = 1.0;
6739 matrix[15] = 1.0;
6740 rc = vmsvga3dSetTransform(pThisCC, pContext->id, SVGA3D_TRANSFORM_PROJECTION, matrix);
6741 }
6742 AssertRC(rc);
6743 return rc;
6744}
6745
6746static int vmsvga3dDrawPrimitivesProcessVertexDecls(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext,
6747 uint32_t iVertexDeclBase, uint32_t numVertexDecls,
6748 SVGA3dVertexDecl *pVertexDecl,
6749 SVGA3dVertexDivisor const *paVertexDivisors)
6750{
6751 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6752 unsigned const sidVertex = pVertexDecl[0].array.surfaceId;
6753
6754 PVMSVGA3DSURFACE pVertexSurface;
6755 int rc = vmsvga3dSurfaceFromSid(pState, sidVertex, &pVertexSurface);
6756 AssertRCReturn(rc, rc);
6757
6758 Log(("vmsvga3dDrawPrimitives: vertex surface sid=%u\n", sidVertex));
6759
6760 /* Create and/or bind the vertex buffer. */
6761 if (pVertexSurface->oglId.buffer == OPENGL_INVALID_ID)
6762 {
6763 Log(("vmsvga3dDrawPrimitives: create vertex buffer fDirty=%d size=%x bytes\n", pVertexSurface->fDirty, pVertexSurface->paMipmapLevels[0].cbSurface));
6764 PVMSVGA3DCONTEXT pSavedCtx = pContext;
6765 pContext = &pState->SharedCtx;
6766 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6767
6768 pState->ext.glGenBuffers(1, &pVertexSurface->oglId.buffer);
6769 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6770 pVertexSurface->enmOGLResType = VMSVGA3D_OGLRESTYPE_BUFFER;
6771
6772 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
6773 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6774
6775 Assert(pVertexSurface->fDirty);
6776 /** @todo rethink usage dynamic/static */
6777 pState->ext.glBufferData(GL_ARRAY_BUFFER, pVertexSurface->paMipmapLevels[0].cbSurface, pVertexSurface->paMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
6778 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6779
6780 pVertexSurface->paMipmapLevels[0].fDirty = false;
6781 pVertexSurface->fDirty = false;
6782
6783 pVertexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
6784
6785 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, OPENGL_INVALID_ID);
6786 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6787
6788 pContext = pSavedCtx;
6789 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6790 }
6791
6792 Assert(pVertexSurface->fDirty == false);
6793 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
6794 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6795
6796 /* Setup the vertex declarations. */
6797 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
6798 {
6799 GLint size;
6800 GLenum type;
6801 GLboolean normalized;
6802 uint32_t cbAttrib;
6803 GLuint index = iVertexDeclBase + iVertex;
6804
6805 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));
6806
6807 rc = vmsvga3dVertexDecl2OGL(pVertexDecl[iVertex].identity, size, type, normalized, cbAttrib);
6808 AssertRCReturn(rc, rc);
6809
6810 ASSERT_GUEST_RETURN( pVertexSurface->paMipmapLevels[0].cbSurface >= pVertexDecl[iVertex].array.offset
6811 && pVertexSurface->paMipmapLevels[0].cbSurface - pVertexDecl[iVertex].array.offset >= cbAttrib,
6812 VERR_INVALID_PARAMETER);
6813 RT_UNTRUSTED_VALIDATED_FENCE();
6814
6815 if (pContext->state.shidVertex != SVGA_ID_INVALID)
6816 {
6817 /* Use numbered vertex arrays (or attributes) when shaders are active. */
6818 if (pVertexDecl[iVertex].array.stride)
6819 {
6820 pState->ext.glEnableVertexAttribArray(index);
6821 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6822 pState->ext.glVertexAttribPointer(index, size, type, normalized, pVertexDecl[iVertex].array.stride,
6823 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6824 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6825
6826 GLuint divisor = paVertexDivisors && paVertexDivisors[index].instanceData ? 1 : 0;
6827 pState->ext.glVertexAttribDivisor(index, divisor);
6828 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6829
6830 /** @todo case SVGA3D_DECLUSAGE_COLOR: color component order not identical!! test GL_BGRA!! */
6831 }
6832 else
6833 {
6834 /*
6835 * D3D and OpenGL have a different meaning of value zero for the vertex array stride:
6836 * - D3D (VMSVGA): "use a zero stride to tell the runtime not to increment the vertex buffer offset."
6837 * - OpenGL: "If stride is 0, the generic vertex attributes are understood to be tightly packed in the array."
6838 * VMSVGA uses the D3D semantics.
6839 *
6840 * Use glVertexAttrib in order to tell OpenGL to reuse the zero stride attributes for each vertex.
6841 */
6842 pState->ext.glDisableVertexAttribArray(index);
6843 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6844
6845 const GLvoid *v = (uint8_t *)pVertexSurface->paMipmapLevels[0].pSurfaceData + pVertexDecl[iVertex].array.offset;
6846 vmsvga3dSetVertexAttrib(pState, index, &pVertexDecl[iVertex].identity, v);
6847 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6848 }
6849 }
6850 else
6851 {
6852 if (pVertexDecl[iVertex].array.stride == 0)
6853 {
6854 /* Zero stride means that the attribute pointer must not be increased.
6855 * See comment about stride in vmsvga3dDrawPrimitives.
6856 */
6857 LogRelMax(8, ("VMSVGA: Warning: zero stride array in fixed function pipeline\n"));
6858 AssertFailed();
6859 }
6860
6861 /* Use the predefined selection of vertex streams for the fixed pipeline. */
6862 switch (pVertexDecl[iVertex].identity.usage)
6863 {
6864 case SVGA3D_DECLUSAGE_POSITIONT:
6865 case SVGA3D_DECLUSAGE_POSITION:
6866 {
6867 glEnableClientState(GL_VERTEX_ARRAY);
6868 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6869 glVertexPointer(size, type, pVertexDecl[iVertex].array.stride,
6870 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6871 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6872 break;
6873 }
6874 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
6875 AssertFailed();
6876 break;
6877 case SVGA3D_DECLUSAGE_BLENDINDICES:
6878 AssertFailed();
6879 break;
6880 case SVGA3D_DECLUSAGE_NORMAL:
6881 glEnableClientState(GL_NORMAL_ARRAY);
6882 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6883 glNormalPointer(type, pVertexDecl[iVertex].array.stride,
6884 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6885 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6886 break;
6887 case SVGA3D_DECLUSAGE_PSIZE:
6888 AssertFailed();
6889 break;
6890 case SVGA3D_DECLUSAGE_TEXCOORD:
6891 /* Specify the affected texture unit. */
6892#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
6893 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
6894#else
6895 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
6896#endif
6897 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
6898 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6899 glTexCoordPointer(size, type, pVertexDecl[iVertex].array.stride,
6900 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6901 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6902 break;
6903 case SVGA3D_DECLUSAGE_TANGENT:
6904 AssertFailed();
6905 break;
6906 case SVGA3D_DECLUSAGE_BINORMAL:
6907 AssertFailed();
6908 break;
6909 case SVGA3D_DECLUSAGE_TESSFACTOR:
6910 AssertFailed();
6911 break;
6912 case SVGA3D_DECLUSAGE_COLOR: /** @todo color component order not identical!! test GL_BGRA!! */
6913 glEnableClientState(GL_COLOR_ARRAY);
6914 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6915 glColorPointer(size, type, pVertexDecl[iVertex].array.stride,
6916 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6917 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6918 break;
6919 case SVGA3D_DECLUSAGE_FOG:
6920 glEnableClientState(GL_FOG_COORD_ARRAY);
6921 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6922 pState->ext.glFogCoordPointer(type, pVertexDecl[iVertex].array.stride,
6923 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
6924 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6925 break;
6926 case SVGA3D_DECLUSAGE_DEPTH:
6927 AssertFailed();
6928 break;
6929 case SVGA3D_DECLUSAGE_SAMPLE:
6930 AssertFailed();
6931 break;
6932 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
6933 }
6934 }
6935
6936#ifdef LOG_ENABLED
6937 if (pVertexDecl[iVertex].array.stride == 0)
6938 Log(("vmsvga3dDrawPrimitives: stride == 0! Can be valid\n"));
6939#endif
6940 }
6941
6942 return VINF_SUCCESS;
6943}
6944
6945static int vmsvga3dDrawPrimitivesCleanupVertexDecls(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t iVertexDeclBase,
6946 uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl)
6947{
6948 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6949
6950 /* Clean up the vertex declarations. */
6951 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
6952 {
6953 if (pVertexDecl[iVertex].identity.usage == SVGA3D_DECLUSAGE_POSITIONT)
6954 {
6955 /* Reset the transformation matrices in case of a switch back from pretransformed mode. */
6956 Log(("vmsvga3dDrawPrimitivesCleanupVertexDecls: reset world and projection matrices after transformation reset (pre-transformed -> transformed)\n"));
6957 vmsvga3dResetTransformMatrices(pThisCC, pContext);
6958 }
6959
6960 if (pContext->state.shidVertex != SVGA_ID_INVALID)
6961 {
6962 /* Use numbered vertex arrays when shaders are active. */
6963 pState->ext.glVertexAttribDivisor(iVertexDeclBase + iVertex, 0);
6964 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6965 pState->ext.glDisableVertexAttribArray(iVertexDeclBase + iVertex);
6966 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6967 }
6968 else
6969 {
6970 /* Use the predefined selection of vertex streams for the fixed pipeline. */
6971 switch (pVertexDecl[iVertex].identity.usage)
6972 {
6973 case SVGA3D_DECLUSAGE_POSITION:
6974 case SVGA3D_DECLUSAGE_POSITIONT:
6975 glDisableClientState(GL_VERTEX_ARRAY);
6976 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6977 break;
6978 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
6979 break;
6980 case SVGA3D_DECLUSAGE_BLENDINDICES:
6981 break;
6982 case SVGA3D_DECLUSAGE_NORMAL:
6983 glDisableClientState(GL_NORMAL_ARRAY);
6984 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6985 break;
6986 case SVGA3D_DECLUSAGE_PSIZE:
6987 break;
6988 case SVGA3D_DECLUSAGE_TEXCOORD:
6989 /* Specify the affected texture unit. */
6990#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
6991 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
6992#else
6993 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
6994#endif
6995 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
6996 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6997 break;
6998 case SVGA3D_DECLUSAGE_TANGENT:
6999 break;
7000 case SVGA3D_DECLUSAGE_BINORMAL:
7001 break;
7002 case SVGA3D_DECLUSAGE_TESSFACTOR:
7003 break;
7004 case SVGA3D_DECLUSAGE_COLOR: /** @todo color component order not identical!! */
7005 glDisableClientState(GL_COLOR_ARRAY);
7006 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7007 break;
7008 case SVGA3D_DECLUSAGE_FOG:
7009 glDisableClientState(GL_FOG_COORD_ARRAY);
7010 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7011 break;
7012 case SVGA3D_DECLUSAGE_DEPTH:
7013 break;
7014 case SVGA3D_DECLUSAGE_SAMPLE:
7015 break;
7016 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
7017 }
7018 }
7019 }
7020 /* Unbind the vertex buffer after usage. */
7021 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0);
7022 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7023 return VINF_SUCCESS;
7024}
7025
7026int vmsvga3dDrawPrimitives(PVGASTATECC pThisCC, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl,
7027 uint32_t numRanges, SVGA3dPrimitiveRange *pRange, uint32_t cVertexDivisor,
7028 SVGA3dVertexDivisor *pVertexDivisor)
7029{
7030 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
7031 AssertReturn(pState, VERR_INTERNAL_ERROR);
7032 uint32_t iCurrentVertex;
7033
7034 Log(("vmsvga3dDrawPrimitives cid=%u numVertexDecls=%d numRanges=%d, cVertexDivisor=%d\n", cid, numVertexDecls, numRanges, cVertexDivisor));
7035
7036 /* Caller already check these, but it cannot hurt to check again... */
7037 AssertReturn(numVertexDecls && numVertexDecls <= SVGA3D_MAX_VERTEX_ARRAYS, VERR_INVALID_PARAMETER);
7038 AssertReturn(numRanges && numRanges <= SVGA3D_MAX_DRAW_PRIMITIVE_RANGES, VERR_INVALID_PARAMETER);
7039 AssertReturn(!cVertexDivisor || cVertexDivisor == numVertexDecls, VERR_INVALID_PARAMETER);
7040
7041 if (!cVertexDivisor)
7042 pVertexDivisor = NULL; /* Be sure. */
7043
7044 PVMSVGA3DCONTEXT pContext;
7045 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
7046 AssertRCReturn(rc, rc);
7047
7048 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7049
7050 /* Check for pretransformed vertex declarations. */
7051 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
7052 {
7053 switch (pVertexDecl[iVertex].identity.usage)
7054 {
7055 case SVGA3D_DECLUSAGE_POSITIONT:
7056 Log(("ShaderSetPositionTransformed: (%d,%d)\n", pContext->state.RectViewPort.w, pContext->state.RectViewPort.h));
7057 RT_FALL_THRU();
7058 case SVGA3D_DECLUSAGE_POSITION:
7059 ShaderSetPositionTransformed(pContext->pShaderContext, pContext->state.RectViewPort.w,
7060 pContext->state.RectViewPort.h,
7061 pVertexDecl[iVertex].identity.usage == SVGA3D_DECLUSAGE_POSITIONT);
7062 break;
7063 default: /* Shut up MSC. */ break;
7064 }
7065 }
7066
7067 /* Flush any shader changes; after (!) checking the vertex declarations to deal with pre-transformed vertices. */
7068 if (pContext->pShaderContext)
7069 {
7070 uint32_t rtHeight = 0;
7071
7072 if (pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] != SVGA_ID_INVALID)
7073 {
7074 PVMSVGA3DSURFACE pRenderTarget;
7075 rc = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pRenderTarget);
7076 AssertRCReturn(rc, rc);
7077
7078 rtHeight = pRenderTarget->paMipmapLevels[0].mipmapSize.height;
7079 }
7080
7081 ShaderUpdateState(pContext->pShaderContext, rtHeight);
7082 }
7083
7084 /* Try to figure out if instancing is used.
7085 * Support simple instancing case with one set of indexed data and one set per-instance data.
7086 */
7087 uint32_t cInstances = 0;
7088 for (uint32_t iVertexDivisor = 0; iVertexDivisor < cVertexDivisor; ++iVertexDivisor)
7089 {
7090 if (pVertexDivisor[iVertexDivisor].indexedData)
7091 {
7092 if (cInstances == 0)
7093 cInstances = pVertexDivisor[iVertexDivisor].count;
7094 else
7095 Assert(cInstances == pVertexDivisor[iVertexDivisor].count);
7096 }
7097 else if (pVertexDivisor[iVertexDivisor].instanceData)
7098 {
7099 Assert(pVertexDivisor[iVertexDivisor].count == 1);
7100 }
7101 }
7102
7103 /* Process all vertex declarations. Each vertex buffer is represented by one stream. */
7104 iCurrentVertex = 0;
7105 while (iCurrentVertex < numVertexDecls)
7106 {
7107 uint32_t sidVertex = SVGA_ID_INVALID;
7108 uint32_t iVertex;
7109
7110 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
7111 {
7112 if ( sidVertex != SVGA_ID_INVALID
7113 && pVertexDecl[iVertex].array.surfaceId != sidVertex
7114 )
7115 break;
7116 sidVertex = pVertexDecl[iVertex].array.surfaceId;
7117 }
7118
7119 rc = vmsvga3dDrawPrimitivesProcessVertexDecls(pThisCC, pContext, iCurrentVertex, iVertex - iCurrentVertex,
7120 &pVertexDecl[iCurrentVertex], pVertexDivisor);
7121 AssertRCReturn(rc, rc);
7122
7123 iCurrentVertex = iVertex;
7124 }
7125
7126 /* Now draw the primitives. */
7127 for (unsigned iPrimitive = 0; iPrimitive < numRanges; iPrimitive++)
7128 {
7129 GLenum modeDraw;
7130 unsigned const sidIndex = pRange[iPrimitive].indexArray.surfaceId;
7131 PVMSVGA3DSURFACE pIndexSurface = NULL;
7132 unsigned cVertices;
7133
7134 Log(("Primitive %d: type %s\n", iPrimitive, vmsvga3dPrimitiveType2String(pRange[iPrimitive].primType)));
7135 rc = vmsvga3dPrimitiveType2OGL(pRange[iPrimitive].primType, &modeDraw, pRange[iPrimitive].primitiveCount, &cVertices);
7136 if (RT_FAILURE(rc))
7137 {
7138 AssertRC(rc);
7139 goto internal_error;
7140 }
7141
7142 if (sidIndex != SVGA3D_INVALID_ID)
7143 {
7144 AssertMsg(pRange[iPrimitive].indexWidth == sizeof(uint32_t) || pRange[iPrimitive].indexWidth == sizeof(uint16_t), ("Unsupported primitive width %d\n", pRange[iPrimitive].indexWidth));
7145
7146 rc = vmsvga3dSurfaceFromSid(pState, sidIndex, &pIndexSurface);
7147 if (RT_FAILURE(rc))
7148 {
7149 AssertRC(rc);
7150 goto internal_error;
7151 }
7152
7153 Log(("vmsvga3dDrawPrimitives: index surface sid=%u\n", sidIndex));
7154
7155 if (pIndexSurface->oglId.buffer == OPENGL_INVALID_ID)
7156 {
7157 Log(("vmsvga3dDrawPrimitives: create index buffer fDirty=%d size=%x bytes\n", pIndexSurface->fDirty, pIndexSurface->paMipmapLevels[0].cbSurface));
7158 pContext = &pState->SharedCtx;
7159 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7160
7161 pState->ext.glGenBuffers(1, &pIndexSurface->oglId.buffer);
7162 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7163 pIndexSurface->enmOGLResType = VMSVGA3D_OGLRESTYPE_BUFFER;
7164
7165 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
7166 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7167
7168 Assert(pIndexSurface->fDirty);
7169
7170 /** @todo rethink usage dynamic/static */
7171 pState->ext.glBufferData(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->paMipmapLevels[0].cbSurface, pIndexSurface->paMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
7172 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7173
7174 pIndexSurface->paMipmapLevels[0].fDirty = false;
7175 pIndexSurface->fDirty = false;
7176
7177 pIndexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
7178
7179 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, OPENGL_INVALID_ID);
7180 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7181
7182 pContext = pState->papContexts[cid];
7183 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7184 }
7185 Assert(pIndexSurface->fDirty == false);
7186
7187 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
7188 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7189 }
7190
7191 if (!pIndexSurface)
7192 {
7193 /* Render without an index buffer */
7194 Log(("DrawPrimitive %d cPrimitives=%d cVertices=%d index index bias=%d cInstances=%d\n", modeDraw, pRange[iPrimitive].primitiveCount, cVertices, pRange[iPrimitive].indexBias, cInstances));
7195 if (cInstances == 0)
7196 {
7197 glDrawArrays(modeDraw, pRange[iPrimitive].indexBias, cVertices);
7198 }
7199 else
7200 {
7201 pState->ext.glDrawArraysInstanced(modeDraw, pRange[iPrimitive].indexBias, cVertices, cInstances);
7202 }
7203 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7204 }
7205 else
7206 {
7207 Assert(pRange[iPrimitive].indexWidth == pRange[iPrimitive].indexArray.stride);
7208
7209 GLenum indexType;
7210 switch (pRange[iPrimitive].indexWidth)
7211 {
7212 case 1: indexType = GL_UNSIGNED_BYTE; break;
7213 case 2: indexType = GL_UNSIGNED_SHORT; break;
7214 default: AssertMsgFailed(("indexWidth %d\n", pRange[iPrimitive].indexWidth));
7215 RT_FALL_THROUGH();
7216 case 4: indexType = GL_UNSIGNED_INT; break;
7217 }
7218
7219 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));
7220 if (cInstances == 0)
7221 {
7222 /* Render with an index buffer */
7223 if (pRange[iPrimitive].indexBias == 0)
7224 glDrawElements(modeDraw,
7225 cVertices,
7226 indexType,
7227 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset); /* byte offset in indices buffer */
7228 else
7229 pState->ext.glDrawElementsBaseVertex(modeDraw,
7230 cVertices,
7231 indexType,
7232 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
7233 pRange[iPrimitive].indexBias); /* basevertex */
7234 }
7235 else
7236 {
7237 /* Render with an index buffer */
7238 if (pRange[iPrimitive].indexBias == 0)
7239 pState->ext.glDrawElementsInstanced(modeDraw,
7240 cVertices,
7241 indexType,
7242 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
7243 cInstances);
7244 else
7245 pState->ext.glDrawElementsInstancedBaseVertex(modeDraw,
7246 cVertices,
7247 indexType,
7248 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
7249 cInstances,
7250 pRange[iPrimitive].indexBias); /* basevertex */
7251 }
7252 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7253
7254 /* Unbind the index buffer after usage. */
7255 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
7256 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7257 }
7258 }
7259
7260internal_error:
7261
7262 /* Deactivate the vertex declarations. */
7263 iCurrentVertex = 0;
7264 while (iCurrentVertex < numVertexDecls)
7265 {
7266 uint32_t sidVertex = SVGA_ID_INVALID;
7267 uint32_t iVertex;
7268
7269 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
7270 {
7271 if ( sidVertex != SVGA_ID_INVALID
7272 && pVertexDecl[iVertex].array.surfaceId != sidVertex
7273 )
7274 break;
7275 sidVertex = pVertexDecl[iVertex].array.surfaceId;
7276 }
7277
7278 rc = vmsvga3dDrawPrimitivesCleanupVertexDecls(pThisCC, pContext, iCurrentVertex,
7279 iVertex - iCurrentVertex, &pVertexDecl[iCurrentVertex]);
7280 AssertRCReturn(rc, rc);
7281
7282 iCurrentVertex = iVertex;
7283 }
7284
7285#ifdef DEBUG
7286 /* Check whether 'activeTexture' on texture unit 'i' matches what we expect. */
7287 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTextures); ++i)
7288 {
7289 if (pContext->aSidActiveTextures[i] != SVGA3D_INVALID_ID)
7290 {
7291 PVMSVGA3DSURFACE pTexture;
7292 int rc2 = vmsvga3dSurfaceFromSid(pState, pContext->aSidActiveTextures[i], &pTexture);
7293 AssertContinue(RT_SUCCESS(rc2));
7294
7295 GLint activeTextureUnit = 0;
7296 glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
7297 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7298
7299 pState->ext.glActiveTexture(GL_TEXTURE0 + i);
7300 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7301
7302 GLint activeTexture = 0;
7303 glGetIntegerv(pTexture->bindingGL, &activeTexture);
7304 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7305
7306 pState->ext.glActiveTexture(activeTextureUnit);
7307 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
7308
7309 AssertMsg(pTexture->oglId.texture == (GLuint)activeTexture,
7310 ("%d vs %d unit %d (active unit %d) sid=%u\n", pTexture->oglId.texture, activeTexture, i,
7311 activeTextureUnit - GL_TEXTURE0, pContext->aSidActiveTextures[i]));
7312 }
7313 }
7314#endif
7315
7316#if 0
7317 /* Dump render target to a bitmap. */
7318 if (pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] != SVGA3D_INVALID_ID)
7319 {
7320 vmsvga3dUpdateHeapBuffersForSurfaces(pThisCC, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0]);
7321 PVMSVGA3DSURFACE pSurface;
7322 int rc2 = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pSurface);
7323 if (RT_SUCCESS(rc2))
7324 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmpgl", "rt", "-post");
7325# if 0
7326 /* Stage 0 texture. */
7327 if (pContext->aSidActiveTextures[0] != SVGA3D_INVALID_ID)
7328 {
7329 vmsvga3dUpdateHeapBuffersForSurfaces(pThisCC, pContext->aSidActiveTextures[0]);
7330 rc2 = vmsvga3dSurfaceFromSid(pState, pContext->aSidActiveTextures[0], &pSurface);
7331 if (RT_SUCCESS(rc2))
7332 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmpgl", "rt", "-post-tx");
7333 }
7334# endif
7335 }
7336#endif
7337
7338 return rc;
7339}
7340
7341
7342int vmsvga3dShaderDefine(PVGASTATECC pThisCC, uint32_t cid, uint32_t shid, SVGA3dShaderType type, uint32_t cbData, uint32_t *pShaderData)
7343{
7344 PVMSVGA3DSHADER pShader;
7345 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
7346 AssertReturn(pState, VERR_NO_MEMORY);
7347
7348 Log(("vmsvga3dShaderDefine cid=%u shid=%d type=%s cbData=0x%x\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cbData));
7349
7350 PVMSVGA3DCONTEXT pContext;
7351 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
7352 AssertRCReturn(rc, rc);
7353
7354 AssertReturn(shid < SVGA3D_MAX_SHADER_IDS, VERR_INVALID_PARAMETER);
7355
7356 rc = vmsvga3dShaderParse(type, cbData, pShaderData);
7357 if (RT_FAILURE(rc))
7358 {
7359 AssertRC(rc);
7360 vmsvga3dShaderLogRel("Failed to parse", type, cbData, pShaderData);
7361 return rc;
7362 }
7363
7364 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7365
7366 if (type == SVGA3D_SHADERTYPE_VS)
7367 {
7368 if (shid >= pContext->cVertexShaders)
7369 {
7370 void *pvNew = RTMemRealloc(pContext->paVertexShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
7371 AssertReturn(pvNew, VERR_NO_MEMORY);
7372 pContext->paVertexShader = (PVMSVGA3DSHADER)pvNew;
7373 memset(&pContext->paVertexShader[pContext->cVertexShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cVertexShaders));
7374 for (uint32_t i = pContext->cVertexShaders; i < shid + 1; i++)
7375 pContext->paVertexShader[i].id = SVGA3D_INVALID_ID;
7376 pContext->cVertexShaders = shid + 1;
7377 }
7378 /* If one already exists with this id, then destroy it now. */
7379 if (pContext->paVertexShader[shid].id != SVGA3D_INVALID_ID)
7380 vmsvga3dShaderDestroy(pThisCC, cid, shid, pContext->paVertexShader[shid].type);
7381
7382 pShader = &pContext->paVertexShader[shid];
7383 }
7384 else
7385 {
7386 Assert(type == SVGA3D_SHADERTYPE_PS);
7387 if (shid >= pContext->cPixelShaders)
7388 {
7389 void *pvNew = RTMemRealloc(pContext->paPixelShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
7390 AssertReturn(pvNew, VERR_NO_MEMORY);
7391 pContext->paPixelShader = (PVMSVGA3DSHADER)pvNew;
7392 memset(&pContext->paPixelShader[pContext->cPixelShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cPixelShaders));
7393 for (uint32_t i = pContext->cPixelShaders; i < shid + 1; i++)
7394 pContext->paPixelShader[i].id = SVGA3D_INVALID_ID;
7395 pContext->cPixelShaders = shid + 1;
7396 }
7397 /* If one already exists with this id, then destroy it now. */
7398 if (pContext->paPixelShader[shid].id != SVGA3D_INVALID_ID)
7399 vmsvga3dShaderDestroy(pThisCC, cid, shid, pContext->paPixelShader[shid].type);
7400
7401 pShader = &pContext->paPixelShader[shid];
7402 }
7403
7404 memset(pShader, 0, sizeof(*pShader));
7405 pShader->id = shid;
7406 pShader->cid = cid;
7407 pShader->type = type;
7408 pShader->cbData = cbData;
7409 pShader->pShaderProgram = RTMemAllocZ(cbData);
7410 AssertReturn(pShader->pShaderProgram, VERR_NO_MEMORY);
7411 memcpy(pShader->pShaderProgram, pShaderData, cbData);
7412
7413#ifdef DUMP_SHADER_DISASSEMBLY
7414 LPD3DXBUFFER pDisassembly;
7415 HRESULT hr = D3DXDisassembleShader((const DWORD *)pShaderData, FALSE, NULL, &pDisassembly);
7416 if (hr == D3D_OK)
7417 {
7418 Log(("Shader disassembly:\n%s\n", pDisassembly->GetBufferPointer()));
7419 pDisassembly->Release();
7420 }
7421#endif
7422
7423 switch (type)
7424 {
7425 case SVGA3D_SHADERTYPE_VS:
7426 rc = ShaderCreateVertexShader(pContext->pShaderContext, (const uint32_t *)pShaderData, cbData, &pShader->u.pVertexShader);
7427 AssertRC(rc);
7428 break;
7429
7430 case SVGA3D_SHADERTYPE_PS:
7431 rc = ShaderCreatePixelShader(pContext->pShaderContext, (const uint32_t *)pShaderData, cbData, &pShader->u.pPixelShader);
7432 AssertRC(rc);
7433 break;
7434
7435 default:
7436 AssertFailedReturn(VERR_INVALID_PARAMETER);
7437 }
7438 if (rc != VINF_SUCCESS)
7439 {
7440 vmsvga3dShaderLogRel("Failed to create", type, cbData, pShaderData);
7441
7442 RTMemFree(pShader->pShaderProgram);
7443 memset(pShader, 0, sizeof(*pShader));
7444 pShader->id = SVGA3D_INVALID_ID;
7445 }
7446
7447 return rc;
7448}
7449
7450int vmsvga3dShaderDestroy(PVGASTATECC pThisCC, uint32_t cid, uint32_t shid, SVGA3dShaderType type)
7451{
7452 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
7453 AssertReturn(pState, VERR_NO_MEMORY);
7454 PVMSVGA3DSHADER pShader = NULL;
7455
7456 Log(("vmsvga3dShaderDestroy cid=%u shid=%d type=%s\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL"));
7457
7458 PVMSVGA3DCONTEXT pContext;
7459 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
7460 AssertRCReturn(rc, rc);
7461
7462 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7463
7464 if (type == SVGA3D_SHADERTYPE_VS)
7465 {
7466 if ( shid < pContext->cVertexShaders
7467 && pContext->paVertexShader[shid].id == shid)
7468 {
7469 pShader = &pContext->paVertexShader[shid];
7470 rc = ShaderDestroyVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
7471 AssertRC(rc);
7472 }
7473 }
7474 else
7475 {
7476 Assert(type == SVGA3D_SHADERTYPE_PS);
7477 if ( shid < pContext->cPixelShaders
7478 && pContext->paPixelShader[shid].id == shid)
7479 {
7480 pShader = &pContext->paPixelShader[shid];
7481 rc = ShaderDestroyPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
7482 AssertRC(rc);
7483 }
7484 }
7485
7486 if (pShader)
7487 {
7488 if (pShader->pShaderProgram)
7489 RTMemFree(pShader->pShaderProgram);
7490 memset(pShader, 0, sizeof(*pShader));
7491 pShader->id = SVGA3D_INVALID_ID;
7492 }
7493 else
7494 AssertFailedReturn(VERR_INVALID_PARAMETER);
7495
7496 return VINF_SUCCESS;
7497}
7498
7499int vmsvga3dShaderSet(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t cid, SVGA3dShaderType type, uint32_t shid)
7500{
7501 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
7502 AssertReturn(pState, VERR_NO_MEMORY);
7503 int rc;
7504
7505 Log(("vmsvga3dShaderSet cid=%u type=%s shid=%d\n", cid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", shid));
7506
7507 if (!pContext)
7508 {
7509 rc = vmsvga3dContextFromCid(pState, cid, &pContext);
7510 AssertRCReturn(rc, rc);
7511 }
7512
7513 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7514
7515 if (type == SVGA3D_SHADERTYPE_VS)
7516 {
7517 /* Save for vm state save/restore. */
7518 pContext->state.shidVertex = shid;
7519 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VERTEXSHADER;
7520
7521 if ( shid < pContext->cVertexShaders
7522 && pContext->paVertexShader[shid].id == shid)
7523 {
7524 PVMSVGA3DSHADER pShader = &pContext->paVertexShader[shid];
7525 Assert(type == pShader->type);
7526
7527 rc = ShaderSetVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
7528 AssertRCReturn(rc, rc);
7529 }
7530 else
7531 if (shid == SVGA_ID_INVALID)
7532 {
7533 /* Unselect shader. */
7534 rc = ShaderSetVertexShader(pContext->pShaderContext, NULL);
7535 AssertRCReturn(rc, rc);
7536 }
7537 else
7538 AssertFailedReturn(VERR_INVALID_PARAMETER);
7539 }
7540 else
7541 {
7542 /* Save for vm state save/restore. */
7543 pContext->state.shidPixel = shid;
7544 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_PIXELSHADER;
7545
7546 Assert(type == SVGA3D_SHADERTYPE_PS);
7547 if ( shid < pContext->cPixelShaders
7548 && pContext->paPixelShader[shid].id == shid)
7549 {
7550 PVMSVGA3DSHADER pShader = &pContext->paPixelShader[shid];
7551 Assert(type == pShader->type);
7552
7553 rc = ShaderSetPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
7554 AssertRCReturn(rc, rc);
7555 }
7556 else
7557 if (shid == SVGA_ID_INVALID)
7558 {
7559 /* Unselect shader. */
7560 rc = ShaderSetPixelShader(pContext->pShaderContext, NULL);
7561 AssertRCReturn(rc, rc);
7562 }
7563 else
7564 AssertFailedReturn(VERR_INVALID_PARAMETER);
7565 }
7566
7567 return VINF_SUCCESS;
7568}
7569
7570int vmsvga3dShaderSetConst(PVGASTATECC pThisCC, uint32_t cid, uint32_t reg, SVGA3dShaderType type, SVGA3dShaderConstType ctype, uint32_t cRegisters, uint32_t *pValues)
7571{
7572 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
7573 AssertReturn(pState, VERR_NO_MEMORY);
7574
7575 Log(("vmsvga3dShaderSetConst cid=%u reg=%x type=%s cregs=%d ctype=%x\n", cid, reg, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cRegisters, ctype));
7576
7577 PVMSVGA3DCONTEXT pContext;
7578 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
7579 AssertRCReturn(rc, rc);
7580
7581 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7582
7583 for (uint32_t i = 0; i < cRegisters; i++)
7584 {
7585#ifdef LOG_ENABLED
7586 switch (ctype)
7587 {
7588 case SVGA3D_CONST_TYPE_FLOAT:
7589 {
7590 float *pValuesF = (float *)pValues;
7591 Log(("ConstantF %d: value=" FLOAT_FMT_STR ", " FLOAT_FMT_STR ", " FLOAT_FMT_STR ", " FLOAT_FMT_STR "\n",
7592 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])));
7593 break;
7594 }
7595
7596 case SVGA3D_CONST_TYPE_INT:
7597 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]));
7598 break;
7599
7600 case SVGA3D_CONST_TYPE_BOOL:
7601 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]));
7602 break;
7603
7604 default:
7605 AssertFailedReturn(VERR_INVALID_PARAMETER);
7606 }
7607#endif
7608 vmsvga3dSaveShaderConst(pContext, reg + i, type, ctype, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]);
7609 }
7610
7611 switch (type)
7612 {
7613 case SVGA3D_SHADERTYPE_VS:
7614 switch (ctype)
7615 {
7616 case SVGA3D_CONST_TYPE_FLOAT:
7617 rc = ShaderSetVertexShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
7618 break;
7619
7620 case SVGA3D_CONST_TYPE_INT:
7621 rc = ShaderSetVertexShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
7622 break;
7623
7624 case SVGA3D_CONST_TYPE_BOOL:
7625 rc = ShaderSetVertexShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
7626 break;
7627
7628 default:
7629 AssertFailedReturn(VERR_INVALID_PARAMETER);
7630 }
7631 AssertRCReturn(rc, rc);
7632 break;
7633
7634 case SVGA3D_SHADERTYPE_PS:
7635 switch (ctype)
7636 {
7637 case SVGA3D_CONST_TYPE_FLOAT:
7638 rc = ShaderSetPixelShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
7639 break;
7640
7641 case SVGA3D_CONST_TYPE_INT:
7642 rc = ShaderSetPixelShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
7643 break;
7644
7645 case SVGA3D_CONST_TYPE_BOOL:
7646 rc = ShaderSetPixelShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
7647 break;
7648
7649 default:
7650 AssertFailedReturn(VERR_INVALID_PARAMETER);
7651 }
7652 AssertRCReturn(rc, rc);
7653 break;
7654
7655 default:
7656 AssertFailedReturn(VERR_INVALID_PARAMETER);
7657 }
7658
7659 return VINF_SUCCESS;
7660}
7661
7662int vmsvga3dOcclusionQueryCreate(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
7663{
7664 AssertReturn(pState->ext.glGenQueries, VERR_NOT_SUPPORTED);
7665 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7666
7667 GLuint idQuery = 0;
7668 pState->ext.glGenQueries(1, &idQuery);
7669 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7670 AssertReturn(idQuery, VERR_INTERNAL_ERROR);
7671 pContext->occlusion.idQuery = idQuery;
7672 return VINF_SUCCESS;
7673}
7674
7675int vmsvga3dOcclusionQueryDelete(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
7676{
7677 AssertReturn(pState->ext.glDeleteQueries, VERR_NOT_SUPPORTED);
7678 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7679
7680 if (pContext->occlusion.idQuery)
7681 {
7682 pState->ext.glDeleteQueries(1, &pContext->occlusion.idQuery);
7683 }
7684 return VINF_SUCCESS;
7685}
7686
7687int vmsvga3dOcclusionQueryBegin(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
7688{
7689 AssertReturn(pState->ext.glBeginQuery, VERR_NOT_SUPPORTED);
7690 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7691
7692 pState->ext.glBeginQuery(GL_ANY_SAMPLES_PASSED, pContext->occlusion.idQuery);
7693 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7694 return VINF_SUCCESS;
7695}
7696
7697int vmsvga3dOcclusionQueryEnd(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
7698{
7699 AssertReturn(pState->ext.glEndQuery, VERR_NOT_SUPPORTED);
7700 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7701
7702 pState->ext.glEndQuery(GL_ANY_SAMPLES_PASSED);
7703 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7704 return VINF_SUCCESS;
7705}
7706
7707int vmsvga3dOcclusionQueryGetData(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t *pu32Pixels)
7708{
7709 AssertReturn(pState->ext.glGetQueryObjectuiv, VERR_NOT_SUPPORTED);
7710 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
7711
7712 GLuint pixels = 0;
7713 pState->ext.glGetQueryObjectuiv(pContext->occlusion.idQuery, GL_QUERY_RESULT, &pixels);
7714 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
7715
7716 *pu32Pixels = (uint32_t)pixels;
7717 return VINF_SUCCESS;
7718}
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