1 """Tools for copying a texture to screen through a shader.
2
3 This is useful for GPGPU.
4
5 @author: Stephan Wenger
6 @date: 2012-03-06
7 """
8
9 from glitter.arrays import VertexArray
10 from glitter.shaders import ShaderProgram
11 from glitter.convenience import Pipeline
12
13 vertex_code = """
14 #version 400 core
15
16 layout(location=0) in vec4 in_position;
17 out vec2 ex_texcoord;
18
19 void main() {
20 gl_Position = in_position;
21 ex_texcoord = 0.5 * in_position.xy + 0.5;
22 }
23 """
24 """Vertex shader for copying a texture onto the screen."""
25
26 fragment_code_rectangle = """
27 #version 400 core
28 #extension GL_ARB_texture_rectangle : enable
29
30 uniform sampler2DRect image;
31 layout(location=0) out vec4 out_color;
32
33 void main() {
34 out_color = texture(image, gl_FragCoord.xy);
35 }
36 """
37 """Fragment shader for copying a rectangle texture onto the screen."""
38
39 fragment_code_2d = """
40 #version 400 core
41
42 in vec2 ex_texcoord;
43 uniform sampler2D image;
44 layout(location=0) out vec4 out_color;
45
46 void main() {
47 out_color = texture(image, ex_texcoord);
48 }
49 """
50 """Fragment shader for copying a 2D texture onto the screen."""
51
52 quad_vertices = ((-1.0, -1.0), (-1.0, 1.0), (1.0, 1.0), (1.0, -1.0))
53 """Vertices of a fullscreen quad."""
54
55 quad_indices = ((0, 1, 2), (0, 2, 3))
56 """Indices of a fullscreen quad."""
57
59 """Get a vertex array containing the vertices of a fullscreen quad.
60
61 @param context: The context to create the program in, or C{None} for the current context.
62 @type context: L{Context}
63 @rtype: L{VertexArray}
64 """
65 return VertexArray(quad_vertices, elements=quad_indices, context=context)
66
68 """Get a shader program for copying a rectangle texture onto the screen.
69
70 @param context: The context to create the program in, or C{None} for the current context.
71 @type context: L{Context}
72 @rtype: L{ShaderProgram}
73 """
74 return ShaderProgram(fragment=fragment_code_rectangle, context=context)
75
77 """Get a shader program for copying a 2D texture onto the screen.
78
79 @param context: The context to create the program in, or C{None} for the current context.
80 @type context: L{Context}
81 @rtype: L{ShaderProgram}
82 """
83 return ShaderProgram(fragment=fragment_code_2d, context=context)
84
93
95 """Get a pipeline for copying a 2D texture onto the screen.
96
97 @param context: The context to create the program in, or C{None} for the current context.
98 @type context: L{Context}
99 @rtype: L{Pipeline}
100 """
101 return Pipeline(get_program_2d(context), in_position=quad_vertices, elements=quad_indices)
102
103 __all__ = [
104 "vertex_code",
105 "fragment_code_rectangle",
106 "fragment_code_2d",
107 "quad_vertices",
108 "quad_indices",
109 "get_fullscreen_quad",
110 "get_program_rectangle",
111 "get_program_2d",
112 "get_pipeline_rectangle",
113 "get_pipeline_2d",
114 ]
115