VirtualBox

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

Last change on this file since 57518 was 57517, checked in by vboxsync, 9 years ago

SVGA3d/ogl: Finally figured out the scrolling issue with the SVGA_3D_CMD_PRESENT implementation.

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