VirtualBox

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

Last change on this file since 82303 was 82165, checked in by vboxsync, 5 years ago

Devices/Graphics: OpenGL backend: fixes for compressed textures.

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