VirtualBox

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

Last change on this file since 65532 was 65381, checked in by vboxsync, 8 years ago

bugref:8282: Additions/linux: submit DRM driver to the Linux kernel: move all graphics device-related header files to a separate sub-directory and add that to the include path where they are needed. The intention is too be able to remove the VBox/ include folder in the DRM driver package.

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