VirtualBox

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

Last change on this file since 63427 was 63211, checked in by vboxsync, 8 years ago

Devices: warnings (gcc)

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