VirtualBox

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

Last change on this file since 70775 was 69904, checked in by vboxsync, 7 years ago

Devices/Graphics: VMSVGA: take screen offset into account; saved state for occlusion query; increase saved state version.

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