changeset 557:0f2af7552462

Don’t hardcode GLSL version in our shaders, instead make them dependent on GL version.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 30 May 2014 16:40:36 +0200
parents c34b23e29d16
children 94725968dabb
files pytouhou/ui/opengl/backend.pxd pytouhou/ui/opengl/backend.pyx pytouhou/ui/opengl/shader.pyx pytouhou/ui/opengl/shaders/eosd.py
diffstat 4 files changed, 12 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/ui/opengl/backend.pxd
+++ b/pytouhou/ui/opengl/backend.pxd
@@ -6,3 +6,4 @@ cdef int major
 cdef int minor
 cdef int double_buffer
 cdef bint is_legacy
+cdef str shader_header
--- a/pytouhou/ui/opengl/backend.pyx
+++ b/pytouhou/ui/opengl/backend.pyx
@@ -11,7 +11,7 @@ GameRenderer = None
 
 
 def init(options):
-    global flavor, version, major, minor, double_buffer, is_legacy, GameRenderer
+    global flavor, version, major, minor, double_buffer, is_legacy, shader_header, GameRenderer
 
     flavor_name = options['flavor']
     assert flavor_name in ('core', 'es', 'compatibility', 'legacy')
@@ -29,6 +29,12 @@ def init(options):
 
     is_legacy = flavor_name == 'legacy'
 
+    try:
+        glsl_version = {'2.0': 110, '2.1': 120, '3.0': 130, '3.1': 140, '3.2': 150}[version]
+    except KeyError:
+        glsl_version = 100 * major + 10 * minor
+    shader_header = '#version %d\n' % glsl_version
+
     #TODO: check for framebuffer/renderbuffer support.
 
     from pytouhou.ui.opengl.gamerenderer import GameRenderer
--- a/pytouhou/ui/opengl/shader.pyx
+++ b/pytouhou/ui/opengl/shader.pyx
@@ -18,6 +18,7 @@ from pytouhou.lib.opengl cimport \
           glUniform4fv, glUniformMatrix4fv, glBindAttribLocation)
 
 from libc.stdlib cimport malloc, free
+from .backend cimport shader_header
 
 
 class GLSLException(Exception):
@@ -51,13 +52,14 @@ cdef class Shader:
 
     cdef void create_shader(self, const GLchar *string, GLenum shader_type):
         cdef GLint temp
-        cdef const GLchar **strings = &string
+        cdef const GLchar *strings[2]
+        strings[:] = [shader_header, string]
 
         # create the shader handle
         shader = glCreateShader(shader_type)
 
         # upload the source strings
-        glShaderSource(shader, 1, strings, NULL)
+        glShaderSource(shader, 2, strings, NULL)
 
         # compile the shader
         glCompileShader(shader)
--- a/pytouhou/ui/opengl/shaders/eosd.py
+++ b/pytouhou/ui/opengl/shaders/eosd.py
@@ -19,8 +19,6 @@ from ..shader import Shader
 class GameShader(Shader):
     def __init__(self):
         Shader.__init__(self, ['''
-            #version 120
-
             attribute vec3 in_position;
             attribute vec2 in_texcoord;
             attribute vec4 in_color;
@@ -37,8 +35,6 @@ class GameShader(Shader):
                 color = in_color;
             }
         '''], ['''
-            #version 120
-
             varying vec2 texcoord;
             varying vec4 color;
 
@@ -54,8 +50,6 @@ class GameShader(Shader):
 class BackgroundShader(Shader):
     def __init__(self):
         Shader.__init__(self, ['''
-            #version 120
-
             attribute vec3 in_position;
             attribute vec2 in_texcoord;
             attribute vec4 in_color;
@@ -72,8 +66,6 @@ class BackgroundShader(Shader):
                 color = in_color;
             }
         '''], ['''
-            #version 120
-
             varying vec2 texcoord;
             varying vec4 color;
 
@@ -95,8 +87,6 @@ class BackgroundShader(Shader):
 class PassthroughShader(Shader):
     def __init__(self):
         Shader.__init__(self, ['''
-            #version 120
-
             attribute vec2 in_position;
             attribute vec2 in_texcoord;
 
@@ -110,8 +100,6 @@ class PassthroughShader(Shader):
                 texcoord = in_texcoord;
             }
         '''], ['''
-            #version 120
-
             varying vec2 texcoord;
 
             uniform sampler2D color_map;