Vuo  1.2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Classes | Macros | Typedefs | Functions | Variables
VuoGlPool.cc File Reference

Description

VuoGlPool implementation.

Classes

struct  VuoGlTexture
 Reference-counting information for an OpenGL texture. More...
 
struct  VuoIoSurfacePoolEntryType
 An entry in the IOSurface pool. More...
 

Macros

#define RETURN_STRING_IF_EQUAL(value)   if (constant == value) return strdup(#value)
 Helper for VuoGl_stringForConstant.
 

Typedefs

typedef map< GLuint, unsigned int > VuoGlPoolReferenceCounts
 The number of times each OpenGL Buffer Object is retained.
 
typedef pair< unsigned short,
unsigned short > 
VuoGlTextureDimensionsType
 Texture width and height.
 
typedef pair< queue< GLuint >
, double > 
VuoGlTextureLastUsed
 A queue of textures of a given format and size, including the last time any of the textures were used.
 
typedef map< GLenum, map
< VuoGlTextureDimensionsType,
VuoGlTextureLastUsed > > 
VuoGlTexturePoolType
 VuoGlTexturePool[internalformat][size] gives a list of unused textures.
 
typedef map< GLuint, VuoGlTextureVuoGlTextureReferenceCounts
 The number of times each glTextureName is retained..
 
typedef map
< VuoGlTextureDimensionsType,
deque
< VuoIoSurfacePoolEntryType > > 
VuoIoSurfacePoolType
 VuoIoSurfacePoolType[size] gives a list of IOSurfaces.
 
typedef pair< GLuint, pair
< GLuint, pair< GLuint, pair
< VuoMesh_ElementAssemblyMethod,
unsigned int > > > > 
VuoGlProgramDescriptorType
 An entry in the GL Program pool: vertexShaderName, geometryShaderName, fragmentShaderName, assemblyMethod, expectedOutputPrimitiveCount.
 
typedef map
< VuoGlProgramDescriptorType,
VuoGlProgram
VuoGlProgramPoolType
 Type for VuoGlProgramPool.
 
typedef std::map< long, GLuint > VuoGlUniformMap
 A quick way to look up a uniform location given a hash of its name.
 
typedef map< GLuint,
dispatch_semaphore_t > 
VuoGlProgramLocksType
 Type for VuoGlProgramLocks.
 

Functions

static void __attribute__ ((constructor)) VuoSceneRenderer_init()
 Initialize VuoGlSemaphore.
 
static map< VuoGlPoolType, map
< unsigned long, vector
< GLuint > > > VuoGlPool 
__attribute__ ((init_priority(101)))
 The reference count for each OpenGL Buffer Object.
 
GLuint VuoGlPool_use (VuoGlPoolType type, unsigned long size)
 Returns an OpenGL Buffer Object of type type.
 
void VuoGlPool_disuse (VuoGlPoolType type, unsigned long size, GLuint name)
 Indicates that the caller is done using the OpenGL object name of type type.
 
void VuoGlPool_retainF (GLuint glBufferName, const char *file, unsigned int line, const char *func)
 Helper for VuoGlPool_retain.
 
void VuoGlPool_releaseF (VuoGlPoolType type, unsigned long size, GLuint glBufferName, const char *file, unsigned int line, const char *func)
 Helper for VuoGlPool_release.
 
GLuint VuoGlTexture_getType (GLuint format)
 Returns the OpenGL texture data type corresponding with OpenGL texture format.
 
unsigned char VuoGlTexture_getChannelCount (GLuint format)
 Returns the number of color+alpha channels in the specified OpenGL texture format.
 
unsigned char VuoGlTexture_getBytesPerPixel (GLuint internalformat, GLuint format)
 Returns the number of bytes required to store each pixel of the specified OpenGL texture format.
 
unsigned char VuoGlTexture_getBytesPerPixelForInternalFormat (GLuint internalformat)
 Returns the number of bytes required to store each pixel of the specified OpenGL texture format.
 
unsigned long VuoGlTexture_getMaximumTextureBytes (VuoGlContext glContext)
 Returns the maximum number of bytes in Video RAM that a texture can occupy.
 
GLint VuoGlTexture_getMaximumTextureDimension (VuoGlContext glContext)
 Returns the maximum dimension (in pixels) a texture can have.
 
