VirtualBox

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

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

DevVGA-SVGA3d: Use PDMDevHlpVMSetError and VERR_VGA_GL_SYMBOL_NOT_FOUND to report errors loading open gl symbol resolving errors. Tested on windows. Left todo (mainly to myself) about better libGL.so/opengl32.dll/OpenGL.dylib loading error reporting, since that'll show up as failure to resolve 'glAlphaFunc' now.

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