home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / lib / site-packages / OpenGL / GL / ARB / shader_objects.py < prev    next >
Encoding:
Python Source  |  2008-12-07  |  5.4 KB  |  167 lines

  1. '''OpenGL extension ARB.shader_objects
  2.  
  3. This module customises the behaviour of the 
  4. OpenGL.raw.GL.ARB.shader_objects to provide a more 
  5. Python-friendly API
  6. '''
  7. from OpenGL import platform, constants, constant, arrays
  8. from OpenGL import extensions, wrapper
  9. from OpenGL.GL import glget
  10. import ctypes
  11. from OpenGL.raw.GL.ARB.shader_objects import *
  12. ### END AUTOGENERATED SECTION
  13. EXTENSION_NAME = 'GL_ARB_shader_objects'
  14. import OpenGL
  15. from OpenGL.lazywrapper import lazy
  16. from OpenGL import converters, error
  17. GL_INFO_LOG_LENGTH_ARB = constant.Constant( 'GL_INFO_LOG_LENGTH_ARB', 0x8B84 )
  18.  
  19. glShaderSourceARB = platform.createExtensionFunction( 
  20.     'glShaderSourceARB', dll=platform.GL,
  21.     resultType=None, 
  22.     argTypes=(constants.GLhandleARB, constants.GLsizei, ctypes.POINTER(ctypes.c_char_p), arrays.GLintArray,),
  23.     doc = 'glShaderSourceARB( GLhandleARB(shaderObj), str( string) ) -> None',
  24.     argNames = ('shaderObj', 'count', 'string', 'length',),
  25.     extension = EXTENSION_NAME,
  26. )
  27. conv = converters.StringLengths( name='string' )
  28. glShaderSourceARB = wrapper.wrapper(
  29.     glShaderSourceARB
  30. ).setPyConverter(
  31.     'count' # number of strings
  32. ).setPyConverter( 
  33.     'length' # lengths of strings
  34. ).setPyConverter(
  35.     'string', conv.stringArray
  36. ).setCResolver(
  37.     'string', conv.stringArrayForC,
  38. ).setCConverter(
  39.     'length', conv,
  40. ).setCConverter(
  41.     'count', conv.totalCount,
  42. )
  43. del conv
  44.  
  45. for size in (1,2,3,4):
  46.     for format,arrayType in (
  47.         ('f',arrays.GLfloatArray),
  48.         ('i',arrays.GLintArray),
  49.     ):
  50.         name = 'glUniform%(size)s%(format)svARB'%globals()
  51.         globals()[name] = arrays.setInputArraySizeType(
  52.             globals()[name],
  53.             size,
  54.             arrayType, 
  55.             'value',
  56.         )
  57.         del format, arrayType
  58.     del size
  59.  
  60. @lazy( glGetObjectParameterivARB )
  61. def glGetObjectParameterivARB( baseOperation, shader, pname ):
  62.     """Retrieve the integer parameter for the given shader"""
  63.     status = arrays.GLintArray.zeros( (1,))
  64.     status[0] = 1 
  65.     baseOperation(
  66.         shader, pname, status
  67.     )
  68.     return status[0]
  69.  
  70. @lazy( glGetObjectParameterfvARB )
  71. def glGetObjectParameterfvARB( baseOperation, shader, pname ):
  72.     """Retrieve the float parameter for the given shader"""
  73.     status = arrays.GLfloatArray.zeros( (1,))
  74.     status[0] = 1.0
  75.     baseOperation(shader, pname,status)
  76.     return status[0]
  77.  
  78. def _afterCheck( key ):
  79.     """Generate an error-checking function for compilation operations"""
  80.     def GLSLCheckError( 
  81.         result,
  82.         baseOperation=None,
  83.         cArguments=None,
  84.         *args
  85.     ):
  86.         result = error.glCheckError( result, baseOperation, cArguments, *args )
  87.         status = glGetObjectParameterivARB(
  88.             cArguments[0], key
  89.         )
  90.         if not status:
  91.             raise error.GLError( 
  92.                 result = result,
  93.                 baseOperation = baseOperation,
  94.                 cArguments = cArguments,
  95.                 description= glGetInfoLogARB( cArguments[0] )
  96.             )
  97.         return result
  98.     return GLSLCheckError
  99.  
  100. if OpenGL.ERROR_CHECKING:
  101.     glCompileShaderARB.errcheck = _afterCheck( GL_OBJECT_COMPILE_STATUS_ARB )
  102. if OpenGL.ERROR_CHECKING:
  103.     glLinkProgramARB.errcheck = _afterCheck( GL_OBJECT_LINK_STATUS_ARB )
  104. ## Not sure why, but these give invalid operation :(
  105. ##if glValidateProgramARB and OpenGL.ERROR_CHECKING:
  106. ##    glValidateProgramARB.errcheck = _afterCheck( GL_OBJECT_VALIDATE_STATUS_ARB )
  107.  
  108. @lazy( glGetInfoLogARB )
  109. def glGetInfoLogARB( baseOperation, obj ):
  110.     """Retrieve the program/shader's error messages as a Python string
  111.     
  112.     returns string which is '' if no message
  113.     """
  114.     length = int(glGetObjectParameterivARB(obj, GL_INFO_LOG_LENGTH_ARB))
  115.     if length > 0:
  116.         log = ctypes.create_string_buffer(length)
  117.         baseOperation(obj, length, None, log)
  118.         return log.value.strip('\000') # null-termination
  119.     return ''
  120.  
  121. @lazy( glGetAttachedObjectsARB )
  122. def glGetAttachedObjectsARB( baseOperation, obj ):
  123.     """Retrieve the attached objects as an array of GLhandleARB instances"""
  124.     length= glGetObjectParameterivARB( obj, GL_OBJECT_ATTACHED_OBJECTS_ARB )
  125.     if length > 0:
  126.         storage = arrays.GLuintArray.zeros( (length,))
  127.         baseOperation( obj, length, None, storage )
  128.         return storage
  129.     return arrays.GLuintArray.zeros( (0,))
  130.  
  131. @lazy( glGetShaderSourceARB )
  132. def glGetShaderSourceARB( baseOperation, obj ):
  133.     """Retrieve the program/shader's source code as a Python string
  134.     
  135.     returns string which is '' if no source code
  136.     """
  137.     length = int(glGetObjectParameterivARB(obj, GL_OBJECT_SHADER_SOURCE_LENGTH_ARB))
  138.     if length > 0:
  139.         source = ctypes.create_string_buffer(length)
  140.         baseOperation(obj, length, None, source)
  141.         return source.value.strip('\000') # null-termination
  142.     return ''
  143.  
  144. @lazy( glGetActiveUniformARB )
  145. def glGetActiveUniformARB(baseOperation, program, index):
  146.     """Retrieve the name, size and type of the uniform of the index in the program"""
  147.     max_index = int(glGetObjectParameterivARB( program, GL_OBJECT_ACTIVE_UNIFORMS_ARB ))
  148.     length = int(glGetObjectParameterivARB( program, GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH_ARB))
  149.     if index < max_index and index >= 0:
  150.         if length > 0:
  151.             name = ctypes.create_string_buffer(length)
  152.             size = arrays.GLintArray.zeros( (1,))
  153.             gl_type = arrays.GLuintArray.zeros( (1,))
  154.             baseOperation(program, index, length, None, size, gl_type, name)
  155.             return name.value, size[0], gl_type[0]
  156.         raise ValueError( """No currently specified uniform names""" )
  157.     raise IndexError, 'Index %s out of range 0 to %i' % (index, max_index - 1, )
  158.  
  159. @lazy( glGetUniformLocationARB )
  160. def glGetUniformLocationARB( baseOperation, program, name ):
  161.     """Check that name is a string with a null byte at the end of it"""
  162.     if not name:
  163.         raise ValueError( """Non-null name required""" )
  164.     elif name[-1] != '\000':
  165.         name = name + '\000'
  166.     return baseOperation( program, name )
  167.