annotate pytouhou/ui/opengl/shader.pyx @ 694:3ff1af76e413

anm0: only use recoverable errors, no panics except for anm0 asserts.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 23 Aug 2019 02:24:08 +0200
parents a6af3ff86612
children
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 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
31 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
32 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
33
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
34 # 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
35 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
36 # 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
37 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
38
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 # 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
40 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
41
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 vertex shader
604
aca9551ee8b4 Fix compiling issues with Cython 0.20 ; don't pretend to concat shader sources
Thibaut Girka <thib@sitedethib.com>
parents: 590
diff changeset
43 vert_src = vert.encode()
aca9551ee8b4 Fix compiling issues with Cython 0.20 ; don't pretend to concat shader sources
Thibaut Girka <thib@sitedethib.com>
parents: 590
diff changeset
44 self.create_shader(vert_src, GL_VERTEX_SHADER)
aca9551ee8b4 Fix compiling issues with Cython 0.20 ; don't pretend to concat shader sources
Thibaut Girka <thib@sitedethib.com>
parents: 590
diff changeset
45
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
604
aca9551ee8b4 Fix compiling issues with Cython 0.20 ; don't pretend to concat shader sources
Thibaut Girka <thib@sitedethib.com>
parents: 590
diff changeset
47 frag_src = frag.encode()
aca9551ee8b4 Fix compiling issues with Cython 0.20 ; don't pretend to concat shader sources
Thibaut Girka <thib@sitedethib.com>
parents: 590
diff changeset
48 self.create_shader(frag_src, 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
49
394
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 385
diff changeset
50 #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
51 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
52 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
53 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
54
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
55 # 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
56 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
57
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
58 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
59 glPopDebugGroup()
579
b8df946d394d Add grouping for OpenGL calls, making traces much more readable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 557
diff changeset
60
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
61 cdef bint create_shader(self, const GLchar *string, GLenum_shader shader_type) except True:
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
62 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
63 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
64 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
65
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 # create the shader handle
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
67 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
68
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
69 # 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
70 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
71
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 # 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
73 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
74
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
75 # retrieve the compile status
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
76 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
77
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 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
79 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
80 # retrieve the log length
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
81 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
82 # 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
83 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
84 # retrieve the log text
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
85 glGetShaderInfoLog(shader, temp, NULL, temp_buf)
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
86 buf = temp_buf[:temp]
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
87 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
88 # 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
89 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
90 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
91 # 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
92 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
93
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
94 cdef bint link(self) except True:
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
95 cdef GLint temp
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
96
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
97 # 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
98 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
99
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
100 # retrieve the link status
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
101 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
102
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 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
104 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
105 # retrieve the log length
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
106 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
107 # 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
108 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
109 # retrieve the log text
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
110 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
111 buf = temp_buf[:temp]
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
112 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
113 # 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
114 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
115 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
116 # 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
117 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
118
606
3c2f96f1d715 Fix compilation under Cython 0.22, by making the pyx and the pxd declarations’ except clause similar.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 604
diff changeset
119 cdef GLint get_uniform_location(self, name) except -1:
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 584
diff changeset
120 if isinstance(name, str):
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 584
diff changeset
121 name = name.encode()
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
122 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
123 loc = glGetUniformLocation(self.handle, name)
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
124 if loc == -1:
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
125 raise GLSLException('Undefined {} uniform.'.format(name))
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
126 self.location_cache[name] = loc
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
127 return self.location_cache[name]
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
128
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 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
130 # 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
131 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
132
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
133 # 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
134 # this program must be currently bound
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
135 cdef bint uniform_1(self, name, GLfloat val) except True:
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
136 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
137
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
138 # upload a vec4 uniform
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
139 cdef bint uniform_4(self, name, GLfloat a, GLfloat b, GLfloat c, GLfloat d) except True:
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
140 cdef GLfloat vals[4]
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
141 vals[0] = a
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
142 vals[1] = b
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
143 vals[2] = c
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
144 vals[3] = d
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
145 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
146
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
147 # 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
148 # 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
149 # as well as euclid matrices
617
a6af3ff86612 Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 606
diff changeset
150 cdef bint uniform_matrix(self, name, Matrix *mat) except True:
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
151 # 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
152 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
153 # uplaod the 4x4 floating point matrix
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
154 glUniformMatrix4fv(loc, 1, False, <GLfloat*>mat)