Source

Coin / src / rendering / CoinOffscreenGLCanvas.cpp

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/**************************************************************************\
 * Copyright (c) Kongsberg Oil & Gas Technologies AS
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/

#include "CoinOffscreenGLCanvas.h"

#include <limits.h>

#include <Inventor/C/glue/gl.h>
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/misc/SoContextHandler.h>
#include <Inventor/elements/SoGLCacheContextElement.h>

#include "tidbitsp.h"

#if defined(HAVE_CONFIG_H)
#include "config.h"
#include "glue/gl_wgl.h"
#endif /* HAVE_CONFIG_H */

// *************************************************************************

unsigned int CoinOffscreenGLCanvas::tilesizeroof = UINT_MAX;

// *************************************************************************

CoinOffscreenGLCanvas::CoinOffscreenGLCanvas(void)
{
  this->size = SbVec2s(0, 0);
  this->context = NULL;
  this->current_hdc = NULL;
}

CoinOffscreenGLCanvas::~CoinOffscreenGLCanvas()
{
  if (this->context) { this->destructContext(); }
}

// *************************************************************************

SbBool
CoinOffscreenGLCanvas::clampSize(SbVec2s & reqsize)
{
  // getMaxTileSize() returns the theoretical maximum gathered from
  // various GL driver information. We're not guaranteed that we'll be
  // able to allocate a buffer of this size -- e.g. due to memory
  // constraints on the gfx card.

  const SbVec2s maxsize = CoinOffscreenGLCanvas::getMaxTileSize();
  if (maxsize == SbVec2s(0, 0)) { return FALSE; }

  reqsize[0] = SbMin(reqsize[0], maxsize[0]);
  reqsize[1] = SbMin(reqsize[1], maxsize[1]);

  // Fit the attempted allocation size to be less than the largest
  // tile size we know have failed allocation. We do this to avoid
  // trying to set the tilesize to dimensions which will very likely
  // fail -- as attempting to find a workable tilesize is an expensive
  // operation when the SoOffscreenRenderer instance already has a GL
  // context set up (destruction and creation of a new one will take
  // time, and it will also kill all GL resources tied to the
  // context).
  while ((((unsigned int)reqsize[0]) * ((unsigned int)reqsize[1])) >
         CoinOffscreenGLCanvas::tilesizeroof) {
    // shrink by halving the largest dimension:
    if (reqsize[0] > reqsize[1]) { reqsize[0] /= 2; }
    else { reqsize[1] /= 2; }
  }

  if ((reqsize[0] == 0) || (reqsize[1] == 0)) { return FALSE; }
  return TRUE;
}

void
CoinOffscreenGLCanvas::setWantedSize(SbVec2s reqsize)
{
  assert((reqsize[0] > 0) && (reqsize[1] > 0) && "invalid dimensions attempted set");

  const SbBool ok = CoinOffscreenGLCanvas::clampSize(reqsize);
  if (!ok) {
    if (this->context) { this->destructContext(); }
    this->size = SbVec2s(0, 0);
    return;
  }

  // We check if the current GL canvas is much larger than what is
  // requested, as to then free up potentially large memory resources,
  // even if we already have a large enough canvas.
  size_t oldres = (size_t)this->size[0] * (size_t)this->size[1];
  size_t newres = (size_t)reqsize[0] * (size_t)reqsize[1];
  const SbBool resourcehog = (oldres > (newres * 16)) && !CoinOffscreenGLCanvas::allowResourcehog();

  // Since the operation of context destruction and reconstruction has
  // the potential to be such a costly operation (because GL caches
  // are smashed, among other things), we try hard to avoid it.
  //
  // So avoid it if not really necessary, by checking that if we
  // already have a working GL context with size equal or larger to
  // the requested, don't destruct (and reconstruct).
  //
  // We can have a different sized internal GL canvas as to what
  // SoOffscreenRenderer wants, because glViewport() is used from
  // SoOffscreenRenderer to render to the correct viewport dimensions.
  if (this->context &&
      (this->size[0] >= reqsize[0]) &&
      (this->size[1] >= reqsize[1]) &&
      !resourcehog) {
    return;
  }

  // Ok, there's no way around it, we need to destruct the GL context:

  if (CoinOffscreenGLCanvas::debug()) {
    SoDebugError::postInfo("CoinOffscreenGLCanvas::setWantedSize",
                           "killing current context, (clamped) reqsize==[%d, %d],"
                           " previous size==[%d, %d], resourcehog==%s",
                           reqsize[0], reqsize[1],
                           this->size[0], this->size[1],
                           resourcehog ? "TRUE" : "FALSE");
  }

  if (resourcehog) {
    // If we were hogging too much memory for the offscreen context,
    // simply go back to the requested size, to free up all that we
    // can.
    this->size = reqsize;
  }
  else {
    // To avoid costly reconstruction on "flutter", by one or two
    // dimensions going a little bit up and down from frame to frame,
    // we try to expand the GL canvas up-front to what perhaps would
    // be sufficient to avoid further GL canvas destruct- /
    // reconstruct-operations.
    this->size[0] = SbMax(reqsize[0], this->size[0]);
    this->size[1] = SbMax(reqsize[1], this->size[1]);
  }

  if (this->context) { this->destructContext(); }
}