GLuint VuoGlTexturePool_use (VuoGlContext glContext, GLenum internalformat, unsigned short width, unsigned short height, GLenum format)
 Returns an OpenGL texture.
 
static void VuoGlTexurePool_disuse (GLenum internalformat, unsigned short width, unsigned short height, GLuint name)
 Indicates that the caller is done using the OpenGL texture name.
 
static void __attribute__ ((constructor(101))) VuoGlTexture_init(void)
 Initializes the texture reference counting system.
 
void VuoGlTexture_retain (GLuint glTextureName, VuoImage_freeCallback freeCallback, void *freeCallbackContext)
 Increments the reference count for glTextureName.
 
void VuoGlTexture_release (GLenum internalformat, unsigned short width, unsigned short height, GLuint glTextureName, GLuint glTextureTarget)
 Decrements the reference count for glTextureName.
 
void VuoGlTexture_disown (GLuint glTextureName)
 Removes glTextureName from Vuo's reference count table (without deleting it like VuoGlTexture_release does).
 
static void VuoGlPool_cleanup (void *blah)
 Periodically cleans up the texture and IOSurface pools.
 
static void __attribute__ ((destructor)) VuoGlPool_fini(void)
 Stops the cleanup timer, and deletes the pools.
 
VuoIoSurface VuoIoSurfacePool_use (VuoGlContext glContext, unsigned short pixelsWide, unsigned short pixelsHigh, GLuint *outputTexture)
 Returns an IOSurface (backed by outputTexture) with the specified dimensions.
 
uint32_t VuoIoSurfacePool_getId (VuoIoSurface vis)
 Returns the IOSurface's interprocess ID.
 
void * VuoIoSurfacePool_getIOSurfaceRef (VuoIoSurface vis)
 Returns the IOSurface.
 
unsigned short VuoIoSurfacePool_getWidth (VuoIoSurface vis)
 Returns the IOSurface's width in pixels.
 
unsigned short VuoIoSurfacePool_getHeight (VuoIoSurface vis)
 Returns the IOSurface's height in pixels.
 
GLuint VuoIoSurfacePool_getTexture (VuoIoSurface vis)
 Returns the IOSurface's OpenGL texture name.
 
void VuoIoSurfacePool_disuse (VuoIoSurface vis)
 Called by the sending end of an IOSurface texture transfer to indicate that it is finished using the IOSurface's texture.
 
void VuoIoSurfacePool_signal (void *ios)
 Called by the receiving end of an IOSurface texture transfer to indicate that it is finished using the IOSurface's texture.
 
void VuoGlShader_printShaderInfoLog (CGLContextObj cgl_ctx, GLuint obj, const GLchar *source)
 Prints GLSL debug information to the console.
 
string VuoGlShader_replaceInclude (string source, string includeFileName, const unsigned char *includeContents, unsigned int includeContentsLength)
 If source contains the string include(includeFileName), replaces that string with includeContents.
 
GLuint VuoGlShader_use (VuoGlContext glContext, GLenum type, const char *source)
 Returns an OpenGL Shader Object representing the specified source.
 
VuoGlProgram VuoGlProgram_use (VuoGlContext glContext, const char *description, GLuint vertexShaderName, GLuint geometryShaderName, GLuint fragmentShaderName, VuoMesh_ElementAssemblyMethod assemblyMethod, unsigned int expectedOutputPrimitiveCount)
 Links shaders together into a program (or finds an existing program if one already exists for the given shaders, element assembly method, and output primitive count), and returns the GL Program Object.
 
int VuoGlProgram_getUniformLocation (VuoGlProgram program, const char *uniformIdentifier)
 Returns the location (suitable for use with glUniform*()) for a given uniform identifier, or -1 if the uniform isn't found.
 
void VuoGlProgram_lock (GLuint programName)
 Waits for the process-wide lock for this GL program to become available, and claims it.
 
void VuoGlProgram_unlock (GLuint programName)
 Releases the process-wide lock for this GL program.
 
char * VuoGl_stringForConstant (GLenum constant)
 Returns a string for the specified OpenGL constant.
 

Variables

dispatch_semaphore_t VuoGlSemaphore
 Work around apparent GL driver bug, wherein attempting to simultaneously bind the same buffer to multiple VAOs on separate contexts causes a crash.
 
