Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CAPI_GL_DistortionShaders.h
Go to the documentation of this file.
1 /************************************************************************************
2 
3  Filename : CAPI_GL_Shaders.h
4  Content : Distortion shader header for GL
5  Created : November 11, 2013
6  Authors : David Borel, Volga Aksoy
7 
8  Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved.
9 
10  Use of this software is subject to the terms of the Oculus Inc license
11  agreement provided at the time of installation or download, or which
12  otherwise accompanies this software in either electronic or hard copy form.
13 
14  ************************************************************************************/
15 
16 
17 #ifndef OVR_CAPI_GL_Shaders_h
18 #define OVR_CAPI_GL_Shaders_h
19 
20 
21 #include "CAPI_GL_Util.h"
22 
23 namespace OVR { namespace CAPI { namespace GL {
24 
25  static const char glsl2Prefix[] =
26  "#version 110\n"
27  "#extension GL_ARB_shader_texture_lod : enable\n"
28  "#define _FRAGCOLOR_DECLARATION\n"
29  "#define _VS_IN attribute\n"
30  "#define _VS_OUT varying\n"
31  "#define _FS_IN varying\n"
32  "#define _TEXTURELOD texture2DLod\n"
33  "#define _FRAGCOLOR gl_FragColor\n";
34 
35  static const char glsl3Prefix[] =
36  "#version 150\n"
37  "#define _FRAGCOLOR_DECLARATION out vec4 FragColor;\n"
38  "#define _VS_IN in\n"
39  "#define _VS_OUT out\n"
40  "#define _FS_IN in\n"
41  "#define _TEXTURELOD textureLod\n"
42  "#define _FRAGCOLOR FragColor\n";
43 
44  static const char SimpleQuad_vs[] =
45  "uniform vec2 PositionOffset;\n"
46  "uniform vec2 Scale;\n"
47 
48  "_VS_IN vec3 Position;\n"
49 
50  "void main()\n"
51  "{\n"
52  " gl_Position = vec4(Position.xy * Scale + PositionOffset, 0.5, 1.0);\n"
53  "}\n";
54 
56  {
57  { "PositionOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
59  };
60 
61  static const char SimpleQuad_fs[] =
62  "uniform vec4 Color;\n"
63 
64  "_FRAGCOLOR_DECLARATION\n"
65 
66  "void main()\n"
67  "{\n"
68  " _FRAGCOLOR = Color;\n"
69  "}\n";
70 
72  {
73  { "Color", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 16 },
74  };
75 
76 
77  static const char Distortion_vs[] =
78  "uniform vec2 EyeToSourceUVScale;\n"
79  "uniform vec2 EyeToSourceUVOffset;\n"
80 
81  "_VS_IN vec2 Position;\n"
82  "_VS_IN vec4 Color;\n"
83  "_VS_IN vec2 TexCoord0;\n"
84 
85  "_VS_OUT vec4 oColor;\n"
86  "_VS_OUT vec2 oTexCoord0;\n"
87 
88  "void main()\n"
89  "{\n"
90  " gl_Position.x = Position.x;\n"
91  " gl_Position.y = Position.y;\n"
92  " gl_Position.z = 0.5;\n"
93  " gl_Position.w = 1.0;\n"
94  // Vertex inputs are in TanEyeAngle space for the R,G,B channels (i.e. after chromatic aberration and distortion).
95  // Scale them into the correct [0-1],[0-1] UV lookup space (depending on eye)
96  " oTexCoord0 = TexCoord0 * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
97  " oTexCoord0.y = 1.0 - oTexCoord0.y;\n"
98  " oColor = Color;\n" // Used for vignette fade.
99  "}\n";
100 
102  {
103  { "EyeToSourceUVScale", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
104  { "EyeToSourceUVOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
105  };
106 
107  static const char Distortion_fs[] =
108  "uniform sampler2D Texture0;\n"
109 
110  "_FS_IN vec4 oColor;\n"
111  "_FS_IN vec2 oTexCoord0;\n"
112 
113  "_FRAGCOLOR_DECLARATION\n"
114 
115  "void main()\n"
116  "{\n"
117  " _FRAGCOLOR = _TEXTURELOD(Texture0, oTexCoord0, 0.0);\n"
118  " _FRAGCOLOR.a = 1.0;\n"
119  "}\n";
120 
121 
122  static const char DistortionTimewarp_vs[] =
123  "uniform vec2 EyeToSourceUVScale;\n"
124  "uniform vec2 EyeToSourceUVOffset;\n"
125  "uniform mat4 EyeRotationStart;\n"
126  "uniform mat4 EyeRotationEnd;\n"
127 
128  "_VS_IN vec2 Position;\n"
129  "_VS_IN vec4 Color;\n"
130  "_VS_IN vec2 TexCoord0;\n"
131 
132  "_FS_IN vec4 oColor;\n"
133  "_FS_IN vec2 oTexCoord0;\n"
134 
135  "void main()\n"
136  "{\n"
137  " gl_Position.x = Position.x;\n"
138  " gl_Position.y = Position.y;\n"
139  " gl_Position.z = 0.0;\n"
140  " gl_Position.w = 1.0;\n"
141 
142  // Vertex inputs are in TanEyeAngle space for the R,G,B channels (i.e. after chromatic aberration and distortion).
143  // These are now "real world" vectors in direction (x,y,1) relative to the eye of the HMD.
144  " vec3 TanEyeAngle = vec3 ( TexCoord0.x, TexCoord0.y, 1.0 );\n"
145 
146  // Accurate time warp lerp vs. faster
147 #if 1
148  // Apply the two 3x3 timewarp rotations to these vectors.
149  " vec3 TransformedStart = (EyeRotationStart * vec4(TanEyeAngle, 0)).xyz;\n"
150  " vec3 TransformedEnd = (EyeRotationEnd * vec4(TanEyeAngle, 0)).xyz;\n"
151  // And blend between them.
152  " vec3 Transformed = mix ( TransformedStart, TransformedEnd, Color.a );\n"
153 #else
154  " mat4 EyeRotation = mix ( EyeRotationStart, EyeRotationEnd, Color.a );\n"
155  " vec3 Transformed = EyeRotation * TanEyeAngle;\n"
156 #endif
157 
158  // Project them back onto the Z=1 plane of the rendered images.
159  " float RecipZ = 1.0 / Transformed.z;\n"
160  " vec2 Flattened = vec2 ( Transformed.x * RecipZ, Transformed.y * RecipZ );\n"
161 
162  // These are now still in TanEyeAngle space.
163  // Scale them into the correct [0-1],[0-1] UV lookup space (depending on eye)
164  " vec2 SrcCoord = Flattened * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
165  " oTexCoord0 = SrcCoord;\n"
166  " oTexCoord0.y = 1.0-oTexCoord0.y;\n"
167  " oColor = vec4(Color.r, Color.r, Color.r, Color.r);\n" // Used for vignette fade.
168  "}\n";
169 
170 
172  {
173  { "EyeToSourceUVScale", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
174  { "EyeToSourceUVOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
175  };
176 
177  static const char DistortionChroma_vs[] =
178  "uniform vec2 EyeToSourceUVScale;\n"
179  "uniform vec2 EyeToSourceUVOffset;\n"
180 
181  "_VS_IN vec2 Position;\n"
182  "_VS_IN vec4 Color;\n"
183  "_VS_IN vec2 TexCoord0;\n"
184  "_VS_IN vec2 TexCoord1;\n"
185  "_VS_IN vec2 TexCoord2;\n"
186 
187  "_VS_OUT vec4 oColor;\n"
188  "_VS_OUT vec2 oTexCoord0;\n"
189  "_VS_OUT vec2 oTexCoord1;\n"
190  "_VS_OUT vec2 oTexCoord2;\n"
191 
192  "void main()\n"
193  "{\n"
194  " gl_Position.x = Position.x;\n"
195  " gl_Position.y = Position.y;\n"
196  " gl_Position.z = 0.5;\n"
197  " gl_Position.w = 1.0;\n"
198 
199  // Vertex inputs are in TanEyeAngle space for the R,G,B channels (i.e. after chromatic aberration and distortion).
200  // Scale them into the correct [0-1],[0-1] UV lookup space (depending on eye)
201  " oTexCoord0 = TexCoord0 * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
202  " oTexCoord0.y = 1.0-oTexCoord0.y;\n"
203  " oTexCoord1 = TexCoord1 * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
204  " oTexCoord1.y = 1.0-oTexCoord1.y;\n"
205  " oTexCoord2 = TexCoord2 * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
206  " oTexCoord2.y = 1.0-oTexCoord2.y;\n"
207 
208  " oColor = Color;\n" // Used for vignette fade.
209  "}\n";
210 
212  {
213  { "EyeToSourceUVScale", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
214  { "EyeToSourceUVOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
215  };
216 
217  static const char DistortionChroma_fs[] =
218  "uniform sampler2D Texture0;\n"
219 
220  "_FS_IN vec4 oColor;\n"
221  "_FS_IN vec2 oTexCoord0;\n"
222  "_FS_IN vec2 oTexCoord1;\n"
223  "_FS_IN vec2 oTexCoord2;\n"
224 
225  "_FRAGCOLOR_DECLARATION\n"
226 
227  "void main()\n"
228  "{\n"
229  " float ResultR = _TEXTURELOD(Texture0, oTexCoord0, 0.0).r;\n"
230  " float ResultG = _TEXTURELOD(Texture0, oTexCoord1, 0.0).g;\n"
231  " float ResultB = _TEXTURELOD(Texture0, oTexCoord2, 0.0).b;\n"
232 
233  " _FRAGCOLOR = vec4(ResultR * oColor.r, ResultG * oColor.g, ResultB * oColor.b, 1.0);\n"
234  "}\n";
235 
236 
237  static const char DistortionTimewarpChroma_vs[] =
238  "uniform vec2 EyeToSourceUVScale;\n"
239  "uniform vec2 EyeToSourceUVOffset;\n"
240  "uniform mat4 EyeRotationStart;\n"
241  "uniform mat4 EyeRotationEnd;\n"
242 
243  "_VS_IN vec2 Position;\n"
244  "_VS_IN vec4 Color;\n"
245  "_VS_IN vec2 TexCoord0;\n"
246  "_VS_IN vec2 TexCoord1;\n"
247  "_VS_IN vec2 TexCoord2;\n"
248 
249  "_VS_OUT vec4 oColor;\n"
250  "_VS_OUT vec2 oTexCoord0;\n"
251  "_VS_OUT vec2 oTexCoord1;\n"
252  "_VS_OUT vec2 oTexCoord2;\n"
253 
254  "void main()\n"
255  "{\n"
256  " gl_Position.x = Position.x;\n"
257  " gl_Position.y = Position.y;\n"
258  " gl_Position.z = 0.0;\n"
259  " gl_Position.w = 1.0;\n"
260 
261  // Vertex inputs are in TanEyeAngle space for the R,G,B channels (i.e. after chromatic aberration and distortion).
262  // These are now "real world" vectors in direction (x,y,1) relative to the eye of the HMD.
263  " vec3 TanEyeAngleR = vec3 ( TexCoord0.x, TexCoord0.y, 1.0 );\n"
264  " vec3 TanEyeAngleG = vec3 ( TexCoord1.x, TexCoord1.y, 1.0 );\n"
265  " vec3 TanEyeAngleB = vec3 ( TexCoord2.x, TexCoord2.y, 1.0 );\n"
266 
267  // Accurate time warp lerp vs. faster
268 #if 1
269  // Apply the two 3x3 timewarp rotations to these vectors.
270  " vec3 TransformedRStart = (EyeRotationStart * vec4(TanEyeAngleR, 0)).xyz;\n"
271  " vec3 TransformedGStart = (EyeRotationStart * vec4(TanEyeAngleG, 0)).xyz;\n"
272  " vec3 TransformedBStart = (EyeRotationStart * vec4(TanEyeAngleB, 0)).xyz;\n"
273  " vec3 TransformedREnd = (EyeRotationEnd * vec4(TanEyeAngleR, 0)).xyz;\n"
274  " vec3 TransformedGEnd = (EyeRotationEnd * vec4(TanEyeAngleG, 0)).xyz;\n"
275  " vec3 TransformedBEnd = (EyeRotationEnd * vec4(TanEyeAngleB, 0)).xyz;\n"
276 
277  // And blend between them.
278  " vec3 TransformedR = mix ( TransformedRStart, TransformedREnd, Color.a );\n"
279  " vec3 TransformedG = mix ( TransformedGStart, TransformedGEnd, Color.a );\n"
280  " vec3 TransformedB = mix ( TransformedBStart, TransformedBEnd, Color.a );\n"
281 #else
282  " mat3 EyeRotation;\n"
283  " EyeRotation[0] = mix ( EyeRotationStart[0], EyeRotationEnd[0], Color.a ).xyz;\n"
284  " EyeRotation[1] = mix ( EyeRotationStart[1], EyeRotationEnd[1], Color.a ).xyz;\n"
285  " EyeRotation[2] = mix ( EyeRotationStart[2], EyeRotationEnd[2], Color.a ).xyz;\n"
286  " vec3 TransformedR = EyeRotation * TanEyeAngleR;\n"
287  " vec3 TransformedG = EyeRotation * TanEyeAngleG;\n"
288  " vec3 TransformedB = EyeRotation * TanEyeAngleB;\n"
289 #endif
290 
291  // Project them back onto the Z=1 plane of the rendered images.
292  " float RecipZR = 1.0 / TransformedR.z;\n"
293  " float RecipZG = 1.0 / TransformedG.z;\n"
294  " float RecipZB = 1.0 / TransformedB.z;\n"
295  " vec2 FlattenedR = vec2 ( TransformedR.x * RecipZR, TransformedR.y * RecipZR );\n"
296  " vec2 FlattenedG = vec2 ( TransformedG.x * RecipZG, TransformedG.y * RecipZG );\n"
297  " vec2 FlattenedB = vec2 ( TransformedB.x * RecipZB, TransformedB.y * RecipZB );\n"
298 
299  // These are now still in TanEyeAngle space.
300  // Scale them into the correct [0-1],[0-1] UV lookup space (depending on eye)
301  " vec2 SrcCoordR = FlattenedR * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
302  " vec2 SrcCoordG = FlattenedG * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
303  " vec2 SrcCoordB = FlattenedB * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
304 
305  " oTexCoord0 = SrcCoordR;\n"
306  " oTexCoord0.y = 1.0-oTexCoord0.y;\n"
307  " oTexCoord1 = SrcCoordG;\n"
308  " oTexCoord1.y = 1.0-oTexCoord1.y;\n"
309  " oTexCoord2 = SrcCoordB;\n"
310  " oTexCoord2.y = 1.0-oTexCoord2.y;\n"
311 
312  " oColor = vec4(Color.r, Color.r, Color.r, Color.r);\n" // Used for vignette fade.
313  "}\n";
314 
315 
317  {
318  { "EyeToSourceUVScale", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
319  { "EyeToSourceUVOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
320  { "EyeRotationStart", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 16, 64 },
321  { "EyeRotationEnd", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 80, 64 },
322  };
323 
324 }}} // OVR::CAPI::GL
325 
326 #endif // OVR_CAPI_GL_Shaders_h
const OVR::CAPI::GL::ShaderBase::Uniform SimpleQuad_vs_refl[]
static const char glsl2Prefix[]
static const char Distortion_vs[]
static const char DistortionTimewarp_vs[]
static const char glsl3Prefix[]
static const char SimpleQuad_vs[]
const OVR::CAPI::GL::ShaderBase::Uniform SimpleQuad_fs_refl[]
static const char SimpleQuad_fs[]
const OVR::CAPI::GL::ShaderBase::Uniform DistortionTimewarp_vs_refl[]
const OVR::CAPI::GL::ShaderBase::Uniform DistortionChroma_vs_refl[]
static const char Distortion_fs[]
const OVR::CAPI::GL::ShaderBase::Uniform Distortion_vs_refl[]
static const char DistortionTimewarpChroma_vs[]
static const char DistortionChroma_vs[]
static const char DistortionChroma_fs[]
const OVR::CAPI::GL::ShaderBase::Uniform DistortionTimewarpChroma_vs_refl[]