VirtualBox

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

Last change on this file since 57583 was 57519, checked in by vboxsync, 9 years ago

SVGA3d/win: Fixed the scrolling issues with the SVGA_3D_CMD_PRESENT implementation (much simpler than OpenGL).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 254.9 KB
Line 
1/* $Id: DevVGA-SVGA3d-ogl.cpp 57519 2015-08-24 23:33:46Z 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 destroy SVGA3D_SURFACE_CUBEMAP */
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 DMA SVGA3D_SURFACE_HINT_DEPTHSTENCIL */
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 Log(("vmsvga3dCommandPresent: sid=%x cRects=%d\n", sid, cRects));
2643 for (uint32_t i=0; i < cRects; i++)
2644 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));
2645
2646 pContext = &pState->SharedCtx;
2647 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
2648 cid = pContext->id;
2649 VMSVGA3D_CLEAR_GL_ERRORS();
2650
2651#if 0 /* Can't make sense of this. SVGA3dCopyRect doesn't allow scaling. non-blit-cube path change to not use it. */
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 (" FLOAT_FMT_STR ", " FLOAT_FMT_STR ")\n",
2663 pSurface->pMipmapLevels[0].size.width, pThis->svga.uWidth,
2664 pSurface->pMipmapLevels[0].size.height, pThis->svga.uHeight,
2665 FLOAT_FMT_ARGS(xMultiplier), FLOAT_FMT_ARGS(yMultiplier) ));
2666
2667 srcViewPort.x = (uint32_t)((float)pThis->svga.viewport.x * xMultiplier);
2668 srcViewPort.y = (uint32_t)((float)pThis->svga.viewport.y * yMultiplier);
2669 srcViewPort.cx = (uint32_t)((float)pThis->svga.viewport.cx * xMultiplier);
2670 srcViewPort.cy = (uint32_t)((float)pThis->svga.viewport.cy * yMultiplier);
2671 }
2672 else
2673 {
2674 srcViewPort.x = pThis->svga.viewport.x;
2675 srcViewPort.y = pThis->svga.viewport.y;
2676 srcViewPort.cx = pThis->svga.viewport.cx;
2677 srcViewPort.cy = pThis->svga.viewport.cy;
2678 }
2679 RTRECT SrcViewPortRect;
2680 SrcViewPortRect.xLeft = srcViewPort.x;
2681 SrcViewPortRect.xRight = srcViewPort.x + srcViewPort.cx;
2682 SrcViewPortRect.yBottom = srcViewPort.y;
2683 SrcViewPortRect.yTop = srcViewPort.y + srcViewPort.cy;
2684#endif
2685
2686
2687#if 0//ndef RT_OS_DARWIN /* blit-cube fails in this path... */
2688 /*
2689 * Note! this path is slightly faster than the glBlitFrameBuffer path below.
2690 */
2691 SVGA3dCopyRect rect;
2692 uint32_t oldVShader, oldPShader;
2693 GLint oldTextureId;
2694
2695 if (cRects == 0)
2696 {
2697 rect.x = rect.y = rect.srcx = rect.srcy = 0;
2698 rect.w = pSurface->pMipmapLevels[0].size.width;
2699 rect.h = pSurface->pMipmapLevels[0].size.height;
2700 pRect = &rect;
2701 cRects = 1;
2702 }
2703
2704 //glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
2705
2706#if 0
2707 glDisable(GL_CULL_FACE);
2708 glDisable(GL_BLEND);
2709 glDisable(GL_ALPHA_TEST);
2710 glDisable(GL_SCISSOR_TEST);
2711 glDisable(GL_STENCIL_TEST);
2712 glEnable(GL_DEPTH_TEST);
2713 glDepthFunc(GL_ALWAYS);
2714 glDepthMask(GL_TRUE);
2715 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2716 glViewport(0, 0, pSurface->pMipmapLevels[0].size.width, pSurface->pMipmapLevels[0].size.height);
2717#endif
2718
2719 VMSVGA3D_ASSERT_GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTextureId), pState, pContext);
2720
2721 oldVShader = pContext->state.shidVertex;
2722 oldPShader = pContext->state.shidPixel;
2723 vmsvga3dShaderSet(pThis, pContext, cid, SVGA3D_SHADERTYPE_VS, SVGA_ID_INVALID);
2724 vmsvga3dShaderSet(pThis, pContext, cid, SVGA3D_SHADERTYPE_PS, SVGA_ID_INVALID);
2725
2726 /* Flush shader changes. */
2727 if (pContext->pShaderContext)
2728 ShaderUpdateState(pContext->pShaderContext, 0);
2729
2730 /* Activate the read and draw framebuffer objects. */
2731 VMSVGA3D_ASSERT_GL_CALL(pState->ext.glBindFramebuffer(GL_READ_FRAMEBUFFER, pContext->idReadFramebuffer), pState, pContext);
2732 VMSVGA3D_ASSERT_GL_CALL(pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0 /* back buffer */), pState, pContext);
2733
2734 VMSVGA3D_ASSERT_GL_CALL(pState->ext.glActiveTexture(GL_TEXTURE0), pState, pContext);
2735 VMSVGA3D_ASSERT_GL_CALL(glEnable(GL_TEXTURE_2D), pState, pContext);;
2736 VMSVGA3D_ASSERT_GL_CALL(glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture), pState, pContext);
2737
2738 VMSVGA3D_ASSERT_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR), pState, pContext);
2739 VMSVGA3D_ASSERT_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR), pState, pContext);;
2740
2741#if 0
2742 VMSVGA3D_ASSERT_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP), pState, pContext);;
2743 VMSVGA3D_ASSERT_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP), pState, pContext);;
2744#endif
2745
2746 /* Reset the transformation matrices. */
2747 VMSVGA3D_ASSERT_GL_CALL(glMatrixMode(GL_MODELVIEW), pState, pContext);
2748 VMSVGA3D_ASSERT_GL_CALL(glPushMatrix(), pState, pContext);
2749 VMSVGA3D_ASSERT_GL_CALL(glLoadIdentity(), pState, pContext);
2750 VMSVGA3D_ASSERT_GL_CALL(glMatrixMode(GL_PROJECTION), pState, pContext);
2751 VMSVGA3D_ASSERT_GL_CALL(glPushMatrix(), pState, pContext);
2752 VMSVGA3D_ASSERT_GL_CALL(glLoadIdentity(), pState, pContext);
2753 VMSVGA3D_ASSERT_GL_CALL(glScalef(1.0f, -1.0f, 1.0f), pState, pContext);
2754 VMSVGA3D_ASSERT_GL_CALL(glOrtho(0, pThis->svga.uWidth, pThis->svga.uHeight, 0, 0.0, -1.0), pState, pContext);
2755
2756 for (uint32_t i = 0; i < cRects; i++)
2757 {
2758 float left, right, top, bottom; /* Texture coordinates */
2759 int vertexLeft, vertexRight, vertexTop, vertexBottom;
2760
2761 pRect[i].srcx = RT_MAX(pRect[i].srcx, (uint32_t)RT_MAX(srcViewPort.x, 0));
2762 pRect[i].srcy = RT_MAX(pRect[i].srcy, (uint32_t)RT_MAX(srcViewPort.y, 0));
2763 pRect[i].x = RT_MAX(pRect[i].x, pThis->svga.viewport.x) - pThis->svga.viewport.x;
2764 pRect[i].y = RT_MAX(pRect[i].y, pThis->svga.viewport.y) - pThis->svga.viewport.y;
2765 pRect[i].w = pThis->svga.viewport.cx;
2766 pRect[i].h = pThis->svga.viewport.cy;
2767
2768 if ( pRect[i].x + pRect[i].w <= pThis->svga.viewport.x
2769 || pThis->svga.viewport.x + pThis->svga.viewport.cx <= pRect[i].x
2770 || pRect[i].y + pRect[i].h <= pThis->svga.viewport.y
2771 || pThis->svga.viewport.y + pThis->svga.viewport.cy <= pRect[i].y)
2772 {
2773 /* Intersection is empty; skip */
2774 continue;
2775 }
2776
2777 left = pRect[i].srcx;
2778 right = pRect[i].srcx + pRect[i].w;
2779 top = pRect[i].srcy + pRect[i].h;
2780 bottom = pRect[i].srcy;
2781
2782 left /= pSurface->pMipmapLevels[0].size.width;
2783 right /= pSurface->pMipmapLevels[0].size.width;
2784 top /= pSurface->pMipmapLevels[0].size.height;
2785 bottom /= pSurface->pMipmapLevels[0].size.height;
2786
2787 vertexLeft = pRect[i].x;
2788 vertexRight = pRect[i].x + pRect[i].w;
2789 vertexTop = ((uint32_t)pThis->svga.uHeight >= pRect[i].y + pRect[i].h) ? pThis->svga.uHeight - pRect[i].y - pRect[i].h : 0;
2790 vertexBottom = pThis->svga.uHeight - pRect[i].y;
2791
2792 Log(("view port (%d,%d)(%d,%d)\n", srcViewPort.x, srcViewPort.y, srcViewPort.cx, srcViewPort.cy));
2793 Log(("vertex (%d,%d) (%d,%d) (%d,%d) (%d,%d)\n", vertexLeft, vertexBottom, vertexLeft, vertexTop, vertexRight, vertexTop, vertexRight, vertexBottom));
2794 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)));
2795
2796 glBegin(GL_QUADS);
2797
2798 /* bottom left */
2799 glTexCoord2f(left, bottom);
2800 glVertex2i(vertexLeft, vertexBottom);
2801
2802 /* top left */
2803 glTexCoord2f(left, top);
2804 glVertex2i(vertexLeft, vertexTop);
2805
2806 /* top right */
2807 glTexCoord2f(right, top);
2808 glVertex2i(vertexRight, vertexTop);
2809
2810 /* bottom right */
2811 glTexCoord2f(right, bottom);
2812 glVertex2i(vertexRight, vertexBottom);
2813
2814 VMSVGA3D_ASSERT_GL_CALL(glEnd(), pState, pContext);
2815 }
2816
2817 /* Restore old settings. */
2818 VMSVGA3D_ASSERT_GL_CALL(glMatrixMode(GL_PROJECTION), pState, pContext);
2819 VMSVGA3D_ASSERT_GL_CALL(glPopMatrix(), pState, pContext);
2820 VMSVGA3D_ASSERT_GL_CALL(glMatrixMode(GL_MODELVIEW), pState, pContext);
2821 VMSVGA3D_ASSERT_GL_CALL(glPopMatrix(), pState, pContext);
2822
2823 //VMSVGA3D_ASSERT_GL_CALL(glPopAttrib(), pState, pContext);
2824
2825 VMSVGA3D_ASSERT_GL_CALL(glBindTexture(GL_TEXTURE_2D, oldTextureId), pState, pContext);
2826 vmsvga3dShaderSet(pThis, pContext, cid, SVGA3D_SHADERTYPE_VS, oldVShader);
2827 vmsvga3dShaderSet(pThis, pContext, cid, SVGA3D_SHADERTYPE_PS, oldPShader);
2828
2829#else
2830 /*
2831 * glBlitFramebuffer variant.
2832 */
2833 /* Activate the read and draw framebuffer objects. */
2834 pState->ext.glBindFramebuffer(GL_READ_FRAMEBUFFER, pContext->idReadFramebuffer);
2835 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2836 pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0 /* back buffer */);
2837 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2838
2839 /* Bind the source objects to the right place. */
2840 pState->ext.glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pSurface->oglId.texture, 0 /* level 0 */);
2841 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
2842
2843
2844 /* Read the destination viewport specs in one go to try avoid some unnecessary update races. */
2845 VMSVGAVIEWPORT const DstViewport = pThis->svga.viewport;
2846 ASMCompilerBarrier(); /* paranoia */
2847 Assert(DstViewport.yHighWC >= DstViewport.yLowWC);
2848
2849 /* If there are no recangles specified, just grab a screenful. */
2850 SVGA3dCopyRect DummyRect;
2851 if (cRects != 0)
2852 { /* likely */ }
2853 else
2854 {
2855 /** @todo Find the usecase for this or check what the original device does.
2856 * The original code was doing some scaling based on the surface
2857 * size... */
2858# ifdef DEBUG_bird
2859 AssertMsgFailed(("No rects to present. Who is doing that and what do they actually expect?\n"));
2860# endif
2861 DummyRect.x = DummyRect.srcx = 0;
2862 DummyRect.y = DummyRect.srcy = 0;
2863 DummyRect.w = pThis->svga.uWidth;
2864 DummyRect.h = pThis->svga.uHeight;
2865 cRects = 1;
2866 pRect = &DummyRect;
2867 }
2868
2869 /*
2870 * Blit the surface rectangle(s) to the back buffer.
2871 */
2872 uint32_t const cxSurface = pSurface->pMipmapLevels[0].size.width;
2873 uint32_t const cySurface = pSurface->pMipmapLevels[0].size.height;
2874 for (uint32_t i = 0; i < cRects; i++)
2875 {
2876 SVGA3dCopyRect ClippedRect = pRect[i];
2877
2878 /*
2879 * Do some sanity checking and limit width and height, all so we
2880 * don't need to think about wrap-arounds below.
2881 */
2882 if (RT_LIKELY( ClippedRect.w
2883 && ClippedRect.x < VMSVGA_MAX_X
2884 && ClippedRect.srcx < VMSVGA_MAX_X
2885 && ClippedRect.h
2886 && ClippedRect.y < VMSVGA_MAX_Y
2887 && ClippedRect.srcy < VMSVGA_MAX_Y
2888 ))
2889 { /* likely */ }
2890 else
2891 continue;
2892
2893 if (RT_LIKELY(ClippedRect.w < VMSVGA_MAX_Y))
2894 { /* likely */ }
2895 else
2896 ClippedRect.w = VMSVGA_MAX_Y;
2897 if (RT_LIKELY(ClippedRect.w < VMSVGA_MAX_Y))
2898 { /* likely */ }
2899 else
2900 ClippedRect.w = VMSVGA_MAX_Y;
2901
2902
2903 /*
2904 * Source surface clipping (paranoia). Straight forward.
2905 */
2906 if (RT_LIKELY(ClippedRect.srcx < cxSurface))
2907 { /* likely */ }
2908 else
2909 continue;
2910 if (RT_LIKELY(ClippedRect.srcx + ClippedRect.w <= cxSurface))
2911 { /* likely */ }
2912 else
2913 {
2914 AssertFailed(); /* remove if annoying. */
2915 ClippedRect.w = cxSurface - ClippedRect.srcx;
2916 }
2917
2918 if (RT_LIKELY(ClippedRect.srcy < cySurface))
2919 { /* likely */ }
2920 else
2921 continue;
2922 if (RT_LIKELY(ClippedRect.srcy + ClippedRect.h <= cySurface))
2923 { /* likely */ }
2924 else
2925 {
2926 AssertFailed(); /* remove if annoying. */
2927 ClippedRect.h = cySurface - ClippedRect.srcy;
2928 }
2929
2930 /*
2931 * Destination viewport clipping - real PITA.
2932 *
2933 * We have to take the following into account here:
2934 * - The source image is Y inverted.
2935 * - The destination framebuffer is in world and not window coordinates,
2936 * just like the source surface. This means working in the first quadrant.
2937 * - The viewport is in window coordinate, that is fourth quadrant and
2938 * negated Y values.
2939 * - The destination framebuffer is not scrolled, so we have to blit
2940 * what's visible into the top of the framebuffer.
2941 *
2942 *
2943 * To illustrate:
2944 *
2945 * source destination 0123456789
2946 * 8 ^---------- 8 ^---------- 0 ----------->
2947 * 7 | | 7 | | 1 | |
2948 * 6 | | 6 | ******* | 2 | ******* |
2949 * 5 | *** | 5 | * | 3 | * |
2950 * 4 | * | => 4 | * | => 4 | * |
2951 * 3 | * | 3 | *** | 5 | *** |
2952 * 2 | ******* | 2 | | 6 | |
2953 * 1 | | 1 | | 7 | |
2954 * 0 -----------> 0 -----------> 8 v----------
2955 * 0123456789 0123456789 Destination window
2956 *
2957 * From the above, it follows that a destination viewport given in
2958 * window coordinates matches the source exactly when srcy = srcx = 0.
2959 *
2960 * Example (Y only):
2961 * ySrc = 0
2962 * yDst = 0
2963 * cyCopy = 9
2964 * cyScreen = cyCopy
2965 * cySurface >= cyCopy
2966 * yViewport = 5
2967 * cyViewport = 2 (i.e. '| *** |'
2968 * '| |' )
2969 * yWCViewportHi = cxScreen - yViewport = 9 - 5 = 4
2970 * yWCViewportLow = cxScreen - yViewport - cyViewport = 4 - 2 = 2
2971 *
2972 * We can see from the illustration that the final result should be:
2973 * SrcRect = (0,7) (11, 5) (cy=2 from y=5)
2974 * DstRect = (0,2) (11, 4)
2975 *
2976 * Let's postpone the switching of SrcRect.yBottom/yTop to make it
2977 * easier to follow:
2978 * SrcRect = (0,5) (11, 7)
2979 *
2980 * From the top, Y values only:
2981 * 0. Copy = { .yDst = 0, .ySrc = 0, .cy = 9 }
2982 *
2983 * 1. CopyRect.yDst (=0) is lower than yWCViewportLow:
2984 * cyAdjust = yWCViewportLow - CopyRect.yDst = 2;
2985 * Copy.yDst += cyAdjust = 2;
2986 * Copy.ySrc = unchanged;
2987 * Copy.cx -= cyAdjust = 7;
2988 * => Copy = { .yDst = 2, .ySrc = 0, .cy = 7 }
2989 *
2990 * 2. CopyRect.yDst + CopyRect.cx (=9) is higher than yWCViewportHi:
2991 * cyAdjust = CopyRect.yDst + CopyRect.cx - yWCViewportHi = 9 - 4 = 5
2992 * Copy.yDst = unchanged;
2993 * Copy.ySrc += cyAdjust = 5;
2994 * Copy.cx -= cyAdjust = 2;
2995 * => Copy = { .yDst = 2, .ySrc = 5, .cy = 2 }
2996 *
2997 */
2998 /* X - no inversion, so kind of simple. */
2999 if (ClippedRect.x >= DstViewport.x)
3000 {
3001 if (ClippedRect.x + ClippedRect.w <= DstViewport.xRight)
3002 { /* typical */ }
3003 else if (ClippedRect.x < DstViewport.xRight)
3004 ClippedRect.w = DstViewport.xRight - ClippedRect.x;
3005 else
3006 continue;
3007 }
3008 else
3009 {
3010 uint32_t cxAdjust = DstViewport.x - ClippedRect.x;
3011 if (cxAdjust < ClippedRect.w)
3012 {
3013 ClippedRect.w -= cxAdjust;
3014 ClippedRect.x += cxAdjust;
3015 ClippedRect.srcx += cxAdjust;
3016 }
3017 else
3018 continue;
3019
3020 if (ClippedRect.x + ClippedRect.w <= DstViewport.xRight)
3021 { /* typical */ }
3022 else
3023 ClippedRect.w = DstViewport.xRight - ClippedRect.x;
3024 }
3025
3026 /* Y - complicated, see above. */
3027 if (ClippedRect.y >= DstViewport.yLowWC)
3028 {
3029 if (ClippedRect.y + ClippedRect.h <= DstViewport.yHighWC)
3030 { /* typical */ }
3031 else if (ClippedRect.y < DstViewport.yHighWC)
3032 {
3033 /* adjustment #2 */
3034 uint32_t cyAdjust = ClippedRect.y + ClippedRect.h - DstViewport.yHighWC;
3035 ClippedRect.srcy += cyAdjust;
3036 ClippedRect.h -= cyAdjust;
3037 }
3038 else
3039 continue;
3040 }
3041 else
3042 {
3043 /* adjustment #1 */
3044 uint32_t cyAdjust = DstViewport.yLowWC - ClippedRect.y;
3045 if (cyAdjust < ClippedRect.h)
3046 {
3047 ClippedRect.y += cyAdjust;
3048 ClippedRect.h -= cyAdjust;
3049 }
3050 else
3051 continue;
3052
3053 if (ClippedRect.y + ClippedRect.h <= DstViewport.yHighWC)
3054 { /* typical */ }
3055 else
3056 {
3057 /* adjustment #2 */
3058 uint32_t cyAdjust = ClippedRect.y + ClippedRect.h - DstViewport.yHighWC;
3059 ClippedRect.srcy += cyAdjust;
3060 ClippedRect.h -= cyAdjust;
3061 }
3062 }
3063
3064 /* Calc source rectangle with y flipping wrt destination. */
3065 RTRECT SrcRect;
3066 SrcRect.xLeft = ClippedRect.srcx;
3067 SrcRect.xRight = ClippedRect.srcx + ClippedRect.w;
3068 SrcRect.yBottom = ClippedRect.srcy + ClippedRect.h;
3069 SrcRect.yTop = ClippedRect.srcy;
3070
3071 /* Calc destination rectangle. */
3072 RTRECT DstRect;
3073 DstRect.xLeft = ClippedRect.x;
3074 DstRect.xRight = ClippedRect.x + ClippedRect.w;
3075 DstRect.yBottom = ClippedRect.y;
3076 DstRect.yTop = ClippedRect.y + ClippedRect.h;
3077
3078 /* Adjust for viewport. */
3079 DstRect.xLeft -= DstViewport.x;
3080 DstRect.xRight -= DstViewport.x;
3081 DstRect.yBottom += DstViewport.y;
3082 DstRect.yTop += DstViewport.y;
3083
3084 Log(("SrcRect: (%d,%d)(%d,%d) DstRect: (%d,%d)(%d,%d)\n",
3085 SrcRect.xLeft, SrcRect.yBottom, SrcRect.xRight, SrcRect.yTop,
3086 DstRect.xLeft, DstRect.yBottom, DstRect.xRight, DstRect.yTop));
3087 pState->ext.glBlitFramebuffer(SrcRect.xLeft, SrcRect.yBottom, SrcRect.xRight, SrcRect.yTop,
3088 DstRect.xLeft, DstRect.yBottom, DstRect.xRight, DstRect.yTop,
3089 GL_COLOR_BUFFER_BIT, GL_LINEAR);
3090 }
3091
3092#endif
3093
3094 /*
3095 * Flip the front and back buffers.
3096 */
3097#ifdef RT_OS_WINDOWS
3098 BOOL ret = SwapBuffers(pContext->hdc);
3099 AssertMsg(ret, ("SwapBuffers failed with %d\n", GetLastError()));
3100#elif defined(RT_OS_DARWIN)
3101 vmsvga3dCocoaSwapBuffers(pContext->cocoaView, pContext->cocoaContext);
3102#else
3103 /* show the window if not already done */
3104 if (!pContext->fMapped)
3105 {
3106 XMapWindow(pState->display, pContext->window);
3107 pContext->fMapped = true;
3108 }
3109 /* now swap the buffers, i.e. display the rendering result */
3110 glXSwapBuffers(pState->display, pContext->window);
3111#endif
3112
3113 /*
3114 * Now we can reset the frame buffer association. Doing it earlier means no
3115 * output on darwin.
3116 */
3117 VMSVGA3D_ASSERT_GL_CALL(pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer), pState, pContext);
3118 return VINF_SUCCESS;
3119}
3120
3121#ifdef RT_OS_LINUX
3122/**
3123 * X11 event handling thread
3124 * @param ThreadSelf thread handle
3125 * @param pvUser pointer to pState structure
3126 * @returns VBox status code
3127 */
3128DECLCALLBACK(int) vmsvga3dXEventThread(RTTHREAD ThreadSelf, void *pvUser)
3129{
3130 PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pvUser;
3131 while (!pState->bTerminate)
3132 {
3133 while (XPending(pState->display) > 0)
3134 {
3135 XEvent event;
3136 XNextEvent(pState->display, &event);
3137
3138 switch (event.type)
3139 {
3140 default:
3141 break;
3142 }
3143 }
3144 /* sleep for 16ms to not burn too many cycles */
3145 RTThreadSleep(16);
3146 }
3147 return VINF_SUCCESS;
3148}
3149#endif // RT_OS_LINUX
3150
3151
3152/**
3153 * Create a new 3d context
3154 *
3155 * @returns VBox status code.
3156 * @param pThis VGA device instance data.
3157 * @param cid Context id
3158 * @param fFlags VMSVGA3D_DEF_CTX_F_XXX.
3159 */
3160int vmsvga3dContextDefineOgl(PVGASTATE pThis, uint32_t cid, uint32_t fFlags)
3161{
3162 int rc;
3163 PVMSVGA3DCONTEXT pContext;
3164 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
3165
3166 AssertReturn(pState, VERR_NO_MEMORY);
3167 AssertReturn( cid < SVGA3D_MAX_CONTEXT_IDS
3168 || (cid == VMSVGA3D_SHARED_CTX_ID && (fFlags & VMSVGA3D_DEF_CTX_F_SHARED_CTX)), VERR_INVALID_PARAMETER);
3169#if !defined(VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE) || !(defined(RT_OS_DARWIN))
3170 AssertReturn(!(fFlags & VMSVGA3D_DEF_CTX_F_OTHER_PROFILE), VERR_INTERNAL_ERROR_3);
3171#endif
3172
3173 Log(("vmsvga3dContextDefine id %x\n", cid));
3174#ifdef DEBUG_DEBUG_GFX_WINDOW_TEST_CONTEXT
3175 if (pState->idTestContext == SVGA_ID_INVALID)
3176 {
3177 pState->idTestContext = 207;
3178 rc = vmsvga3dContextDefine(pThis, pState->idTestContext);
3179 AssertRCReturn(rc, rc);
3180 }
3181#endif
3182
3183 if (cid == VMSVGA3D_SHARED_CTX_ID)
3184 pContext = &pState->SharedCtx;
3185 else
3186 {
3187 if (cid >= pState->cContexts)
3188 {
3189 /* Grow the array. */
3190 uint32_t cNew = RT_ALIGN(cid + 15, 16);
3191 void *pvNew = RTMemRealloc(pState->papContexts, sizeof(pState->papContexts[0]) * cNew);
3192 AssertReturn(pvNew, VERR_NO_MEMORY);
3193 pState->papContexts = (PVMSVGA3DCONTEXT *)pvNew;
3194 while (pState->cContexts < cNew)
3195 {
3196 pContext = (PVMSVGA3DCONTEXT)RTMemAllocZ(sizeof(*pContext));
3197 AssertReturn(pContext, VERR_NO_MEMORY);
3198 pContext->id = SVGA3D_INVALID_ID;
3199 pState->papContexts[pState->cContexts++] = pContext;
3200 }
3201 }
3202 /* If one already exists with this id, then destroy it now. */
3203 if (pState->papContexts[cid]->id != SVGA3D_INVALID_ID)
3204 vmsvga3dContextDestroy(pThis, cid);
3205
3206 pContext = pState->papContexts[cid];
3207 }
3208
3209 /*
3210 * Find or create the shared context if needed (necessary for sharing e.g. textures between contexts).
3211 */
3212 PVMSVGA3DCONTEXT pSharedCtx = NULL;
3213 if (!(fFlags & (VMSVGA3D_DEF_CTX_F_INIT | VMSVGA3D_DEF_CTX_F_SHARED_CTX)))
3214 {
3215 pSharedCtx = &pState->SharedCtx;
3216 if (pSharedCtx->id != VMSVGA3D_SHARED_CTX_ID)
3217 {
3218 rc = vmsvga3dContextDefineOgl(pThis, VMSVGA3D_SHARED_CTX_ID, VMSVGA3D_DEF_CTX_F_SHARED_CTX);
3219 AssertLogRelRCReturn(rc, rc);
3220 }
3221 }
3222
3223 /*
3224 * Initialize the context.
3225 */
3226 memset(pContext, 0, sizeof(*pContext));
3227 pContext->id = cid;
3228 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTexture); i++)
3229 pContext->aSidActiveTexture[i] = SVGA3D_INVALID_ID;
3230
3231 pContext->sidRenderTarget = SVGA3D_INVALID_ID;
3232 pContext->state.shidVertex = SVGA3D_INVALID_ID;
3233 pContext->state.shidPixel = SVGA3D_INVALID_ID;
3234 pContext->idFramebuffer = OPENGL_INVALID_ID;
3235 pContext->idReadFramebuffer = OPENGL_INVALID_ID;
3236 pContext->idDrawFramebuffer = OPENGL_INVALID_ID;
3237
3238 rc = ShaderContextCreate(&pContext->pShaderContext);
3239 AssertRCReturn(rc, rc);
3240
3241 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); i++)
3242 pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
3243
3244 AssertReturn(pThis->svga.u64HostWindowId, VERR_INTERNAL_ERROR);
3245
3246#ifdef RT_OS_WINDOWS
3247 /* Create a context window. */
3248 CREATESTRUCT cs;
3249 cs.lpCreateParams = NULL;
3250 cs.dwExStyle = WS_EX_NOACTIVATE | WS_EX_NOPARENTNOTIFY | WS_EX_TRANSPARENT;
3251# ifdef DEBUG_GFX_WINDOW
3252 cs.lpszName = (char *)RTMemAllocZ(256);
3253 RTStrPrintf((char *)cs.lpszName, 256, "Context %d OpenGL Window", cid);
3254# else
3255 cs.lpszName = NULL;
3256# endif
3257 cs.lpszClass = 0;
3258# ifdef DEBUG_GFX_WINDOW
3259 cs.style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE | WS_CAPTION;
3260# else
3261 cs.style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED | WS_CHILD | WS_VISIBLE;
3262# endif
3263 cs.x = 0;
3264 cs.y = 0;
3265 cs.cx = pThis->svga.uWidth;
3266 cs.cy = pThis->svga.uHeight;
3267 cs.hwndParent = (HWND)pThis->svga.u64HostWindowId;
3268 cs.hMenu = NULL;
3269 cs.hInstance = pState->hInstance;
3270
3271 rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_CREATEWINDOW, (WPARAM)&pContext->hwnd, (LPARAM)&cs);
3272 AssertRCReturn(rc, rc);
3273
3274 pContext->hdc = GetDC(pContext->hwnd);
3275 AssertMsgReturn(pContext->hdc, ("GetDC %x failed with %d\n", pContext->hwnd, GetLastError()), VERR_INTERNAL_ERROR);
3276
3277 PIXELFORMATDESCRIPTOR pfd = {
3278 sizeof(PIXELFORMATDESCRIPTOR), /* size of this pfd */
3279 1, /* version number */
3280 PFD_DRAW_TO_WINDOW | /* support window */
3281 PFD_DOUBLEBUFFER | /* support double buffering */
3282 PFD_SUPPORT_OPENGL, /* support OpenGL */
3283 PFD_TYPE_RGBA, /* RGBA type */
3284 24, /* 24-bit color depth */
3285 0, 0, 0, 0, 0, 0, /* color bits ignored */
3286 8, /* alpha buffer */
3287 0, /* shift bit ignored */
3288 0, /* no accumulation buffer */
3289 0, 0, 0, 0, /* accum bits ignored */
3290 16, /* set depth buffer */
3291 16, /* set stencil buffer */
3292 0, /* no auxiliary buffer */
3293 PFD_MAIN_PLANE, /* main layer */
3294 0, /* reserved */
3295 0, 0, 0 /* layer masks ignored */
3296 };
3297 int pixelFormat;
3298 BOOL ret;
3299
3300 pixelFormat = ChoosePixelFormat(pContext->hdc, &pfd);
3301 /** @todo is this really necessary?? */
3302 pixelFormat = ChoosePixelFormat(pContext->hdc, &pfd);
3303 AssertMsgReturn(pixelFormat != 0, ("ChoosePixelFormat failed with %d\n", GetLastError()), VERR_INTERNAL_ERROR);
3304
3305 ret = SetPixelFormat(pContext->hdc, pixelFormat, &pfd);
3306 AssertMsgReturn(ret == TRUE, ("SetPixelFormat failed with %d\n", GetLastError()), VERR_INTERNAL_ERROR);
3307
3308 pContext->hglrc = wglCreateContext(pContext->hdc);
3309 AssertMsgReturn(pContext->hglrc, ("wglCreateContext %x failed with %d\n", pContext->hdc, GetLastError()), VERR_INTERNAL_ERROR);
3310
3311 if (pSharedCtx)
3312 {
3313 ret = wglShareLists(pSharedCtx->hglrc, pContext->hglrc);
3314 AssertMsg(ret == TRUE, ("wglShareLists(%p, %p) failed with %d\n", pSharedCtx->hglrc, pContext->hglrc, GetLastError()));
3315 }
3316
3317#elif defined(RT_OS_DARWIN)
3318 pContext->fOtherProfile = RT_BOOL(fFlags & VMSVGA3D_DEF_CTX_F_OTHER_PROFILE);
3319
3320 NativeNSOpenGLContextRef pShareContext = pSharedCtx ? pSharedCtx->cocoaContext : NULL;
3321 NativeNSViewRef pHostView = (NativeNSViewRef)pThis->svga.u64HostWindowId;
3322 vmsvga3dCocoaCreateViewAndContext(&pContext->cocoaView, &pContext->cocoaContext,
3323 pSharedCtx ? NULL : pHostView, /* Only attach one subview, the one we'll present in. */ /** @todo screen objects and stuff. */
3324 pThis->svga.uWidth, pThis->svga.uHeight,
3325 pShareContext, pContext->fOtherProfile);
3326
3327#else
3328 Window hostWindow = (Window)pThis->svga.u64HostWindowId;
3329
3330 if (pState->display == NULL)
3331 {
3332 /* get an X display and make sure we have glX 1.3 */
3333 pState->display = XOpenDisplay(0);
3334 Assert(pState->display);
3335 int glxMajor, glxMinor;
3336 Bool ret = glXQueryVersion(pState->display, &glxMajor, &glxMinor);
3337 AssertMsgReturn(ret && glxMajor == 1 && glxMinor >= 3, ("glX >=1.3 not present"), VERR_INTERNAL_ERROR);
3338 /* start our X event handling thread */
3339 rc = RTThreadCreate(&pState->pWindowThread, vmsvga3dXEventThread, pState, 0, RTTHREADTYPE_GUI, RTTHREADFLAGS_WAITABLE, "VMSVGA3DXEVENT");
3340 if (RT_FAILURE(rc))
3341 {
3342 AssertMsgFailed(("%s: Async IO Thread creation for 3d window handling failed rc=%d\n", __FUNCTION__, rc));
3343 return rc;
3344 }
3345 }
3346 int attrib[] =
3347 {
3348 GLX_RGBA,
3349 GLX_RED_SIZE, 1,
3350 GLX_GREEN_SIZE, 1,
3351 GLX_BLUE_SIZE, 1,
3352 //GLX_ALPHA_SIZE, 1, this flips the bbos screen
3353 GLX_DOUBLEBUFFER,
3354 None
3355 };
3356 XVisualInfo *vi = glXChooseVisual(pState->display, DefaultScreen(pState->display), attrib);
3357 XSetWindowAttributes swa;
3358 swa.colormap = XCreateColormap(pState->display, XDefaultRootWindow(pState->display), vi->visual, AllocNone);
3359 swa.border_pixel = 0;
3360 swa.background_pixel = 0;
3361 swa.event_mask = StructureNotifyMask | ExposureMask;
3362 unsigned long flags = CWBorderPixel | CWBackPixel | CWColormap | CWEventMask;
3363 pContext->window = XCreateWindow(pState->display, hostWindow,//XDefaultRootWindow(pState->display),//hostWindow,
3364 0, 0, pThis->svga.uWidth, pThis->svga.uHeight,
3365 0, vi->depth, InputOutput,
3366 vi->visual, flags, &swa);
3367 AssertMsgReturn(pContext->window, ("XCreateWindow failed"), VERR_INTERNAL_ERROR);
3368 uint32_t cardinal_alpha = (uint32_t) (0.5 * (uint32_t)-1) ;
3369
3370 /* the window is hidden by default and only mapped when CommandPresent is executed on it */
3371
3372 GLXContext shareContext = pSharedCtx ? pSharedCtx->glxContext : NULL;
3373 pContext->glxContext = glXCreateContext(pState->display, vi, shareContext, GL_TRUE);
3374 AssertMsgReturn(pContext->glxContext, ("glXCreateContext failed"), VERR_INTERNAL_ERROR);
3375#endif
3376
3377 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3378
3379 /* NULL during the first PowerOn call. */
3380 if (pState->ext.glGenFramebuffers)
3381 {
3382 /* Create a framebuffer object for this context. */
3383 pState->ext.glGenFramebuffers(1, &pContext->idFramebuffer);
3384 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3385
3386 /* Bind the object to the framebuffer target. */
3387 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, pContext->idFramebuffer);
3388 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3389
3390 /* Create read and draw framebuffer objects for this context. */
3391 pState->ext.glGenFramebuffers(1, &pContext->idReadFramebuffer);
3392 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3393
3394 pState->ext.glGenFramebuffers(1, &pContext->idDrawFramebuffer);
3395 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3396
3397 }
3398#if 0
3399 /** @todo move to shader lib!!! */
3400 /* Clear the screen */
3401 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3402
3403 glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
3404 glClearIndex(0);
3405 glClearDepth(1);
3406 glClearStencil(0xffff);
3407 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
3408 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
3409 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
3410 if (pState->ext.glProvokingVertex)
3411 pState->ext.glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);
3412 /** @todo move to shader lib!!! */
3413#endif
3414 return VINF_SUCCESS;
3415}
3416
3417
3418/**
3419 * Create a new 3d context
3420 *
3421 * @returns VBox status code.
3422 * @param pThis VGA device instance data.
3423 * @param cid Context id
3424 */
3425int vmsvga3dContextDefine(PVGASTATE pThis, uint32_t cid)
3426{
3427 return vmsvga3dContextDefineOgl(pThis, cid, 0/*fFlags*/);
3428}
3429
3430/**
3431 * Destroys a 3d context.
3432 *
3433 * @returns VBox status code.
3434 * @param pThis VGA device instance data.
3435 * @param pContext The context to destroy.
3436 * @param cid Context id
3437 */
3438static int vmsvga3dContextDestroyOgl(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext, uint32_t cid)
3439{
3440 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
3441 AssertReturn(pState, VERR_NO_MEMORY);
3442 AssertReturn(pContext, VERR_INVALID_PARAMETER);
3443 AssertReturn(pContext->id == cid, VERR_INVALID_PARAMETER);
3444 Log(("vmsvga3dContextDestroyOgl id %x\n", cid));
3445
3446 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3447
3448 /* Destroy all leftover pixel shaders. */
3449 for (uint32_t i = 0; i < pContext->cPixelShaders; i++)
3450 {
3451 if (pContext->paPixelShader[i].id != SVGA3D_INVALID_ID)
3452 vmsvga3dShaderDestroy(pThis, pContext->paPixelShader[i].cid, pContext->paPixelShader[i].id, pContext->paPixelShader[i].type);
3453 }
3454 if (pContext->paPixelShader)
3455 RTMemFree(pContext->paPixelShader);
3456
3457 /* Destroy all leftover vertex shaders. */
3458 for (uint32_t i = 0; i < pContext->cVertexShaders; i++)
3459 {
3460 if (pContext->paVertexShader[i].id != SVGA3D_INVALID_ID)
3461 vmsvga3dShaderDestroy(pThis, pContext->paVertexShader[i].cid, pContext->paVertexShader[i].id, pContext->paVertexShader[i].type);
3462 }
3463 if (pContext->paVertexShader)
3464 RTMemFree(pContext->paVertexShader);
3465
3466 if (pContext->state.paVertexShaderConst)
3467 RTMemFree(pContext->state.paVertexShaderConst);
3468 if (pContext->state.paPixelShaderConst)
3469 RTMemFree(pContext->state.paPixelShaderConst);
3470
3471 if (pContext->pShaderContext)
3472 {
3473 int rc = ShaderContextDestroy(pContext->pShaderContext);
3474 AssertRC(rc);
3475 }
3476
3477 if (pContext->idFramebuffer != OPENGL_INVALID_ID)
3478 {
3479 /* Unbind the object from the framebuffer target. */
3480 pState->ext.glBindFramebuffer(GL_FRAMEBUFFER, 0 /* back buffer */);
3481 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3482 pState->ext.glDeleteFramebuffers(1, &pContext->idFramebuffer);
3483 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3484
3485 if (pContext->idReadFramebuffer != OPENGL_INVALID_ID)
3486 {
3487 pState->ext.glDeleteFramebuffers(1, &pContext->idReadFramebuffer);
3488 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3489 }
3490 if (pContext->idDrawFramebuffer != OPENGL_INVALID_ID)
3491 {
3492 pState->ext.glDeleteFramebuffers(1, &pContext->idDrawFramebuffer);
3493 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3494 }
3495 }
3496#ifdef RT_OS_WINDOWS
3497 wglMakeCurrent(pContext->hdc, NULL);
3498 wglDeleteContext(pContext->hglrc);
3499 ReleaseDC(pContext->hwnd, pContext->hdc);
3500
3501 /* Destroy the window we've created. */
3502 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_DESTROYWINDOW, (WPARAM)pContext->hwnd, 0);
3503 AssertRC(rc);
3504#elif defined(RT_OS_DARWIN)
3505 vmsvga3dCocoaDestroyViewAndContext(pContext->cocoaView, pContext->cocoaContext);
3506#elif defined(RT_OS_LINUX)
3507 glXMakeCurrent(pState->display, None, NULL);
3508 glXDestroyContext(pState->display, pContext->glxContext);
3509 XDestroyWindow(pState->display, pContext->window);
3510#endif
3511
3512 memset(pContext, 0, sizeof(*pContext));
3513 pContext->id = SVGA3D_INVALID_ID;
3514
3515 VMSVGA3D_CLEAR_CURRENT_CONTEXT(pState);
3516 return VINF_SUCCESS;
3517}
3518
3519/**
3520 * Destroy an existing 3d context
3521 *
3522 * @returns VBox status code.
3523 * @param pThis VGA device instance data.
3524 * @param cid Context id
3525 */
3526int vmsvga3dContextDestroy(PVGASTATE pThis, uint32_t cid)
3527{
3528 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
3529 AssertReturn(pState, VERR_WRONG_ORDER);
3530
3531 /*
3532 * Resolve the context and hand it to the common worker function.
3533 */
3534 if ( cid < pState->cContexts
3535 && pState->papContexts[cid]->id == cid)
3536 return vmsvga3dContextDestroyOgl(pThis, pState->papContexts[cid], cid);
3537
3538 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
3539 return VINF_SUCCESS;
3540}
3541
3542/**
3543 * Worker for vmsvga3dChangeMode that resizes a context.
3544 *
3545 * @param pThis The VGA device instance data.
3546 * @param pState The VMSVGA3d state.
3547 * @param pContext The context.
3548 */
3549static void vmsvga3dChangeModeOneContext(PVGASTATE pThis, PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
3550{
3551#ifdef RT_OS_WINDOWS
3552 /* Resize the window. */
3553 CREATESTRUCT cs;
3554 RT_ZERO(cs);
3555 cs.cx = pThis->svga.uWidth;
3556 cs.cy = pThis->svga.uHeight;
3557 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_RESIZEWINDOW, (WPARAM)pContext->hwnd, (LPARAM)&cs);
3558 AssertRC(rc);
3559
3560#elif defined(RT_OS_DARWIN)
3561 vmsvga3dCocoaViewSetSize(pContext->cocoaView, pThis->svga.uWidth, pThis->svga.uHeight);
3562
3563#elif defined(RT_OS_LINUX)
3564 XWindowChanges wc;
3565 wc.width = pThis->svga.uWidth;
3566 wc.height = pThis->svga.uHeight;
3567 XConfigureWindow(pState->display, pContext->window, CWWidth | CWHeight, &wc);
3568#endif
3569}
3570
3571/* Handle resize */
3572int vmsvga3dChangeMode(PVGASTATE pThis)
3573{
3574 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
3575 AssertReturn(pState, VERR_NO_MEMORY);
3576
3577 /* Resize the shared context too. */
3578 if (pState->SharedCtx.id == VMSVGA3D_SHARED_CTX_ID)
3579 vmsvga3dChangeModeOneContext(pThis, pState, &pState->SharedCtx);
3580
3581 /* Resize all active contexts. */
3582 for (uint32_t i = 0; i < pState->cContexts; i++)
3583 {
3584 PVMSVGA3DCONTEXT pContext = pState->papContexts[i];
3585 if (pContext->id != SVGA3D_INVALID_ID)
3586 vmsvga3dChangeModeOneContext(pThis, pState, pContext);
3587 }
3588
3589 return VINF_SUCCESS;
3590}
3591
3592
3593int vmsvga3dSetTransform(PVGASTATE pThis, uint32_t cid, SVGA3dTransformType type, float matrix[16])
3594{
3595 PVMSVGA3DCONTEXT pContext;
3596 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
3597 AssertReturn(pState, VERR_NO_MEMORY);
3598 bool fModelViewChanged = false;
3599
3600 Log(("vmsvga3dSetTransform cid=%x %s\n", cid, vmsvgaTransformToString(type)));
3601
3602 if ( cid >= pState->cContexts
3603 || pState->papContexts[cid]->id != cid)
3604 {
3605 Log(("vmsvga3dSetTransform invalid context id!\n"));
3606 return VERR_INVALID_PARAMETER;
3607 }
3608 pContext = pState->papContexts[cid];
3609 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3610
3611 /* Save this matrix for vm state save/restore. */
3612 pContext->state.aTransformState[type].fValid = true;
3613 memcpy(pContext->state.aTransformState[type].matrix, matrix, sizeof(pContext->state.aTransformState[type].matrix));
3614 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_TRANSFORM;
3615
3616 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)));
3617 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)));
3618 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)));
3619 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)));
3620
3621 switch (type)
3622 {
3623 case SVGA3D_TRANSFORM_VIEW:
3624 /* View * World = Model View */
3625 glMatrixMode(GL_MODELVIEW);
3626 glLoadMatrixf(matrix);
3627 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_WORLD].fValid)
3628 glMultMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_WORLD].matrix);
3629 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3630 fModelViewChanged = true;
3631 break;
3632
3633 case SVGA3D_TRANSFORM_PROJECTION:
3634 {
3635 int rc = ShaderTransformProjection(pContext->state.RectViewPort.w, pContext->state.RectViewPort.h, matrix);
3636 AssertRCReturn(rc, rc);
3637 break;
3638 }
3639
3640 case SVGA3D_TRANSFORM_TEXTURE0:
3641 glMatrixMode(GL_TEXTURE);
3642 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3643 glLoadMatrixf(matrix);
3644 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3645 break;
3646
3647 case SVGA3D_TRANSFORM_TEXTURE1:
3648 case SVGA3D_TRANSFORM_TEXTURE2:
3649 case SVGA3D_TRANSFORM_TEXTURE3:
3650 case SVGA3D_TRANSFORM_TEXTURE4:
3651 case SVGA3D_TRANSFORM_TEXTURE5:
3652 case SVGA3D_TRANSFORM_TEXTURE6:
3653 case SVGA3D_TRANSFORM_TEXTURE7:
3654 Log(("vmsvga3dSetTransform: unsupported SVGA3D_TRANSFORM_TEXTUREx transform!!\n"));
3655 return VERR_INVALID_PARAMETER;
3656
3657 case SVGA3D_TRANSFORM_WORLD:
3658 /* View * World = Model View */
3659 glMatrixMode(GL_MODELVIEW);
3660 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].fValid)
3661 glLoadMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
3662 else
3663 glLoadIdentity();
3664 glMultMatrixf(matrix);
3665 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3666 fModelViewChanged = true;
3667 break;
3668
3669 case SVGA3D_TRANSFORM_WORLD1:
3670 case SVGA3D_TRANSFORM_WORLD2:
3671 case SVGA3D_TRANSFORM_WORLD3:
3672 Log(("vmsvga3dSetTransform: unsupported SVGA3D_TRANSFORM_WORLDx transform!!\n"));
3673 return VERR_INVALID_PARAMETER;
3674
3675 default:
3676 Log(("vmsvga3dSetTransform: unknown type!!\n"));
3677 return VERR_INVALID_PARAMETER;
3678 }
3679
3680 /* Apparently we need to reset the light and clip data after modifying the modelview matrix. */
3681 if (fModelViewChanged)
3682 {
3683 /* Reprogram the clip planes. */
3684 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aClipPlane); j++)
3685 {
3686 if (pContext->state.aClipPlane[j].fValid == true)
3687 vmsvga3dSetClipPlane(pThis, cid, j, pContext->state.aClipPlane[j].plane);
3688 }
3689
3690 /* Reprogram the light data. */
3691 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aLightData); j++)
3692 {
3693 if (pContext->state.aLightData[j].fValidData == true)
3694 vmsvga3dSetLightData(pThis, cid, j, &pContext->state.aLightData[j].data);
3695 }
3696 }
3697
3698 return VINF_SUCCESS;
3699}
3700
3701int vmsvga3dSetZRange(PVGASTATE pThis, uint32_t cid, SVGA3dZRange zRange)
3702{
3703 PVMSVGA3DCONTEXT pContext;
3704 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
3705 AssertReturn(pState, VERR_NO_MEMORY);
3706
3707 Log(("vmsvga3dSetZRange cid=%x min=%d max=%d\n", cid, (uint32_t)(zRange.min * 100.0), (uint32_t)(zRange.max * 100.0)));
3708
3709 if ( cid >= pState->cContexts
3710 || pState->papContexts[cid]->id != cid)
3711 {
3712 Log(("vmsvga3dSetZRange invalid context id!\n"));
3713 return VERR_INVALID_PARAMETER;
3714 }
3715 pContext = pState->papContexts[cid];
3716 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3717
3718 pContext->state.zRange = zRange;
3719 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_ZRANGE;
3720
3721 if (zRange.min < -1.0)
3722 zRange.min = -1.0;
3723 if (zRange.max > 1.0)
3724 zRange.max = 1.0;
3725
3726 glDepthRange((GLdouble)zRange.min, (GLdouble)zRange.max);
3727 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3728 return VINF_SUCCESS;
3729}
3730
3731/**
3732 * Convert SVGA blend op value to its OpenGL equivalent
3733 */
3734static GLenum vmsvga3dBlendOp2GL(uint32_t blendOp)
3735{
3736 switch (blendOp)
3737 {
3738 case SVGA3D_BLENDOP_ZERO:
3739 return GL_ZERO;
3740 case SVGA3D_BLENDOP_ONE:
3741 return GL_ONE;
3742 case SVGA3D_BLENDOP_SRCCOLOR:
3743 return GL_SRC_COLOR;
3744 case SVGA3D_BLENDOP_INVSRCCOLOR:
3745 return GL_ONE_MINUS_SRC_COLOR;
3746 case SVGA3D_BLENDOP_SRCALPHA:
3747 return GL_SRC_ALPHA;
3748 case SVGA3D_BLENDOP_INVSRCALPHA:
3749 return GL_ONE_MINUS_SRC_ALPHA;
3750 case SVGA3D_BLENDOP_DESTALPHA:
3751 return GL_DST_ALPHA;
3752 case SVGA3D_BLENDOP_INVDESTALPHA:
3753 return GL_ONE_MINUS_DST_ALPHA;
3754 case SVGA3D_BLENDOP_DESTCOLOR:
3755 return GL_DST_COLOR;
3756 case SVGA3D_BLENDOP_INVDESTCOLOR:
3757 return GL_ONE_MINUS_DST_COLOR;
3758 case SVGA3D_BLENDOP_SRCALPHASAT:
3759 return GL_SRC_ALPHA_SATURATE;
3760 case SVGA3D_BLENDOP_BLENDFACTOR:
3761 return GL_CONSTANT_ALPHA; /** @todo correct?? */
3762 case SVGA3D_BLENDOP_INVBLENDFACTOR:
3763 return GL_ONE_MINUS_CONSTANT_ALPHA; /** @todo correct?? */
3764 default:
3765 AssertFailed();
3766 return GL_ONE;
3767 }
3768}
3769
3770static GLenum vmsvga3dBlendEquation2GL(uint32_t blendEq)
3771{
3772 switch (blendEq)
3773 {
3774 case SVGA3D_BLENDEQ_ADD:
3775 return GL_FUNC_ADD;
3776 case SVGA3D_BLENDEQ_SUBTRACT:
3777 return GL_FUNC_SUBTRACT;
3778 case SVGA3D_BLENDEQ_REVSUBTRACT:
3779 return GL_FUNC_REVERSE_SUBTRACT;
3780 case SVGA3D_BLENDEQ_MINIMUM:
3781 return GL_MIN;
3782 case SVGA3D_BLENDEQ_MAXIMUM:
3783 return GL_MAX;
3784 default:
3785 AssertMsgFailed(("blendEq=%d (%#x)\n", blendEq, blendEq));
3786 return GL_FUNC_ADD;
3787 }
3788}
3789
3790static GLenum vmsvgaCmpFunc2GL(uint32_t cmpFunc)
3791{
3792 switch (cmpFunc)
3793 {
3794 case SVGA3D_CMP_NEVER:
3795 return GL_NEVER;
3796 case SVGA3D_CMP_LESS:
3797 return GL_LESS;
3798 case SVGA3D_CMP_EQUAL:
3799 return GL_EQUAL;
3800 case SVGA3D_CMP_LESSEQUAL:
3801 return GL_LEQUAL;
3802 case SVGA3D_CMP_GREATER:
3803 return GL_GREATER;
3804 case SVGA3D_CMP_NOTEQUAL:
3805 return GL_NOTEQUAL;
3806 case SVGA3D_CMP_GREATEREQUAL:
3807 return GL_GEQUAL;
3808 case SVGA3D_CMP_ALWAYS:
3809 return GL_ALWAYS;
3810 default:
3811 AssertFailed();
3812 return GL_LESS;
3813 }
3814}
3815
3816static GLenum vmsvgaStencipOp2GL(uint32_t stencilOp)
3817{
3818 switch (stencilOp)
3819 {
3820 case SVGA3D_STENCILOP_KEEP:
3821 return GL_KEEP;
3822 case SVGA3D_STENCILOP_ZERO:
3823 return GL_ZERO;
3824 case SVGA3D_STENCILOP_REPLACE:
3825 return GL_REPLACE;
3826 case SVGA3D_STENCILOP_INCRSAT:
3827 return GL_INCR_WRAP;
3828 case SVGA3D_STENCILOP_DECRSAT:
3829 return GL_DECR_WRAP;
3830 case SVGA3D_STENCILOP_INVERT:
3831 return GL_INVERT;
3832 case SVGA3D_STENCILOP_INCR:
3833 return GL_INCR;
3834 case SVGA3D_STENCILOP_DECR:
3835 return GL_DECR;
3836 default:
3837 AssertFailed();
3838 return GL_KEEP;
3839 }
3840}
3841
3842int vmsvga3dSetRenderState(PVGASTATE pThis, uint32_t cid, uint32_t cRenderStates, SVGA3dRenderState *pRenderState)
3843{
3844 uint32_t val;
3845 PVMSVGA3DCONTEXT pContext;
3846 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
3847 AssertReturn(pState, VERR_NO_MEMORY);
3848
3849 Log(("vmsvga3dSetRenderState cid=%x cRenderStates=%d\n", cid, cRenderStates));
3850
3851 if ( cid >= pState->cContexts
3852 || pState->papContexts[cid]->id != cid)
3853 {
3854 Log(("vmsvga3dSetRenderState invalid context id!\n"));
3855 return VERR_INVALID_PARAMETER;
3856 }
3857 pContext = pState->papContexts[cid];
3858 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
3859
3860 for (unsigned i = 0; i < cRenderStates; i++)
3861 {
3862 GLenum enableCap = ~0U;
3863 Log(("vmsvga3dSetRenderState: cid=%x state=%s (%d) val=%x\n", cid, vmsvga3dGetRenderStateName(pRenderState[i].state), pRenderState[i].state, pRenderState[i].uintValue));
3864 /* Save the render state for vm state saving. */
3865 if (pRenderState[i].state < SVGA3D_RS_MAX)
3866 pContext->state.aRenderState[pRenderState[i].state] = pRenderState[i];
3867
3868 switch (pRenderState[i].state)
3869 {
3870 case SVGA3D_RS_ZENABLE: /* SVGA3dBool */
3871 enableCap = GL_DEPTH_TEST;
3872 val = pRenderState[i].uintValue;
3873 break;
3874
3875 case SVGA3D_RS_ZWRITEENABLE: /* SVGA3dBool */
3876 glDepthMask(!!pRenderState[i].uintValue);
3877 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3878 break;
3879
3880 case SVGA3D_RS_ALPHATESTENABLE: /* SVGA3dBool */
3881 enableCap = GL_ALPHA_TEST;
3882 val = pRenderState[i].uintValue;
3883 break;
3884
3885 case SVGA3D_RS_DITHERENABLE: /* SVGA3dBool */
3886 enableCap = GL_DITHER;
3887 val = pRenderState[i].uintValue;
3888 break;
3889
3890 case SVGA3D_RS_FOGENABLE: /* SVGA3dBool */
3891 enableCap = GL_FOG;
3892 val = pRenderState[i].uintValue;
3893 break;
3894
3895 case SVGA3D_RS_SPECULARENABLE: /* SVGA3dBool */
3896 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
3897 break;
3898
3899 case SVGA3D_RS_LIGHTINGENABLE: /* SVGA3dBool */
3900 enableCap = GL_LIGHTING;
3901 val = pRenderState[i].uintValue;
3902 break;
3903
3904 case SVGA3D_RS_NORMALIZENORMALS: /* SVGA3dBool */
3905 /* not applicable */
3906 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
3907 break;
3908
3909 case SVGA3D_RS_POINTSPRITEENABLE: /* SVGA3dBool */
3910 enableCap = GL_POINT_SPRITE_ARB;
3911 val = pRenderState[i].uintValue;
3912 break;
3913
3914 case SVGA3D_RS_POINTSIZE: /* float */
3915 /** @todo we need to apply scaling for point sizes below the min or above the max; see Wine) */
3916 if (pRenderState[i].floatValue < pState->caps.flPointSize[0])
3917 pRenderState[i].floatValue = pState->caps.flPointSize[0];
3918 if (pRenderState[i].floatValue > pState->caps.flPointSize[1])
3919 pRenderState[i].floatValue = pState->caps.flPointSize[1];
3920
3921 glPointSize(pRenderState[i].floatValue);
3922 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3923 Log(("SVGA3D_RS_POINTSIZE: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3924 break;
3925
3926 case SVGA3D_RS_POINTSIZEMIN: /* float */
3927 pState->ext.glPointParameterf(GL_POINT_SIZE_MIN, pRenderState[i].floatValue);
3928 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3929 Log(("SVGA3D_RS_POINTSIZEMIN: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3930 break;
3931
3932 case SVGA3D_RS_POINTSIZEMAX: /* float */
3933 pState->ext.glPointParameterf(GL_POINT_SIZE_MAX, pRenderState[i].floatValue);
3934 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3935 Log(("SVGA3D_RS_POINTSIZEMAX: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3936 break;
3937
3938 case SVGA3D_RS_POINTSCALEENABLE: /* SVGA3dBool */
3939 case SVGA3D_RS_POINTSCALE_A: /* float */
3940 case SVGA3D_RS_POINTSCALE_B: /* float */
3941 case SVGA3D_RS_POINTSCALE_C: /* float */
3942 Log(("vmsvga3dSetRenderState: WARNING: not applicable.\n"));
3943 break;
3944
3945 case SVGA3D_RS_AMBIENT: /* SVGA3dColor */
3946 {
3947 GLfloat color[4]; /* red, green, blue, alpha */
3948
3949 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &color[0], &color[1], &color[2], &color[3]);
3950
3951 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, color);
3952 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3953 break;
3954 }
3955
3956 case SVGA3D_RS_CLIPPLANEENABLE: /* SVGA3dClipPlanes */
3957 {
3958 AssertCompile(SVGA3D_CLIPPLANE_MAX == (1 << 5));
3959 for (uint32_t j = 0; j <= 5; j++)
3960 {
3961 if (pRenderState[i].uintValue & RT_BIT(j))
3962 glEnable(GL_CLIP_PLANE0 + j);
3963 else
3964 glDisable(GL_CLIP_PLANE0 + j);
3965 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3966 }
3967 break;
3968 }
3969
3970 case SVGA3D_RS_FOGCOLOR: /* SVGA3dColor */
3971 {
3972 GLfloat color[4]; /* red, green, blue, alpha */
3973
3974 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &color[0], &color[1], &color[2], &color[3]);
3975
3976 glFogfv(GL_FOG_COLOR, color);
3977 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3978 break;
3979 }
3980
3981 case SVGA3D_RS_FOGSTART: /* float */
3982 glFogf(GL_FOG_START, pRenderState[i].floatValue);
3983 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3984 break;
3985
3986 case SVGA3D_RS_FOGEND: /* float */
3987 glFogf(GL_FOG_END, pRenderState[i].floatValue);
3988 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3989 break;
3990
3991 case SVGA3D_RS_FOGDENSITY: /* float */
3992 glFogf(GL_FOG_DENSITY, pRenderState[i].floatValue);
3993 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3994 break;
3995
3996 case SVGA3D_RS_RANGEFOGENABLE: /* SVGA3dBool */
3997 glFogi(GL_FOG_COORD_SRC, (pRenderState[i].uintValue) ? GL_FOG_COORD : GL_FRAGMENT_DEPTH);
3998 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
3999 break;
4000
4001 case SVGA3D_RS_FOGMODE: /* SVGA3dFogMode */
4002 {
4003 SVGA3dFogMode mode;
4004 mode.uintValue = pRenderState[i].uintValue;
4005
4006 enableCap = GL_FOG_MODE;
4007 switch (mode.s.function)
4008 {
4009 case SVGA3D_FOGFUNC_EXP:
4010 val = GL_EXP;
4011 break;
4012 case SVGA3D_FOGFUNC_EXP2:
4013 val = GL_EXP2;
4014 break;
4015 case SVGA3D_FOGFUNC_LINEAR:
4016 val = GL_LINEAR;
4017 break;
4018 default:
4019 AssertMsgFailedReturn(("Unexpected fog function %d\n", mode.s.function), VERR_INTERNAL_ERROR);
4020 break;
4021 }
4022
4023 /** @todo how to switch between vertex and pixel fog modes??? */
4024 Assert(mode.s.type == SVGA3D_FOGTYPE_PIXEL);
4025#if 0
4026 /* The fog type determines the render state. */
4027 switch (mode.s.type)
4028 {
4029 case SVGA3D_FOGTYPE_VERTEX:
4030 renderState = D3DRS_FOGVERTEXMODE;
4031 break;
4032 case SVGA3D_FOGTYPE_PIXEL:
4033 renderState = D3DRS_FOGTABLEMODE;
4034 break;
4035 default:
4036 AssertMsgFailedReturn(("Unexpected fog type %d\n", mode.s.type), VERR_INTERNAL_ERROR);
4037 break;
4038 }
4039#endif
4040
4041 /* Set the fog base to depth or range. */
4042 switch (mode.s.base)
4043 {
4044 case SVGA3D_FOGBASE_DEPTHBASED:
4045 glFogi(GL_FOG_COORD_SRC, GL_FRAGMENT_DEPTH);
4046 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4047 break;
4048 case SVGA3D_FOGBASE_RANGEBASED:
4049 glFogi(GL_FOG_COORD_SRC, GL_FOG_COORD);
4050 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4051 break;
4052 default:
4053 /* ignore */
4054 AssertMsgFailed(("Unexpected fog base %d\n", mode.s.base));
4055 break;
4056 }
4057 break;
4058 }
4059
4060 case SVGA3D_RS_FILLMODE: /* SVGA3dFillMode */
4061 {
4062 SVGA3dFillMode mode;
4063
4064 mode.uintValue = pRenderState[i].uintValue;
4065
4066 switch (mode.s.mode)
4067 {
4068 case SVGA3D_FILLMODE_POINT:
4069 val = GL_POINT;
4070 break;
4071 case SVGA3D_FILLMODE_LINE:
4072 val = GL_LINE;
4073 break;
4074 case SVGA3D_FILLMODE_FILL:
4075 val = GL_FILL;
4076 break;
4077 default:
4078 AssertMsgFailedReturn(("Unexpected fill mode %d\n", mode.s.mode), VERR_INTERNAL_ERROR);
4079 break;
4080 }
4081 /* @note only front and back faces */
4082 Assert(mode.s.face == SVGA3D_FACE_FRONT_BACK);
4083 glPolygonMode(GL_FRONT_AND_BACK, val);
4084 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4085 break;
4086 }
4087
4088 case SVGA3D_RS_SHADEMODE: /* SVGA3dShadeMode */
4089 switch (pRenderState[i].uintValue)
4090 {
4091 case SVGA3D_SHADEMODE_FLAT:
4092 val = GL_FLAT;
4093 break;
4094
4095 case SVGA3D_SHADEMODE_SMOOTH:
4096 val = GL_SMOOTH;
4097 break;
4098
4099 default:
4100 AssertMsgFailedReturn(("Unexpected shade mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
4101 break;
4102 }
4103
4104 glShadeModel(val);
4105 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4106 break;
4107
4108 case SVGA3D_RS_LINEPATTERN: /* SVGA3dLinePattern */
4109 /* No longer supported by d3d; mesagl comments suggest not all backends support it */
4110 /** @todo */
4111 Log(("WARNING: SVGA3D_RS_LINEPATTERN %x not supported!!\n", pRenderState[i].uintValue));
4112 /*
4113 renderState = D3DRS_LINEPATTERN;
4114 val = pRenderState[i].uintValue;
4115 */
4116 break;
4117
4118 case SVGA3D_RS_LINEAA: /* SVGA3dBool */
4119 enableCap = GL_LINE_SMOOTH;
4120 val = pRenderState[i].uintValue;
4121 break;
4122
4123 case SVGA3D_RS_LINEWIDTH: /* float */
4124 glLineWidth(pRenderState[i].floatValue);
4125 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4126 break;
4127
4128 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
4129 {
4130 /* Refresh the blending state based on the new enable setting. */
4131 SVGA3dRenderState renderstate[2];
4132
4133 renderstate[0].state = SVGA3D_RS_SRCBLEND;
4134 renderstate[0].uintValue = pContext->state.aRenderState[SVGA3D_RS_SRCBLEND].uintValue;
4135 renderstate[1].state = SVGA3D_RS_BLENDEQUATION;
4136 renderstate[1].uintValue = pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATION].uintValue;
4137
4138 int rc = vmsvga3dSetRenderState(pThis, cid, 2, renderstate);
4139 AssertRCReturn(rc, rc);
4140
4141 if (pContext->state.aRenderState[SVGA3D_RS_BLENDENABLE].uintValue != 0)
4142 continue; /* ignore if blend is already enabled */
4143 /* no break */
4144 }
4145
4146 case SVGA3D_RS_BLENDENABLE: /* SVGA3dBool */
4147 enableCap = GL_BLEND;
4148 val = pRenderState[i].uintValue;
4149 break;
4150
4151 case SVGA3D_RS_SRCBLENDALPHA: /* SVGA3dBlendOp */
4152 case SVGA3D_RS_DSTBLENDALPHA: /* SVGA3dBlendOp */
4153 case SVGA3D_RS_SRCBLEND: /* SVGA3dBlendOp */
4154 case SVGA3D_RS_DSTBLEND: /* SVGA3dBlendOp */
4155 {
4156 GLint srcRGB, srcAlpha, dstRGB, dstAlpha;
4157 GLint blendop = vmsvga3dBlendOp2GL(pRenderState[i].uintValue);
4158
4159 glGetIntegerv(GL_BLEND_SRC_RGB, &srcRGB);
4160 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4161 glGetIntegerv(GL_BLEND_DST_RGB, &dstRGB);
4162 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4163 glGetIntegerv(GL_BLEND_DST_ALPHA, &dstAlpha);
4164 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4165 glGetIntegerv(GL_BLEND_SRC_ALPHA, &srcAlpha);
4166 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4167
4168 switch (pRenderState[i].state)
4169 {
4170 case SVGA3D_RS_SRCBLEND:
4171 srcRGB = blendop;
4172 break;
4173 case SVGA3D_RS_DSTBLEND:
4174 dstRGB = blendop;
4175 break;
4176 case SVGA3D_RS_SRCBLENDALPHA:
4177 srcAlpha = blendop;
4178 break;
4179 case SVGA3D_RS_DSTBLENDALPHA:
4180 dstAlpha = blendop;
4181 break;
4182 default:
4183 /* not possible; shut up gcc */
4184 AssertFailed();
4185 break;
4186 }
4187
4188 if (pContext->state.aRenderState[SVGA3D_RS_SEPARATEALPHABLENDENABLE].uintValue != 0)
4189 pState->ext.glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
4190 else
4191 glBlendFunc(srcRGB, dstRGB);
4192 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4193 break;
4194 }
4195
4196 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation */
4197 case SVGA3D_RS_BLENDEQUATION: /* SVGA3dBlendEquation */
4198 if (pContext->state.aRenderState[SVGA3D_RS_SEPARATEALPHABLENDENABLE].uintValue != 0)
4199 pState->ext.glBlendEquationSeparate(vmsvga3dBlendEquation2GL(pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATION].uintValue),
4200 vmsvga3dBlendEquation2GL(pContext->state.aRenderState[SVGA3D_RS_BLENDEQUATIONALPHA].uintValue));
4201 else
4202 {
4203#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x102
4204 glBlendEquation(vmsvga3dBlendEquation2GL(pRenderState[i].uintValue));
4205#else
4206 pState->ext.glBlendEquation(vmsvga3dBlendEquation2GL(pRenderState[i].uintValue));
4207#endif
4208 }
4209 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4210 break;
4211
4212 case SVGA3D_RS_BLENDCOLOR: /* SVGA3dColor */
4213 {
4214 GLfloat red, green, blue, alpha;
4215
4216 vmsvgaColor2GLFloatArray(pRenderState[i].uintValue, &red, &green, &blue, &alpha);
4217
4218#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x102
4219 glBlendColor(red, green, blue, alpha);
4220#else
4221 pState->ext.glBlendColor(red, green, blue, alpha);
4222#endif
4223 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4224 break;
4225 }
4226
4227 case SVGA3D_RS_CULLMODE: /* SVGA3dFace */
4228 {
4229 GLenum mode = GL_BACK; /* default for OpenGL */
4230
4231 switch (pRenderState[i].uintValue)
4232 {
4233 case SVGA3D_FACE_NONE:
4234 break;
4235 case SVGA3D_FACE_FRONT:
4236 mode = GL_FRONT;
4237 break;
4238 case SVGA3D_FACE_BACK:
4239 mode = GL_BACK;
4240 break;
4241 case SVGA3D_FACE_FRONT_BACK:
4242 mode = GL_FRONT_AND_BACK;
4243 break;
4244 default:
4245 AssertMsgFailedReturn(("Unexpected cull mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
4246 break;
4247 }
4248 enableCap = GL_CULL_FACE;
4249 if (pRenderState[i].uintValue != SVGA3D_FACE_NONE)
4250 {
4251 glCullFace(mode);
4252 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4253 val = 1;
4254 }
4255 else
4256 val = 0;
4257 break;
4258 }
4259
4260 case SVGA3D_RS_ZFUNC: /* SVGA3dCmpFunc */
4261 glDepthFunc(vmsvgaCmpFunc2GL(pRenderState[i].uintValue));
4262 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4263 break;
4264
4265 case SVGA3D_RS_ALPHAFUNC: /* SVGA3dCmpFunc */
4266 {
4267 GLclampf ref;
4268
4269 glGetFloatv(GL_ALPHA_TEST_REF, &ref);
4270 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4271 glAlphaFunc(vmsvgaCmpFunc2GL(pRenderState[i].uintValue), ref);
4272 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4273 break;
4274 }
4275
4276 case SVGA3D_RS_ALPHAREF: /* float (0.0 .. 1.0) */
4277 {
4278 GLint func;
4279
4280 glGetIntegerv(GL_ALPHA_TEST_FUNC, &func);
4281 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4282 glAlphaFunc(func, pRenderState[i].floatValue);
4283 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4284 break;
4285 }
4286
4287 case SVGA3D_RS_STENCILENABLE: /* SVGA3dBool */
4288 enableCap = GL_STENCIL_TEST;
4289 val = pRenderState[i].uintValue;
4290 break;
4291
4292 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc */
4293 case SVGA3D_RS_STENCILREF: /* uint32_t */
4294 case SVGA3D_RS_STENCILMASK: /* uint32_t */
4295 {
4296 GLint func, ref;
4297 GLuint mask;
4298
4299 glGetIntegerv(GL_STENCIL_FUNC, &func);
4300 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4301 glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)&mask);
4302 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4303 glGetIntegerv(GL_STENCIL_REF, &ref);
4304 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4305
4306 switch (pRenderState[i].state)
4307 {
4308 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc */
4309 func = vmsvgaCmpFunc2GL(pRenderState[i].uintValue);
4310 break;
4311
4312 case SVGA3D_RS_STENCILREF: /* uint32_t */
4313 ref = pRenderState[i].uintValue;
4314 break;
4315
4316 case SVGA3D_RS_STENCILMASK: /* uint32_t */
4317 mask = pRenderState[i].uintValue;
4318 break;
4319
4320 default:
4321 /* not possible; shut up gcc */
4322 AssertFailed();
4323 break;
4324 }
4325
4326 glStencilFunc(func, ref, mask);
4327 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4328 break;
4329 }
4330
4331 case SVGA3D_RS_STENCILWRITEMASK: /* uint32_t */
4332 glStencilMask(pRenderState[i].uintValue);
4333 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4334 break;
4335
4336 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp */
4337 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp */
4338 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp */
4339 {
4340 GLint sfail, dpfail, dppass;
4341 GLenum stencilop = vmsvgaStencipOp2GL(pRenderState[i].uintValue);
4342
4343 glGetIntegerv(GL_STENCIL_FAIL, &sfail);
4344 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4345 glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, &dpfail);
4346 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4347 glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, &dppass);
4348 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4349
4350 switch (pRenderState[i].state)
4351 {
4352 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp */
4353 sfail = stencilop;
4354 break;
4355 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp */
4356 dpfail = stencilop;
4357 break;
4358 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp */
4359 dppass = stencilop;
4360 break;
4361 default:
4362 /* not possible; shut up gcc */
4363 AssertFailed();
4364 break;
4365 }
4366 glStencilOp(sfail, dpfail, dppass);
4367 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4368 break;
4369 }
4370
4371 case SVGA3D_RS_STENCILENABLE2SIDED: /* SVGA3dBool */
4372 /* @note GL_EXT_stencil_two_side required! */
4373 if (pState->ext.fEXT_stencil_two_side)
4374 {
4375 enableCap = GL_STENCIL_TEST_TWO_SIDE_EXT;
4376 val = pRenderState[i].uintValue;
4377 }
4378 else
4379 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_STENCILENABLE2SIDED\n"));
4380 break;
4381
4382 case SVGA3D_RS_CCWSTENCILFUNC: /* SVGA3dCmpFunc */
4383 {
4384 /** @todo SVGA3D_RS_STENCILFAIL/ZFAIL/PASS for front & back faces
4385 * SVGA3D_RS_CCWSTENCILFAIL/ZFAIL/PASS for back faces ??
4386 */
4387 GLint ref;
4388 GLuint mask;
4389
4390 glGetIntegerv(GL_STENCIL_BACK_VALUE_MASK, (GLint *)&mask);
4391 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4392 glGetIntegerv(GL_STENCIL_BACK_REF, &ref);
4393 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4394
4395 pState->ext.glStencilFuncSeparate(GL_BACK, vmsvgaCmpFunc2GL(pRenderState[i].uintValue), ref, mask);
4396 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4397 break;
4398 }
4399
4400 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp */
4401 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp */
4402 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp */
4403 {
4404 /** @todo SVGA3D_RS_STENCILFAIL/ZFAIL/PASS for front & back faces
4405 * SVGA3D_RS_CCWSTENCILFAIL/ZFAIL/PASS for back faces ??
4406 */
4407 GLint sfail, dpfail, dppass;
4408 GLenum stencilop = vmsvgaStencipOp2GL(pRenderState[i].uintValue);
4409
4410 glGetIntegerv(GL_STENCIL_BACK_FAIL, &sfail);
4411 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4412 glGetIntegerv(GL_STENCIL_BACK_PASS_DEPTH_FAIL, &dpfail);
4413 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4414 glGetIntegerv(GL_STENCIL_BACK_PASS_DEPTH_PASS, &dppass);
4415 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4416
4417 switch (pRenderState[i].state)
4418 {
4419 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp */
4420 sfail = stencilop;
4421 break;
4422 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp */
4423 dpfail = stencilop;
4424 break;
4425 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp */
4426 dppass = stencilop;
4427 break;
4428 default:
4429 /* not possible; shut up gcc */
4430 AssertFailed();
4431 break;
4432 }
4433 pState->ext.glStencilOpSeparate(GL_BACK, sfail, dpfail, dppass);
4434 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4435 break;
4436 }
4437
4438 case SVGA3D_RS_ZBIAS: /* float */
4439 /** @todo unknown meaning; depth bias is not identical
4440 renderState = D3DRS_DEPTHBIAS;
4441 val = pRenderState[i].uintValue;
4442 */
4443 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_ZBIAS\n"));
4444 break;
4445
4446 case SVGA3D_RS_DEPTHBIAS: /* float */
4447 {
4448 GLfloat factor;
4449
4450 /** @todo not sure if the d3d & ogl definitions are identical. */
4451
4452 /* Do not change the factor part. */
4453 glGetFloatv(GL_POLYGON_OFFSET_FACTOR, &factor);
4454 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4455
4456 glPolygonOffset(factor, pRenderState[i].floatValue);
4457 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4458 break;
4459 }
4460
4461 case SVGA3D_RS_SLOPESCALEDEPTHBIAS: /* float */
4462 {
4463 GLfloat units;
4464
4465 /** @todo not sure if the d3d & ogl definitions are identical. */
4466
4467 /* Do not change the factor part. */
4468 glGetFloatv(GL_POLYGON_OFFSET_UNITS, &units);
4469 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4470
4471 glPolygonOffset(pRenderState[i].floatValue, units);
4472 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4473 break;
4474 }
4475
4476 case SVGA3D_RS_COLORWRITEENABLE: /* SVGA3dColorMask */
4477 {
4478 GLboolean red, green, blue, alpha;
4479 SVGA3dColorMask mask;
4480
4481 mask.uintValue = pRenderState[i].uintValue;
4482
4483 red = mask.s.red;
4484 green = mask.s.green;
4485 blue = mask.s.blue;
4486 alpha = mask.s.alpha;
4487
4488 glColorMask(red, green, blue, alpha);
4489 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4490 break;
4491 }
4492
4493 case SVGA3D_RS_COLORWRITEENABLE1: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
4494 case SVGA3D_RS_COLORWRITEENABLE2: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
4495 case SVGA3D_RS_COLORWRITEENABLE3: /* SVGA3dColorMask to D3DCOLORWRITEENABLE_* */
4496 Log(("vmsvga3dSetRenderState: WARNING SVGA3D_RS_COLORWRITEENABLEx not supported!!\n"));
4497 break;
4498
4499 case SVGA3D_RS_SCISSORTESTENABLE: /* SVGA3dBool */
4500 enableCap = GL_SCISSOR_TEST;
4501 val = pRenderState[i].uintValue;
4502 break;
4503
4504#if 0
4505 case SVGA3D_RS_DIFFUSEMATERIALSOURCE: /* SVGA3dVertexMaterial */
4506 AssertCompile(D3DMCS_COLOR2 == SVGA3D_VERTEXMATERIAL_SPECULAR);
4507 renderState = D3DRS_DIFFUSEMATERIALSOURCE;
4508 val = pRenderState[i].uintValue;
4509 break;
4510
4511 case SVGA3D_RS_SPECULARMATERIALSOURCE: /* SVGA3dVertexMaterial */
4512 renderState = D3DRS_SPECULARMATERIALSOURCE;
4513 val = pRenderState[i].uintValue;
4514 break;
4515
4516 case SVGA3D_RS_AMBIENTMATERIALSOURCE: /* SVGA3dVertexMaterial */
4517 renderState = D3DRS_AMBIENTMATERIALSOURCE;
4518 val = pRenderState[i].uintValue;
4519 break;
4520
4521 case SVGA3D_RS_EMISSIVEMATERIALSOURCE: /* SVGA3dVertexMaterial */
4522 renderState = D3DRS_EMISSIVEMATERIALSOURCE;
4523 val = pRenderState[i].uintValue;
4524 break;
4525#endif
4526
4527 case SVGA3D_RS_WRAP3: /* SVGA3dWrapFlags */
4528 case SVGA3D_RS_WRAP4: /* SVGA3dWrapFlags */
4529 case SVGA3D_RS_WRAP5: /* SVGA3dWrapFlags */
4530 case SVGA3D_RS_WRAP6: /* SVGA3dWrapFlags */
4531 case SVGA3D_RS_WRAP7: /* SVGA3dWrapFlags */
4532 case SVGA3D_RS_WRAP8: /* SVGA3dWrapFlags */
4533 case SVGA3D_RS_WRAP9: /* SVGA3dWrapFlags */
4534 case SVGA3D_RS_WRAP10: /* SVGA3dWrapFlags */
4535 case SVGA3D_RS_WRAP11: /* SVGA3dWrapFlags */
4536 case SVGA3D_RS_WRAP12: /* SVGA3dWrapFlags */
4537 case SVGA3D_RS_WRAP13: /* SVGA3dWrapFlags */
4538 case SVGA3D_RS_WRAP14: /* SVGA3dWrapFlags */
4539 case SVGA3D_RS_WRAP15: /* SVGA3dWrapFlags */
4540 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_WRAPx (x >= 3)\n"));
4541 break;
4542
4543 case SVGA3D_RS_LASTPIXEL: /* SVGA3dBool */
4544 case SVGA3D_RS_TWEENFACTOR: /* float */
4545 case SVGA3D_RS_INDEXEDVERTEXBLENDENABLE: /* SVGA3dBool */
4546 case SVGA3D_RS_VERTEXBLEND: /* SVGA3dVertexBlendFlags */
4547 Log(("vmsvga3dSetRenderState: WARNING not applicable!!\n"));
4548 break;
4549
4550 case SVGA3D_RS_MULTISAMPLEANTIALIAS: /* SVGA3dBool */
4551 enableCap = GL_MULTISAMPLE;
4552 val = pRenderState[i].uintValue;
4553 break;
4554
4555 case SVGA3D_RS_MULTISAMPLEMASK: /* uint32_t */
4556 case SVGA3D_RS_ANTIALIASEDLINEENABLE: /* SVGA3dBool */
4557 Log(("vmsvga3dSetRenderState: WARNING not applicable??!!\n"));
4558 break;
4559
4560 case SVGA3D_RS_COORDINATETYPE: /* SVGA3dCoordinateType */
4561 Assert(pRenderState[i].uintValue == SVGA3D_COORDINATE_LEFTHANDED);
4562 /** @todo setup a view matrix to scale the world space by -1 in the z-direction for right handed coordinates. */
4563 /*
4564 renderState = D3DRS_COORDINATETYPE;
4565 val = pRenderState[i].uintValue;
4566 */
4567 break;
4568
4569 case SVGA3D_RS_FRONTWINDING: /* SVGA3dFrontWinding */
4570 Assert(pRenderState[i].uintValue == SVGA3D_FRONTWINDING_CW);
4571 /* Invert the selected mode because of y-inversion (?) */
4572 glFrontFace((pRenderState[i].uintValue != SVGA3D_FRONTWINDING_CW) ? GL_CW : GL_CCW);
4573 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4574 break;
4575
4576 case SVGA3D_RS_OUTPUTGAMMA: /* float */
4577 //AssertFailed();
4578 /*
4579 D3DRS_SRGBWRITEENABLE ??
4580 renderState = D3DRS_OUTPUTGAMMA;
4581 val = pRenderState[i].uintValue;
4582 */
4583 break;
4584
4585#if 0
4586
4587 case SVGA3D_RS_VERTEXMATERIALENABLE: /* SVGA3dBool */
4588 //AssertFailed();
4589 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE; /* correct?? */
4590 val = pRenderState[i].uintValue;
4591 break;
4592
4593 case SVGA3D_RS_TEXTUREFACTOR: /* SVGA3dColor */
4594 renderState = D3DRS_TEXTUREFACTOR;
4595 val = pRenderState[i].uintValue;
4596 break;
4597
4598 case SVGA3D_RS_LOCALVIEWER: /* SVGA3dBool */
4599 renderState = D3DRS_LOCALVIEWER;
4600 val = pRenderState[i].uintValue;
4601 break;
4602
4603 case SVGA3D_RS_ZVISIBLE: /* SVGA3dBool */
4604 AssertFailed();
4605 /*
4606 renderState = D3DRS_ZVISIBLE;
4607 val = pRenderState[i].uintValue;
4608 */
4609 break;
4610
4611 case SVGA3D_RS_CLIPPING: /* SVGA3dBool */
4612 renderState = D3DRS_CLIPPING;
4613 val = pRenderState[i].uintValue;
4614 break;
4615
4616 case SVGA3D_RS_WRAP0: /* SVGA3dWrapFlags */
4617 glTexParameter GL_TEXTURE_WRAP_S
4618 Assert(SVGA3D_WRAPCOORD_3 == D3DWRAPCOORD_3);
4619 renderState = D3DRS_WRAP0;
4620 val = pRenderState[i].uintValue;
4621 break;
4622
4623 case SVGA3D_RS_WRAP1: /* SVGA3dWrapFlags */
4624 glTexParameter GL_TEXTURE_WRAP_T
4625 renderState = D3DRS_WRAP1;
4626 val = pRenderState[i].uintValue;
4627 break;
4628
4629 case SVGA3D_RS_WRAP2: /* SVGA3dWrapFlags */
4630 glTexParameter GL_TEXTURE_WRAP_R
4631 renderState = D3DRS_WRAP2;
4632 val = pRenderState[i].uintValue;
4633 break;
4634
4635
4636 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
4637 renderState = D3DRS_SEPARATEALPHABLENDENABLE;
4638 val = pRenderState[i].uintValue;
4639 break;
4640
4641
4642 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation */
4643 renderState = D3DRS_BLENDOPALPHA;
4644 val = pRenderState[i].uintValue;
4645 break;
4646
4647 case SVGA3D_RS_TRANSPARENCYANTIALIAS: /* SVGA3dTransparencyAntialiasType */
4648 AssertFailed();
4649 /*
4650 renderState = D3DRS_TRANSPARENCYANTIALIAS;
4651 val = pRenderState[i].uintValue;
4652 */
4653 break;
4654
4655#endif
4656 default:
4657 AssertFailed();
4658 break;
4659 }
4660
4661 if (enableCap != ~0U)
4662 {
4663 if (val)
4664 glEnable(enableCap);
4665 else
4666 glDisable(enableCap);
4667 }
4668 }
4669
4670 return VINF_SUCCESS;
4671}
4672
4673int vmsvga3dSetRenderTarget(PVGASTATE pThis, uint32_t cid, SVGA3dRenderTargetType type, SVGA3dSurfaceImageId target)
4674{
4675 PVMSVGA3DCONTEXT pContext;
4676 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4677 PVMSVGA3DSURFACE pRenderTarget;
4678
4679 AssertReturn(pState, VERR_NO_MEMORY);
4680 AssertReturn(type < SVGA3D_RT_MAX, VERR_INVALID_PARAMETER);
4681 AssertReturn(target.face == 0, VERR_INVALID_PARAMETER);
4682
4683 Log(("vmsvga3dSetRenderTarget cid=%x type=%x surface id=%x\n", cid, type, target.sid));
4684
4685 if ( cid >= pState->cContexts
4686 || pState->papContexts[cid]->id != cid)
4687 {
4688 Log(("vmsvga3dSetRenderTarget invalid context id!\n"));
4689 return VERR_INVALID_PARAMETER;
4690 }
4691 pContext = pState->papContexts[cid];
4692 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4693
4694 /* Save for vm state save/restore. */
4695 pContext->state.aRenderTargets[type] = target.sid;
4696
4697 if (target.sid == SVGA3D_INVALID_ID)
4698 {
4699 /* Disable render target. */
4700 switch (type)
4701 {
4702 case SVGA3D_RT_DEPTH:
4703 case SVGA3D_RT_STENCIL:
4704 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER, (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
4705 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4706 break;
4707
4708 case SVGA3D_RT_COLOR0:
4709 case SVGA3D_RT_COLOR1:
4710 case SVGA3D_RT_COLOR2:
4711 case SVGA3D_RT_COLOR3:
4712 case SVGA3D_RT_COLOR4:
4713 case SVGA3D_RT_COLOR5:
4714 case SVGA3D_RT_COLOR6:
4715 case SVGA3D_RT_COLOR7:
4716 pContext->sidRenderTarget = SVGA3D_INVALID_ID;
4717 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0, 0, 0, 0);
4718 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4719 break;
4720
4721 default:
4722 AssertFailedReturn(VERR_INVALID_PARAMETER);
4723 }
4724 return VINF_SUCCESS;
4725 }
4726
4727 AssertReturn(target.sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
4728 AssertReturn(target.sid < pState->cSurfaces && pState->papSurfaces[target.sid]->id == target.sid, VERR_INVALID_PARAMETER);
4729 pRenderTarget = pState->papSurfaces[target.sid];
4730
4731 switch (type)
4732 {
4733 case SVGA3D_RT_DEPTH:
4734 case SVGA3D_RT_STENCIL:
4735 AssertReturn(target.mipmap == 0, VERR_INVALID_PARAMETER);
4736 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
4737 {
4738 Log(("vmsvga3dSetRenderTarget: create renderbuffer to be used as render target; surface id=%x type=%d format=%d\n", target.sid, pRenderTarget->flags, pRenderTarget->internalFormatGL));
4739 pContext = &pState->SharedCtx;
4740 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4741
4742 pState->ext.glGenRenderbuffers(1, &pRenderTarget->oglId.renderbuffer);
4743 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4744
4745 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
4746 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4747
4748 pState->ext.glRenderbufferStorage(GL_RENDERBUFFER,
4749 pRenderTarget->internalFormatGL,
4750 pRenderTarget->pMipmapLevels[0].size.width,
4751 pRenderTarget->pMipmapLevels[0].size.height);
4752 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4753
4754 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, OPENGL_INVALID_ID);
4755 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4756
4757 pContext = pState->papContexts[cid];
4758 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
4759 pRenderTarget->idWeakContextAssociation = cid;
4760 }
4761
4762 pState->ext.glBindRenderbuffer(GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
4763 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4764 Assert(!pRenderTarget->fDirty);
4765 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
4766
4767 pRenderTarget->flags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
4768
4769 pState->ext.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
4770 (type == SVGA3D_RT_DEPTH) ? GL_DEPTH_ATTACHMENT : GL_STENCIL_ATTACHMENT,
4771 GL_RENDERBUFFER, pRenderTarget->oglId.renderbuffer);
4772 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4773 break;
4774
4775 case SVGA3D_RT_COLOR0:
4776 case SVGA3D_RT_COLOR1:
4777 case SVGA3D_RT_COLOR2:
4778 case SVGA3D_RT_COLOR3:
4779 case SVGA3D_RT_COLOR4:
4780 case SVGA3D_RT_COLOR5:
4781 case SVGA3D_RT_COLOR6:
4782 case SVGA3D_RT_COLOR7:
4783 {
4784 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
4785 if (pRenderTarget->oglId.texture == OPENGL_INVALID_ID)
4786 {
4787 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));
4788 int rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
4789 AssertRCReturn(rc, rc);
4790 }
4791
4792 AssertReturn(pRenderTarget->oglId.texture != OPENGL_INVALID_ID, VERR_INVALID_PARAMETER);
4793 Assert(!pRenderTarget->fDirty);
4794
4795 pRenderTarget->flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
4796
4797 pState->ext.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + type - SVGA3D_RT_COLOR0, GL_TEXTURE_2D, pRenderTarget->oglId.texture, target.mipmap);
4798 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
4799
4800 pContext->sidRenderTarget = target.sid;
4801
4802#ifdef DEBUG
4803 GLenum status = pState->ext.glCheckFramebufferStatus(GL_FRAMEBUFFER);
4804 if (status != GL_FRAMEBUFFER_COMPLETE)
4805 Log(("vmsvga3dSetRenderTarget: WARNING: glCheckFramebufferStatus returned %x\n", status));
4806#endif
4807 /** @todo use glDrawBuffers too? */
4808 break;
4809 }
4810
4811 default:
4812 AssertFailedReturn(VERR_INVALID_PARAMETER);
4813 }
4814
4815 return VINF_SUCCESS;
4816}
4817
4818#if 0
4819/**
4820 * Convert SVGA texture combiner value to its D3D equivalent
4821 */
4822static DWORD vmsvga3dTextureCombiner2D3D(uint32_t value)
4823{
4824 switch (value)
4825 {
4826 case SVGA3D_TC_DISABLE:
4827 return D3DTOP_DISABLE;
4828 case SVGA3D_TC_SELECTARG1:
4829 return D3DTOP_SELECTARG1;
4830 case SVGA3D_TC_SELECTARG2:
4831 return D3DTOP_SELECTARG2;
4832 case SVGA3D_TC_MODULATE:
4833 return D3DTOP_MODULATE;
4834 case SVGA3D_TC_ADD:
4835 return D3DTOP_ADD;
4836 case SVGA3D_TC_ADDSIGNED:
4837 return D3DTOP_ADDSIGNED;
4838 case SVGA3D_TC_SUBTRACT:
4839 return D3DTOP_SUBTRACT;
4840 case SVGA3D_TC_BLENDTEXTUREALPHA:
4841 return D3DTOP_BLENDTEXTUREALPHA;
4842 case SVGA3D_TC_BLENDDIFFUSEALPHA:
4843 return D3DTOP_BLENDDIFFUSEALPHA;
4844 case SVGA3D_TC_BLENDCURRENTALPHA:
4845 return D3DTOP_BLENDCURRENTALPHA;
4846 case SVGA3D_TC_BLENDFACTORALPHA:
4847 return D3DTOP_BLENDFACTORALPHA;
4848 case SVGA3D_TC_MODULATE2X:
4849 return D3DTOP_MODULATE2X;
4850 case SVGA3D_TC_MODULATE4X:
4851 return D3DTOP_MODULATE4X;
4852 case SVGA3D_TC_DSDT:
4853 AssertFailed(); /** @todo ??? */
4854 return D3DTOP_DISABLE;
4855 case SVGA3D_TC_DOTPRODUCT3:
4856 return D3DTOP_DOTPRODUCT3;
4857 case SVGA3D_TC_BLENDTEXTUREALPHAPM:
4858 return D3DTOP_BLENDTEXTUREALPHAPM;
4859 case SVGA3D_TC_ADDSIGNED2X:
4860 return D3DTOP_ADDSIGNED2X;
4861 case SVGA3D_TC_ADDSMOOTH:
4862 return D3DTOP_ADDSMOOTH;
4863 case SVGA3D_TC_PREMODULATE:
4864 return D3DTOP_PREMODULATE;
4865 case SVGA3D_TC_MODULATEALPHA_ADDCOLOR:
4866 return D3DTOP_MODULATEALPHA_ADDCOLOR;
4867 case SVGA3D_TC_MODULATECOLOR_ADDALPHA:
4868 return D3DTOP_MODULATECOLOR_ADDALPHA;
4869 case SVGA3D_TC_MODULATEINVALPHA_ADDCOLOR:
4870 return D3DTOP_MODULATEINVALPHA_ADDCOLOR;
4871 case SVGA3D_TC_MODULATEINVCOLOR_ADDALPHA:
4872 return D3DTOP_MODULATEINVCOLOR_ADDALPHA;
4873 case SVGA3D_TC_BUMPENVMAPLUMINANCE:
4874 return D3DTOP_BUMPENVMAPLUMINANCE;
4875 case SVGA3D_TC_MULTIPLYADD:
4876 return D3DTOP_MULTIPLYADD;
4877 case SVGA3D_TC_LERP:
4878 return D3DTOP_LERP;
4879 default:
4880 AssertFailed();
4881 return D3DTOP_DISABLE;
4882 }
4883}
4884
4885/**
4886 * Convert SVGA texture arg data value to its D3D equivalent
4887 */
4888static DWORD vmsvga3dTextureArgData2D3D(uint32_t value)
4889{
4890 switch (value)
4891 {
4892 case SVGA3D_TA_CONSTANT:
4893 return D3DTA_CONSTANT;
4894 case SVGA3D_TA_PREVIOUS:
4895 return D3DTA_CURRENT; /* current = previous */
4896 case SVGA3D_TA_DIFFUSE:
4897 return D3DTA_DIFFUSE;
4898 case SVGA3D_TA_TEXTURE:
4899 return D3DTA_TEXTURE;
4900 case SVGA3D_TA_SPECULAR:
4901 return D3DTA_SPECULAR;
4902 default:
4903 AssertFailed();
4904 return 0;
4905 }
4906}
4907
4908/**
4909 * Convert SVGA texture transform flag value to its D3D equivalent
4910 */
4911static DWORD vmsvga3dTextTransformFlags2D3D(uint32_t value)
4912{
4913 switch (value)
4914 {
4915 case SVGA3D_TEX_TRANSFORM_OFF:
4916 return D3DTTFF_DISABLE;
4917 case SVGA3D_TEX_TRANSFORM_S:
4918 return D3DTTFF_COUNT1; /** @todo correct? */
4919 case SVGA3D_TEX_TRANSFORM_T:
4920 return D3DTTFF_COUNT2; /** @todo correct? */
4921 case SVGA3D_TEX_TRANSFORM_R:
4922 return D3DTTFF_COUNT3; /** @todo correct? */
4923 case SVGA3D_TEX_TRANSFORM_Q:
4924 return D3DTTFF_COUNT4; /** @todo correct? */
4925 case SVGA3D_TEX_PROJECTED:
4926 return D3DTTFF_PROJECTED;
4927 default:
4928 AssertFailed();
4929 return 0;
4930 }
4931}
4932#endif
4933
4934static GLenum vmsvga3dTextureAddress2OGL(SVGA3dTextureAddress value)
4935{
4936 switch (value)
4937 {
4938 case SVGA3D_TEX_ADDRESS_WRAP:
4939 return GL_REPEAT;
4940 case SVGA3D_TEX_ADDRESS_MIRROR:
4941 return GL_MIRRORED_REPEAT;
4942 case SVGA3D_TEX_ADDRESS_CLAMP:
4943 return GL_CLAMP_TO_EDGE;
4944 case SVGA3D_TEX_ADDRESS_BORDER:
4945 return GL_CLAMP_TO_BORDER;
4946 case SVGA3D_TEX_ADDRESS_MIRRORONCE:
4947 AssertFailed();
4948 return GL_CLAMP_TO_EDGE_SGIS; /** @todo correct? */
4949
4950 case SVGA3D_TEX_ADDRESS_EDGE:
4951 case SVGA3D_TEX_ADDRESS_INVALID:
4952 default:
4953 AssertFailed();
4954 return GL_REPEAT; /* default */
4955 }
4956}
4957
4958static GLenum vmsvga3dTextureFilter2OGL(SVGA3dTextureFilter value)
4959{
4960 switch (value)
4961 {
4962 case SVGA3D_TEX_FILTER_NONE:
4963 case SVGA3D_TEX_FILTER_LINEAR:
4964 return GL_LINEAR;
4965 case SVGA3D_TEX_FILTER_NEAREST:
4966 return GL_NEAREST;
4967 case SVGA3D_TEX_FILTER_ANISOTROPIC:
4968 /** @todo */
4969 case SVGA3D_TEX_FILTER_FLATCUBIC: // Deprecated, not implemented
4970 case SVGA3D_TEX_FILTER_GAUSSIANCUBIC: // Deprecated, not implemented
4971 case SVGA3D_TEX_FILTER_PYRAMIDALQUAD: // Not currently implemented
4972 case SVGA3D_TEX_FILTER_GAUSSIANQUAD: // Not currently implemented
4973 default:
4974 AssertFailed();
4975 return GL_LINEAR; /* default */
4976 }
4977}
4978
4979uint32_t vmsvga3dSVGA3dColor2RGBA(SVGA3dColor value)
4980{
4981 /* flip the red and blue bytes */
4982 uint8_t blue = value & 0xff;
4983 uint8_t red = (value >> 16) & 0xff;
4984 return (value & 0xff00ff00) | red | (blue << 16);
4985}
4986
4987int vmsvga3dSetTextureState(PVGASTATE pThis, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
4988{
4989 GLenum val;
4990 GLenum currentStage = ~0L;
4991 PVMSVGA3DCONTEXT pContext;
4992 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4993 AssertReturn(pState, VERR_NO_MEMORY);
4994
4995 Log(("vmsvga3dSetTextureState %x cTextureState=%d\n", cid, cTextureStates));
4996
4997 if ( cid >= pState->cContexts
4998 || pState->papContexts[cid]->id != cid)
4999 {
5000 Log(("vmsvga3dSetTextureState invalid context id!\n"));
5001 return VERR_INVALID_PARAMETER;
5002 }
5003 pContext = pState->papContexts[cid];
5004 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5005
5006 for (unsigned i = 0; i < cTextureStates; i++)
5007 {
5008 GLenum textureType = ~0U;
5009 GLenum samplerType = ~0U;
5010
5011 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));
5012 /* Record the texture state for vm state saving. */
5013 if ( pTextureState[i].stage < SVGA3D_MAX_TEXTURE_STAGE
5014 && pTextureState[i].name < SVGA3D_TS_MAX)
5015 {
5016 pContext->state.aTextureState[pTextureState[i].stage][pTextureState[i].name] = pTextureState[i];
5017 }
5018
5019 /* Active the right texture unit for subsequent texture state changes. */
5020 if (pTextureState[i].stage != currentStage || i == 0)
5021 {
5022 /** @todo Is this the appropriate limit for all kinds of textures? It is the
5023 * size of aSidActiveTexture and for binding/unbinding we cannot exceed it. */
5024 if (pTextureState[i].stage < SVGA3D_MAX_TEXTURE_STAGE)
5025 {
5026 pState->ext.glActiveTexture(GL_TEXTURE0 + pTextureState[i].stage);
5027 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5028 currentStage = pTextureState[i].stage;
5029 }
5030 else
5031 {
5032 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x\n", i, pTextureState[i].stage, pTextureState[i].name));
5033 continue;
5034 }
5035 }
5036
5037 switch (pTextureState[i].name)
5038 {
5039 case SVGA3D_TS_BUMPENVMAT00: /* float */
5040 case SVGA3D_TS_BUMPENVMAT01: /* float */
5041 case SVGA3D_TS_BUMPENVMAT10: /* float */
5042 case SVGA3D_TS_BUMPENVMAT11: /* float */
5043 case SVGA3D_TS_BUMPENVLSCALE: /* float */
5044 case SVGA3D_TS_BUMPENVLOFFSET: /* float */
5045 Log(("vmsvga3dSetTextureState: bump mapping texture options not supported!!\n"));
5046 break;
5047
5048 case SVGA3D_TS_COLOROP: /* SVGA3dTextureCombiner */
5049 case SVGA3D_TS_COLORARG0: /* SVGA3dTextureArgData */
5050 case SVGA3D_TS_COLORARG1: /* SVGA3dTextureArgData */
5051 case SVGA3D_TS_COLORARG2: /* SVGA3dTextureArgData */
5052 case SVGA3D_TS_ALPHAOP: /* SVGA3dTextureCombiner */
5053 case SVGA3D_TS_ALPHAARG0: /* SVGA3dTextureArgData */
5054 case SVGA3D_TS_ALPHAARG1: /* SVGA3dTextureArgData */
5055 case SVGA3D_TS_ALPHAARG2: /* SVGA3dTextureArgData */
5056 /** @todo not used by MesaGL */
5057 Log(("vmsvga3dSetTextureState: colorop/alphaop not yet supported!!\n"));
5058 break;
5059#if 0
5060
5061 case SVGA3D_TS_TEXCOORDINDEX: /* uint32_t */
5062 textureType = D3DTSS_TEXCOORDINDEX;
5063 val = pTextureState[i].value;
5064 break;
5065
5066 case SVGA3D_TS_TEXTURETRANSFORMFLAGS: /* SVGA3dTexTransformFlags */
5067 textureType = D3DTSS_TEXTURETRANSFORMFLAGS;
5068 val = vmsvga3dTextTransformFlags2D3D(pTextureState[i].value);
5069 break;
5070#endif
5071
5072 case SVGA3D_TS_BIND_TEXTURE: /* SVGA3dSurfaceId */
5073 if (pTextureState[i].value == SVGA3D_INVALID_ID)
5074 {
5075 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture surface id=%x replacing=%x\n",
5076 currentStage, pTextureState[i].value, pContext->aSidActiveTexture[currentStage]));
5077
5078 pContext->aSidActiveTexture[currentStage] = SVGA3D_INVALID_ID;
5079 /* Unselect the currently associated texture. */
5080 glBindTexture(GL_TEXTURE_2D, 0);
5081 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5082 /* Necessary for the fixed pipeline. */
5083 glDisable(GL_TEXTURE_2D);
5084 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5085 }
5086 else
5087 {
5088 uint32_t sid = pTextureState[i].value;
5089
5090 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
5091 AssertReturn(sid < pState->cSurfaces && pState->papSurfaces[sid]->id == sid, VERR_INVALID_PARAMETER);
5092
5093 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
5094
5095 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture surface id=%x (%d,%d) replacing=%x\n",
5096 currentStage, pTextureState[i].value, pSurface->pMipmapLevels[0].size.width,
5097 pSurface->pMipmapLevels[0].size.height, pContext->aSidActiveTexture[currentStage]));
5098
5099 if (pSurface->oglId.texture == OPENGL_INVALID_ID)
5100 {
5101 Log(("CreateTexture (%d,%d) level=%d\n", pSurface->pMipmapLevels[0].size.width, pSurface->pMipmapLevels[0].size.height, pSurface->faces[0].numMipLevels));
5102 int rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pSurface);
5103 AssertRCReturn(rc, rc);
5104 }
5105
5106 glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture);
5107 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5108
5109 /* Necessary for the fixed pipeline. */
5110 glEnable(GL_TEXTURE_2D);
5111 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5112
5113 if (pContext->aSidActiveTexture[currentStage] != sid)
5114 {
5115 /* Recreate the texture state as glBindTexture resets them all (sigh). */
5116 for (uint32_t iStage = 0; iStage < SVGA3D_MAX_TEXTURE_STAGE; iStage++)
5117 {
5118 for (uint32_t j = 0; j < SVGA3D_TS_MAX; j++)
5119 {
5120 SVGA3dTextureState *pTextureStateIter = &pContext->state.aTextureState[iStage][j];
5121
5122 if ( pTextureStateIter->name != SVGA3D_TS_INVALID
5123 && pTextureStateIter->name != SVGA3D_TS_BIND_TEXTURE)
5124 vmsvga3dSetTextureState(pThis, pContext->id, 1, pTextureStateIter);
5125 }
5126 }
5127 }
5128 pContext->aSidActiveTexture[currentStage] = sid;
5129 }
5130 /* Finished; continue with the next one. */
5131 continue;
5132
5133 case SVGA3D_TS_ADDRESSW: /* SVGA3dTextureAddress */
5134 textureType = GL_TEXTURE_WRAP_R; /* R = W */
5135 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
5136 break;
5137
5138 case SVGA3D_TS_ADDRESSU: /* SVGA3dTextureAddress */
5139 textureType = GL_TEXTURE_WRAP_S; /* S = U */
5140 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
5141 break;
5142
5143 case SVGA3D_TS_ADDRESSV: /* SVGA3dTextureAddress */
5144 textureType = GL_TEXTURE_WRAP_T; /* T = V */
5145 val = vmsvga3dTextureAddress2OGL((SVGA3dTextureAddress)pTextureState[i].value);
5146 break;
5147
5148 case SVGA3D_TS_MIPFILTER: /* SVGA3dTextureFilter */
5149 case SVGA3D_TS_MINFILTER: /* SVGA3dTextureFilter */
5150 {
5151 uint32_t mipFilter = pContext->state.aTextureState[currentStage][SVGA3D_TS_MIPFILTER].value;
5152 uint32_t minFilter = pContext->state.aTextureState[currentStage][SVGA3D_TS_MINFILTER].value;
5153
5154 /* If SVGA3D_TS_MIPFILTER is set to NONE, then use SVGA3D_TS_MIPFILTER, otherwise SVGA3D_TS_MIPFILTER enables mipmap minification. */
5155 textureType = GL_TEXTURE_MIN_FILTER;
5156 if (mipFilter != SVGA3D_TEX_FILTER_NONE)
5157 {
5158 if (minFilter == SVGA3D_TEX_FILTER_NEAREST)
5159 {
5160 if (mipFilter == SVGA3D_TEX_FILTER_LINEAR)
5161 val = GL_NEAREST_MIPMAP_LINEAR;
5162 else
5163 val = GL_NEAREST_MIPMAP_NEAREST;
5164 }
5165 else
5166 {
5167 if (mipFilter == SVGA3D_TEX_FILTER_LINEAR)
5168 val = GL_LINEAR_MIPMAP_LINEAR;
5169 else
5170 val = GL_LINEAR_MIPMAP_NEAREST;
5171 }
5172 }
5173 else
5174 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)minFilter);
5175 break;
5176 }
5177
5178 case SVGA3D_TS_MAGFILTER: /* SVGA3dTextureFilter */
5179 textureType = GL_TEXTURE_MAG_FILTER;
5180 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)pTextureState[i].value);
5181 Assert(val == GL_NEAREST || val == GL_LINEAR);
5182 break;
5183
5184 case SVGA3D_TS_BORDERCOLOR: /* SVGA3dColor */
5185 {
5186 GLfloat color[4]; /* red, green, blue, alpha */
5187
5188 vmsvgaColor2GLFloatArray(pTextureState[i].value, &color[0], &color[1], &color[2], &color[3]);
5189
5190 glTexParameterfv(GL_TEXTURE_2D /** @todo flexible type */, GL_TEXTURE_BORDER_COLOR, color); /* Identical; default 0.0 identical too */
5191 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5192 break;
5193 }
5194
5195 case SVGA3D_TS_TEXTURE_LOD_BIAS: /* float */
5196 glTexParameterf(GL_TEXTURE_2D /** @todo flexible type */, GL_TEXTURE_LOD_BIAS, pTextureState[i].value); /* Identical; default 0.0 identical too */
5197 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5198 break;
5199
5200 case SVGA3D_TS_TEXTURE_MIPMAP_LEVEL: /* uint32_t */
5201 textureType = GL_TEXTURE_BASE_LEVEL;
5202 val = pTextureState[i].value;
5203 break;
5204
5205#if 0
5206 case SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL: /* uint32_t */
5207 samplerType = D3DSAMP_MAXANISOTROPY;
5208 val = pTextureState[i].value; /* Identical?? */
5209 break;
5210
5211 case SVGA3D_TS_GAMMA: /* float */
5212 samplerType = D3DSAMP_SRGBTEXTURE;
5213 /* Boolean in D3D */
5214 if (pTextureState[i].floatValue == 1.0f)
5215 val = FALSE;
5216 else
5217 val = TRUE;
5218 break;
5219#endif
5220 /* Internal commands, that don't map directly to the SetTextureStageState API. */
5221 case SVGA3D_TS_TEXCOORDGEN: /* SVGA3dTextureCoordGen */
5222 AssertFailed();
5223 break;
5224
5225 default:
5226 //AssertFailed();
5227 break;
5228 }
5229
5230 if (textureType != ~0U)
5231 {
5232 glTexParameteri(GL_TEXTURE_2D /** @todo flexible type */, textureType, val);
5233 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5234 }
5235 }
5236
5237 return VINF_SUCCESS;
5238}
5239
5240int vmsvga3dSetMaterial(PVGASTATE pThis, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
5241{
5242 PVMSVGA3DCONTEXT pContext;
5243 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5244 AssertReturn(pState, VERR_NO_MEMORY);
5245 GLenum oglFace;
5246
5247 Log(("vmsvga3dSetMaterial cid=%x face %d\n", cid, face));
5248
5249 if ( cid >= pState->cContexts
5250 || pState->papContexts[cid]->id != cid)
5251 {
5252 Log(("vmsvga3dSetMaterial invalid context id!\n"));
5253 return VERR_INVALID_PARAMETER;
5254 }
5255 pContext = pState->papContexts[cid];
5256 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5257
5258 switch (face)
5259 {
5260 case SVGA3D_FACE_NONE:
5261 case SVGA3D_FACE_FRONT:
5262 oglFace = GL_FRONT;
5263 break;
5264
5265 case SVGA3D_FACE_BACK:
5266 oglFace = GL_BACK;
5267 break;
5268
5269 case SVGA3D_FACE_FRONT_BACK:
5270 oglFace = GL_FRONT_AND_BACK;
5271 break;
5272
5273 default:
5274 AssertFailedReturn(VERR_INVALID_PARAMETER);
5275 }
5276
5277 /* Save for vm state save/restore. */
5278 pContext->state.aMaterial[face].fValid = true;
5279 pContext->state.aMaterial[face].material = *pMaterial;
5280 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_MATERIAL;
5281
5282 glMaterialfv(oglFace, GL_DIFFUSE, pMaterial->diffuse);
5283 glMaterialfv(oglFace, GL_AMBIENT, pMaterial->ambient);
5284 glMaterialfv(oglFace, GL_SPECULAR, pMaterial->specular);
5285 glMaterialfv(oglFace, GL_EMISSION, pMaterial->emissive);
5286 glMaterialfv(oglFace, GL_SHININESS, &pMaterial->shininess);
5287 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5288
5289 return VINF_SUCCESS;
5290}
5291
5292/** @todo Move into separate library as we are using logic from Wine here. */
5293int vmsvga3dSetLightData(PVGASTATE pThis, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
5294{
5295 PVMSVGA3DCONTEXT pContext;
5296 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5297 AssertReturn(pState, VERR_NO_MEMORY);
5298 float QuadAttenuation;
5299
5300 Log(("vmsvga3dSetLightData cid=%x index=%d type=%d\n", cid, index, pData->type));
5301
5302 if ( cid >= pState->cContexts
5303 || pState->papContexts[cid]->id != cid)
5304 {
5305 Log(("vmsvga3dSetLightData invalid context id!\n"));
5306 return VERR_INVALID_PARAMETER;
5307 }
5308 pContext = pState->papContexts[cid];
5309 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5310
5311 /* Store for vm state save/restore */
5312 if (index < SVGA3D_MAX_LIGHTS)
5313 {
5314 pContext->state.aLightData[index].fValidData = true;
5315 pContext->state.aLightData[index].data = *pData;
5316 }
5317 else
5318 AssertFailed();
5319
5320 if ( pData->attenuation0 < 0.0f
5321 || pData->attenuation1 < 0.0f
5322 || pData->attenuation2 < 0.0f)
5323 {
5324 Log(("vmsvga3dSetLightData: invalid negative attenuation values!!\n"));
5325 return VINF_SUCCESS; /* ignore; could crash the GL driver */
5326 }
5327
5328 /* Light settings are affected by the model view in OpenGL, the View transform in direct3d */
5329 glMatrixMode(GL_MODELVIEW);
5330 glPushMatrix();
5331 glLoadMatrixf(pContext->state.aTransformState[SVGA3D_TRANSFORM_VIEW].matrix);
5332
5333 glLightfv(GL_LIGHT0 + index, GL_DIFFUSE, pData->diffuse);
5334 glLightfv(GL_LIGHT0 + index, GL_SPECULAR, pData->specular);
5335 glLightfv(GL_LIGHT0 + index, GL_AMBIENT, pData->ambient);
5336
5337 if (pData->range * pData->range >= FLT_MIN)
5338 QuadAttenuation = 1.4f / (pData->range * pData->range);
5339 else
5340 QuadAttenuation = 0.0f;
5341
5342 switch (pData->type)
5343 {
5344 case SVGA3D_LIGHTTYPE_POINT:
5345 {
5346 GLfloat position[4];
5347
5348 position[0] = pData->position[0];
5349 position[1] = pData->position[1];
5350 position[2] = pData->position[2];
5351 position[3] = 1.0f;
5352
5353 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
5354 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5355
5356 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
5357 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5358
5359 /* Attenuation - Are these right? guessing... */
5360 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
5361 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5362
5363 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
5364 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5365
5366 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
5367 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5368
5369 /** @todo range */
5370 break;
5371 }
5372
5373 case SVGA3D_LIGHTTYPE_SPOT1:
5374 {
5375 GLfloat exponent;
5376 GLfloat position[4];
5377 const GLfloat pi = 4.0f * atanf(1.0f);
5378
5379 position[0] = pData->position[0];
5380 position[1] = pData->position[1];
5381 position[2] = pData->position[2];
5382 position[3] = 1.0f;
5383
5384 glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
5385 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5386
5387 position[0] = pData->direction[0];
5388 position[1] = pData->direction[1];
5389 position[2] = pData->direction[2];
5390 position[3] = 1.0f;
5391
5392 glLightfv(GL_LIGHT0 + index, GL_SPOT_DIRECTION, position);
5393 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5394
5395 /*
5396 * opengl-ish and d3d-ish spot lights use too different models for the
5397 * light "intensity" as a function of the angle towards the main light direction,
5398 * so we only can approximate very roughly.
5399 * however spot lights are rather rarely used in games (if ever used at all).
5400 * furthermore if still used, probably nobody pays attention to such details.
5401 */
5402 if (pData->falloff == 0)
5403 {
5404 /* Falloff = 0 is easy, because d3d's and opengl's spot light equations have the
5405 * falloff resp. exponent parameter as an exponent, so the spot light lighting
5406 * will always be 1.0 for both of them, and we don't have to care for the
5407 * rest of the rather complex calculation
5408 */
5409 exponent = 0.0f;
5410 }
5411 else
5412 {
5413 float rho = pData->theta + (pData->phi - pData->theta) / (2 * pData->falloff);
5414 if (rho < 0.0001f)
5415 rho = 0.0001f;
5416 exponent = -0.3f/log(cos(rho/2));
5417 }
5418 if (exponent > 128.0f)
5419 exponent = 128.0f;
5420
5421 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, exponent);
5422 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5423
5424 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, pData->phi * 90.0 / pi);
5425 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5426
5427 /* Attenuation - Are these right? guessing... */
5428 glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, pData->attenuation0);
5429 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5430
5431 glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, pData->attenuation1);
5432 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5433
5434 glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, (QuadAttenuation < pData->attenuation2) ? pData->attenuation2 : QuadAttenuation);
5435 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5436
5437 /** @todo range */
5438 break;
5439 }
5440
5441 case SVGA3D_LIGHTTYPE_DIRECTIONAL:
5442 {
5443 GLfloat position[4];
5444
5445 position[0] = -pData->direction[0];
5446 position[1] = -pData->direction[1];
5447 position[2] = -pData->direction[2];
5448 position[3] = 0.0f;
5449
5450 glLightfv(GL_LIGHT0 + index, GL_POSITION, position); /* Note gl uses w position of 0 for direction! */
5451 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5452
5453 glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f);
5454 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5455
5456 glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, 0.0f);
5457 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
5458 break;
5459 }
5460
5461 case SVGA3D_LIGHTTYPE_SPOT2:
5462 default:
5463 Log(("Unsupported light type!!\n"));
5464 return VERR_INVALID_PARAMETER;
5465 }
5466
5467 /* Restore the modelview matrix */
5468 glPopMatrix();
5469
5470 return VINF_SUCCESS;
5471}
5472
5473int vmsvga3dSetLightEnabled(PVGASTATE pThis, uint32_t cid, uint32_t index, uint32_t enabled)
5474{
5475 PVMSVGA3DCONTEXT pContext;
5476 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5477 AssertReturn(pState, VERR_NO_MEMORY);
5478
5479 Log(("vmsvga3dSetLightEnabled cid=%x %d -> %d\n", cid, index, enabled));
5480
5481 if ( cid >= pState->cContexts
5482 || pState->papContexts[cid]->id != cid)
5483 {
5484 Log(("vmsvga3dSetLightEnabled invalid context id!\n"));
5485 return VERR_INVALID_PARAMETER;
5486 }
5487 pContext = pState->papContexts[cid];
5488 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5489
5490 /* Store for vm state save/restore */
5491 if (index < SVGA3D_MAX_LIGHTS)
5492 pContext->state.aLightData[index].fEnabled = !!enabled;
5493 else
5494 AssertFailed();
5495
5496 if (enabled)
5497 {
5498 /* Load the default settings if none have been set yet. */
5499 if (!pContext->state.aLightData[index].fValidData)
5500 vmsvga3dSetLightData(pThis, cid, index, (SVGA3dLightData *)&vmsvga3d_default_light);
5501 glEnable(GL_LIGHT0 + index);
5502 }
5503 else
5504 glDisable(GL_LIGHT0 + index);
5505
5506 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5507 return VINF_SUCCESS;
5508}
5509
5510int vmsvga3dSetViewPort(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
5511{
5512 PVMSVGA3DCONTEXT pContext;
5513 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5514 AssertReturn(pState, VERR_NO_MEMORY);
5515
5516 Log(("vmsvga3dSetViewPort cid=%x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
5517
5518 if ( cid >= pState->cContexts
5519 || pState->papContexts[cid]->id != cid)
5520 {
5521 Log(("vmsvga3dSetViewPort invalid context id!\n"));
5522 return VERR_INVALID_PARAMETER;
5523 }
5524 pContext = pState->papContexts[cid];
5525 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5526
5527 /* Save for vm state save/restore. */
5528 pContext->state.RectViewPort = *pRect;
5529 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VIEWPORT;
5530
5531 /** @todo y-inversion for partial viewport coordinates? */
5532 glViewport(pRect->x, pRect->y, pRect->w, pRect->h);
5533 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5534
5535 /* Reset the projection matrix as that relies on the viewport setting. */
5536 if (pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].fValid == true)
5537 {
5538 vmsvga3dSetTransform(pThis, cid, SVGA3D_TRANSFORM_PROJECTION, pContext->state.aTransformState[SVGA3D_TRANSFORM_PROJECTION].matrix);
5539 }
5540 else
5541 {
5542 float matrix[16];
5543
5544 /* identity matrix if no matrix set. */
5545 memset(matrix, 0, sizeof(matrix));
5546 matrix[0] = 1.0;
5547 matrix[5] = 1.0;
5548 matrix[10] = 1.0;
5549 matrix[15] = 1.0;
5550 vmsvga3dSetTransform(pThis, cid, SVGA3D_TRANSFORM_PROJECTION, matrix);
5551 }
5552
5553 return VINF_SUCCESS;
5554}
5555
5556int vmsvga3dSetClipPlane(PVGASTATE pThis, uint32_t cid, uint32_t index, float plane[4])
5557{
5558 PVMSVGA3DCONTEXT pContext;
5559 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5560 AssertReturn(pState, VERR_NO_MEMORY);
5561 double oglPlane[4];
5562
5563 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)));
5564 AssertReturn(index < SVGA3D_CLIPPLANE_MAX, VERR_INVALID_PARAMETER);
5565
5566 if ( cid >= pState->cContexts
5567 || pState->papContexts[cid]->id != cid)
5568 {
5569 Log(("vmsvga3dSetClipPlane invalid context id!\n"));
5570 return VERR_INVALID_PARAMETER;
5571 }
5572 pContext = pState->papContexts[cid];
5573 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5574
5575 /* Store for vm state save/restore. */
5576 pContext->state.aClipPlane[index].fValid = true;
5577 memcpy(pContext->state.aClipPlane[index].plane, plane, sizeof(pContext->state.aClipPlane[index].plane));
5578
5579 /** @todo clip plane affected by model view in OpenGL & view in D3D + vertex shader -> not transformed (see Wine; state.c clipplane) */
5580 oglPlane[0] = (double)plane[0];
5581 oglPlane[1] = (double)plane[1];
5582 oglPlane[2] = (double)plane[2];
5583 oglPlane[3] = (double)plane[3];
5584
5585 glClipPlane(GL_CLIP_PLANE0 + index, oglPlane);
5586 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5587
5588 return VINF_SUCCESS;
5589}
5590
5591int vmsvga3dSetScissorRect(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
5592{
5593 PVMSVGA3DCONTEXT pContext;
5594 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5595 AssertReturn(pState, VERR_NO_MEMORY);
5596
5597 Log(("vmsvga3dSetScissorRect cid=%x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
5598
5599 if ( cid >= pState->cContexts
5600 || pState->papContexts[cid]->id != cid)
5601 {
5602 Log(("vmsvga3dSetScissorRect invalid context id!\n"));
5603 return VERR_INVALID_PARAMETER;
5604 }
5605 pContext = pState->papContexts[cid];
5606 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5607
5608 /* Store for vm state save/restore. */
5609 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_SCISSORRECT;
5610 pContext->state.RectScissor = *pRect;
5611
5612 glScissor(pRect->x, pRect->y, pRect->w, pRect->h);
5613 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5614
5615 return VINF_SUCCESS;
5616}
5617
5618static void vmsvgaColor2GLFloatArray(uint32_t color, GLfloat *pRed, GLfloat *pGreen, GLfloat *pBlue, GLfloat *pAlpha)
5619{
5620 /* Convert byte color components to float (0-1.0) */
5621 *pAlpha = (GLfloat)(color >> 24) / 255.0;
5622 *pRed = (GLfloat)((color >> 16) & 0xff) / 255.0;
5623 *pGreen = (GLfloat)((color >> 8) & 0xff) / 255.0;
5624 *pBlue = (GLfloat)(color & 0xff) / 255.0;
5625}
5626
5627int vmsvga3dCommandClear(PVGASTATE pThis, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth, uint32_t stencil,
5628 uint32_t cRects, SVGA3dRect *pRect)
5629{
5630 GLbitfield mask = 0;
5631 PVMSVGA3DCONTEXT pContext;
5632 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5633 AssertReturn(pState, VERR_NO_MEMORY);
5634 GLboolean fDepthWriteEnabled = GL_FALSE;
5635
5636 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));
5637
5638 if ( cid >= pState->cContexts
5639 || pState->papContexts[cid]->id != cid)
5640 {
5641 Log(("vmsvga3dCommandClear invalid context id!\n"));
5642 return VERR_INVALID_PARAMETER;
5643 }
5644 pContext = pState->papContexts[cid];
5645 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5646
5647 if (clearFlag & SVGA3D_CLEAR_COLOR)
5648 {
5649 GLfloat red, green, blue, alpha;
5650
5651 vmsvgaColor2GLFloatArray(color, &red, &green, &blue, &alpha);
5652
5653 /* Set the color clear value. */
5654 glClearColor(red, green, blue, alpha);
5655
5656 mask |= GL_COLOR_BUFFER_BIT;
5657 }
5658 if (clearFlag & SVGA3D_CLEAR_STENCIL)
5659 {
5660 /** @todo possibly the same problem as with glDepthMask */
5661 glClearStencil(stencil);
5662 mask |= GL_STENCIL_BUFFER_BIT;
5663 }
5664 if (clearFlag & SVGA3D_CLEAR_DEPTH)
5665 {
5666 glClearDepth((GLdouble)depth);
5667 mask |= GL_DEPTH_BUFFER_BIT;
5668
5669 /* glClear will not clear the depth buffer if writing is disabled. */
5670 glGetBooleanv(GL_DEPTH_WRITEMASK, &fDepthWriteEnabled);
5671 if (fDepthWriteEnabled == GL_FALSE)
5672 glDepthMask(GL_TRUE);
5673 }
5674
5675 if (cRects)
5676 {
5677 /* Save the current scissor test bit and scissor box. */
5678 glPushAttrib(GL_SCISSOR_BIT);
5679 glEnable(GL_SCISSOR_TEST);
5680 for (unsigned i=0; i < cRects; i++)
5681 {
5682 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));
5683 glScissor(pRect[i].x, pRect[i].y, pRect[i].w, pRect[i].h);
5684 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5685 glClear(mask);
5686 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5687 }
5688 /* Restore the old scissor test bit and box */
5689 glPopAttrib();
5690 }
5691 else
5692 {
5693 glClear(mask);
5694 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5695 }
5696
5697 /* Restore depth write state. */
5698 if ( (clearFlag & SVGA3D_CLEAR_DEPTH)
5699 && fDepthWriteEnabled == GL_FALSE)
5700 glDepthMask(GL_FALSE);
5701
5702 return VINF_SUCCESS;
5703}
5704
5705/* Convert VMWare vertex declaration to its OpenGL equivalent. */
5706int vmsvga3dVertexDecl2OGL(SVGA3dVertexArrayIdentity &identity, GLint &size, GLenum &type, GLboolean &normalized)
5707{
5708 normalized = GL_FALSE;
5709 switch (identity.type)
5710 {
5711 case SVGA3D_DECLTYPE_FLOAT1:
5712 size = 1;
5713 type = GL_FLOAT;
5714 break;
5715 case SVGA3D_DECLTYPE_FLOAT2:
5716 size = 2;
5717 type = GL_FLOAT;
5718 break;
5719 case SVGA3D_DECLTYPE_FLOAT3:
5720 size = 3;
5721 type = GL_FLOAT;
5722 break;
5723 case SVGA3D_DECLTYPE_FLOAT4:
5724 size = 4;
5725 type = GL_FLOAT;
5726 break;
5727
5728 case SVGA3D_DECLTYPE_D3DCOLOR:
5729 size = GL_BGRA; /* @note requires GL_ARB_vertex_array_bgra */
5730 type = GL_UNSIGNED_BYTE;
5731 normalized = GL_TRUE; /* glVertexAttribPointer fails otherwise */
5732 break;
5733
5734 case SVGA3D_DECLTYPE_UBYTE4N:
5735 normalized = GL_TRUE;
5736 /* no break */
5737 case SVGA3D_DECLTYPE_UBYTE4:
5738 size = 4;
5739 type = GL_UNSIGNED_BYTE;
5740 break;
5741
5742 case SVGA3D_DECLTYPE_SHORT2N:
5743 normalized = GL_TRUE;
5744 /* no break */
5745 case SVGA3D_DECLTYPE_SHORT2:
5746 size = 2;
5747 type = GL_SHORT;
5748 break;
5749
5750 case SVGA3D_DECLTYPE_SHORT4N:
5751 normalized = GL_TRUE;
5752 /* no break */
5753 case SVGA3D_DECLTYPE_SHORT4:
5754 size = 4;
5755 type = GL_SHORT;
5756 break;
5757
5758 case SVGA3D_DECLTYPE_USHORT4N:
5759 normalized = GL_TRUE;
5760 size = 4;
5761 type = GL_UNSIGNED_SHORT;
5762 break;
5763
5764 case SVGA3D_DECLTYPE_USHORT2N:
5765 normalized = GL_TRUE;
5766 size = 2;
5767 type = GL_UNSIGNED_SHORT;
5768 break;
5769
5770 case SVGA3D_DECLTYPE_UDEC3:
5771 size = 3;
5772 type = GL_UNSIGNED_INT_2_10_10_10_REV; /** @todo correct? */
5773 break;
5774
5775 case SVGA3D_DECLTYPE_DEC3N:
5776 normalized = true;
5777 size = 3;
5778 type = GL_INT_2_10_10_10_REV; /** @todo correct? */
5779 break;
5780
5781 case SVGA3D_DECLTYPE_FLOAT16_2:
5782 size = 2;
5783 type = GL_HALF_FLOAT;
5784 break;
5785 case SVGA3D_DECLTYPE_FLOAT16_4:
5786 size = 4;
5787 type = GL_HALF_FLOAT;
5788 break;
5789 default:
5790 AssertFailedReturn(VERR_INVALID_PARAMETER);
5791 }
5792
5793 //pVertexElement->Method = identity.method;
5794 //pVertexElement->Usage = identity.usage;
5795
5796 return VINF_SUCCESS;
5797}
5798
5799/* Convert VMWare primitive type to its OpenGL equivalent. */
5800/* Calculate the vertex count based on the primitive type and nr of primitives. */
5801int vmsvga3dPrimitiveType2OGL(SVGA3dPrimitiveType PrimitiveType, GLenum *pMode, uint32_t cPrimitiveCount, uint32_t *pcVertices)
5802{
5803 switch (PrimitiveType)
5804 {
5805 case SVGA3D_PRIMITIVE_TRIANGLELIST:
5806 *pMode = GL_TRIANGLES;
5807 *pcVertices = cPrimitiveCount * 3;
5808 break;
5809 case SVGA3D_PRIMITIVE_POINTLIST:
5810 *pMode = GL_POINTS;
5811 *pcVertices = cPrimitiveCount;
5812 break;
5813 case SVGA3D_PRIMITIVE_LINELIST:
5814 *pMode = GL_LINES;
5815 *pcVertices = cPrimitiveCount * 2;
5816 break;
5817 case SVGA3D_PRIMITIVE_LINESTRIP:
5818 *pMode = GL_LINE_STRIP;
5819 *pcVertices = cPrimitiveCount + 1;
5820 break;
5821 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
5822 *pMode = GL_TRIANGLE_STRIP;
5823 *pcVertices = cPrimitiveCount + 2;
5824 break;
5825 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
5826 *pMode = GL_TRIANGLE_FAN;
5827 *pcVertices = cPrimitiveCount + 2;
5828 break;
5829 default:
5830 return VERR_INVALID_PARAMETER;
5831 }
5832 return VINF_SUCCESS;
5833}
5834
5835int vmsvga3dDrawPrimitivesProcessVertexDecls(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t iVertexDeclBase, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl)
5836{
5837 unsigned sidVertex = pVertexDecl[0].array.surfaceId;
5838 PVMSVGA3DSURFACE pVertexSurface;
5839
5840 AssertReturn(sidVertex < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
5841 AssertReturn(sidVertex < pState->cSurfaces && pState->papSurfaces[sidVertex]->id == sidVertex, VERR_INVALID_PARAMETER);
5842
5843 pVertexSurface = pState->papSurfaces[sidVertex];
5844 Log(("vmsvga3dDrawPrimitives: vertex surface %x\n", sidVertex));
5845
5846 /* Create and/or bind the vertex buffer. */
5847 if (pVertexSurface->oglId.buffer == OPENGL_INVALID_ID)
5848 {
5849 Log(("vmsvga3dDrawPrimitives: create vertex buffer fDirty=%d size=%x bytes\n", pVertexSurface->fDirty, pVertexSurface->pMipmapLevels[0].cbSurface));
5850 PVMSVGA3DCONTEXT pSavedCtx = pContext;
5851 pContext = &pState->SharedCtx;
5852 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5853
5854 pState->ext.glGenBuffers(1, &pVertexSurface->oglId.buffer);
5855 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5856
5857 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
5858 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5859
5860 Assert(pVertexSurface->fDirty);
5861 /** @todo rethink usage dynamic/static */
5862 pState->ext.glBufferData(GL_ARRAY_BUFFER, pVertexSurface->pMipmapLevels[0].cbSurface, pVertexSurface->pMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
5863 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5864
5865 pVertexSurface->pMipmapLevels[0].fDirty = false;
5866 pVertexSurface->fDirty = false;
5867
5868 pVertexSurface->flags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
5869
5870 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, OPENGL_INVALID_ID);
5871 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5872
5873 pContext = pSavedCtx;
5874 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
5875 }
5876
5877 Assert(pVertexSurface->fDirty == false);
5878 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pVertexSurface->oglId.buffer);
5879 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5880
5881 /* Setup the vertex declarations. */
5882 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
5883 {
5884 GLint size;
5885 GLenum type;
5886 GLboolean normalized;
5887 GLuint index = iVertexDeclBase + iVertex;
5888
5889 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));
5890
5891 int rc = vmsvga3dVertexDecl2OGL(pVertexDecl[iVertex].identity, size, type, normalized);
5892 AssertRCReturn(rc, rc);
5893
5894 if (pContext->state.shidVertex != SVGA_ID_INVALID)
5895 {
5896 /* Use numbered vertex arrays when shaders are active. */
5897 pState->ext.glEnableVertexAttribArray(index);
5898 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5899 pState->ext.glVertexAttribPointer(index, size, type, normalized, pVertexDecl[iVertex].array.stride,
5900 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5901 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5902 /** @todo case SVGA3D_DECLUSAGE_COLOR: color component order not identical!! test GL_BGRA!! */
5903 }
5904 else
5905 {
5906 /* Use the predefined selection of vertex streams for the fixed pipeline. */
5907 switch (pVertexDecl[iVertex].identity.usage)
5908 {
5909 case SVGA3D_DECLUSAGE_POSITION:
5910 glEnableClientState(GL_VERTEX_ARRAY);
5911 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5912 glVertexPointer(size, type, pVertexDecl[iVertex].array.stride,
5913 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5914 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5915 break;
5916 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
5917 AssertFailed();
5918 break;
5919 case SVGA3D_DECLUSAGE_BLENDINDICES:
5920 AssertFailed();
5921 break;
5922 case SVGA3D_DECLUSAGE_NORMAL:
5923 glEnableClientState(GL_NORMAL_ARRAY);
5924 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5925 glNormalPointer(type, pVertexDecl[iVertex].array.stride,
5926 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5927 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5928 break;
5929 case SVGA3D_DECLUSAGE_PSIZE:
5930 AssertFailed();
5931 break;
5932 case SVGA3D_DECLUSAGE_TEXCOORD:
5933 /* Specify the affected texture unit. */
5934#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
5935 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
5936#else
5937 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
5938#endif
5939 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
5940 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5941 glTexCoordPointer(size, type, pVertexDecl[iVertex].array.stride,
5942 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5943 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5944 break;
5945 case SVGA3D_DECLUSAGE_TANGENT:
5946 AssertFailed();
5947 break;
5948 case SVGA3D_DECLUSAGE_BINORMAL:
5949 AssertFailed();
5950 break;
5951 case SVGA3D_DECLUSAGE_TESSFACTOR:
5952 AssertFailed();
5953 break;
5954 case SVGA3D_DECLUSAGE_POSITIONT:
5955 AssertFailed(); /* see position_transformed in Wine */
5956 break;
5957 case SVGA3D_DECLUSAGE_COLOR: /** @todo color component order not identical!! test GL_BGRA!! */
5958 glEnableClientState(GL_COLOR_ARRAY);
5959 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5960 glColorPointer(size, type, pVertexDecl[iVertex].array.stride,
5961 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5962 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5963 break;
5964 case SVGA3D_DECLUSAGE_FOG:
5965 glEnableClientState(GL_FOG_COORD_ARRAY);
5966 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5967 pState->ext.glFogCoordPointer(type, pVertexDecl[iVertex].array.stride,
5968 (const GLvoid *)(uintptr_t)pVertexDecl[iVertex].array.offset);
5969 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
5970 break;
5971 case SVGA3D_DECLUSAGE_DEPTH:
5972 AssertFailed();
5973 break;
5974 case SVGA3D_DECLUSAGE_SAMPLE:
5975 AssertFailed();
5976 break;
5977 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
5978 }
5979 }
5980
5981#ifdef LOG_ENABLED
5982 if (pVertexDecl[iVertex].array.stride == 0)
5983 Log(("vmsvga3dDrawPrimitives: stride == 0! Can be valid\n"));
5984#endif
5985 }
5986
5987 return VINF_SUCCESS;
5988}
5989
5990int vmsvga3dDrawPrimitivesCleanupVertexDecls(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t iVertexDeclBase, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl)
5991{
5992 /* Setup the vertex declarations. */
5993 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
5994 {
5995 if (pContext->state.shidVertex != SVGA_ID_INVALID)
5996 {
5997 /* Use numbered vertex arrays when shaders are active. */
5998 pState->ext.glDisableVertexAttribArray(iVertexDeclBase + iVertex);
5999 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6000 }
6001 else
6002 {
6003 /* Use the predefined selection of vertex streams for the fixed pipeline. */
6004 switch (pVertexDecl[iVertex].identity.usage)
6005 {
6006 case SVGA3D_DECLUSAGE_POSITION:
6007 glDisableClientState(GL_VERTEX_ARRAY);
6008 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6009 break;
6010 case SVGA3D_DECLUSAGE_BLENDWEIGHT:
6011 break;
6012 case SVGA3D_DECLUSAGE_BLENDINDICES:
6013 break;
6014 case SVGA3D_DECLUSAGE_NORMAL:
6015 glDisableClientState(GL_NORMAL_ARRAY);
6016 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6017 break;
6018 case SVGA3D_DECLUSAGE_PSIZE:
6019 break;
6020 case SVGA3D_DECLUSAGE_TEXCOORD:
6021 /* Specify the affected texture unit. */
6022#if VBOX_VMSVGA3D_GL_HACK_LEVEL >= 0x103
6023 glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
6024#else
6025 pState->ext.glClientActiveTexture(GL_TEXTURE0 + pVertexDecl[iVertex].identity.usageIndex);
6026#endif
6027 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
6028 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6029 break;
6030 case SVGA3D_DECLUSAGE_TANGENT:
6031 break;
6032 case SVGA3D_DECLUSAGE_BINORMAL:
6033 break;
6034 case SVGA3D_DECLUSAGE_TESSFACTOR:
6035 break;
6036 case SVGA3D_DECLUSAGE_POSITIONT:
6037 break;
6038 case SVGA3D_DECLUSAGE_COLOR: /** @todo color component order not identical!! */
6039 glDisableClientState(GL_COLOR_ARRAY);
6040 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6041 break;
6042 case SVGA3D_DECLUSAGE_FOG:
6043 glDisableClientState(GL_FOG_COORD_ARRAY);
6044 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6045 break;
6046 case SVGA3D_DECLUSAGE_DEPTH:
6047 break;
6048 case SVGA3D_DECLUSAGE_SAMPLE:
6049 break;
6050 case SVGA3D_DECLUSAGE_MAX: AssertFailed(); break; /* shut up gcc */
6051 }
6052 }
6053 }
6054 /* Unbind the vertex buffer after usage. */
6055 pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0);
6056 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6057 return VINF_SUCCESS;
6058}
6059
6060int vmsvga3dDrawPrimitives(PVGASTATE pThis, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl, uint32_t numRanges, SVGA3dPrimitiveRange *pRange, uint32_t cVertexDivisor, SVGA3dVertexDivisor *pVertexDivisor)
6061{
6062 PVMSVGA3DCONTEXT pContext;
6063 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6064 AssertReturn(pState, VERR_INTERNAL_ERROR);
6065 int rc = VERR_NOT_IMPLEMENTED;
6066 uint32_t iCurrentVertex;
6067
6068 Log(("vmsvga3dDrawPrimitives cid=%x numVertexDecls=%d numRanges=%d, cVertexDivisor=%d\n", cid, numVertexDecls, numRanges, cVertexDivisor));
6069
6070 AssertReturn(numVertexDecls && numVertexDecls <= SVGA3D_MAX_VERTEX_ARRAYS, VERR_INVALID_PARAMETER);
6071 AssertReturn(numRanges && numRanges <= SVGA3D_MAX_DRAW_PRIMITIVE_RANGES, VERR_INVALID_PARAMETER);
6072 AssertReturn(!cVertexDivisor || cVertexDivisor == numVertexDecls, VERR_INVALID_PARAMETER);
6073 /** @todo Non-zero cVertexDivisor */
6074 Assert(!cVertexDivisor);
6075
6076 if ( cid >= pState->cContexts
6077 || pState->papContexts[cid]->id != cid)
6078 {
6079 Log(("vmsvga3dDrawPrimitives invalid context id!\n"));
6080 return VERR_INVALID_PARAMETER;
6081 }
6082 pContext = pState->papContexts[cid];
6083 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6084
6085 /* Flush any shader changes. */
6086 if (pContext->pShaderContext)
6087 {
6088 uint32_t rtHeight = 0;
6089
6090 if (pContext->sidRenderTarget != SVGA_ID_INVALID)
6091 {
6092 PVMSVGA3DSURFACE pRenderTarget = pState->papSurfaces[pContext->sidRenderTarget];
6093 rtHeight = pRenderTarget->pMipmapLevels[0].size.height;
6094 }
6095
6096 ShaderUpdateState(pContext->pShaderContext, rtHeight);
6097 }
6098
6099 /* Process all vertex declarations. Each vertex buffer is represented by one stream. */
6100 iCurrentVertex = 0;
6101 while (iCurrentVertex < numVertexDecls)
6102 {
6103 uint32_t sidVertex = SVGA_ID_INVALID;
6104 uint32_t iVertex;
6105
6106 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
6107 {
6108 if ( sidVertex != SVGA_ID_INVALID
6109 && pVertexDecl[iVertex].array.surfaceId != sidVertex
6110 )
6111 break;
6112 sidVertex = pVertexDecl[iVertex].array.surfaceId;
6113 }
6114
6115 rc = vmsvga3dDrawPrimitivesProcessVertexDecls(pState, pContext, iCurrentVertex, iVertex - iCurrentVertex, &pVertexDecl[iCurrentVertex]);
6116 AssertRCReturn(rc, rc);
6117
6118 iCurrentVertex = iVertex;
6119 }
6120
6121 /* Now draw the primitives. */
6122 for (unsigned iPrimitive = 0; iPrimitive < numRanges; iPrimitive++)
6123 {
6124 GLenum modeDraw;
6125 unsigned sidIndex = pRange[iPrimitive].indexArray.surfaceId;
6126 PVMSVGA3DSURFACE pIndexSurface = NULL;
6127 unsigned cVertices;
6128
6129 Log(("Primitive %d: type %s\n", iPrimitive, vmsvga3dPrimitiveType2String(pRange[iPrimitive].primType)));
6130 rc = vmsvga3dPrimitiveType2OGL(pRange[iPrimitive].primType, &modeDraw, pRange[iPrimitive].primitiveCount, &cVertices);
6131 if (RT_FAILURE(rc))
6132 {
6133 AssertRC(rc);
6134 goto internal_error;
6135 }
6136
6137 if (sidIndex != SVGA3D_INVALID_ID)
6138 {
6139 AssertMsg(pRange[iPrimitive].indexWidth == sizeof(uint32_t) || pRange[iPrimitive].indexWidth == sizeof(uint16_t), ("Unsupported primitive width %d\n", pRange[iPrimitive].indexWidth));
6140
6141 if ( sidIndex >= SVGA3D_MAX_SURFACE_IDS
6142 || sidIndex >= pState->cSurfaces
6143 || pState->papSurfaces[sidIndex]->id != sidIndex)
6144 {
6145 Assert(sidIndex < SVGA3D_MAX_SURFACE_IDS);
6146 Assert(sidIndex < pState->cSurfaces && pState->papSurfaces[sidIndex]->id == sidIndex);
6147 rc = VERR_INVALID_PARAMETER;
6148 goto internal_error;
6149 }
6150 pIndexSurface = pState->papSurfaces[sidIndex];
6151 Log(("vmsvga3dDrawPrimitives: index surface %x\n", sidIndex));
6152
6153 if (pIndexSurface->oglId.buffer == OPENGL_INVALID_ID)
6154 {
6155 Log(("vmsvga3dDrawPrimitives: create index buffer fDirty=%d size=%x bytes\n", pIndexSurface->fDirty, pIndexSurface->pMipmapLevels[0].cbSurface));
6156 pContext = &pState->SharedCtx;
6157 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6158
6159 pState->ext.glGenBuffers(1, &pIndexSurface->oglId.buffer);
6160 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6161
6162 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
6163 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6164
6165 Assert(pIndexSurface->fDirty);
6166
6167 /** @todo rethink usage dynamic/static */
6168 pState->ext.glBufferData(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->pMipmapLevels[0].cbSurface, pIndexSurface->pMipmapLevels[0].pSurfaceData, GL_DYNAMIC_DRAW);
6169 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6170
6171 pIndexSurface->pMipmapLevels[0].fDirty = false;
6172 pIndexSurface->fDirty = false;
6173
6174 pIndexSurface->flags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
6175
6176 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, OPENGL_INVALID_ID);
6177 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6178
6179 pContext = pState->papContexts[cid];
6180 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6181 }
6182 Assert(pIndexSurface->fDirty == false);
6183
6184 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pIndexSurface->oglId.buffer);
6185 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6186 }
6187
6188 if (!pIndexSurface)
6189 {
6190 /* Render without an index buffer */
6191 Log(("DrawPrimitive %x cPrimitives=%d cVertices=%d index index bias=%d\n", modeDraw, pRange[iPrimitive].primitiveCount, cVertices, pRange[iPrimitive].indexBias));
6192 glDrawArrays(modeDraw, pRange[iPrimitive].indexBias, cVertices);
6193 }
6194 else
6195 {
6196 Assert(pRange[iPrimitive].indexBias >= 0); /** @todo indexBias */
6197 Assert(pRange[iPrimitive].indexWidth == pRange[iPrimitive].indexArray.stride);
6198
6199 /* Render with an index buffer */
6200 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));
6201 if (pRange[iPrimitive].indexBias == 0)
6202 glDrawElements(modeDraw,
6203 cVertices,
6204 (pRange[iPrimitive].indexWidth == sizeof(uint16_t)) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
6205 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset); /* byte offset in indices buffer */
6206 else
6207 pState->ext.glDrawElementsBaseVertex(modeDraw,
6208 cVertices,
6209 (pRange[iPrimitive].indexWidth == sizeof(uint16_t)) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
6210 (GLvoid *)(uintptr_t)pRange[iPrimitive].indexArray.offset, /* byte offset in indices buffer */
6211 pRange[iPrimitive].indexBias); /* basevertex */
6212
6213 /* Unbind the index buffer after usage. */
6214 pState->ext.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
6215 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6216 }
6217 VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
6218 }
6219
6220internal_error:
6221
6222 /* Deactivate the vertex declarations. */
6223 iCurrentVertex = 0;
6224 while (iCurrentVertex < numVertexDecls)
6225 {
6226 uint32_t sidVertex = SVGA_ID_INVALID;
6227 uint32_t iVertex;
6228
6229 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
6230 {
6231 if ( sidVertex != SVGA_ID_INVALID
6232 && pVertexDecl[iVertex].array.surfaceId != sidVertex
6233 )
6234 break;
6235 sidVertex = pVertexDecl[iVertex].array.surfaceId;
6236 }
6237
6238 rc = vmsvga3dDrawPrimitivesCleanupVertexDecls(pState, pContext, iCurrentVertex, iVertex - iCurrentVertex, &pVertexDecl[iCurrentVertex]);
6239 AssertRCReturn(rc, rc);
6240
6241 iCurrentVertex = iVertex;
6242 }
6243#ifdef DEBUG
6244 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTexture); i++)
6245 {
6246 if (pContext->aSidActiveTexture[i] != SVGA3D_INVALID_ID)
6247 {
6248 GLint activeTexture = 0;
6249 GLint activeTextureUnit = 0;
6250
6251 glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
6252 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6253 pState->ext.glActiveTexture(GL_TEXTURE0 + i);
6254 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6255
6256 glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture);
6257 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6258 pState->ext.glActiveTexture(activeTextureUnit);
6259 VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext);
6260
6261# 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. */
6262 if (pContext->aSidActiveTexture[activeTextureUnit - GL_TEXTURE0] != SVGA3D_INVALID_ID)
6263 {
6264 PVMSVGA3DSURFACE pTexture;
6265 pTexture = pState->papSurfaces[pContext->aSidActiveTexture[activeTextureUnit - GL_TEXTURE0]];
6266
6267 AssertMsg(pTexture->oglId.texture == (GLuint)activeTexture, ("%x vs %x unit %d - %d\n", pTexture->oglId.texture, activeTexture, i, activeTextureUnit - GL_TEXTURE0));
6268 }
6269# else
6270 PVMSVGA3DSURFACE pTexture = pState->papSurfaces[pContext->aSidActiveTexture[i]];
6271 AssertMsg(pTexture->id == pContext->aSidActiveTexture[i], ("%x vs %x\n", pTexture->id == pContext->aSidActiveTexture[i]));
6272 AssertMsg(pTexture->oglId.texture == (GLuint)activeTexture,
6273 ("%x vs %x unit %d (active unit %d) sid=%x\n", pTexture->oglId.texture, activeTexture, i,
6274 activeTextureUnit - GL_TEXTURE0, pContext->aSidActiveTexture[i]));
6275# endif
6276 }
6277 }
6278#endif
6279
6280#ifdef DEBUG_GFX_WINDOW
6281 if (pContext->aSidActiveTexture[0])
6282 {
6283 SVGA3dCopyRect rect;
6284
6285 rect.srcx = rect.srcy = rect.x = rect.y = 0;
6286 rect.w = 800;
6287 rect.h = 600;
6288 vmsvga3dCommandPresent(pThis, pContext->sidRenderTarget, 0, NULL);
6289 }
6290#endif
6291 return rc;
6292}
6293
6294
6295int vmsvga3dShaderDefine(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type, uint32_t cbData, uint32_t *pShaderData)
6296{
6297 PVMSVGA3DCONTEXT pContext;
6298 PVMSVGA3DSHADER pShader;
6299 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6300 AssertReturn(pState, VERR_NO_MEMORY);
6301 int rc;
6302
6303 Log(("vmsvga3dShaderDefine cid=%x shid=%x type=%s cbData=%x\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cbData));
6304 Log3(("shader code:\n%.*Rhxd\n", cbData, pShaderData));
6305
6306 if ( cid >= pState->cContexts
6307 || pState->papContexts[cid]->id != cid)
6308 {
6309 Log(("vmsvga3dShaderDefine invalid context id!\n"));
6310 return VERR_INVALID_PARAMETER;
6311 }
6312 pContext = pState->papContexts[cid];
6313 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6314
6315 AssertReturn(shid < SVGA3D_MAX_SHADER_IDS, VERR_INVALID_PARAMETER);
6316 if (type == SVGA3D_SHADERTYPE_VS)
6317 {
6318 if (shid >= pContext->cVertexShaders)
6319 {
6320 void *pvNew = RTMemRealloc(pContext->paVertexShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
6321 AssertReturn(pvNew, VERR_NO_MEMORY);
6322 pContext->paVertexShader = (PVMSVGA3DSHADER)pvNew;
6323 memset(&pContext->paVertexShader[pContext->cVertexShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cVertexShaders));
6324 for (uint32_t i = pContext->cVertexShaders; i < shid + 1; i++)
6325 pContext->paVertexShader[i].id = SVGA3D_INVALID_ID;
6326 pContext->cVertexShaders = shid + 1;
6327 }
6328 /* If one already exists with this id, then destroy it now. */
6329 if (pContext->paVertexShader[shid].id != SVGA3D_INVALID_ID)
6330 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paVertexShader[shid].type);
6331
6332 pShader = &pContext->paVertexShader[shid];
6333 }
6334 else
6335 {
6336 Assert(type == SVGA3D_SHADERTYPE_PS);
6337 if (shid >= pContext->cPixelShaders)
6338 {
6339 void *pvNew = RTMemRealloc(pContext->paPixelShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
6340 AssertReturn(pvNew, VERR_NO_MEMORY);
6341 pContext->paPixelShader = (PVMSVGA3DSHADER)pvNew;
6342 memset(&pContext->paPixelShader[pContext->cPixelShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cPixelShaders));
6343 for (uint32_t i = pContext->cPixelShaders; i < shid + 1; i++)
6344 pContext->paPixelShader[i].id = SVGA3D_INVALID_ID;
6345 pContext->cPixelShaders = shid + 1;
6346 }
6347 /* If one already exists with this id, then destroy it now. */
6348 if (pContext->paPixelShader[shid].id != SVGA3D_INVALID_ID)
6349 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paPixelShader[shid].type);
6350
6351 pShader = &pContext->paPixelShader[shid];
6352 }
6353
6354 memset(pShader, 0, sizeof(*pShader));
6355 pShader->id = shid;
6356 pShader->cid = cid;
6357 pShader->type = type;
6358 pShader->cbData = cbData;
6359 pShader->pShaderProgram = RTMemAllocZ(cbData);
6360 AssertReturn(pShader->pShaderProgram, VERR_NO_MEMORY);
6361 memcpy(pShader->pShaderProgram, pShaderData, cbData);
6362
6363#ifdef DUMP_SHADER_DISASSEMBLY
6364 LPD3DXBUFFER pDisassembly;
6365 HRESULT hr = D3DXDisassembleShader((const DWORD *)pShaderData, FALSE, NULL, &pDisassembly);
6366 if (hr == D3D_OK)
6367 {
6368 Log(("Shader disassembly:\n%s\n", pDisassembly->GetBufferPointer()));
6369 pDisassembly->Release();
6370 }
6371#endif
6372
6373 switch (type)
6374 {
6375 case SVGA3D_SHADERTYPE_VS:
6376 rc = ShaderCreateVertexShader(pContext->pShaderContext, (const uint32_t *)pShaderData, &pShader->u.pVertexShader);
6377 break;
6378
6379 case SVGA3D_SHADERTYPE_PS:
6380 rc = ShaderCreatePixelShader(pContext->pShaderContext, (const uint32_t *)pShaderData, &pShader->u.pPixelShader);
6381 break;
6382
6383 default:
6384 AssertFailedReturn(VERR_INVALID_PARAMETER);
6385 }
6386 if (rc != VINF_SUCCESS)
6387 {
6388 RTMemFree(pShader->pShaderProgram);
6389 memset(pShader, 0, sizeof(*pShader));
6390 pShader->id = SVGA3D_INVALID_ID;
6391 }
6392
6393 return rc;
6394}
6395
6396int vmsvga3dShaderDestroy(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type)
6397{
6398 PVMSVGA3DCONTEXT pContext;
6399 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6400 AssertReturn(pState, VERR_NO_MEMORY);
6401 PVMSVGA3DSHADER pShader = NULL;
6402 int rc;
6403
6404 Log(("vmsvga3dShaderDestroy cid=%x shid=%x type=%s\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL"));
6405
6406 if ( cid >= pState->cContexts
6407 || pState->papContexts[cid]->id != cid)
6408 {
6409 Log(("vmsvga3dShaderDestroy invalid context id!\n"));
6410 return VERR_INVALID_PARAMETER;
6411 }
6412 pContext = pState->papContexts[cid];
6413 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6414
6415 if (type == SVGA3D_SHADERTYPE_VS)
6416 {
6417 if ( shid < pContext->cVertexShaders
6418 && pContext->paVertexShader[shid].id == shid)
6419 {
6420 pShader = &pContext->paVertexShader[shid];
6421 rc = ShaderDestroyVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
6422 AssertRC(rc);
6423 }
6424 }
6425 else
6426 {
6427 Assert(type == SVGA3D_SHADERTYPE_PS);
6428 if ( shid < pContext->cPixelShaders
6429 && pContext->paPixelShader[shid].id == shid)
6430 {
6431 pShader = &pContext->paPixelShader[shid];
6432 rc = ShaderDestroyPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
6433 AssertRC(rc);
6434 }
6435 }
6436
6437 if (pShader)
6438 {
6439 if (pShader->pShaderProgram)
6440 RTMemFree(pShader->pShaderProgram);
6441 memset(pShader, 0, sizeof(*pShader));
6442 pShader->id = SVGA3D_INVALID_ID;
6443 }
6444 else
6445 AssertFailedReturn(VERR_INVALID_PARAMETER);
6446
6447 return VINF_SUCCESS;
6448}
6449
6450int vmsvga3dShaderSet(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext, uint32_t cid, SVGA3dShaderType type, uint32_t shid)
6451{
6452 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6453 AssertReturn(pState, VERR_NO_MEMORY);
6454 int rc;
6455
6456 Log(("vmsvga3dShaderSet cid=%x type=%s shid=%d\n", cid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", shid));
6457
6458 if ( !pContext
6459 && cid < pState->cContexts
6460 && pState->papContexts[cid]->id == cid)
6461 pContext = pState->papContexts[cid];
6462 else if (!pContext)
6463 {
6464 AssertMsgFailed(("cid=%#x cContexts=%#x\n", cid, pState->cContexts));
6465 Log(("vmsvga3dShaderSet invalid context id!\n"));
6466 return VERR_INVALID_PARAMETER;
6467 }
6468 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6469
6470 if (type == SVGA3D_SHADERTYPE_VS)
6471 {
6472 /* Save for vm state save/restore. */
6473 pContext->state.shidVertex = shid;
6474 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VERTEXSHADER;
6475
6476 if ( shid < pContext->cVertexShaders
6477 && pContext->paVertexShader[shid].id == shid)
6478 {
6479 PVMSVGA3DSHADER pShader = &pContext->paVertexShader[shid];
6480 Assert(type == pShader->type);
6481
6482 rc = ShaderSetVertexShader(pContext->pShaderContext, pShader->u.pVertexShader);
6483 AssertRCReturn(rc, rc);
6484 }
6485 else
6486 if (shid == SVGA_ID_INVALID)
6487 {
6488 /* Unselect shader. */
6489 rc = ShaderSetVertexShader(pContext->pShaderContext, NULL);
6490 AssertRCReturn(rc, rc);
6491 }
6492 else
6493 AssertFailedReturn(VERR_INVALID_PARAMETER);
6494 }
6495 else
6496 {
6497 /* Save for vm state save/restore. */
6498 pContext->state.shidPixel = shid;
6499 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_PIXELSHADER;
6500
6501 Assert(type == SVGA3D_SHADERTYPE_PS);
6502 if ( shid < pContext->cPixelShaders
6503 && pContext->paPixelShader[shid].id == shid)
6504 {
6505 PVMSVGA3DSHADER pShader = &pContext->paPixelShader[shid];
6506 Assert(type == pShader->type);
6507
6508 rc = ShaderSetPixelShader(pContext->pShaderContext, pShader->u.pPixelShader);
6509 AssertRCReturn(rc, rc);
6510 }
6511 else
6512 if (shid == SVGA_ID_INVALID)
6513 {
6514 /* Unselect shader. */
6515 rc = ShaderSetPixelShader(pContext->pShaderContext, NULL);
6516 AssertRCReturn(rc, rc);
6517 }
6518 else
6519 AssertFailedReturn(VERR_INVALID_PARAMETER);
6520 }
6521
6522 return VINF_SUCCESS;
6523}
6524
6525int vmsvga3dShaderSetConst(PVGASTATE pThis, uint32_t cid, uint32_t reg, SVGA3dShaderType type, SVGA3dShaderConstType ctype, uint32_t cRegisters, uint32_t *pValues)
6526{
6527 PVMSVGA3DCONTEXT pContext;
6528 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6529 AssertReturn(pState, VERR_NO_MEMORY);
6530 int rc;
6531
6532 Log(("vmsvga3dShaderSetConst cid=%x reg=%x type=%s cregs=%d ctype=%x\n", cid, reg, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cRegisters, ctype));
6533
6534 if ( cid >= pState->cContexts
6535 || pState->papContexts[cid]->id != cid)
6536 {
6537 Log(("vmsvga3dShaderSetConst invalid context id!\n"));
6538 return VERR_INVALID_PARAMETER;
6539 }
6540 pContext = pState->papContexts[cid];
6541 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
6542
6543 for (uint32_t i = 0; i < cRegisters; i++)
6544 {
6545#ifdef LOG_ENABLED
6546 switch (ctype)
6547 {
6548 case SVGA3D_CONST_TYPE_FLOAT:
6549 {
6550 float *pValuesF = (float *)pValues;
6551 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)));
6552 break;
6553 }
6554
6555 case SVGA3D_CONST_TYPE_INT:
6556 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]));
6557 break;
6558
6559 case SVGA3D_CONST_TYPE_BOOL:
6560 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]));
6561 break;
6562 }
6563#endif
6564 vmsvga3dSaveShaderConst(pContext, reg + i, type, ctype, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]);
6565 }
6566
6567 switch (type)
6568 {
6569 case SVGA3D_SHADERTYPE_VS:
6570 switch (ctype)
6571 {
6572 case SVGA3D_CONST_TYPE_FLOAT:
6573 rc = ShaderSetVertexShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
6574 break;
6575
6576 case SVGA3D_CONST_TYPE_INT:
6577 rc = ShaderSetVertexShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
6578 break;
6579
6580 case SVGA3D_CONST_TYPE_BOOL:
6581 rc = ShaderSetVertexShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
6582 break;
6583
6584 default:
6585 AssertFailedReturn(VERR_INVALID_PARAMETER);
6586 }
6587 AssertRCReturn(rc, rc);
6588 break;
6589
6590 case SVGA3D_SHADERTYPE_PS:
6591 switch (ctype)
6592 {
6593 case SVGA3D_CONST_TYPE_FLOAT:
6594 rc = ShaderSetPixelShaderConstantF(pContext->pShaderContext, reg, (const float *)pValues, cRegisters);
6595 break;
6596
6597 case SVGA3D_CONST_TYPE_INT:
6598 rc = ShaderSetPixelShaderConstantI(pContext->pShaderContext, reg, (const int32_t *)pValues, cRegisters);
6599 break;
6600
6601 case SVGA3D_CONST_TYPE_BOOL:
6602 rc = ShaderSetPixelShaderConstantB(pContext->pShaderContext, reg, (const uint8_t *)pValues, cRegisters);
6603 break;
6604
6605 default:
6606 AssertFailedReturn(VERR_INVALID_PARAMETER);
6607 }
6608 AssertRCReturn(rc, rc);
6609 break;
6610
6611 default:
6612 AssertFailedReturn(VERR_INVALID_PARAMETER);
6613 }
6614
6615 return VINF_SUCCESS;
6616}
6617
6618
6619int vmsvga3dQueryBegin(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type)
6620{
6621 AssertFailed();
6622 return VERR_NOT_IMPLEMENTED;
6623}
6624
6625int vmsvga3dQueryEnd(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type, SVGAGuestPtr guestResult)
6626{
6627 AssertFailed();
6628 return VERR_NOT_IMPLEMENTED;
6629}
6630
6631int vmsvga3dQueryWait(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type, SVGAGuestPtr guestResult)
6632{
6633 AssertFailed();
6634 return VERR_NOT_IMPLEMENTED;
6635}
6636
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