static dispatch_semaphore_t VuoGlPool_semaphore
 Serializes access to VuoGlPool.
 
static dispatch_semaphore_t VuoGlPool_referenceCountsSemaphore = NULL
 Synchronizes access to VuoGlPool_referenceCounts.
 
static VuoGlTexturePoolTypeVuoGlTexturePool
 A pool of GL Textures.
 
static dispatch_semaphore_t VuoGlTexturePool_semaphore
 Serializes access to VuoGlTexturePool.
 
static unsigned long VuoGlTexturePool_allocatedBytes = 0
 The approximate current amount of allocated texture storage (only includes textures obtained via VuoGlTexturePool_use).
 
static unsigned long VuoGlTexturePool_allocatedBytesMax = 0
 The approximate maximum amount of allocated texture storage (only includes textures obtained via VuoGlTexturePool_use).
 
static
VuoGlTextureReferenceCounts
VuoGlTexture_referenceCounts
 The reference count for each OpenGL Texture Object.
 
static dispatch_semaphore_t VuoGlTexture_referenceCountsSemaphore = NULL
 Synchronizes access to VuoGlTexture_referenceCounts.
 
static VuoIoSurfacePoolTypeVuoIoSurfacePool
 Unused IOSurfaces.
 
static VuoIoSurfacePoolTypeVuoIoSurfaceQuarantine
 IOSurfaces which might still be in use by the receiver.
 
static dispatch_semaphore_t VuoIoSurfacePool_semaphore
 Serializes access to the IOSurface pool.
 
static CFStringRef receiverFinishedWithIoSurfaceKey = CFSTR("VuoReceiverFinished")
 Signals from the receiver to the sender, when the receiver is finished using the IOSurface.
 
static double cleanupInterval = 0.1
 Interval (in seconds) to flush the texture and IOSurface pools.
 
static dispatch_source_t VuoGlPool_timer
 Periodically cleans up the IOSurface pool.
 
static dispatch_semaphore_t VuoGlPool_canceledAndCompleted
 Signals when the final cleanup has completed.
 
dispatch_semaphore_t VuoGlShaderPool_semaphore = NULL
 Synchronizes access to VuoGlShaderPool.
 
static const std::locale VuoGlPool_locale
 For hashing strings.
 
static const std::collate< char > & VuoGlPool_collate = std::use_facet<std::collate<char> >(VuoGlPool_locale)
 For hashing strings.
 
static VuoGlProgramPoolType VuoGlProgramPool
 All the GL Programs.
 
static dispatch_semaphore_t VuoGlProgramPool_semaphore
 Serializes access to VuoGlProgramPool.
 
static VuoGlProgramLocksType VuoGlProgramLocks
 A semaphore for each GL program.
 
static dispatch_semaphore_t VuoGlProgramLocks_semaphore
 Serializes access to VuoGlProgramLocks.
 

Class Documentation

struct VuoGlTexture
Class Members
VuoImage_freeCallback freeCallback
void * freeCallbackContext
unsigned int referenceCount
struct VuoIoSurfacePoolEntryType
Class Members
IOSurfaceRef ioSurface
double lastUsedTime
unsigned short pixelsHigh
unsigned short pixelsWide
GLuint texture

Macro Definition Documentation

#define RETURN_STRING_IF_EQUAL (   value)    if (constant == value) return strdup(#value)

Typedef Documentation

typedef map<GLuint, unsigned int> VuoGlPoolReferenceCounts

The number of times each OpenGL Buffer Object is retained.

typedef pair<GLuint, pair<GLuint, pair<GLuint, pair<VuoMesh_ElementAssemblyMethod, unsigned int> > > > VuoGlProgramDescriptorType

An entry in the GL Program pool: vertexShaderName, geometryShaderName, fragmentShaderName, assemblyMethod, expectedOutputPrimitiveCount.

typedef map<GLuint, dispatch_semaphore_t> VuoGlProgramLocksType

Type for VuoGlProgramLocks.

Type for VuoGlProgramPool.

typedef pair<unsigned short,unsigned short> VuoGlTextureDimensionsType

Texture width and height.

typedef pair<queue<GLuint>,double> VuoGlTextureLastUsed

A queue of textures of a given format and size, including the last time any of the textures were used.

