VirtualBox

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

Last change on this file since 57444 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

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