const SbVec2s &
CoinOffscreenGLCanvas::getActualSize(void) const
{
  return this->size;
}

// *************************************************************************

uint32_t
CoinOffscreenGLCanvas::tryActivateGLContext(void)
{
  if (this->size == SbVec2s(0, 0)) { return 0; }

  if (this->context == NULL) {
#if defined(HAVE_WGL)
    /* NOTE: This discrepancy between the different glue flavors is due to a
    driver bug that causes the coordinates fed to the gl_FragCoord fragment
    shader input register to be flipped when using render-to-texture capable
    pbuffers. Ref COINSUPPORT-1284. 20101214 tarjei. */
    this->context = wglglue_context_create_offscreen(this->size[0],
                                                     this->size[1],
                                                     FALSE);
#else
    this->context = cc_glglue_context_create_offscreen(this->size[0],
                                                       this->size[1]);
#endif
    if (CoinOffscreenGLCanvas::debug()) {
      SoDebugError::postInfo("CoinOffscreenGLCanvas::tryActivateGLContext",
                             "Tried to create offscreen context of dimensions "
                             "<%d, %d> -- %s",
                             this->size[0], this->size[1],
                             this->context == NULL ? "failed" : "succeeded");
    }

    if (this->context == NULL) { return 0; }

    // Set up mapping from GL context to SoGLRenderAction context id.
    this->renderid = SoGLCacheContextElement::getUniqueCacheContext();

    // need to change this, for the getHDC() function, since a
    // reference to current_hdc is returned (yes, this is dumb, but
    // such is the TGS / Mercury Inventor API)
    this->current_hdc = cc_glglue_win32_HDC(this->context);
  }

  if (cc_glglue_context_make_current(this->context) == FALSE) {
    if (CoinOffscreenGLCanvas::debug()) {
      SoDebugError::post("CoinOffscreenGLCanvas::tryActivateGLContext",
                         "Couldn't make context current.");
    }
    return 0;
  }
  return this->renderid;
}

void
CoinOffscreenGLCanvas::clampToPixelSizeRoof(SbVec2s & s)
{
  unsigned int pixelsize;
  do {
    pixelsize = (unsigned int)s[0] * (unsigned int)s[1];
    if (pixelsize == 0) { return; } // avoid never-ending loop

    if (pixelsize >= CoinOffscreenGLCanvas::tilesizeroof) {
      // halve the largest dimension, and try again:
      if (s[0] > s[1]) { s[0] /= 2; }
      else { s[1] /= 2; }
    }
  } while (pixelsize >= CoinOffscreenGLCanvas::tilesizeroof);
}

// Activates an offscreen GL context, and returns a guaranteed unique
// id to use with SoGLRenderAction::setCacheContext().
//
// If the given context can not be made current (due to e.g. any error
// condition resulting from the attempt at setting up the offscreen GL
// context), 0 is returned.
uint32_t
CoinOffscreenGLCanvas::activateGLContext(void)
{
  // We try to allocate the wanted size, and then if we fail,
  // successively try with smaller sizes (alternating between halving
  // width and height) until either a workable offscreen buffer was
  // found, or no buffer could be made.
  uint32_t ctx;
  do {
    CoinOffscreenGLCanvas::clampToPixelSizeRoof(this->size);

    ctx = this->tryActivateGLContext();
    if (ctx != 0) { break; }

    // if we've allocated a context, but couldn't make it current
    if (this->context) { this->destructContext(); }

    // we failed with this size, so make sure we only try with smaller
    // tile sizes later
    const unsigned int failedsize =
      (unsigned int)this->size[0] * (unsigned int)this->size[1];
    assert(failedsize <= CoinOffscreenGLCanvas::tilesizeroof);
    CoinOffscreenGLCanvas::tilesizeroof = failedsize;

    // keep trying until 32x32 -- if even those dimensions doesn't
    // work, give up, as too small tiles will cause the processing
    // time to go through the roof due to the huge number of passes:
    if ((this->size[0] <= 32) && (this->size[1] <= 32)) { break; }
  } while (TRUE);

  return ctx;
}

void
CoinOffscreenGLCanvas::deactivateGLContext(void)
{
  assert(this->context);
  cc_glglue_context_reinstate_previous(this->context);
}

// *************************************************************************