VuoGlTexturePool[internalformat][size] gives a list of unused textures.

The number of times each glTextureName is retained..

typedef std::map<long, GLuint> VuoGlUniformMap

A quick way to look up a uniform location given a hash of its name.

VuoIoSurfacePoolType[size] gives a list of IOSurfaces.

Function Documentation

static void __attribute__ ( (constructor)  )
static

Initialize VuoGlSemaphore.

Initializes VuoGlShaderPool_semaphore.

map< GLenum, map< long, GLuint > > VuoGlShaderPool __attribute__ ( (init_priority(101))  )
static

The reference count for each OpenGL Buffer Object.

Shaders, keyed by type (vertex, fragment, ...) and source code hash.

static void __attribute__ ( (constructor(101))  )
static

Initializes the texture reference counting system.

static void __attribute__ ( (destructor)  )
static

Stops the cleanup timer, and deletes the pools.

char* VuoGl_stringForConstant ( GLenum  constant)

Returns a string for the specified OpenGL constant.

The caller is responsible for freeing the string returned by this function.

static void VuoGlPool_cleanup ( void *  blah)
static

Periodically cleans up the texture and IOSurface pools.

void VuoGlPool_disuse ( VuoGlPoolType  type,
unsigned long  size,
GLuint  name 
)

Indicates that the caller is done using the OpenGL object name of type type.

The object is returned to the pool, so other callers can use it (which is more efficient than deleting and re-generating objects).

This function may be called from any thread. (However, the caller is responsible for ensuring that the GL context is not used simultaneously on multiple threads.)

void VuoGlPool_releaseF ( VuoGlPoolType  type,
unsigned long  size,
GLuint  glBufferName,
const char *  file,
unsigned int  line,
const char *  func 
)

Helper for VuoGlPool_release.

void VuoGlPool_retainF ( GLuint  glBufferName,
const char *  file,
unsigned int  line,
const char *  func 
)

Helper for VuoGlPool_retain.

GLuint VuoGlPool_use ( VuoGlPoolType  type,
unsigned long  size 
)

Returns an OpenGL Buffer Object of type type.

If an existing, unused buffer of the specified type and size is available, it is returned. Otherwise, a new buffer is created.

The returned buffer's storage is will be preallocated (so the caller can efficiently upload data using glBufferSubData).

This function may be called from any thread. (However, the caller is responsible for ensuring that the GL context is not used simultaneously on multiple threads.)

int VuoGlProgram_getUniformLocation ( VuoGlProgram  program,
const char *  uniformIdentifier 
)

Returns the location (suitable for use with glUniform*()) for a given uniform identifier, or -1 if the uniform isn't found.

void VuoGlProgram_lock ( GLuint  programName)

Waits for the process-wide lock for this GL program to become available, and claims it.

void VuoGlProgram_unlock ( GLuint  programName)

Releases the process-wide lock for this GL program.

VuoGlProgram VuoGlProgram_use ( VuoGlContext  glContext,
const char *  description,
GLuint  vertexShaderName,
GLuint  geometryShaderName,
GLuint  fragmentShaderName,
VuoMesh_ElementAssemblyMethod  assemblyMethod,
unsigned int  expectedOutputPrimitiveCount 
)

Links shaders together into a program (or finds an existing program if one already exists for the given shaders, element assembly method, and output primitive count), and returns the GL Program Object.

vertexShaderName must be nonzero, but it's OK for geometryShaderName and/or fragmentShaderName to be 0 (same rules as VuoShader_addSource).

description is used just to print errors/warnings; it is not used for cache matching.

Do not call glDeleteShaders() on the returned program; it's expected to persist throughout the lifetime of the process.

Be sure to call VuoGlProgram_lock() before you call glUseProgram().

void VuoGlShader_printShaderInfoLog ( CGLContextObj  cgl_ctx,
GLuint  obj,
const GLchar *  source 
)

Prints GLSL debug information to the console.

This function may be called from any thread. (However, the caller is responsible for ensuring that the GL context is not used simultaneously on multiple threads.)

string VuoGlShader_replaceInclude ( string  source,
string  includeFileName,
const unsigned char *  includeContents,
unsigned int  includeContentsLength 
)

If source contains the string include(includeFileName), replaces that string with includeContents.

