public
Created

This module shows... a) how GLSL error locations are presented depending on how the shader source is passed (one string vs array of strings), b) whether a single token can spread across several source strings.

  • Download Gist
shaderlines.py
Python
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
'''
This module shows...
a) how GLSL error locations are presented depending on how the shader source is passed
(one string vs array of strings),
b) whether a single token can spread across several source strings.
 
'''
 
import ctypes
from OpenGL import GL
 
# This is a deep dependency from my framework,
# please replace it with your own decorator that makes a function run through a valid GL context
from debugwindow import gltest
 
 
# An example shader with an error in line 5
 
source = '''\
#version 330 // 1
// 2
void main() { // 3
// 4
gl_Position = vec4(1,2,three,4); // 5 error here
// 6
} // 7
'''
 
# This shader is correct, but is split into two strings inside a token
 
alt_source_list = [
'#version 330\nvo',
'id main() { gl_Position = vec4(0); }'
]
 
 
@gltest
def run(splits):
 
shader = GL.glCreateShader(GL.GL_VERTEX_SHADER)
 
if splits:
keep_eols = True
source_lines = source.splitlines(keep_eols) # It is important to keep newlines.
c_sources = map(ctypes.c_char_p, source_lines)
else:
c_sources = [ctypes.c_char_p(source)]
 
ctype_char_p_arr = ctypes.c_char_p * len(c_sources)
c_sources_array = ctype_char_p_arr(*c_sources)
 
if splits:
assert len(c_sources) > 1
else:
assert len(c_sources) == 1
 
GL.glShaderSource.wrappedOperation(shader, len(c_sources), c_sources_array, None)
GL.glCompileShader(shader)
print 'Compile status:', bool(GL.glGetShaderiv(shader, GL.GL_COMPILE_STATUS))
print 'Info log:'
print GL.glGetShaderInfoLog(shader)
 
 
@gltest
def run2():
 
shader = GL.glCreateShader(GL.GL_VERTEX_SHADER)
c_sources = map(ctypes.c_char_p, alt_source_list)
ctype_char_p_arr = ctypes.c_char_p * len(c_sources)
c_sources_array = ctype_char_p_arr(*c_sources)
 
assert len(c_sources) == 2
 
GL.glShaderSource.wrappedOperation(shader, len(c_sources), c_sources_array, None)
GL.glCompileShader(shader)
print 'Compile status:', bool(GL.glGetShaderiv(shader, GL.GL_COMPILE_STATUS))
print 'Info log:'
print GL.glGetShaderInfoLog(shader)
 
 
 
def main():
print '# Shader as lines:'
print '(Expected an error in line 5)'
run(True)
print '# Shader as one block:'
print '(Expected an error in line 5)'
run(False)
print '# Tokens across block'
print '(Expected success if the token can span through strings)'
run2()
 
 
if __name__ == '__main__':
main()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.