VirtualBox

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

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

Devices/Graphics: VMSVGA: removed sidRenderTarget, because it does not work if the guest uses multiple render targets and render targets are already tracked anyway.

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