VirtualBox

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

Last change on this file since 68253 was 68083, checked in by vboxsync, 7 years ago

Devices/Graphics/DevVGA-SVGA3d-ogl.cpp: detect shader model from GL_SHADING_LANGUAGE_VERSION

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