VirtualBox

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

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

DevVGA-SVGA3d: dynamic loading of OpenGL: log failures

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