annotate pytouhou/ui/opengl/shader.pyx @ 568:e7a4731a278b

Add a GTK+ main menu, mimicking the original EoSD one.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 01 Jul 2014 23:18:15 +0200
parents 0f2af7552462
children b8df946d394d
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,
458
1b56d62250ab Make pytouhou.ui.{window,shader,game{runner,renderer}} extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
18 glUniform4fv, glUniformMatrix4fv, glBindAttribLocation)
383
0537af9125a7 Replace wildcard imports with normal ones.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
19
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
20 from libc.stdlib cimport malloc, free
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
21 from .backend cimport shader_header
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
22
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 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
25 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
26
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
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
28 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
29 # 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
30 # 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
31 def __init__(self, vert=None, frag=None):
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 # 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
33 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
34 # 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
35 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
36
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 # 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
38 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
39
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 # create the vertex shader
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
41 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
42 # create the fragment shader
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
43 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
44
394
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 385
diff changeset
45 #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
46 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
47 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
48 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
49
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
50 # 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
51 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
52
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
53 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
54 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
55 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
56 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
57
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
58 # create the shader handle
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
59 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
60
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
61 # 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
62 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
63
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 # 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
65 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
66
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 # retrieve the compile status
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
68 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
69
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 # 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
71 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
72 # retrieve the log length
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
73 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
74 # 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
75 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
76 # retrieve the log text
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
77 glGetShaderInfoLog(shader, temp, NULL, temp_buf)
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
78 buf = temp_buf[:temp]
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
79 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
80 # 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
81 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
82 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
83 # 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
84 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
85
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
86 cdef void link(self):
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
87 cdef GLint temp
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
88
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 # 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
90 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
91
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 # retrieve the link status
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
93 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
94
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
95 # 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
96 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
97 # retrieve the log length
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
98 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
99 # 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
100 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
101 # retrieve the log text
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
102 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
103 buf = temp_buf[:temp]
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
104 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
105 # 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
106 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
107 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
108 # 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
109 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
110
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
111 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
112 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
113 loc = glGetUniformLocation(self.handle, name)
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
114 if loc == -1:
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
115 raise GLSLException('Undefined {} uniform.'.format(name))
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
116 self.location_cache[name] = loc
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
117 return self.location_cache[name]
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
118
458
1b56d62250ab Make pytouhou.ui.{window,shader,game{runner,renderer}} extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
119 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
120 # 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
121 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
122
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
123 # 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
124 # 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
125 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
126 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
127
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
128 # 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
129 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
130 cdef GLfloat vals[4]
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
131 vals[0] = a
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
132 vals[1] = b
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
133 vals[2] = c
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
134 vals[3] = d
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
135 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
136
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
137 # 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
138 # 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
139 # as well as euclid matrices
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
140 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
141 # 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
142 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
143 # uplaod the 4x4 floating point matrix
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
144 glUniformMatrix4fv(loc, 1, False, <GLfloat*>mat)