VirtualBox

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

Last change on this file since 73363 was 73363, checked in by vboxsync, 6 years ago

DevVGA-SVGA3d-ogl.cpp: cleaned up loading OpenGL functions.

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