GLuint VuoGlShader_use ( VuoGlContext  glContext,
GLenum  type,
const char *  source 
)

Returns an OpenGL Shader Object representing the specified source.

To improve performance, this function keeps a cache of precompiled shaders. If a precompiled shader exists for the specified source, that shader is returned. Otherwise, source is passed off to OpenGL to be compiled.

Do not call glDeleteShaders() on the returned shader; it's expected to persist throughout the lifetime of the process.

Todo:
prefix the following with VuoGlsl_
void VuoGlTexture_disown ( GLuint  glTextureName)

Removes glTextureName from Vuo's reference count table (without deleting it like VuoGlTexture_release does).

glTextureName must have a reference count of exactly 1 (i.e., a texture being used in multiple places throughout Vuo cannot be disowned).

After Vuo disowns the texture, the caller is responsible for eventually deleting it.

This function may be called from any thread.

unsigned char VuoGlTexture_getBytesPerPixel ( GLuint  internalformat,
GLuint  format 
)

Returns the number of bytes required to store each pixel of the specified OpenGL texture format.

unsigned char VuoGlTexture_getBytesPerPixelForInternalFormat ( GLuint  internalformat)

Returns the number of bytes required to store each pixel of the specified OpenGL texture format.

unsigned char VuoGlTexture_getChannelCount ( GLuint  format)

Returns the number of color+alpha channels in the specified OpenGL texture format.

unsigned long VuoGlTexture_getMaximumTextureBytes ( VuoGlContext  glContext)

Returns the maximum number of bytes in Video RAM that a texture can occupy.

GLint VuoGlTexture_getMaximumTextureDimension ( VuoGlContext  glContext)

Returns the maximum dimension (in pixels) a texture can have.

GLuint VuoGlTexture_getType ( GLuint  format)

Returns the OpenGL texture data type corresponding with OpenGL texture format.

void VuoGlTexture_release ( GLenum  internalformat,
unsigned short  width,
unsigned short  height,
GLuint  glTextureName,
GLuint  glTextureTarget 
)

Decrements the reference count for glTextureName.

This function may be called from any thread.

void VuoGlTexture_retain ( GLuint  glTextureName,
VuoImage_freeCallback  freeCallback,
void *  freeCallbackContext 
)

Increments the reference count for glTextureName.

This function may be called from any thread.

GLuint VuoGlTexturePool_use ( VuoGlContext  glContext,
GLenum  internalformat,
unsigned short  width,
unsigned short  height,
GLenum  format 
)

Returns an OpenGL texture.

If an existing, unused texture matching the specified internalformat, width, and height is available, it is returned. Otherwise, a new texture is created.

The returned texture's storage is preallocated (so the caller can efficiently upload data using glTexSubImage2D), and its texturing properties are set to the defaults:

  • wrapping: clamp to border
  • filtering: linear

See glTexImage2D for information about internalformat and format.

Returns 0 if the texture would be too large to fit in Video RAM.

This function may be called from any thread. (However, the caller is responsible for ensuring that the GL context is not used simultaneously on multiple threads.)

Todo:
Better error handling per https://b33p.net/kosada/node/4724
Todo:
Better error handling per https://b33p.net/kosada/node/4724
static void VuoGlTexurePool_disuse ( GLenum  internalformat,
unsigned short  width,
unsigned short  height,
GLuint  name 
)
static

Indicates that the caller is done using the OpenGL texture name.

The texture is returned to the pool, so other callers can use it (which is more efficient than deleting and re-generating textures).

This function may be called from any thread.

void VuoIoSurfacePool_disuse ( VuoIoSurface  vis)

Called by the sending end of an IOSurface texture transfer to indicate that it is finished using the IOSurface's texture.

This enables the IOSurface to be reused for another texture transfer (once the receiving end indicates it is done with it, via VuoIoSurfacePool_signal()).

unsigned short VuoIoSurfacePool_getHeight ( VuoIoSurface  vis)

Returns the IOSurface's height in pixels.

uint32_t VuoIoSurfacePool_getId ( VuoIoSurface  vis)

Returns the IOSurface's interprocess ID.

void* VuoIoSurfacePool_getIOSurfaceRef ( VuoIoSurface  vis)

Returns the IOSurface.