void
CoinOffscreenGLCanvas::destructContext(void)
{
  assert(this->context);

  if (cc_glglue_context_make_current(this->context)) {
    SoContextHandler::destructingContext(this->renderid);
    this->deactivateGLContext();
  }
  else {
    if (CoinOffscreenGLCanvas::debug()) {
      SoDebugError::post("CoinOffscreenGLCanvas::destructContext",
                         "Couldn't activate context -- resource clean-up "
                         "not complete.");
    }
  }

  cc_glglue_context_destruct(this->context);

  this->context = NULL;
  this->renderid = 0;
  this->current_hdc = NULL;
}

// *************************************************************************

/* This abomination is needed to support SoOffscreenRenderer::getDC(). */
const void * const &
CoinOffscreenGLCanvas::getHDC(void) const
{
  return this->current_hdc;
}

void CoinOffscreenGLCanvas::updateDCBitmap()
{
cc_glglue_win32_updateHDCBitmap(this->context);
}
// *************************************************************************

// Pushes the rendered pixels into the internal memory array.
void
CoinOffscreenGLCanvas::readPixels(uint8_t * dst,
                                  const SbVec2s & vpdims,
                                  unsigned int dstrowsize,
                                  unsigned int nrcomponents) const
{
  glPushAttrib(GL_ALL_ATTRIB_BITS);

  // First reset all settings that can influence the result of a
  // glReadPixels() call, to make sure we get the actual contents of
  // the buffer, unmodified.
  //
  // The values set up below matches the default settings of an
  // OpenGL driver.

  glPixelStorei(GL_PACK_SWAP_BYTES, 0);
  glPixelStorei(GL_PACK_LSB_FIRST, 0);
  glPixelStorei(GL_PACK_ROW_LENGTH, (GLint)dstrowsize);
  glPixelStorei(GL_PACK_SKIP_ROWS, 0);
  glPixelStorei(GL_PACK_SKIP_PIXELS, 0);

  // FIXME: should use best possible alignment, for speediest
  // operation. 20050617 mortene.
//   glPixelStorei(GL_PACK_ALIGNMENT, 4);
  glPixelStorei(GL_PACK_ALIGNMENT, 1);

  glPixelTransferi(GL_MAP_COLOR, 0);
  glPixelTransferi(GL_MAP_STENCIL, 0);
  glPixelTransferi(GL_INDEX_SHIFT, 0);
  glPixelTransferi(GL_INDEX_OFFSET, 0);
  glPixelTransferf(GL_RED_SCALE, 1);
  glPixelTransferf(GL_RED_BIAS, 0);
  glPixelTransferf(GL_GREEN_SCALE, 1);
  glPixelTransferf(GL_GREEN_BIAS, 0);
  glPixelTransferf(GL_BLUE_SCALE, 1);
  glPixelTransferf(GL_BLUE_BIAS, 0);
  glPixelTransferf(GL_ALPHA_SCALE, 1);
  glPixelTransferf(GL_ALPHA_BIAS, 0);
  glPixelTransferf(GL_DEPTH_SCALE, 1);
  glPixelTransferf(GL_DEPTH_BIAS, 0);

  GLuint i = 0;
  GLfloat f = 0.0f;
  glPixelMapfv(GL_PIXEL_MAP_I_TO_I, 1, &f);
  glPixelMapuiv(GL_PIXEL_MAP_S_TO_S, 1, &i);
  glPixelMapfv(GL_PIXEL_MAP_I_TO_R, 1, &f);
  glPixelMapfv(GL_PIXEL_MAP_I_TO_G, 1, &f);
  glPixelMapfv(GL_PIXEL_MAP_I_TO_B, 1, &f);
  glPixelMapfv(GL_PIXEL_MAP_I_TO_A, 1, &f);
  glPixelMapfv(GL_PIXEL_MAP_R_TO_R, 1, &f);
  glPixelMapfv(GL_PIXEL_MAP_G_TO_G, 1, &f);
  glPixelMapfv(GL_PIXEL_MAP_B_TO_B, 1, &f);
  glPixelMapfv(GL_PIXEL_MAP_A_TO_A, 1, &f);

  // The flushing of the OpenGL pipeline before and after the
  // glReadPixels() call is done as a work-around for a reported
  // OpenGL driver bug: on a Win2000 system with ATI Radeon graphics
  // card, the system would hang hard if the flushing was not done.
  //
  // This is obviously an OpenGL driver bug, but the workaround of
  // doing excessive flushing has no real ill effects, so we just do
  // it unconditionally for all drivers. Note that it might not be
  // necessary to flush both before and after glReadPixels() to work
  // around the bug (this was not established with the external
  // reporter), but again it shouldn't matter if we do.
  //
  // For reference, the specific driver which was reported to fail has
  // the following characteristics:
  //
  // GL_VENDOR="ATI Technologies Inc."
  // GL_RENDERER="Radeon 9000 DDR x86/SSE2"
  // GL_VERSION="1.3.3446 Win2000 Release"
  //
  // mortene.

  glFlush(); glFinish();

  assert((nrcomponents >= 1) && (nrcomponents <= 4));

  if (nrcomponents < 3) {
    unsigned char * tmp = new unsigned char[vpdims[0]*vpdims[1]*4];
    glReadPixels(0, 0, vpdims[0], vpdims[1],
                 nrcomponents == 1 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, tmp);

    const unsigned char * src = tmp;
    // manually convert to grayscale
    for (short y = 0; y < vpdims[1]; y++) {
      for (short x = 0; x < vpdims[0]; x++) {
        double v = src[0] * 0.3 + src[1] * 0.59 + src[2] * 0.11;
        *dst++ = (unsigned char) v;
        if (nrcomponents == 2) {
          *dst++ = src[3];
        }
        src += nrcomponents == 1 ? 3 : 4;
      }
    }
    delete[] tmp;
  }
  else {
    glReadPixels(0, 0, vpdims[0], vpdims[1],
                 nrcomponents == 3 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, dst);
  }
  glFlush(); glFinish();

  glPopAttrib();
}

