VirtualBox

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

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

DevVGA-SVGA3d-ogl.cpp: comment.

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