GLuint VuoIoSurfacePool_getTexture ( VuoIoSurface  vis)

Returns the IOSurface's OpenGL texture name.

unsigned short VuoIoSurfacePool_getWidth ( VuoIoSurface  vis)

Returns the IOSurface's width in pixels.

void VuoIoSurfacePool_signal ( void *  ios)

Called by the receiving end of an IOSurface texture transfer to indicate that it is finished using the IOSurface's texture.

This enables the sender to reuse the IOSurface for another texture transfer.

VuoIoSurface VuoIoSurfacePool_use ( VuoGlContext  glContext,
unsigned short  pixelsWide,
unsigned short  pixelsHigh,
GLuint *  outputTexture 
)

Returns an IOSurface (backed by outputTexture) with the specified dimensions.

Uses an IOSurface from the pool, if possible. If not, creates a new IOSurface.

Todo:
kIOSurfaceIsGlobal is deprecated on 10.11; replace int32 lookup with IOSurfaceCreateXPCObject or something. http://lists.apple.com/archives/mac-opengl/2009/Sep/msg00110.html

Variable Documentation

double cleanupInterval = 0.1
static

Interval (in seconds) to flush the texture and IOSurface pools.

CFStringRef receiverFinishedWithIoSurfaceKey = CFSTR("VuoReceiverFinished")
static

Signals from the receiver to the sender, when the receiver is finished using the IOSurface.

dispatch_semaphore_t VuoGlPool_canceledAndCompleted
static

Signals when the final cleanup has completed.

const std::collate<char>& VuoGlPool_collate = std::use_facet<std::collate<char> >(VuoGlPool_locale)
static

For hashing strings.

const std::locale VuoGlPool_locale
static

For hashing strings.

dispatch_semaphore_t VuoGlPool_referenceCountsSemaphore = NULL
static

Synchronizes access to VuoGlPool_referenceCounts.

dispatch_semaphore_t VuoGlPool_semaphore
static

Serializes access to VuoGlPool.

dispatch_source_t VuoGlPool_timer
static

Periodically cleans up the IOSurface pool.

VuoGlProgramLocksType VuoGlProgramLocks
static

A semaphore for each GL program.

dispatch_semaphore_t VuoGlProgramLocks_semaphore
static

Serializes access to VuoGlProgramLocks.

VuoGlProgramPoolType VuoGlProgramPool
static

All the GL Programs.

dispatch_semaphore_t VuoGlProgramPool_semaphore
static

Serializes access to VuoGlProgramPool.

dispatch_semaphore_t VuoGlSemaphore

Work around apparent GL driver bug, wherein attempting to simultaneously bind the same buffer to multiple VAOs on separate contexts causes a crash.

(Try running CompareCameras.vuo without this.)

Also, using VuoSceneObjectRenderer and VuoSceneRender simultaneously seems to lead to crashes, so we also use this semaphore to serialize OpenGL Transform Feedback. https://b33p.net/kosada/node/8498

dispatch_semaphore_t VuoGlShaderPool_semaphore = NULL

Synchronizes access to VuoGlShaderPool.

VuoGlTextureReferenceCounts* VuoGlTexture_referenceCounts
static

The reference count for each OpenGL Texture Object.

dispatch_semaphore_t VuoGlTexture_referenceCountsSemaphore = NULL
static

Synchronizes access to VuoGlTexture_referenceCounts.

VuoGlTexturePoolType* VuoGlTexturePool
static

A pool of GL Textures.

unsigned long VuoGlTexturePool_allocatedBytes = 0
static

The approximate current amount of allocated texture storage (only includes textures obtained via VuoGlTexturePool_use).

unsigned long VuoGlTexturePool_allocatedBytesMax = 0
static

The approximate maximum amount of allocated texture storage (only includes textures obtained via VuoGlTexturePool_use).

dispatch_semaphore_t VuoGlTexturePool_semaphore
static

Serializes access to VuoGlTexturePool.

VuoIoSurfacePoolType* VuoIoSurfacePool
static

Unused IOSurfaces.

dispatch_semaphore_t VuoIoSurfacePool_semaphore
static

Serializes access to the IOSurface pool.

VuoIoSurfacePoolType* VuoIoSurfaceQuarantine
static

IOSurfaces which might still be in use by the receiver.