// *************************************************************************

static SbBool tilesize_cached = FALSE;
static unsigned int maxtile[2] = { 0, 0 };

static void tilesize_cleanup(void)
{
  tilesize_cached = FALSE;
  maxtile[0] = maxtile[1] = 0;
}

// Return largest size of offscreen canvas system can handle. Will
// cache result, so only the first look-up is expensive.
SbVec2s
CoinOffscreenGLCanvas::getMaxTileSize(void)
{
  // cache the values in static variables so that a new context is not
  // created every time render() is called in SoOffscreenRenderer
  if (tilesize_cached) return SbVec2s((short)maxtile[0], (short)maxtile[1]);

  tilesize_cached = TRUE; // Flip on first run.

  coin_atexit((coin_atexit_f*) tilesize_cleanup, CC_ATEXIT_NORMAL);

  unsigned int width, height;
  cc_glglue_context_max_dimensions(&width, &height);

  if (CoinOffscreenGLCanvas::debug()) {
    SoDebugError::postInfo("CoinOffscreenGLCanvas::getMaxTileSize",
                           "cc_glglue_context_max_dimensions()==[%u, %u]",
                           width, height);
  }

  // Makes it possible to override the default tilesizes. Should prove
  // useful for debugging problems on remote sites.
  const char * env = coin_getenv("COIN_OFFSCREENRENDERER_TILEWIDTH");
  const unsigned int forcedtilewidth = env ? atoi(env) : 0;
  env = coin_getenv("COIN_OFFSCREENRENDERER_TILEHEIGHT");
  const unsigned int forcedtileheight = env ? atoi(env) : 0;

  if (forcedtilewidth != 0) { width = forcedtilewidth; }
  if (forcedtileheight != 0) { height = forcedtileheight; }

  // Also make it possible to force a maximum tilesize.
  env = coin_getenv("COIN_OFFSCREENRENDERER_MAX_TILESIZE");
  const unsigned int maxtilesize = env ? atoi(env) : 0;
  if (maxtilesize != 0) {
    width = SbMin(width, maxtilesize);
    height = SbMin(height, maxtilesize);
  }

  // cache result for later calls, and clamp to fit within a short
  // integer type
  maxtile[0] = SbMin(width, (unsigned int)SHRT_MAX);
  maxtile[1] = SbMin(height, (unsigned int)SHRT_MAX);

  return SbVec2s((short)maxtile[0], (short)maxtile[1]);
}

// *************************************************************************

SbBool
CoinOffscreenGLCanvas::debug(void)
{
  static int flag = -1; // -1 means "not initialized" in this context
  if (flag == -1) {
    const char * env = coin_getenv("COIN_DEBUG_SOOFFSCREENRENDERER");
    flag = env && (atoi(env) > 0);
  }
  return flag;
}

SbBool
CoinOffscreenGLCanvas::allowResourcehog(void)
{
  static int resourcehog_flag = -1; // -1 means "not initialized" in this context
  if (resourcehog_flag == -1) {
    const char * env = coin_getenv("COIN_SOOFFSCREENRENDERER_ALLOW_RESOURCEHOG");
    resourcehog_flag = env && (atoi(env) > 0);
    SoDebugError::postInfo("CoinOffscreenGLCanvas",
                           "Ignoring resource hogging due to set COIN_SOOFFSCREENRENDERER_ALLOW_RESOURCEHOG environment variable.");
  }
  return resourcehog_flag;
}

// *************************************************************************