VirtualBox

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

Last change on this file since 69304 was 69294, checked in by vboxsync, 7 years ago

Devices/Graphics: scm cleanups

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