annotate pytouhou/ui/opengl/shader.pyx @ 582:6e79756b7f42

Don’t call gl*DebugGroup if it isn’t exposed by the driver.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 05 Oct 2014 17:46:51 +0200
parents b8df946d394d
children 538b52aafbca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
1 # -*- encoding: utf-8 -*-
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 #
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 # Copyright Tristam Macdonald 2008.
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 # Copyright Emmanuel Gil Peyrot 2012.
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 #
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 # Distributed under the Boost Software License, Version 1.0
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 # (see http://www.boost.org/LICENSE_1_0.txt)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 #
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 # Source: https://swiftcoder.wordpress.com/2008/12/19/simple-glsl-wrapper-for-pyglet/
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 #
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
12 from pytouhou.lib.opengl cimport \
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
13 (glCreateProgram, glCreateShader, GL_VERTEX_SHADER,
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
14 GL_FRAGMENT_SHADER, glShaderSource, glCompileShader, glGetShaderiv,
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
15 GL_COMPILE_STATUS, GL_INFO_LOG_LENGTH, glGetShaderInfoLog,
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
16 glAttachShader, glLinkProgram, glGetProgramiv, glGetProgramInfoLog,
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
17 GL_LINK_STATUS, glUseProgram, glGetUniformLocation, glUniform1fv,
579
b8df946d394d Add grouping for OpenGL calls, making traces much more readable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 557
diff changeset
18 glUniform4fv, glUniformMatrix4fv, glBindAttribLocation,
b8df946d394d Add grouping for OpenGL calls, making traces much more readable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 557
diff changeset
19 glPushDebugGroup, GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup)
383
0537af9125a7 Replace wildcard imports with normal ones.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
20
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
21 from libc.stdlib cimport malloc, free
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
22 from .backend cimport shader_header, use_debug_group
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 class GLSLException(Exception):
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 pass
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
29 cdef class Shader:
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 # vert and frag take arrays of source strings the arrays will be
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 # concattenated into one string by OpenGL
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 def __init__(self, vert=None, frag=None):
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
33 if use_debug_group:
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
34 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Program creation")
579
b8df946d394d Add grouping for OpenGL calls, making traces much more readable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 557
diff changeset
35
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 # create the program handle
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 self.handle = glCreateProgram()
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 # we are not linked yet
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 self.linked = False
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 # cache the uniforms location
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 self.location_cache = {}
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 # create the vertex shader
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
45 self.create_shader(vert[0], GL_VERTEX_SHADER)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 # create the fragment shader
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
47 self.create_shader(frag[0], GL_FRAGMENT_SHADER)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48
394
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 385
diff changeset
49 #TODO: put those elsewhere.
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 385
diff changeset
50 glBindAttribLocation(self.handle, 0, 'in_position')
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 385
diff changeset
51 glBindAttribLocation(self.handle, 1, 'in_texcoord')
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 385
diff changeset
52 glBindAttribLocation(self.handle, 2, 'in_color')
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 385
diff changeset
53
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 # attempt to link the program
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 self.link()
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
57 if use_debug_group:
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
58 glPopDebugGroup()
579
b8df946d394d Add grouping for OpenGL calls, making traces much more readable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 557
diff changeset
59
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
60 cdef void create_shader(self, const GLchar *string, GLenum shader_type):
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
61 cdef GLint temp
557
0f2af7552462 Don’t hardcode GLSL version in our shaders, instead make them dependent on GL version.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 523
diff changeset
62 cdef const GLchar *strings[2]
0f2af7552462 Don’t hardcode GLSL version in our shaders, instead make them dependent on GL version.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 523
diff changeset
63 strings[:] = [shader_header, string]
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 # create the shader handle
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
66 shader = glCreateShader(shader_type)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
68 # upload the source strings
557
0f2af7552462 Don’t hardcode GLSL version in our shaders, instead make them dependent on GL version.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 523
diff changeset
69 glShaderSource(shader, 2, strings, NULL)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 # compile the shader
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 glCompileShader(shader)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 # retrieve the compile status
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
75 glGetShaderiv(shader, GL_COMPILE_STATUS, &temp)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 # if compilation failed, print the log
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 if not temp:
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 # retrieve the log length
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
80 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &temp)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 # create a buffer for the log
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
82 temp_buf = <GLchar*>malloc(temp * sizeof(GLchar))
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 # retrieve the log text
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
84 glGetShaderInfoLog(shader, temp, NULL, temp_buf)
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
85 buf = temp_buf[:temp]
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
86 free(temp_buf)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 # print the log to the console
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
88 raise GLSLException(buf)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 else:
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 # all is well, so attach the shader to the program
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
91 glAttachShader(self.handle, shader)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
93 cdef void link(self):
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
94 cdef GLint temp
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
95
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 # link the program
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 glLinkProgram(self.handle)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 # retrieve the link status
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
100 glGetProgramiv(self.handle, GL_LINK_STATUS, &temp)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 # if linking failed, print the log
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 if not temp:
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 # retrieve the log length
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
105 glGetProgramiv(self.handle, GL_INFO_LOG_LENGTH, &temp)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 # create a buffer for the log
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
107 temp_buf = <GLchar*>malloc(temp * sizeof(GLchar))
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 # retrieve the log text
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
109 glGetProgramInfoLog(self.handle, temp, NULL, temp_buf)
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
110 buf = temp_buf[:temp]
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
111 free(temp_buf)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 # print the log to the console
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
113 raise GLSLException(buf)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 else:
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115 # all is well, so we are linked
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 self.linked = True
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
118 cdef GLint get_uniform_location(self, name):
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
119 if name not in self.location_cache:
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
120 loc = glGetUniformLocation(self.handle, name)
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
121 if loc == -1:
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
122 raise GLSLException('Undefined {} uniform.'.format(name))
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
123 self.location_cache[name] = loc
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
124 return self.location_cache[name]
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
125
458
1b56d62250ab Make pytouhou.ui.{window,shader,game{runner,renderer}} extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
126 cdef void bind(self) nogil:
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 # bind the program
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 glUseProgram(self.handle)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130 # upload a floating point uniform
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 # this program must be currently bound
458
1b56d62250ab Make pytouhou.ui.{window,shader,game{runner,renderer}} extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
132 cdef void uniform_1(self, name, GLfloat val):
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
133 glUniform1fv(self.get_uniform_location(name), 1, &val)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
135 # upload a vec4 uniform
458
1b56d62250ab Make pytouhou.ui.{window,shader,game{runner,renderer}} extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
136 cdef void uniform_4(self, name, GLfloat a, GLfloat b, GLfloat c, GLfloat d):
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
137 cdef GLfloat vals[4]
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
138 vals[0] = a
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
139 vals[1] = b
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
140 vals[2] = c
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
141 vals[3] = d
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
142 glUniform4fv(self.get_uniform_location(name), 1, vals)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 # upload a uniform matrix
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 # works with matrices stored as lists,
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 # as well as euclid matrices
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
147 cdef void uniform_matrix(self, name, Matrix *mat):
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
148 # obtain the uniform location
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 loc = self.get_uniform_location(name)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 # uplaod the 4x4 floating point matrix
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
151 glUniformMatrix4fv(loc, 1, False, <GLfloat*>mat)