VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/util/blitter.cpp@ 53601

Last change on this file since 53601 was 53601, checked in by vboxsync, 10 years ago

Preps for replacing DevVGA-SVGA3d-cocoa.m with renderspu_cocoa_helper.m, i.e. sharing the objective-C OpenGL wrappers between the two 3D solutions.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 58.8 KB
Line 
1/* $Id$ */
2/** @file
3 * Blitter API implementation
4 */
5
6/*
7 * Copyright (C) 2013-2014 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#ifdef IN_VMSVGA3D
23# include <OpenGL/OpenGL.h>
24# include "../include/cr_blitter.h"
25# include <iprt/assert.h>
26# define WARN AssertMsgFailed
27# define CRASSERT Assert
28DECLINLINE(void) crWarning(const char *format, ... ) {}
29#else
30# include "cr_blitter.h"
31# include "cr_spu.h"
32# include "chromium.h"
33# include "cr_error.h"
34# include "cr_net.h"
35# include "cr_rand.h"
36# include "cr_mem.h"
37# include "cr_string.h"
38# include "cr_bmpscale.h"
39#endif
40
41#include <iprt/cdefs.h>
42#include <iprt/types.h>
43#include <iprt/mem.h>
44
45
46
47static void crMClrFillMem(uint32_t *pu32Dst, int32_t cbDstPitch, uint32_t width, uint32_t height, uint32_t u32Color)
48{
49 for (uint32_t i = 0; i < height; ++i)
50 {
51 for (uint32_t j = 0; j < width; ++j)
52 {
53 pu32Dst[j] = u32Color;
54 }
55
56 pu32Dst = (uint32_t*)(((uint8_t*)pu32Dst) + cbDstPitch);
57 }
58}
59
60void CrMClrFillImgRect(CR_BLITTER_IMG *pDst, const RTRECT *pCopyRect, uint32_t u32Color)
61{
62 int32_t x = pCopyRect->xLeft;
63 int32_t y = pCopyRect->yTop;
64 int32_t width = pCopyRect->xRight - pCopyRect->xLeft;
65 int32_t height = pCopyRect->yBottom - pCopyRect->yTop;
66 Assert(x >= 0);
67 Assert(y >= 0);
68 uint8_t *pu8Dst = ((uint8_t*)pDst->pvData) + pDst->pitch * y + x * 4;
69
70 crMClrFillMem((uint32_t*)pu8Dst, pDst->pitch, width, height, u32Color);
71}
72
73void CrMClrFillImg(CR_BLITTER_IMG *pImg, uint32_t cRects, const RTRECT *pRects, uint32_t u32Color)
74{
75 RTRECT Rect;
76 Rect.xLeft = 0;
77 Rect.yTop = 0;
78 Rect.xRight = pImg->width;
79 Rect.yBottom = pImg->height;
80
81
82 RTRECT Intersection;
83 const RTPOINT ZeroPoint = {0, 0};
84
85 for (uint32_t i = 0; i < cRects; ++i)
86 {
87 const RTRECT * pRect = &pRects[i];
88 VBoxRectIntersected(pRect, &Rect, &Intersection);
89
90 if (VBoxRectIsZero(&Intersection))
91 continue;
92
93 CrMClrFillImgRect(pImg, &Intersection, u32Color);
94 }
95}
96
97static void crMBltMem(const uint8_t *pu8Src, int32_t cbSrcPitch, uint8_t *pu8Dst, int32_t cbDstPitch, uint32_t width, uint32_t height)
98{
99 uint32_t cbCopyRow = width * 4;
100
101 for (uint32_t i = 0; i < height; ++i)
102 {
103 memcpy(pu8Dst, pu8Src, cbCopyRow);
104
105 pu8Src += cbSrcPitch;
106 pu8Dst += cbDstPitch;
107 }
108}
109
110void CrMBltImgRect(const CR_BLITTER_IMG *pSrc, const RTPOINT *pSrcDataPoint, bool fSrcInvert, const RTRECT *pCopyRect, CR_BLITTER_IMG *pDst)
111{
112 int32_t srcX = pCopyRect->xLeft - pSrcDataPoint->x;
113 int32_t srcY = pCopyRect->yTop - pSrcDataPoint->y;
114 Assert(srcX >= 0);
115 Assert(srcY >= 0);
116 Assert(srcX < (int32_t)pSrc->width);
117 Assert(srcY < (int32_t)pSrc->height);
118
119 int32_t dstX = pCopyRect->xLeft;
120 int32_t dstY = pCopyRect->yTop;
121 Assert(dstX >= 0);
122 Assert(dstY >= 0);
123 Assert(dstX < (int32_t)pDst->width);
124 Assert(dstY < (int32_t)pDst->height);
125
126 uint8_t *pu8Src = ((uint8_t*)pSrc->pvData) + pSrc->pitch * (!fSrcInvert ? srcY : pSrc->height - srcY - 1) + srcX * 4;
127 uint8_t *pu8Dst = ((uint8_t*)pDst->pvData) + pDst->pitch * dstY + dstX * 4;
128
129 crMBltMem(pu8Src, fSrcInvert ? -((int32_t)pSrc->pitch) : (int32_t)pSrc->pitch, pu8Dst, pDst->pitch, pCopyRect->xRight - pCopyRect->xLeft, pCopyRect->yBottom - pCopyRect->yTop);
130}
131
132void CrMBltImg(const CR_BLITTER_IMG *pSrc, const RTPOINT *pPos, uint32_t cRects, const RTRECT *pRects, CR_BLITTER_IMG *pDst)
133{
134 RTRECT Intersection;
135 RTRECT RestrictSrcRect;
136 RestrictSrcRect.xLeft = 0;
137 RestrictSrcRect.yTop = 0;
138 RestrictSrcRect.xRight = pSrc->width;
139 RestrictSrcRect.yBottom = pSrc->height;
140 RTRECT RestrictDstRect;
141 RestrictDstRect.xLeft = 0;
142 RestrictDstRect.yTop = 0;
143 RestrictDstRect.xRight = pDst->width;
144 RestrictDstRect.yBottom = pDst->height;
145
146 for (uint32_t i = 0; i < cRects; ++i)
147 {
148 const RTRECT * pRect = &pRects[i];
149 VBoxRectIntersected(pRect, &RestrictDstRect, &Intersection);
150
151 RTRECT TranslatedSrc;
152 VBoxRectTranslated(&RestrictSrcRect, pPos->x, pPos->y, &TranslatedSrc);
153
154 VBoxRectIntersect(&Intersection, &TranslatedSrc);
155
156 if (VBoxRectIsZero(&Intersection))
157 continue;
158
159 CrMBltImgRect(pSrc, pPos, false, &Intersection, pDst);
160 }
161}
162
163#ifndef IN_VMSVGA3D
164
165void CrMBltImgRectScaled(const CR_BLITTER_IMG *pSrc, const RTPOINT *pPos, bool fSrcInvert, const RTRECT *pCopyRect, float strX, float strY, CR_BLITTER_IMG *pDst)
166{
167 RTPOINT UnscaledPos;
168 UnscaledPos.x = CR_FLOAT_RCAST(int32_t, pPos->x / strX);
169 UnscaledPos.y = CR_FLOAT_RCAST(int32_t, pPos->y / strY);
170
171 RTRECT UnscaledCopyRect;
172
173 VBoxRectUnscaled(pCopyRect, strX, strY, &UnscaledCopyRect);
174
175 if (VBoxRectIsZero(&UnscaledCopyRect))
176 {
177 WARN(("ups"));
178 return;
179 }
180
181 int32_t srcX = UnscaledCopyRect.xLeft - UnscaledPos.x;
182 int32_t srcY = UnscaledCopyRect.yTop - UnscaledPos.y;
183 if (srcX < 0)
184 {
185 WARN(("ups"));
186 srcX = 0;
187 }
188 if (srcY < 0)
189 {
190 WARN(("ups"));
191 srcY = 0;
192 }
193
194 if (srcX >= pSrc->width)
195 {
196 WARN(("ups"));
197 return;
198 }
199
200 if (srcY >= pSrc->height)
201 {
202 WARN(("ups"));
203 return;
204 }
205
206 Assert(srcX >= 0);
207 Assert(srcY >= 0);
208 Assert(srcX < (int32_t)pSrc->width);
209 Assert(srcY < (int32_t)pSrc->height);
210
211 int32_t dstX = pCopyRect->xLeft;
212 int32_t dstY = pCopyRect->yTop;
213 Assert(dstX >= 0);
214 Assert(dstY >= 0);
215
216 int32_t UnscaledSrcWidth = UnscaledCopyRect.xRight - UnscaledCopyRect.xLeft;
217 int32_t UnscaledSrcHeight = UnscaledCopyRect.yBottom - UnscaledCopyRect.yTop;
218
219 if (UnscaledSrcWidth + srcX > pSrc->width)
220 UnscaledSrcWidth = pSrc->width - srcX;
221
222 if (UnscaledSrcHeight + srcY > pSrc->height)
223 UnscaledSrcHeight = pSrc->height - srcY;
224
225 uint8_t *pu8Src = ((uint8_t*)pSrc->pvData) + pSrc->pitch * (!fSrcInvert ? srcY : pSrc->height - srcY - 1) + srcX * 4;
226 uint8_t *pu8Dst = ((uint8_t*)pDst->pvData) + pDst->pitch * dstY + dstX * 4;
227
228 CrBmpScale32(pu8Dst, pDst->pitch,
229 pCopyRect->xRight - pCopyRect->xLeft, pCopyRect->yBottom - pCopyRect->yTop,
230 pu8Src,
231 fSrcInvert ? -((int32_t)pSrc->pitch) : (int32_t)pSrc->pitch,
232 UnscaledSrcWidth, UnscaledSrcHeight);
233}
234
235
236void CrMBltImgScaled(const CR_BLITTER_IMG *pSrc, const RTRECTSIZE *pSrcRectSize, const RTRECT *pDstRect, uint32_t cRects, const RTRECT *pRects, CR_BLITTER_IMG *pDst)
237{
238 int32_t srcWidth = pSrcRectSize->cx;
239 int32_t srcHeight = pSrcRectSize->cy;
240 int32_t dstWidth = pDstRect->xRight - pDstRect->xLeft;
241 int32_t dstHeight = pDstRect->yBottom - pDstRect->yTop;
242
243 float strX = ((float)dstWidth) / srcWidth;
244 float strY = ((float)dstHeight) / srcHeight;
245 bool fScale = (dstWidth != srcWidth || dstHeight != srcHeight);
246 Assert(fScale);
247
248 RTRECT Intersection;
249 RTRECT ScaledRestrictSrcRect;
250 ScaledRestrictSrcRect.xLeft = 0;
251 ScaledRestrictSrcRect.yTop = 0;
252 ScaledRestrictSrcRect.xRight = CR_FLOAT_RCAST(int32_t, pSrc->width * strX);
253 ScaledRestrictSrcRect.yBottom = CR_FLOAT_RCAST(int32_t, pSrc->height * strY);
254 RTRECT RestrictDstRect;
255 RestrictDstRect.xLeft = 0;
256 RestrictDstRect.yTop = 0;
257 RestrictDstRect.xRight = pDst->width;
258 RestrictDstRect.yBottom = pDst->height;
259
260 RTPOINT Pos = {pDstRect->xLeft, pDstRect->yTop};
261
262 for (uint32_t i = 0; i < cRects; ++i)
263 {
264 const RTRECT * pRect = &pRects[i];
265 VBoxRectIntersected(pRect, &RestrictDstRect, &Intersection);
266
267 RTRECT TranslatedSrc;
268 VBoxRectTranslated(&ScaledRestrictSrcRect, Pos.x, Pos.y, &TranslatedSrc);
269
270 VBoxRectIntersect(&Intersection, &TranslatedSrc);
271
272 if (VBoxRectIsZero(&Intersection))
273 continue;
274
275 CrMBltImgRectScaled(pSrc, &Pos, false, &Intersection, strX, strY, pDst);
276 }
277}
278
279#endif /* !IN_VMSVGA3D */
280
281
282/**
283 *
284 * @param pBlitter The blitter to initialize.
285 * @param pCtxBase Contains the blitter context info. Its value is
286 * treated differently depending on the fCreateNewCtx
287 * value.
288 * @param fCreateNewCtx If true, then @a pCtxBase must NOT be NULL. Its
289 * visualBits is used as a visual bits info for the new
290 * context, its id field is used to specified the
291 * shared context id to be used for blitter context.
292 * The id can be null to specify no shared context is
293 * needed
294 *
295 * If false and @a pCtxBase is NOT null AND its id
296 * field is NOT null, then specified the blitter
297 * context to be used blitter treats it as if it has
298 * default ogl state.
299 *
300 * Otherwise, the blitter works in a "no-context" mode,
301 * i.e. the§ caller is responsible for making a proper
302 * context current before calling the blitter. Note
303 * that BltEnter/Leave MUST still be called, but the
304 * proper context must be set before doing BltEnter,
305 * and ResoreContext info is ignored in that case. Also
306 * note that the blitter caches the current window
307 * info, and assumes the current context's values are
308 * preserved wrt that window before the calls, so if
309 * one uses different contexts for one blitter, the
310 * blitter current window values must be explicitly
311 * reset by doing CrBltMuralSetCurrentInfo(pBlitter,
312 * NULL).
313 * @param fForceDrawBlt If true this forces the blitter to always use
314 * glDrawXxx-based blits even if
315 * GL_EXT_framebuffer_blit. This is needed because
316 * BlitFramebufferEXT is often known to be buggy, and
317 * glDrawXxx-based blits appear to be more reliable.
318 * @param pShaders
319 * @param pDispatch
320 */
321VBOXBLITTERDECL(int) CrBltInit(PCR_BLITTER pBlitter, const CR_BLITTER_CONTEXT *pCtxBase,
322 bool fCreateNewCtx, bool fForceDrawBlt, const CR_GLSL_CACHE *pShaders,
323 SPUDispatchTable *pDispatch)
324{
325 if (pCtxBase && pCtxBase->Base.id < 0)
326 {
327 crWarning("Default share context not initialized!");
328 return VERR_INVALID_PARAMETER;
329 }
330
331 if (!pCtxBase && fCreateNewCtx)
332 {
333 crWarning("pCtxBase is zero while fCreateNewCtx is set!");
334 return VERR_INVALID_PARAMETER;
335 }
336
337 RT_ZERO(*pBlitter);
338
339 pBlitter->pDispatch = pDispatch;
340 if (pCtxBase)
341 pBlitter->CtxInfo = *pCtxBase;
342
343 pBlitter->Flags.ForceDrawBlit = fForceDrawBlt;
344
345 if (fCreateNewCtx)
346 {
347#ifdef IN_VMSVGA3D
348 /** @todo IN_VMSVGA3D */
349 pBlitter->CtxInfo.Base.id = 0;
350#else
351 pBlitter->CtxInfo.Base.id = pDispatch->CreateContext("", pCtxBase->Base.visualBits, pCtxBase->Base.id);
352#endif
353 if (!pBlitter->CtxInfo.Base.id)
354 {
355 RT_ZERO(*pBlitter);
356 crWarning("CreateContext failed!");
357 return VERR_GENERAL_FAILURE;
358 }
359 pBlitter->Flags.CtxCreated = 1;
360 }
361
362 if (pShaders)
363 {
364 pBlitter->pGlslCache = pShaders;
365 pBlitter->Flags.ShadersGloal = 1;
366 }
367 else
368 {
369 CrGlslInit(&pBlitter->LocalGlslCache, pDispatch);
370 pBlitter->pGlslCache = &pBlitter->LocalGlslCache;
371 }
372
373 return VINF_SUCCESS;
374}
375
376VBOXBLITTERDECL(int) CrBltCleanup(PCR_BLITTER pBlitter)
377{
378 if (CrBltIsEntered(pBlitter))
379 {
380 WARN(("CrBltBlitTexTex: blitter is entered"));
381 return VERR_INVALID_STATE;
382 }
383
384 if (pBlitter->Flags.ShadersGloal || !CrGlslNeedsCleanup(&pBlitter->LocalGlslCache))
385 return VINF_SUCCESS;
386
387 int rc = CrBltEnter(pBlitter);
388 if (!RT_SUCCESS(rc))
389 {
390 WARN(("CrBltEnter failed, rc %d", rc));
391 return rc;
392 }
393
394 CrGlslCleanup(&pBlitter->LocalGlslCache);
395
396 CrBltLeave(pBlitter);
397
398 return VINF_SUCCESS;
399}
400
401void CrBltTerm(PCR_BLITTER pBlitter)
402{
403#ifdef IN_VMSVGA3D
404 /** @todo IN_VMSVGA3D */
405#else
406 if (pBlitter->Flags.CtxCreated)
407 pBlitter->pDispatch->DestroyContext(pBlitter->CtxInfo.Base.id);
408#endif
409 memset(pBlitter, 0, sizeof (*pBlitter));
410}
411
412int CrBltMuralSetCurrentInfo(PCR_BLITTER pBlitter, const CR_BLITTER_WINDOW *pMural)
413{
414 if (pMural)
415 {
416 if (!memcmp(&pBlitter->CurrentMural, pMural, sizeof (pBlitter->CurrentMural)))
417 return VINF_SUCCESS;
418 memcpy(&pBlitter->CurrentMural, pMural, sizeof (pBlitter->CurrentMural));
419 }
420 else
421 {
422 if (CrBltIsEntered(pBlitter))
423 {
424 WARN(("can not set null mural for entered bleater"));
425 return VERR_INVALID_STATE;
426 }
427 if (!pBlitter->CurrentMural.Base.id)
428 return VINF_SUCCESS;
429 pBlitter->CurrentMural.Base.id = 0;
430 }
431
432 pBlitter->Flags.CurrentMuralChanged = 1;
433
434 if (!CrBltIsEntered(pBlitter))
435 return VINF_SUCCESS;
436
437 if (!pBlitter->CtxInfo.Base.id)
438 {
439 WARN(("setting current mural for entered no-context blitter"));
440 return VERR_INVALID_STATE;
441 }
442
443 WARN(("changing mural for entered blitter, is is somewhat expected?"));
444
445#ifdef IN_VMSVGA3D
446 /** @todo IN_VMSVGA3D */
447#else
448 pBlitter->pDispatch->Flush();
449
450 pBlitter->pDispatch->MakeCurrent(pMural->Base.id, pBlitter->i32MakeCurrentUserData, pBlitter->CtxInfo.Base.id);
451#endif
452
453 return VINF_SUCCESS;
454}
455
456
457#ifndef IN_VMSVGA3D
458
459static DECLCALLBACK(int) crBltBlitTexBufImplFbo(PCR_BLITTER pBlitter, const VBOXVR_TEXTURE *pSrc, const RTRECT *paSrcRect, const RTRECTSIZE *pDstSize, const RTRECT *paDstRect, uint32_t cRects, uint32_t fFlags)
460{
461 GLenum filter = CRBLT_FILTER_FROM_FLAGS(fFlags);
462 pBlitter->pDispatch->BindFramebufferEXT(GL_READ_FRAMEBUFFER, pBlitter->idFBO);
463 pBlitter->pDispatch->FramebufferTexture2DEXT(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, pSrc->target, pSrc->hwid, 0);
464 pBlitter->pDispatch->ReadBuffer(GL_COLOR_ATTACHMENT0);
465
466 for (uint32_t i = 0; i < cRects; ++i)
467 {
468 const RTRECT * pSrcRect = &paSrcRect[i];
469 const RTRECT * pDstRect = &paDstRect[i];
470 int32_t srcY1;
471 int32_t srcY2;
472 int32_t dstY1;
473 int32_t dstY2;
474 int32_t srcX1 = pSrcRect->xLeft;
475 int32_t srcX2 = pSrcRect->xRight;
476 int32_t dstX1 = pDstRect->xLeft;
477 int32_t dstX2 = pDstRect->xRight;
478
479 if (CRBLT_F_INVERT_SRC_YCOORDS & fFlags)
480 {
481 srcY1 = pSrc->height - pSrcRect->yTop;
482 srcY2 = pSrc->height - pSrcRect->yBottom;
483 }
484 else
485 {
486 srcY1 = pSrcRect->yTop;
487 srcY2 = pSrcRect->yBottom;
488 }
489
490 if (CRBLT_F_INVERT_DST_YCOORDS & fFlags)
491 {
492 dstY1 = pDstSize->cy - pDstRect->yTop;
493 dstY2 = pDstSize->cy - pDstRect->yBottom;
494 }
495 else
496 {
497 dstY1 = pDstRect->yTop;
498 dstY2 = pDstRect->yBottom;
499 }
500
501 if (srcY1 > srcY2)
502 {
503 if (dstY1 > dstY2)
504 {
505 /* use srcY1 < srcY2 && dstY1 < dstY2 whenever possible to avoid GPU driver bugs */
506 int32_t tmp = srcY1;
507 srcY1 = srcY2;
508 srcY2 = tmp;
509 tmp = dstY1;
510 dstY1 = dstY2;
511 dstY2 = tmp;
512 }
513 }
514
515 if (srcX1 > srcX2)
516 {
517 if (dstX1 > dstX2)
518 {
519 /* use srcX1 < srcX2 && dstX1 < dstX2 whenever possible to avoid GPU driver bugs */
520 int32_t tmp = srcX1;
521 srcX1 = srcX2;
522 srcX2 = tmp;
523 tmp = dstX1;
524 dstX1 = dstX2;
525 dstX2 = tmp;
526 }
527 }
528
529 pBlitter->pDispatch->BlitFramebufferEXT(srcX1, srcY1, srcX2, srcY2,
530 dstX1, dstY1, dstX2, dstY2,
531 GL_COLOR_BUFFER_BIT, filter);
532 }
533
534 return VINF_SUCCESS;
535}
536
537/* GL_TRIANGLE_FAN */
538DECLINLINE(GLfloat*) crBltVtRectTFNormalized(const RTRECT *pRect, uint32_t normalX, uint32_t normalY, GLfloat* pBuff, uint32_t height)
539{
540 /* going ccw:
541 * 1. (left;top) 4. (right;top)
542 * | ^
543 * > |
544 * 2. (left;bottom) -> 3. (right;bottom) */
545 /* xLeft yTop */
546 pBuff[0] = ((float)pRect->xLeft)/((float)normalX);
547 pBuff[1] = ((float)(height ? height - pRect->yTop : pRect->yTop))/((float)normalY);
548
549 /* xLeft yBottom */
550 pBuff[2] = pBuff[0];
551 pBuff[3] = ((float)(height ? height - pRect->yBottom : pRect->yBottom))/((float)normalY);
552
553 /* xRight yBottom */
554 pBuff[4] = ((float)pRect->xRight)/((float)normalX);
555 pBuff[5] = pBuff[3];
556
557 /* xRight yTop */
558 pBuff[6] = pBuff[4];
559 pBuff[7] = pBuff[1];
560 return &pBuff[8];
561}
562
563DECLINLINE(GLfloat*) crBltVtRectsTFNormalized(const RTRECT *paRects, uint32_t cRects, uint32_t normalX, uint32_t normalY, GLfloat* pBuff, uint32_t height)
564{
565 for (uint32_t i = 0; i < cRects; ++i)
566 {
567 pBuff = crBltVtRectTFNormalized(&paRects[i], normalX, normalY, pBuff, height);
568 }
569 return pBuff;
570}
571
572DECLINLINE(GLint*) crBltVtRectTF(const RTRECT *pRect, uint32_t normalX, uint32_t normalY, GLint* pBuff, uint32_t height)
573{
574 /* xLeft yTop */
575 pBuff[0] = pRect->xLeft;
576 pBuff[1] = height ? height - pRect->yTop : pRect->yTop;
577
578 /* xLeft yBottom */
579 pBuff[2] = pBuff[0];
580 pBuff[3] = height ? height - pRect->yBottom : pRect->yBottom;
581
582 /* xRight yBottom */
583 pBuff[4] = pRect->xRight;
584 pBuff[5] = pBuff[3];
585
586 /* xRight yTop */
587 pBuff[6] = pBuff[4];
588 pBuff[7] = pBuff[1];
589 return &pBuff[8];
590}
591
592DECLINLINE(GLubyte*) crBltVtFillRectIndicies(GLubyte *pIndex, GLubyte *piBase)
593{
594 GLubyte iBase = *piBase;
595 /* triangle 1 */
596 pIndex[0] = iBase;
597 pIndex[1] = iBase + 1;
598 pIndex[2] = iBase + 2;
599
600 /* triangle 2 */
601 pIndex[3] = iBase;
602 pIndex[4] = iBase + 2;
603 pIndex[5] = iBase + 3;
604 *piBase = iBase + 4;
605 return pIndex + 6;
606}
607
608/* Indexed GL_TRIANGLES */
609DECLINLINE(GLfloat*) crBltVtRectITNormalized(const RTRECT *pRect, uint32_t normalX, uint32_t normalY, GLfloat* pBuff, uint32_t height)
610{
611 GLfloat* ret = crBltVtRectTFNormalized(pRect, normalX, normalY, pBuff, height);
612 return ret;
613}
614
615DECLINLINE(GLint*) crBltVtRectIT(RTRECT *pRect, uint32_t normalX, uint32_t normalY, GLint* pBuff, GLubyte **ppIndex, GLubyte *piBase, uint32_t height)
616{
617 GLint* ret = crBltVtRectTF(pRect, normalX, normalY, pBuff, height);
618
619 if (ppIndex)
620 *ppIndex = crBltVtFillRectIndicies(*ppIndex, piBase);
621
622 return ret;
623}
624
625DECLINLINE(GLuint) crBltVtGetNumVerticiesTF(GLuint cRects)
626{
627 return cRects * 4;
628}
629
630#define crBltVtGetNumVerticiesIT crBltVtGetNumVerticiesTF
631
632DECLINLINE(GLuint) crBltVtGetNumIndiciesIT(GLuint cRects)
633{
634 return 6 * cRects;
635}
636
637
638static GLfloat* crBltVtRectsITNormalized(const RTRECT *paRects, uint32_t cRects, uint32_t normalX, uint32_t normalY, GLfloat* pBuff, GLubyte **ppIndex, GLubyte *piBase, uint32_t height)
639{
640 uint32_t i;
641 for (i = 0; i < cRects; ++i)
642 {
643 pBuff = crBltVtRectITNormalized(&paRects[i], normalX, normalY, pBuff, height);
644 }
645
646
647 if (ppIndex)
648 {
649 GLubyte *pIndex = (GLubyte*)pBuff;
650 *ppIndex = pIndex;
651 for (i = 0; i < cRects; ++i)
652 {
653 pIndex = crBltVtFillRectIndicies(pIndex, piBase);
654 }
655 pBuff = (GLfloat*)pIndex;
656 }
657
658 return pBuff;
659}
660
661static void *crBltBufGet(PCR_BLITTER_BUFFER pBuffer, GLuint cbBuffer)
662{
663 if (pBuffer->cbBuffer < cbBuffer)
664 {
665 if (pBuffer->pvBuffer)
666 {
667 RTMemFree(pBuffer->pvBuffer);
668 }
669
670#ifndef DEBUG_misha
671 /* debugging: ensure we calculate proper buffer size */
672 cbBuffer += 16;
673#endif
674
675 pBuffer->pvBuffer = RTMemAlloc(cbBuffer);
676 if (pBuffer->pvBuffer)
677 pBuffer->cbBuffer = cbBuffer;
678 else
679 {
680 crWarning("failed to allocate buffer of size %d", cbBuffer);
681 pBuffer->cbBuffer = 0;
682 }
683 }
684 return pBuffer->pvBuffer;
685}
686
687#endif /* !IN_VMSVGA3D */
688
689
690static void crBltCheckSetupViewport(PCR_BLITTER pBlitter, const RTRECTSIZE *pDstSize, bool fFBODraw)
691{
692 bool fUpdateViewport = pBlitter->Flags.CurrentMuralChanged;
693 if ( pBlitter->CurrentSetSize.cx != pDstSize->cx
694 || pBlitter->CurrentSetSize.cy != pDstSize->cy)
695 {
696 pBlitter->CurrentSetSize = *pDstSize;
697#ifdef IN_VMSVGA3D
698 /** @todo IN_VMSVGA3D */
699#else
700 pBlitter->pDispatch->MatrixMode(GL_PROJECTION);
701 pBlitter->pDispatch->LoadIdentity();
702 pBlitter->pDispatch->Ortho(0, pDstSize->cx, 0, pDstSize->cy, -1, 1);
703#endif
704 fUpdateViewport = true;
705 }
706
707 if (fUpdateViewport)
708 {
709#ifdef IN_VMSVGA3D
710 /** @todo IN_VMSVGA3D */
711#else
712 pBlitter->pDispatch->Viewport(0, 0, pBlitter->CurrentSetSize.cx, pBlitter->CurrentSetSize.cy);
713#endif
714 pBlitter->Flags.CurrentMuralChanged = 0;
715 }
716
717 pBlitter->Flags.LastWasFBODraw = fFBODraw;
718}
719
720
721#ifndef IN_VMSVGA3D
722
723static DECLCALLBACK(int) crBltBlitTexBufImplDraw2D(PCR_BLITTER pBlitter, const VBOXVR_TEXTURE *pSrc, const RTRECT *paSrcRect, const RTRECTSIZE *pDstSize, const RTRECT *paDstRect, uint32_t cRects, uint32_t fFlags)
724{
725 GLuint normalX, normalY;
726 uint32_t srcHeight = (fFlags & CRBLT_F_INVERT_SRC_YCOORDS) ? pSrc->height : 0;
727 uint32_t dstHeight = (fFlags & CRBLT_F_INVERT_DST_YCOORDS) ? pDstSize->cy : 0;
728
729 switch (pSrc->target)
730 {
731 case GL_TEXTURE_2D:
732 {
733 normalX = pSrc->width;
734 normalY = pSrc->height;
735 break;
736 }
737
738 case GL_TEXTURE_RECTANGLE_ARB:
739 {
740 normalX = 1;
741 normalY = 1;
742 break;
743 }
744
745 default:
746 {
747 crWarning("Unsupported texture target 0x%x", pSrc->target);
748 return VERR_INVALID_PARAMETER;
749 }
750 }
751
752 Assert(pSrc->hwid);
753
754 pBlitter->pDispatch->BindTexture(pSrc->target, pSrc->hwid);
755
756 if (cRects == 1)
757 {
758 /* just optimization to draw a single rect with GL_TRIANGLE_FAN */
759 GLfloat *pVerticies;
760 GLfloat *pTexCoords;
761 GLuint cElements = crBltVtGetNumVerticiesTF(cRects);
762
763 pVerticies = (GLfloat*)crBltBufGet(&pBlitter->Verticies, cElements * 2 * 2 * sizeof (*pVerticies));
764 pTexCoords = crBltVtRectsTFNormalized(paDstRect, cRects, 1, 1, pVerticies, dstHeight);
765 crBltVtRectsTFNormalized(paSrcRect, cRects, normalX, normalY, pTexCoords, srcHeight);
766
767 pBlitter->pDispatch->EnableClientState(GL_VERTEX_ARRAY);
768 pBlitter->pDispatch->VertexPointer(2, GL_FLOAT, 0, pVerticies);
769
770 pBlitter->pDispatch->EnableClientState(GL_TEXTURE_COORD_ARRAY);
771 pBlitter->pDispatch->TexCoordPointer(2, GL_FLOAT, 0, pTexCoords);
772
773 pBlitter->pDispatch->Enable(pSrc->target);
774
775 pBlitter->pDispatch->DrawArrays(GL_TRIANGLE_FAN, 0, cElements);
776
777 pBlitter->pDispatch->Disable(pSrc->target);
778
779 pBlitter->pDispatch->DisableClientState(GL_TEXTURE_COORD_ARRAY);
780 pBlitter->pDispatch->DisableClientState(GL_VERTEX_ARRAY);
781 }
782 else
783 {
784 GLfloat *pVerticies;
785 GLfloat *pTexCoords;
786 GLubyte *pIndicies;
787 GLuint cElements = crBltVtGetNumVerticiesIT(cRects);
788 GLuint cIndicies = crBltVtGetNumIndiciesIT(cRects);
789 GLubyte iIdxBase = 0;
790
791 pVerticies = (GLfloat*)crBltBufGet(&pBlitter->Verticies, cElements * 2 * 2 * sizeof (*pVerticies) + cIndicies * sizeof (*pIndicies));
792 pTexCoords = crBltVtRectsITNormalized(paDstRect, cRects, 1, 1, pVerticies, &pIndicies, &iIdxBase, dstHeight);
793 crBltVtRectsITNormalized(paSrcRect, cRects, normalX, normalY, pTexCoords, NULL, NULL, srcHeight);
794
795 pBlitter->pDispatch->EnableClientState(GL_VERTEX_ARRAY);
796 pBlitter->pDispatch->VertexPointer(2, GL_FLOAT, 0, pVerticies);
797
798 pBlitter->pDispatch->EnableClientState(GL_TEXTURE_COORD_ARRAY);
799 pBlitter->pDispatch->TexCoordPointer(2, GL_FLOAT, 0, pTexCoords);
800
801 pBlitter->pDispatch->Enable(pSrc->target);
802
803 pBlitter->pDispatch->DrawElements(GL_TRIANGLES, cIndicies, GL_UNSIGNED_BYTE, pIndicies);
804
805 pBlitter->pDispatch->Disable(pSrc->target);
806
807 pBlitter->pDispatch->DisableClientState(GL_TEXTURE_COORD_ARRAY);
808 pBlitter->pDispatch->DisableClientState(GL_VERTEX_ARRAY);
809 }
810
811 pBlitter->pDispatch->BindTexture(pSrc->target, 0);
812
813 return VINF_SUCCESS;
814}
815
816static int crBltInitOnMakeCurent(PCR_BLITTER pBlitter)
817{
818 const char * pszExtension = (const char*)pBlitter->pDispatch->GetString(GL_EXTENSIONS);
819 if (crStrstr(pszExtension, "GL_EXT_framebuffer_object"))
820 {
821 pBlitter->Flags.SupportsFBO = 1;
822 pBlitter->pDispatch->GenFramebuffersEXT(1, &pBlitter->idFBO);
823 Assert(pBlitter->idFBO);
824 }
825 else
826 crWarning("GL_EXT_framebuffer_object not supported, blitter can only blit to window");
827
828 if (crStrstr(pszExtension, "GL_ARB_pixel_buffer_object"))
829 pBlitter->Flags.SupportsPBO = 1;
830 else
831 crWarning("GL_ARB_pixel_buffer_object not supported");
832
833 /* BlitFramebuffer seems to be buggy on Intel,
834 * try always glDrawXxx for now */
835 if (!pBlitter->Flags.ForceDrawBlit && crStrstr(pszExtension, "GL_EXT_framebuffer_blit"))
836 {
837 pBlitter->pfnBlt = crBltBlitTexBufImplFbo;
838 }
839 else
840 {
841// crWarning("GL_EXT_framebuffer_blit not supported, will use Draw functions for blitting, which might be less efficient");
842 pBlitter->pfnBlt = crBltBlitTexBufImplDraw2D;
843 }
844
845 /* defaults. but just in case */
846 pBlitter->pDispatch->MatrixMode(GL_TEXTURE);
847 pBlitter->pDispatch->LoadIdentity();
848 pBlitter->pDispatch->MatrixMode(GL_MODELVIEW);
849 pBlitter->pDispatch->LoadIdentity();
850
851 return VINF_SUCCESS;
852}
853
854#endif /* !IN_VMSVGA3D */
855
856
857void CrBltLeave(PCR_BLITTER pBlitter)
858{
859 if (!pBlitter->cEnters)
860 {
861 WARN(("blitter not entered!"));
862 return;
863 }
864
865 if (--pBlitter->cEnters)
866 return;
867
868#ifdef IN_VMSVGA3D
869 /** @todo IN_VMSVGA3D */
870#else
871 if (pBlitter->Flags.SupportsFBO)
872 {
873 pBlitter->pDispatch->BindFramebufferEXT(GL_FRAMEBUFFER, 0);
874 pBlitter->pDispatch->DrawBuffer(GL_BACK);
875 pBlitter->pDispatch->ReadBuffer(GL_BACK);
876 }
877
878 pBlitter->pDispatch->Flush();
879
880 if (pBlitter->CtxInfo.Base.id)
881 pBlitter->pDispatch->MakeCurrent(0, 0, 0);
882#endif
883}
884
885int CrBltEnter(PCR_BLITTER pBlitter)
886{
887 if (!pBlitter->CurrentMural.Base.id && pBlitter->CtxInfo.Base.id)
888 {
889 WARN(("current mural not initialized!"));
890 return VERR_INVALID_STATE;
891 }
892
893 if (pBlitter->cEnters++)
894 return VINF_SUCCESS;
895
896 if (pBlitter->CurrentMural.Base.id) /* <- pBlitter->CurrentMural.Base.id can be null if the blitter is in a "no-context" mode (see comments to BltInit for detail)*/
897 {
898#ifdef IN_VMSVGA3D
899 /** @todo IN_VMSVGA3D */
900#else
901 pBlitter->pDispatch->MakeCurrent(pBlitter->CurrentMural.Base.id, pBlitter->i32MakeCurrentUserData, pBlitter->CtxInfo.Base.id);
902#endif
903 }
904
905 if (pBlitter->Flags.Initialized)
906 return VINF_SUCCESS;
907
908#ifdef IN_VMSVGA3D
909 /** @todo IN_VMSVGA3D */
910 int rc = VINF_SUCCESS;
911#else
912 int rc = crBltInitOnMakeCurent(pBlitter);
913#endif
914 if (RT_SUCCESS(rc))
915 {
916 pBlitter->Flags.Initialized = 1;
917 return VINF_SUCCESS;
918 }
919
920 WARN(("crBltInitOnMakeCurent failed, rc %d", rc));
921 CrBltLeave(pBlitter);
922 return rc;
923}
924
925
926static void crBltBlitTexBuf(PCR_BLITTER pBlitter, const VBOXVR_TEXTURE *pSrc, const RTRECT *paSrcRects, GLenum enmDstBuff, const RTRECTSIZE *pDstSize, const RTRECT *paDstRects, uint32_t cRects, uint32_t fFlags)
927{
928#ifdef IN_VMSVGA3D
929 /** @todo IN_VMSVGA3D */
930#else
931 pBlitter->pDispatch->DrawBuffer(enmDstBuff);
932
933 crBltCheckSetupViewport(pBlitter, pDstSize, enmDstBuff == GL_DRAW_FRAMEBUFFER);
934
935 if (!(fFlags & CRBLT_F_NOALPHA))
936 pBlitter->pfnBlt(pBlitter, pSrc, paSrcRects, pDstSize, paDstRects, cRects, fFlags);
937 else
938 {
939 int rc = pBlitter->Flags.ShadersGloal
940 ? CrGlslProgUseNoAlpha(pBlitter->pGlslCache, pSrc->target)
941 : CrGlslProgUseGenNoAlpha(&pBlitter->LocalGlslCache, pSrc->target);
942
943 if (!RT_SUCCESS(rc))
944 {
945 crWarning("Failed to use no-alpha program rc %d!, falling back to default blit", rc);
946 pBlitter->pfnBlt(pBlitter, pSrc, paSrcRects, pDstSize, paDstRects, cRects, fFlags);
947 return;
948 }
949
950 /* since we use shaders, we need to use draw commands rather than framebuffer blits.
951 * force using draw-based blitting */
952 crBltBlitTexBufImplDraw2D(pBlitter, pSrc, paSrcRects, pDstSize, paDstRects, cRects, fFlags);
953
954 Assert(pBlitter->Flags.ShadersGloal || &pBlitter->LocalGlslCache == pBlitter->pGlslCache);
955
956 CrGlslProgClear(pBlitter->pGlslCache);
957 }
958#endif
959}
960
961void CrBltCheckUpdateViewport(PCR_BLITTER pBlitter)
962{
963 RTRECTSIZE DstSize = {pBlitter->CurrentMural.width, pBlitter->CurrentMural.height};
964 crBltCheckSetupViewport(pBlitter, &DstSize, false);
965}
966
967void CrBltBlitTexMural(PCR_BLITTER pBlitter, bool fBb, const VBOXVR_TEXTURE *pSrc, const RTRECT *paSrcRects, const RTRECT *paDstRects, uint32_t cRects, uint32_t fFlags)
968{
969 if (!CrBltIsEntered(pBlitter))
970 {
971 WARN(("CrBltBlitTexMural: blitter not entered"));
972 return;
973 }
974
975 RTRECTSIZE DstSize = {pBlitter->CurrentMural.width, pBlitter->CurrentMural.height};
976
977#ifdef IN_VMSVGA3D
978 /** @todo IN_VMSVGA3D */
979#else
980 pBlitter->pDispatch->BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
981#endif
982
983 crBltBlitTexBuf(pBlitter, pSrc, paSrcRects, fBb ? GL_BACK : GL_FRONT, &DstSize, paDstRects, cRects, fFlags);
984}
985
986
987#ifndef IN_VMSVGA3D
988
989void CrBltBlitTexTex(PCR_BLITTER pBlitter, const VBOXVR_TEXTURE *pSrc, const RTRECT *pSrcRect, const VBOXVR_TEXTURE *pDst, const RTRECT *pDstRect, uint32_t cRects, uint32_t fFlags)
990{
991 if (!CrBltIsEntered(pBlitter))
992 {
993 WARN(("CrBltBlitTexTex: blitter not entered"));
994 return;
995 }
996
997 RTRECTSIZE DstSize = {(uint32_t)pDst->width, (uint32_t)pDst->height};
998
999 pBlitter->pDispatch->BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, pBlitter->idFBO);
1000
1001 /* TODO: mag/min filters ? */
1002
1003 pBlitter->pDispatch->FramebufferTexture2DEXT(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, pDst->target, pDst->hwid, 0);
1004
1005// pBlitter->pDispatch->FramebufferTexture2DEXT(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
1006// pBlitter->pDispatch->FramebufferTexture2DEXT(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
1007
1008 crBltBlitTexBuf(pBlitter, pSrc, pSrcRect, GL_DRAW_FRAMEBUFFER, &DstSize, pDstRect, cRects, fFlags);
1009
1010 pBlitter->pDispatch->FramebufferTexture2DEXT(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, pDst->target, 0, 0);
1011}
1012
1013void CrBltPresent(PCR_BLITTER pBlitter)
1014{
1015 if (!CrBltIsEntered(pBlitter))
1016 {
1017 WARN(("CrBltPresent: blitter not entered"));
1018 return;
1019 }
1020
1021 if (pBlitter->CtxInfo.Base.visualBits & CR_DOUBLE_BIT)
1022 pBlitter->pDispatch->SwapBuffers(pBlitter->CurrentMural.Base.id, 0);
1023 else
1024 pBlitter->pDispatch->Flush();
1025}
1026
1027static int crBltImgInitBaseForTex(const VBOXVR_TEXTURE *pSrc, CR_BLITTER_IMG *pDst, GLenum enmFormat)
1028{
1029 memset(pDst, 0, sizeof (*pDst));
1030 if (enmFormat != GL_RGBA
1031 && enmFormat != GL_BGRA)
1032 {
1033 WARN(("unsupported format 0x%x", enmFormat));
1034 return VERR_NOT_IMPLEMENTED;
1035 }
1036
1037 uint32_t bpp = 32;
1038
1039 uint32_t pitch = ((bpp * pSrc->width) + 7) >> 3;
1040 uint32_t cbData = pitch * pSrc->height;
1041 pDst->cbData = cbData;
1042 pDst->enmFormat = enmFormat;
1043 pDst->width = pSrc->width;
1044 pDst->height = pSrc->height;
1045 pDst->bpp = bpp;
1046 pDst->pitch = pitch;
1047 return VINF_SUCCESS;
1048}
1049
1050static int crBltImgCreateForTex(const VBOXVR_TEXTURE *pSrc, CR_BLITTER_IMG *pDst, GLenum enmFormat)
1051{
1052 int rc = crBltImgInitBaseForTex(pSrc, pDst, enmFormat);
1053 if (!RT_SUCCESS(rc))
1054 {
1055 crWarning("crBltImgInitBaseForTex failed rc %d", rc);
1056 return rc;
1057 }
1058
1059 uint32_t cbData = pDst->cbData;
1060 pDst->pvData = RTMemAllocZ(cbData);
1061 if (!pDst->pvData)
1062 {
1063 crWarning("RTMemAlloc failed");
1064 return VERR_NO_MEMORY;
1065 }
1066
1067#ifdef DEBUG_misha
1068 {
1069 char *pTmp = (char*)pDst->pvData;
1070 for (uint32_t i = 0; i < cbData; ++i)
1071 {
1072 pTmp[i] = (char)((1 << i) % 255);
1073 }
1074 }
1075#endif
1076 return VINF_SUCCESS;
1077}
1078
1079VBOXBLITTERDECL(int) CrBltImgGetTex(PCR_BLITTER pBlitter, const VBOXVR_TEXTURE *pSrc, GLenum enmFormat, CR_BLITTER_IMG *pDst)
1080{
1081 if (!CrBltIsEntered(pBlitter))
1082 {
1083 WARN(("CrBltImgGetTex: blitter not entered"));
1084 return VERR_INVALID_STATE;
1085 }
1086
1087 int rc = crBltImgCreateForTex(pSrc, pDst, enmFormat);
1088 if (!RT_SUCCESS(rc))
1089 {
1090 crWarning("crBltImgCreateForTex failed, rc %d", rc);
1091 return rc;
1092 }
1093 pBlitter->pDispatch->BindTexture(pSrc->target, pSrc->hwid);
1094
1095#ifdef DEBUG_misha
1096 {
1097 GLint width = 0, height = 0, depth = 0;
1098 pBlitter->pDispatch->GetTexLevelParameteriv(pSrc->target, 0, GL_TEXTURE_WIDTH, &width);
1099 pBlitter->pDispatch->GetTexLevelParameteriv(pSrc->target, 0, GL_TEXTURE_HEIGHT, &height);
1100 pBlitter->pDispatch->GetTexLevelParameteriv(pSrc->target, 0, GL_TEXTURE_DEPTH, &depth);
1101
1102 Assert(width == pSrc->width);
1103 Assert(height == pSrc->height);
1104// Assert(depth == pSrc->depth);
1105 }
1106#endif
1107
1108 pBlitter->pDispatch->GetTexImage(pSrc->target, 0, enmFormat, GL_UNSIGNED_BYTE, pDst->pvData);
1109
1110 pBlitter->pDispatch->BindTexture(pSrc->target, 0);
1111 return VINF_SUCCESS;
1112}
1113
1114VBOXBLITTERDECL(int) CrBltImgGetMural(PCR_BLITTER pBlitter, bool fBb, CR_BLITTER_IMG *pDst)
1115{
1116 if (!CrBltIsEntered(pBlitter))
1117 {
1118 WARN(("CrBltImgGetMural: blitter not entered"));
1119 return VERR_INVALID_STATE;
1120 }
1121
1122 WARN(("NOT IMPLEMENTED"));
1123 return VERR_NOT_IMPLEMENTED;
1124}
1125
1126VBOXBLITTERDECL(void) CrBltImgFree(PCR_BLITTER pBlitter, CR_BLITTER_IMG *pDst)
1127{
1128 if (!CrBltIsEntered(pBlitter))
1129 {
1130 WARN(("CrBltImgFree: blitter not entered"));
1131 return;
1132 }
1133
1134 if (pDst->pvData)
1135 {
1136 RTMemFree(pDst->pvData);
1137 pDst->pvData = NULL;
1138 }
1139}
1140
1141
1142VBOXBLITTERDECL(bool) CrGlslIsSupported(CR_GLSL_CACHE *pCache)
1143{
1144 if (pCache->iGlVersion == 0)
1145 {
1146 const char * pszStr = (const char*)pCache->pDispatch->GetString(GL_VERSION);
1147 pCache->iGlVersion = crStrParseGlVersion(pszStr);
1148 if (pCache->iGlVersion <= 0)
1149 {
1150 crWarning("crStrParseGlVersion returned %d", pCache->iGlVersion);
1151 pCache->iGlVersion = -1;
1152 }
1153 }
1154
1155 if (pCache->iGlVersion >= CR_GLVERSION_COMPOSE(2, 0, 0))
1156 return true;
1157
1158 crWarning("GLSL unsuported, gl version %d", pCache->iGlVersion);
1159
1160 /* @todo: we could also check for GL_ARB_shader_objects and GL_ARB_fragment_shader,
1161 * but seems like chromium does not support properly gl*Object versions of shader functions used with those extensions */
1162 return false;
1163}
1164
1165#define CR_GLSL_STR_V_120 "#version 120\n"
1166#define CR_GLSL_STR_EXT_TR "#extension GL_ARB_texture_rectangle : enable\n"
1167#define CR_GLSL_STR_2D "2D"
1168#define CR_GLSL_STR_2DRECT "2DRect"
1169
1170#define CR_GLSL_PATTERN_FS_NOALPHA(_ver, _ext, _tex) \
1171 _ver \
1172 _ext \
1173 "uniform sampler" _tex " sampler0;\n" \
1174 "void main()\n" \
1175 "{\n" \
1176 "vec2 srcCoord = vec2(gl_TexCoord[0]);\n" \
1177 "gl_FragData[0].xyz = (texture" _tex "(sampler0, srcCoord).xyz);\n" \
1178 "gl_FragData[0].w = 1.0;\n" \
1179 "}\n"
1180
1181static const char* crGlslGetFsStringNoAlpha(CR_GLSL_CACHE *pCache, GLenum enmTexTarget)
1182{
1183 if (!CrGlslIsSupported(pCache))
1184 {
1185 crWarning("CrGlslIsSupported is false");
1186 return NULL;
1187 }
1188
1189 if (pCache->iGlVersion >= CR_GLVERSION_COMPOSE(2, 1, 0))
1190 {
1191 if (enmTexTarget == GL_TEXTURE_2D)
1192 return CR_GLSL_PATTERN_FS_NOALPHA(CR_GLSL_STR_V_120, "", CR_GLSL_STR_2D);
1193 else if (enmTexTarget == GL_TEXTURE_RECTANGLE_ARB)
1194 return CR_GLSL_PATTERN_FS_NOALPHA(CR_GLSL_STR_V_120, CR_GLSL_STR_EXT_TR, CR_GLSL_STR_2DRECT);
1195
1196 crWarning("invalid enmTexTarget %#x", enmTexTarget);
1197 return NULL;
1198 }
1199 else if (pCache->iGlVersion >= CR_GLVERSION_COMPOSE(2, 0, 0))
1200 {
1201 if (enmTexTarget == GL_TEXTURE_2D)
1202 return CR_GLSL_PATTERN_FS_NOALPHA("", "", CR_GLSL_STR_2D);
1203 else if (enmTexTarget == GL_TEXTURE_RECTANGLE_ARB)
1204 return CR_GLSL_PATTERN_FS_NOALPHA("", CR_GLSL_STR_EXT_TR, CR_GLSL_STR_2DRECT);
1205
1206 crWarning("invalid enmTexTarget %#x", enmTexTarget);
1207 return NULL;
1208 }
1209
1210 crError("crGlslGetFsStringNoAlpha: we should not be here!");
1211 return NULL;
1212}
1213
1214static int crGlslProgGenNoAlpha(CR_GLSL_CACHE *pCache, GLenum enmTexTarget, GLuint *puiProgram)
1215{
1216 *puiProgram = 0;
1217
1218 const char*pStrFsShader = crGlslGetFsStringNoAlpha(pCache, enmTexTarget);
1219 if (!pStrFsShader)
1220 {
1221 crWarning("crGlslGetFsStringNoAlpha failed");
1222 return VERR_NOT_SUPPORTED;
1223 }
1224
1225 int rc = VINF_SUCCESS;
1226 GLchar * pBuf = NULL;
1227 GLuint uiProgram = 0;
1228 GLint iUniform = -1;
1229 GLuint uiShader = pCache->pDispatch->CreateShader(GL_FRAGMENT_SHADER);
1230 if (!uiShader)
1231 {
1232 crWarning("CreateShader failed");
1233 return VERR_NOT_SUPPORTED;
1234 }
1235
1236 pCache->pDispatch->ShaderSource(uiShader, 1, &pStrFsShader, NULL);
1237
1238 pCache->pDispatch->CompileShader(uiShader);
1239
1240 GLint compiled = 0;
1241 pCache->pDispatch->GetShaderiv(uiShader, GL_COMPILE_STATUS, &compiled);
1242
1243#ifndef DEBUG_misha
1244 if(!compiled)
1245#endif
1246 {
1247 if (!pBuf)
1248 pBuf = (GLchar *)RTMemAlloc(16300);
1249 pCache->pDispatch->GetShaderInfoLog(uiShader, 16300, NULL, pBuf);
1250#ifdef DEBUG_misha
1251 if (compiled)
1252 crDebug("compile success:\n-------------------\n%s\n--------\n", pBuf);
1253 else
1254#endif
1255 {
1256 crWarning("compile FAILURE:\n-------------------\n%s\n--------\n", pBuf);
1257 rc = VERR_NOT_SUPPORTED;
1258 goto end;
1259 }
1260 }
1261
1262 Assert(compiled);
1263
1264 uiProgram = pCache->pDispatch->CreateProgram();
1265 if (!uiProgram)
1266 {
1267 rc = VERR_NOT_SUPPORTED;
1268 goto end;
1269 }
1270
1271 pCache->pDispatch->AttachShader(uiProgram, uiShader);
1272
1273 pCache->pDispatch->LinkProgram(uiProgram);
1274
1275 GLint linked;
1276 pCache->pDispatch->GetProgramiv(uiProgram, GL_LINK_STATUS, &linked);
1277#ifndef DEBUG_misha
1278 if(!linked)
1279#endif
1280 {
1281 if (!pBuf)
1282 pBuf = (GLchar *)RTMemAlloc(16300);
1283 pCache->pDispatch->GetProgramInfoLog(uiProgram, 16300, NULL, pBuf);
1284#ifdef DEBUG_misha
1285 if (linked)
1286 crDebug("link success:\n-------------------\n%s\n--------\n", pBuf);
1287 else
1288#endif
1289 {
1290 crWarning("link FAILURE:\n-------------------\n%s\n--------\n", pBuf);
1291 rc = VERR_NOT_SUPPORTED;
1292 goto end;
1293 }
1294 }
1295
1296 Assert(linked);
1297
1298 iUniform = pCache->pDispatch->GetUniformLocation(uiProgram, "sampler0");
1299 if (iUniform == -1)
1300 {
1301 crWarning("GetUniformLocation failed for sampler0");
1302 }
1303 else
1304 {
1305 pCache->pDispatch->Uniform1i(iUniform, 0);
1306 }
1307
1308 *puiProgram = uiProgram;
1309
1310 /* avoid end finalizer from cleaning it */
1311 uiProgram = 0;
1312
1313 end:
1314 if (uiShader)
1315 pCache->pDispatch->DeleteShader(uiShader);
1316 if (uiProgram)
1317 pCache->pDispatch->DeleteProgram(uiProgram);
1318 if (pBuf)
1319 RTMemFree(pBuf);
1320 return rc;
1321}
1322
1323DECLINLINE(GLuint) crGlslProgGetNoAlpha(const CR_GLSL_CACHE *pCache, GLenum enmTexTarget)
1324{
1325 switch (enmTexTarget)
1326 {
1327 case GL_TEXTURE_2D:
1328 return pCache->uNoAlpha2DProg;
1329 case GL_TEXTURE_RECTANGLE_ARB:
1330 return pCache->uNoAlpha2DRectProg;
1331 default:
1332 crWarning("invalid tex enmTexTarget %#x", enmTexTarget);
1333 return 0;
1334 }
1335}
1336
1337DECLINLINE(GLuint*) crGlslProgGetNoAlphaPtr(CR_GLSL_CACHE *pCache, GLenum enmTexTarget)
1338{
1339 switch (enmTexTarget)
1340 {
1341 case GL_TEXTURE_2D:
1342 return &pCache->uNoAlpha2DProg;
1343 case GL_TEXTURE_RECTANGLE_ARB:
1344 return &pCache->uNoAlpha2DRectProg;
1345 default:
1346 crWarning("invalid tex enmTexTarget %#x", enmTexTarget);
1347 return NULL;
1348 }
1349}
1350
1351VBOXBLITTERDECL(int) CrGlslProgGenNoAlpha(CR_GLSL_CACHE *pCache, GLenum enmTexTarget)
1352{
1353 GLuint*puiProgram = crGlslProgGetNoAlphaPtr(pCache, enmTexTarget);
1354 if (!puiProgram)
1355 return VERR_INVALID_PARAMETER;
1356
1357 if (*puiProgram)
1358 return VINF_SUCCESS;
1359
1360 return crGlslProgGenNoAlpha(pCache, enmTexTarget, puiProgram);
1361}
1362
1363VBOXBLITTERDECL(int) CrGlslProgGenAllNoAlpha(CR_GLSL_CACHE *pCache)
1364{
1365 int rc = CrGlslProgGenNoAlpha(pCache, GL_TEXTURE_2D);
1366 if (!RT_SUCCESS(rc))
1367 {
1368 crWarning("CrGlslProgGenNoAlpha GL_TEXTURE_2D failed rc %d", rc);
1369 return rc;
1370 }
1371
1372 rc = CrGlslProgGenNoAlpha(pCache, GL_TEXTURE_RECTANGLE_ARB);
1373 if (!RT_SUCCESS(rc))
1374 {
1375 crWarning("CrGlslProgGenNoAlpha GL_TEXTURE_RECTANGLE failed rc %d", rc);
1376 return rc;
1377 }
1378
1379 return VINF_SUCCESS;
1380}
1381
1382VBOXBLITTERDECL(void) CrGlslProgClear(const CR_GLSL_CACHE *pCache)
1383{
1384 pCache->pDispatch->UseProgram(0);
1385}
1386
1387VBOXBLITTERDECL(int) CrGlslProgUseNoAlpha(const CR_GLSL_CACHE *pCache, GLenum enmTexTarget)
1388{
1389 GLuint uiProg = crGlslProgGetNoAlpha(pCache, enmTexTarget);
1390 if (!uiProg)
1391 {
1392 crWarning("request to use inexistent program!");
1393 return VERR_INVALID_STATE;
1394 }
1395
1396 Assert(uiProg);
1397
1398 pCache->pDispatch->UseProgram(uiProg);
1399
1400 return VINF_SUCCESS;
1401}
1402
1403VBOXBLITTERDECL(int) CrGlslProgUseGenNoAlpha(CR_GLSL_CACHE *pCache, GLenum enmTexTarget)
1404{
1405 GLuint uiProg = crGlslProgGetNoAlpha(pCache, enmTexTarget);
1406 if (!uiProg)
1407 {
1408 int rc = CrGlslProgGenNoAlpha(pCache, enmTexTarget);
1409 if (!RT_SUCCESS(rc))
1410 {
1411 crWarning("CrGlslProgGenNoAlpha failed, rc %d", rc);
1412 return rc;
1413 }
1414
1415 uiProg = crGlslProgGetNoAlpha(pCache, enmTexTarget);
1416 CRASSERT(uiProg);
1417 }
1418
1419 Assert(uiProg);
1420
1421 pCache->pDispatch->UseProgram(uiProg);
1422
1423 return VINF_SUCCESS;
1424}
1425
1426#endif /* !IN_VMSVGA3D */
1427
1428VBOXBLITTERDECL(bool) CrGlslNeedsCleanup(const CR_GLSL_CACHE *pCache)
1429{
1430 return pCache->uNoAlpha2DProg || pCache->uNoAlpha2DRectProg;
1431}
1432
1433VBOXBLITTERDECL(void) CrGlslCleanup(CR_GLSL_CACHE *pCache)
1434{
1435 if (pCache->uNoAlpha2DProg)
1436 {
1437#ifdef IN_VMSVGA3D
1438 /** @todo IN_VMSVGA3D */
1439#else
1440 pCache->pDispatch->DeleteProgram(pCache->uNoAlpha2DProg);
1441#endif
1442 pCache->uNoAlpha2DProg = 0;
1443 }
1444
1445 if (pCache->uNoAlpha2DRectProg)
1446 {
1447#ifdef IN_VMSVGA3D
1448 /** @todo IN_VMSVGA3D */
1449#else
1450 pCache->pDispatch->DeleteProgram(pCache->uNoAlpha2DRectProg);
1451#endif
1452 pCache->uNoAlpha2DRectProg = 0;
1453 }
1454}
1455
1456VBOXBLITTERDECL(void) CrGlslTerm(CR_GLSL_CACHE *pCache)
1457{
1458 CRASSERT(!CrGlslNeedsCleanup(pCache));
1459
1460 CrGlslCleanup(pCache);
1461
1462 /* sanity */
1463 memset(pCache, 0, sizeof (*pCache));
1464}
1465
1466#ifndef IN_VMSVGA3D
1467
1468/*TdBlt*/
1469static void crTdBltCheckPBO(PCR_TEXDATA pTex)
1470{
1471 if (pTex->idPBO)
1472 return;
1473
1474 PCR_BLITTER pBlitter = pTex->pBlitter;
1475
1476 if (!pBlitter->Flags.SupportsPBO)
1477 return;
1478
1479 pBlitter->pDispatch->GenBuffersARB(1, &pTex->idPBO);
1480 if (!pTex->idPBO)
1481 {
1482 crWarning("PBO create failed");
1483 return;
1484 }
1485
1486 pBlitter->pDispatch->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pTex->idPBO);
1487 pBlitter->pDispatch->BufferDataARB(GL_PIXEL_PACK_BUFFER_ARB,
1488 pTex->Tex.width*pTex->Tex.height*4,
1489 0, GL_STREAM_READ_ARB);
1490 pBlitter->pDispatch->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
1491}
1492
1493static uint32_t crTdBltTexCreate(PCR_BLITTER pBlitter, uint32_t width, uint32_t height, GLenum enmTarget)
1494{
1495 uint32_t tex = 0;
1496 pBlitter->pDispatch->GenTextures(1, &tex);
1497 if (!tex)
1498 {
1499 crWarning("Tex create failed");
1500 return 0;
1501 }
1502
1503 pBlitter->pDispatch->BindTexture(enmTarget, tex);
1504 pBlitter->pDispatch->TexParameteri(enmTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1505 pBlitter->pDispatch->TexParameteri(enmTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1506 pBlitter->pDispatch->TexParameteri(enmTarget, GL_TEXTURE_WRAP_S, GL_CLAMP);
1507 pBlitter->pDispatch->TexParameteri(enmTarget, GL_TEXTURE_WRAP_T, GL_CLAMP);
1508 pBlitter->pDispatch->TexImage2D(enmTarget, 0, GL_RGBA8,
1509 width, height,
1510 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
1511
1512
1513 /*Restore gl state*/
1514 pBlitter->pDispatch->BindTexture(enmTarget, 0);
1515
1516 return tex;
1517}
1518
1519int crTdBltCheckInvertTex(PCR_TEXDATA pTex)
1520{
1521 if (pTex->idInvertTex)
1522 return VINF_SUCCESS;
1523
1524 pTex->idInvertTex = crTdBltTexCreate(pTex->pBlitter, pTex->Tex.width, pTex->Tex.height, pTex->Tex.target);
1525 if (!pTex->idInvertTex)
1526 {
1527 crWarning("Invert Tex create failed");
1528 return VERR_GENERAL_FAILURE;
1529 }
1530 return VINF_SUCCESS;
1531}
1532
1533#endif /* !IN_VMSVGA3D */
1534
1535
1536void crTdBltImgRelease(PCR_TEXDATA pTex)
1537{
1538 pTex->Flags.DataValid = 0;
1539}
1540
1541void crTdBltImgFree(PCR_TEXDATA pTex)
1542{
1543 if (!pTex->Img.pvData)
1544 {
1545 Assert(!pTex->Flags.DataValid);
1546 return;
1547 }
1548
1549 crTdBltImgRelease(pTex);
1550
1551 Assert(!pTex->Flags.DataValid);
1552
1553
1554 if (pTex->idPBO)
1555 {
1556 PCR_BLITTER pBlitter = pTex->pBlitter;
1557
1558 Assert(CrBltIsEntered(pBlitter));
1559#ifdef IN_VMSVGA3D
1560 /** @todo IN_VMSVGA3D */
1561#else
1562 pBlitter->pDispatch->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pTex->idPBO);
1563 pBlitter->pDispatch->UnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
1564 pBlitter->pDispatch->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
1565#endif
1566 }
1567 else
1568 {
1569 Assert(pTex->Img.pvData);
1570 RTMemFree(pTex->Img.pvData);
1571 }
1572
1573 pTex->Img.pvData = NULL;
1574}
1575
1576
1577#ifndef IN_VMSVGA3D
1578
1579int crTdBltImgAcquire(PCR_TEXDATA pTex, GLenum enmFormat, bool fInverted)
1580{
1581 void *pvData = pTex->Img.pvData;
1582 Assert(!pTex->Flags.DataValid);
1583 int rc = crBltImgInitBaseForTex(&pTex->Tex, &pTex->Img, enmFormat);
1584 if (!RT_SUCCESS(rc))
1585 {
1586 WARN(("crBltImgInitBaseForTex failed rc %d", rc));
1587 return rc;
1588 }
1589
1590 PCR_BLITTER pBlitter = pTex->pBlitter;
1591 Assert(CrBltIsEntered(pBlitter));
1592 pBlitter->pDispatch->BindTexture(pTex->Tex.target, fInverted ? pTex->idInvertTex : pTex->Tex.hwid);
1593
1594 pBlitter->pDispatch->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pTex->idPBO);
1595
1596 if (pvData)
1597 {
1598 if (pTex->idPBO)
1599 {
1600 pBlitter->pDispatch->UnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
1601 pvData = NULL;
1602
1603 }
1604 }
1605 else
1606 {
1607 if (!pTex->idPBO)
1608 {
1609 pvData = RTMemAlloc(4*pTex->Tex.width*pTex->Tex.height);
1610 if (!pvData)
1611 {
1612 WARN(("Out of memory in crTdBltImgAcquire"));
1613 pBlitter->pDispatch->BindTexture(pTex->Tex.target, 0);
1614 return VERR_NO_MEMORY;
1615 }
1616 }
1617 }
1618
1619 Assert(!pvData == !!pTex->idPBO);
1620
1621 /*read the texture, note pixels are NULL for PBO case as it's offset in the buffer*/
1622 pBlitter->pDispatch->GetTexImage(GL_TEXTURE_2D, 0, enmFormat, GL_UNSIGNED_BYTE, pvData);
1623
1624 /*restore gl state*/
1625 pBlitter->pDispatch->BindTexture(pTex->Tex.target, 0);
1626
1627 if (pTex->idPBO)
1628 {
1629 pvData = pBlitter->pDispatch->MapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
1630 if (!pvData)
1631 {
1632 WARN(("Failed to MapBuffer in CrHlpGetTexImage"));
1633 return VERR_GENERAL_FAILURE;
1634 }
1635
1636 pBlitter->pDispatch->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
1637 }
1638
1639 Assert(pvData);
1640 pTex->Img.pvData = pvData;
1641 pTex->Flags.DataValid = 1;
1642 pTex->Flags.DataInverted = fInverted;
1643 return VINF_SUCCESS;
1644}
1645
1646#endif /* !IN_VMSVGA3D */
1647
1648
1649/* release the texture data, the data remains cached in the CR_TEXDATA object until it is discarded with CrTdBltDataInvalidateNe or CrTdBltDataCleanup */
1650VBOXBLITTERDECL(int) CrTdBltDataRelease(PCR_TEXDATA pTex)
1651{
1652 if (!pTex->Flags.Entered)
1653 {
1654 WARN(("tex not entered"));
1655 return VERR_INVALID_STATE;
1656 }
1657
1658 if (!pTex->Flags.DataAcquired)
1659 {
1660 WARN(("Data NOT acquired"));
1661 return VERR_INVALID_STATE;
1662 }
1663
1664 Assert(pTex->Img.pvData);
1665 Assert(pTex->Flags.DataValid);
1666
1667 pTex->Flags.DataAcquired = 0;
1668
1669 return VINF_SUCCESS;
1670}
1671
1672static void crTdBltDataFree(PCR_TEXDATA pTex)
1673{
1674 crTdBltImgFree(pTex);
1675
1676 if (pTex->pScaledCache)
1677 CrTdBltDataFreeNe(pTex->pScaledCache);
1678}
1679
1680/* discard the texture data cached with previous CrTdBltDataAcquire.
1681 * Must be called wit data released (CrTdBltDataRelease) */
1682VBOXBLITTERDECL(int) CrTdBltDataFree(PCR_TEXDATA pTex)
1683{
1684 if (!pTex->Flags.Entered)
1685 {
1686 WARN(("tex not entered"));
1687 return VERR_INVALID_STATE;
1688 }
1689
1690 crTdBltDataFree(pTex);
1691
1692 return VINF_SUCCESS;
1693}
1694
1695VBOXBLITTERDECL(void) CrTdBltDataInvalidateNe(PCR_TEXDATA pTex)
1696{
1697 crTdBltImgRelease(pTex);
1698
1699 if (pTex->pScaledCache)
1700 CrTdBltDataInvalidateNe(pTex->pScaledCache);
1701}
1702
1703VBOXBLITTERDECL(int) CrTdBltDataFreeNe(PCR_TEXDATA pTex)
1704{
1705 if (!pTex->Img.pvData)
1706 return VINF_SUCCESS;
1707
1708 bool fEntered = false;
1709 if (pTex->idPBO)
1710 {
1711 int rc = CrTdBltEnter(pTex);
1712 if (!RT_SUCCESS(rc))
1713 {
1714 WARN(("err"));
1715 return rc;
1716 }
1717
1718 fEntered = true;
1719 }
1720
1721 crTdBltDataFree(pTex);
1722
1723 if (fEntered)
1724 CrTdBltLeave(pTex);
1725
1726 return VINF_SUCCESS;
1727}
1728
1729static void crTdBltSdCleanupCacheNe(PCR_TEXDATA pTex)
1730{
1731 if (pTex->pScaledCache)
1732 {
1733 CrTdBltDataCleanupNe(pTex->pScaledCache);
1734 CrTdRelease(pTex->pScaledCache);
1735 pTex->pScaledCache = NULL;
1736 }
1737}
1738
1739static void crTdBltDataCleanup(PCR_TEXDATA pTex)
1740{
1741 crTdBltImgFree(pTex);
1742
1743 PCR_BLITTER pBlitter = pTex->pBlitter;
1744
1745 if (pTex->idPBO)
1746 {
1747 Assert(CrBltIsEntered(pBlitter));
1748#ifdef IN_VMSVGA3D
1749 /** @todo IN_VMSVGA3D */
1750#else
1751 pBlitter->pDispatch->DeleteBuffersARB(1, &pTex->idPBO);
1752#endif
1753 pTex->idPBO = 0;
1754 }
1755
1756 if (pTex->idInvertTex)
1757 {
1758 Assert(CrBltIsEntered(pBlitter));
1759#ifdef IN_VMSVGA3D
1760 /** @todo IN_VMSVGA3D */
1761#else
1762 pBlitter->pDispatch->DeleteTextures(1, &pTex->idInvertTex);
1763#endif
1764 pTex->idInvertTex = 0;
1765 }
1766
1767 crTdBltSdCleanupCacheNe(pTex);
1768}
1769
1770/* does same as CrTdBltDataFree, and in addition cleans up */
1771VBOXBLITTERDECL(int) CrTdBltDataCleanup(PCR_TEXDATA pTex)
1772{
1773 if (!pTex->Flags.Entered)
1774 {
1775 WARN(("tex not entered"));
1776 return VERR_INVALID_STATE;
1777 }
1778
1779 crTdBltDataCleanup(pTex);
1780
1781 return VINF_SUCCESS;
1782}
1783
1784VBOXBLITTERDECL(int) CrTdBltDataCleanupNe(PCR_TEXDATA pTex)
1785{
1786 bool fEntered = false;
1787 if (pTex->idPBO || pTex->idInvertTex)
1788 {
1789 int rc = CrTdBltEnter(pTex);
1790 if (!RT_SUCCESS(rc))
1791 {
1792 WARN(("err"));
1793 return rc;
1794 }
1795
1796 fEntered = true;
1797 }
1798
1799 crTdBltDataCleanup(pTex);
1800
1801 if (fEntered)
1802 CrTdBltLeave(pTex);
1803
1804 return VINF_SUCCESS;
1805}
1806
1807
1808#ifndef IN_VMSVGA3D
1809
1810/* acquire the texture data, returns the cached data in case it is cached.
1811 * the data remains cached in the CR_TEXDATA object until it is discarded with CrTdBltDataFree or CrTdBltDataCleanup.
1812 * */
1813VBOXBLITTERDECL(int) CrTdBltDataAcquire(PCR_TEXDATA pTex, GLenum enmFormat, bool fInverted, const CR_BLITTER_IMG**ppImg)
1814{
1815 if (!pTex->Flags.Entered)
1816 {
1817 WARN(("tex not entered"));
1818 return VERR_INVALID_STATE;
1819 }
1820
1821 if (pTex->Flags.DataAcquired)
1822 {
1823 WARN(("Data acquired already"));
1824 return VERR_INVALID_STATE;
1825 }
1826
1827 if (pTex->Flags.DataValid && pTex->Img.enmFormat == enmFormat && !pTex->Flags.DataInverted == !fInverted)
1828 {
1829 Assert(pTex->Img.pvData);
1830 *ppImg = &pTex->Img;
1831 pTex->Flags.DataAcquired = 1;
1832 return VINF_SUCCESS;
1833 }
1834
1835 crTdBltImgRelease(pTex);
1836
1837 crTdBltCheckPBO(pTex);
1838
1839 int rc;
1840
1841 if (fInverted)
1842 {
1843 rc = crTdBltCheckInvertTex(pTex);
1844 if (!RT_SUCCESS(rc))
1845 {
1846 WARN(("crTdBltCheckInvertTex failed rc %d", rc));
1847 return rc;
1848 }
1849
1850 RTRECT SrcRect, DstRect;
1851 VBOXVR_TEXTURE InvertTex;
1852
1853 InvertTex = pTex->Tex;
1854 InvertTex.hwid = pTex->idInvertTex;
1855
1856 SrcRect.xLeft = 0;
1857 SrcRect.yTop = InvertTex.height;
1858 SrcRect.xRight = InvertTex.width;
1859 SrcRect.yBottom = 0;
1860
1861 DstRect.xLeft = 0;
1862 DstRect.yTop = 0;
1863 DstRect.xRight = InvertTex.width;
1864 DstRect.yBottom = InvertTex.height;
1865
1866 CrBltBlitTexTex(pTex->pBlitter, &pTex->Tex, &SrcRect, &InvertTex, &DstRect, 1, 0);
1867 }
1868
1869 rc = crTdBltImgAcquire(pTex, enmFormat, fInverted);
1870 if (!RT_SUCCESS(rc))
1871 {
1872 WARN(("crTdBltImgAcquire failed rc %d", rc));
1873 return rc;
1874 }
1875
1876 Assert(pTex->Img.pvData);
1877 *ppImg = &pTex->Img;
1878 pTex->Flags.DataAcquired = 1;
1879
1880 return VINF_SUCCESS;
1881}
1882
1883DECLINLINE(void) crTdResize(PCR_TEXDATA pTex, const VBOXVR_TEXTURE *pVrTex)
1884{
1885 crTdBltDataCleanup(pTex);
1886
1887 pTex->Tex = *pVrTex;
1888}
1889
1890static DECLCALLBACK(void) ctTdBltSdReleased(struct CR_TEXDATA *pTexture)
1891{
1892 PCR_BLITTER pBlitter = pTexture->pBlitter;
1893
1894 int rc = CrBltEnter(pBlitter);
1895 if (!RT_SUCCESS(rc))
1896 {
1897 WARN(("CrBltEnter failed, rc %d", rc));
1898 return;
1899 }
1900
1901 CrTdBltDataCleanupNe(pTexture);
1902
1903 pBlitter->pDispatch->DeleteTextures(1, &pTexture->Tex.hwid);
1904
1905 CrBltLeave(pBlitter);
1906
1907 RTMemFree(pTexture);
1908}
1909
1910static int ctTdBltSdCreate(PCR_BLITTER pBlitter, uint32_t width, uint32_t height, GLenum enmTarget, PCR_TEXDATA *ppScaledCache)
1911{
1912 PCR_TEXDATA pScaledCache;
1913
1914 Assert(CrBltIsEntered(pBlitter));
1915
1916 *ppScaledCache = NULL;
1917
1918 pScaledCache = (PCR_TEXDATA)RTMemAlloc(sizeof (*pScaledCache));
1919 if (!pScaledCache)
1920 {
1921 WARN(("RTMemAlloc failed"));
1922 return VERR_NO_MEMORY;
1923 }
1924
1925 VBOXVR_TEXTURE Tex;
1926 Tex.width = width;
1927 Tex.height = height;
1928 Tex.target = enmTarget;
1929 Tex.hwid = crTdBltTexCreate(pBlitter, width, height, enmTarget);
1930 if (!Tex.hwid)
1931 {
1932 WARN(("Tex create failed"));
1933 RTMemFree(pScaledCache);
1934 return VERR_GENERAL_FAILURE;
1935 }
1936
1937 CrTdInit(pScaledCache, &Tex, pBlitter, ctTdBltSdReleased);
1938
1939 *ppScaledCache = pScaledCache;
1940
1941 return VINF_SUCCESS;
1942}
1943
1944static int ctTdBltSdGet(PCR_TEXDATA pTex, uint32_t width, uint32_t height, PCR_TEXDATA *ppScaledCache)
1945{
1946 Assert(CrBltIsEntered(pTex->pBlitter));
1947
1948 PCR_TEXDATA pScaledCache;
1949
1950 *ppScaledCache = NULL;
1951
1952 if (!pTex->pScaledCache)
1953 {
1954 int rc = ctTdBltSdCreate(pTex->pBlitter, width, height, pTex->Tex.target, &pScaledCache);
1955 if (!RT_SUCCESS(rc))
1956 {
1957 WARN(("ctTdBltSdCreate failed %d", rc));
1958 return rc;
1959 }
1960
1961 pTex->pScaledCache = pScaledCache;
1962 }
1963 else
1964 {
1965 int cmp = pTex->pScaledCache->Tex.width - width;
1966 if (cmp <= 0)
1967 cmp = pTex->pScaledCache->Tex.height - height;
1968
1969 if (!cmp)
1970 pScaledCache = pTex->pScaledCache;
1971 else if (cmp < 0) /* current cache is "less" than the requested */
1972 {
1973 int rc = ctTdBltSdCreate(pTex->pBlitter, width, height, pTex->Tex.target, &pScaledCache);
1974 if (!RT_SUCCESS(rc))
1975 {
1976 WARN(("ctTdBltSdCreate failed %d", rc));
1977 return rc;
1978 }
1979
1980 pScaledCache->pScaledCache = pTex->pScaledCache;
1981 pTex->pScaledCache = pScaledCache;
1982 }
1983 else /* cmp > 0 */
1984 {
1985 int rc = ctTdBltSdGet(pTex->pScaledCache, width, height, &pScaledCache);
1986 if (!RT_SUCCESS(rc))
1987 {
1988 WARN(("ctTdBltSdGet failed %d", rc));
1989 return rc;
1990 }
1991 }
1992 }
1993
1994 Assert(pScaledCache);
1995
1996#if 0
1997 {
1998 VBOXVR_TEXTURE Tex;
1999 Tex.width = width;
2000 Tex.height = height;
2001 Tex.target = pTex->Tex.target;
2002 Tex.hwid = crTdBltTexCreate(pTex, width, height);
2003 if (!Tex.hwid)
2004 {
2005 WARN(("Tex create failed"));
2006 return VERR_GENERAL_FAILURE;
2007 }
2008
2009 pTex->pBlitter->pDispatch->DeleteTextures(1, &pTex->pScaledCache->Tex.hwid);
2010
2011 crTdResize(pTex->pScaledCache, &Tex);
2012 }
2013#endif
2014
2015 *ppScaledCache = pScaledCache;
2016 return VINF_SUCCESS;
2017}
2018
2019static int ctTdBltSdGetUpdated(PCR_TEXDATA pTex, uint32_t width, uint32_t height, PCR_TEXDATA *ppScaledCache)
2020{
2021 PCR_TEXDATA pScaledCache;
2022
2023 *ppScaledCache = NULL;
2024 int rc = ctTdBltSdGet(pTex, width, height, &pScaledCache);
2025 if (!RT_SUCCESS(rc))
2026 {
2027 WARN(("ctTdBltSdGet failed %d", rc));
2028 return rc;
2029 }
2030
2031 Assert(width == pScaledCache->Tex.width);
2032 Assert(height == pScaledCache->Tex.height);
2033
2034 if (!pScaledCache->Flags.DataValid)
2035 {
2036 RTRECT SrcRect, DstRect;
2037
2038 SrcRect.xLeft = 0;
2039 SrcRect.yTop = 0;
2040 SrcRect.xRight = pTex->Tex.width;
2041 SrcRect.yBottom = pTex->Tex.height;
2042
2043 DstRect.xLeft = 0;
2044 DstRect.yTop = 0;
2045 DstRect.xRight = width;
2046 DstRect.yBottom = height;
2047
2048 CrBltBlitTexTex(pTex->pBlitter, &pTex->Tex, &SrcRect, &pScaledCache->Tex, &DstRect, 1, 0);
2049 }
2050
2051 *ppScaledCache = pScaledCache;
2052
2053 return VINF_SUCCESS;
2054}
2055
2056VBOXBLITTERDECL(int) CrTdBltDataAcquireScaled(PCR_TEXDATA pTex, GLenum enmFormat, bool fInverted, uint32_t width, uint32_t height, const CR_BLITTER_IMG**ppImg)
2057{
2058 if (pTex->Tex.width == width && pTex->Tex.height == height)
2059 return CrTdBltDataAcquire(pTex, enmFormat, fInverted, ppImg);
2060
2061 if (!pTex->Flags.Entered)
2062 {
2063 WARN(("tex not entered"));
2064 return VERR_INVALID_STATE;
2065 }
2066
2067 PCR_TEXDATA pScaledCache;
2068
2069 int rc = ctTdBltSdGetUpdated(pTex, width, height, &pScaledCache);
2070 if (!RT_SUCCESS(rc))
2071 {
2072 WARN(("ctTdBltSdGetUpdated failed rc %d", rc));
2073 return rc;
2074 }
2075
2076 rc = CrTdBltEnter(pScaledCache);
2077 if (!RT_SUCCESS(rc))
2078 {
2079 WARN(("CrTdBltEnter failed rc %d", rc));
2080 return rc;
2081 }
2082
2083 rc = CrTdBltDataAcquire(pScaledCache, enmFormat, fInverted, ppImg);
2084 if (!RT_SUCCESS(rc))
2085 {
2086 WARN(("CrTdBltDataAcquire failed rc %d", rc));
2087 CrTdBltLeave(pTex->pScaledCache);
2088 return rc;
2089 }
2090
2091 return VINF_SUCCESS;
2092}
2093
2094VBOXBLITTERDECL(int) CrTdBltDataReleaseScaled(PCR_TEXDATA pTex, const CR_BLITTER_IMG *pImg)
2095{
2096 PCR_TEXDATA pScaledCache = RT_FROM_MEMBER(pImg, CR_TEXDATA, Img);
2097 int rc = CrTdBltDataRelease(pScaledCache);
2098 if (!RT_SUCCESS(rc))
2099 {
2100 WARN(("CrTdBltDataRelease failed rc %d", rc));
2101 return rc;
2102 }
2103
2104 if (pScaledCache != pTex)
2105 CrTdBltLeave(pScaledCache);
2106
2107 return VINF_SUCCESS;
2108}
2109
2110VBOXBLITTERDECL(void) CrTdBltScaleCacheMoveTo(PCR_TEXDATA pTex, PCR_TEXDATA pDstTex)
2111{
2112 if (!pTex->pScaledCache)
2113 return;
2114
2115 crTdBltSdCleanupCacheNe(pDstTex);
2116
2117 pDstTex->pScaledCache = pTex->pScaledCache;
2118 pTex->pScaledCache = NULL;
2119}
2120
2121#endif /* !IN_VMSVGA3D */
2122
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