ovr_sdk

view LibOVR/Src/CAPI/GL/CAPI_GL_DistortionShaders.h @ 3:f12a8f74fe1f

added the Xcode project
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 21 Jan 2015 11:37:50 +0200
parents
children
line source
1 /************************************************************************************
3 Filename : CAPI_GL_Shaders.h
4 Content : Distortion shader header for GL
5 Created : November 11, 2013
6 Authors : David Borel, Volga Aksoy
8 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
10 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
11 you may not use the Oculus VR Rift SDK except in compliance with the License,
12 which is provided at the time of installation or download, or which
13 otherwise accompanies this software in either electronic or hard copy form.
15 You may obtain a copy of the License at
17 http://www.oculusvr.com/licenses/LICENSE-3.2
19 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
25 ************************************************************************************/
28 #ifndef OVR_CAPI_GL_Shaders_h
29 #define OVR_CAPI_GL_Shaders_h
32 #include "CAPI_GL_Util.h"
34 namespace OVR { namespace CAPI { namespace GL {
36 static const char glsl2Prefix[] =
37 "#version 110\n"
38 "#extension GL_ARB_shader_texture_lod : enable\n"
39 "#extension GL_ARB_draw_buffers : enable\n"
40 "#extension GL_EXT_gpu_shader4 : enable\n"
41 "#define _FRAGCOLOR_DECLARATION\n"
42 "#define _MRTFRAGCOLOR0_DECLARATION\n"
43 "#define _MRTFRAGCOLOR1_DECLARATION\n"
44 "#define _GLFRAGCOORD_DECLARATION\n"
45 "#define _VS_IN attribute\n"
46 "#define _VS_OUT varying\n"
47 "#define _FS_IN varying\n"
48 "#define _TEXTURELOD texture2DLod\n"
49 "#define _TEXTURE texture2D\n"
50 "#define _FRAGCOLOR gl_FragColor\n"
51 "#define _MRTFRAGCOLOR0 gl_FragData[0]\n"
52 "#define _MRTFRAGCOLOR1 gl_FragData[1]\n" // The texture coordinate [0.0,1.0] for texel i of a texture of size N is: (2i + 1)/2N
53 "#ifdef GL_EXT_gpu_shader4\n"
54 " #define _TEXELFETCHDECL vec4 texelFetch(sampler2D tex, ivec2 coord, int lod){ ivec2 size = textureSize2D(tex, lod); return texture2D(tex, vec2(float((coord.x * 2) + 1) / float(size.x * 2), float((coord.y * 2) + 1) / float(size.y * 2))); }\n"
55 "#endif\n";
57 static const char glsl3Prefix[] =
58 "#version 150\n"
59 "#define _FRAGCOLOR_DECLARATION out vec4 FragColor;\n"
60 "#define _MRTFRAGCOLOR0_DECLARATION out vec4 FragData0;\n"
61 "#define _MRTFRAGCOLOR1_DECLARATION out vec4 FragData1;\n"
62 "#define _GLFRAGCOORD_DECLARATION in vec4 gl_FragCoord;\n"
63 "#define _VS_IN in\n"
64 "#define _VS_OUT out\n"
65 "#define _FS_IN in\n"
66 "#define _TEXTURELOD textureLod\n"
67 "#define _TEXTURE texture\n"
68 "#define _FRAGCOLOR FragColor\n"
69 "#define _MRTFRAGCOLOR0 FragData0\n"
70 "#define _MRTFRAGCOLOR1 FragData1\n"
71 "#define _TEXELFETCHDECL\n";
73 static const char SimpleQuad_vs[] =
74 "uniform vec2 PositionOffset;\n"
75 "uniform vec2 Scale;\n"
77 "_VS_IN vec3 Position;\n"
79 "void main()\n"
80 "{\n"
81 " gl_Position = vec4(Position.xy * Scale + PositionOffset, 0.5, 1.0);\n"
82 "}\n";
84 const OVR::CAPI::GL::ShaderBase::Uniform SimpleQuad_vs_refl[] =
85 {
86 { "PositionOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
87 { "Scale", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
88 };
90 static const char SimpleQuad_fs[] =
91 "uniform vec4 Color;\n"
93 "_FRAGCOLOR_DECLARATION\n"
95 "void main()\n"
96 "{\n"
97 " _FRAGCOLOR = Color;\n"
98 "}\n";
100 const OVR::CAPI::GL::ShaderBase::Uniform SimpleQuad_fs_refl[] =
101 {
102 { "Color", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 16 },
103 };
105 static const char SimpleQuadGamma_fs[] =
106 "uniform vec4 Color;\n"
108 "_FRAGCOLOR_DECLARATION\n"
110 "void main()\n"
111 "{\n"
112 " _FRAGCOLOR.rgb = pow(Color.rgb, vec3(2.2));\n"
113 " _FRAGCOLOR.a = Color.a;\n"
114 "}\n";
116 const OVR::CAPI::GL::ShaderBase::Uniform SimpleQuadGamma_fs_refl[] =
117 {
118 { "Color", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 16 },
119 };
121 // This must be prefixed with glsl2Prefix or glsl3Prefix before being compiled.
122 static const char SimpleTexturedQuad_vs[] =
123 "uniform vec2 PositionOffset;\n"
124 "uniform vec2 Scale;\n"
126 "_VS_IN vec3 Position;\n"
127 "_VS_IN vec4 Color;\n"
128 "_VS_IN vec2 TexCoord;\n"
130 "_VS_OUT vec4 oColor;\n"
131 "_VS_OUT vec2 oTexCoord;\n"
133 "void main()\n"
134 "{\n"
135 " gl_Position = vec4(Position.xy * Scale + PositionOffset, 0.5, 1.0);\n"
136 " oColor = Color;\n"
137 " oTexCoord = TexCoord;\n"
138 "}\n";
140 // The following declaration is copied from the generated D3D SimpleTexturedQuad_vs_refl.h file, with D3D_NS renamed to GL.
141 const OVR::CAPI::GL::ShaderBase::Uniform SimpleTexturedQuad_vs_refl[] =
142 {
143 { "PositionOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
144 { "Scale", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
145 };
148 // This must be prefixed with glsl2Prefix or glsl3Prefix before being compiled.
149 static const char SimpleTexturedQuad_ps[] =
150 "uniform sampler2D Texture0;\n"
152 "_FS_IN vec4 oColor;\n"
153 "_FS_IN vec2 oTexCoord;\n"
155 "_FRAGCOLOR_DECLARATION\n"
157 "void main()\n"
158 "{\n"
159 " _FRAGCOLOR = oColor * _TEXTURE(Texture0, oTexCoord);\n"
160 "}\n";
162 // The following is copied from the generated D3D SimpleTexturedQuad_ps_refl.h file, with D3D_NS renamed to GL.
163 const OVR::CAPI::GL::ShaderBase::Uniform SimpleTexturedQuad_ps_refl[] =
164 {
165 { "Color", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 16 },
166 };
169 static const char Distortion_vs[] =
170 "uniform vec2 EyeToSourceUVScale;\n"
171 "uniform vec2 EyeToSourceUVOffset;\n"
173 "_VS_IN vec2 Position;\n"
174 "_VS_IN vec4 Color;\n"
175 "_VS_IN vec2 TexCoord0;\n"
177 "_VS_OUT vec4 oColor;\n"
178 "_VS_OUT vec2 oTexCoord0;\n"
180 "void main()\n"
181 "{\n"
182 " gl_Position.x = Position.x;\n"
183 " gl_Position.y = Position.y;\n"
184 " gl_Position.z = 0.5;\n"
185 " gl_Position.w = 1.0;\n"
186 // Vertex inputs are in TanEyeAngle space for the R,G,B channels (i.e. after chromatic aberration and distortion).
187 // Scale them into the correct [0-1],[0-1] UV lookup space (depending on eye)
188 " oTexCoord0 = TexCoord0 * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
189 " oColor = Color;\n" // Used for vignette fade.
190 "}\n";
192 const OVR::CAPI::GL::ShaderBase::Uniform Distortion_vs_refl[] =
193 {
194 { "EyeToSourceUVScale", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
195 { "EyeToSourceUVOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
196 };
198 static const char Distortion_fs[] =
199 "uniform sampler2D Texture0;\n"
201 "_FS_IN vec4 oColor;\n"
202 "_FS_IN vec2 oTexCoord0;\n"
204 "_FRAGCOLOR_DECLARATION\n"
206 "void main()\n"
207 "{\n"
208 " _FRAGCOLOR = _TEXTURE(Texture0, oTexCoord0, 0.0);\n"
209 " _FRAGCOLOR.a = 1.0;\n"
210 "}\n";
213 static const char DistortionTimewarp_vs[] =
214 "uniform vec2 EyeToSourceUVScale;\n"
215 "uniform vec2 EyeToSourceUVOffset;\n"
216 "uniform mat4 EyeRotationStart;\n"
217 "uniform mat4 EyeRotationEnd;\n"
219 "_VS_IN vec2 Position;\n"
220 "_VS_IN vec4 Color;\n"
221 "_VS_IN vec2 TexCoord0;\n"
223 "_VS_OUT vec4 oColor;\n"
224 "_VS_OUT vec2 oTexCoord0;\n"
226 "void main()\n"
227 "{\n"
228 " gl_Position.x = Position.x;\n"
229 " gl_Position.y = Position.y;\n"
230 " gl_Position.z = 0.0;\n"
231 " gl_Position.w = 1.0;\n"
233 // Vertex inputs are in TanEyeAngle space for the R,G,B channels (i.e. after chromatic aberration and distortion).
234 // These are now "real world" vectors in direction (x,y,1) relative to the eye of the HMD.
235 " vec3 TanEyeAngle = vec3 ( TexCoord0.x, TexCoord0.y, 1.0 );\n"
237 // Accurate time warp lerp vs. faster
238 #if 1
239 // Apply the two 3x3 timewarp rotations to these vectors.
240 " vec3 TransformedStart = (EyeRotationStart * vec4(TanEyeAngle, 0)).xyz;\n"
241 " vec3 TransformedEnd = (EyeRotationEnd * vec4(TanEyeAngle, 0)).xyz;\n"
242 // And blend between them.
243 " vec3 Transformed = mix ( TransformedStart, TransformedEnd, Color.a );\n"
244 #else
245 " mat4 EyeRotation = mix ( EyeRotationStart, EyeRotationEnd, Color.a );\n"
246 " vec3 Transformed = EyeRotation * TanEyeAngle;\n"
247 #endif
249 // Project them back onto the Z=1 plane of the rendered images.
250 " float RecipZ = 1.0 / Transformed.z;\n"
251 " vec2 Flattened = vec2 ( Transformed.x * RecipZ, Transformed.y * RecipZ );\n"
253 // These are now still in TanEyeAngle space.
254 // Scale them into the correct [0-1],[0-1] UV lookup space (depending on eye)
255 " vec2 SrcCoord = Flattened * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
256 " oTexCoord0 = SrcCoord;\n"
257 " oColor = vec4(Color.r, Color.r, Color.r, Color.r);\n" // Used for vignette fade.
258 "}\n";
261 const OVR::CAPI::GL::ShaderBase::Uniform DistortionTimewarp_vs_refl[] =
262 {
263 { "EyeToSourceUVScale", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
264 { "EyeToSourceUVOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
265 };
267 static const char DistortionChroma_vs[] =
268 "uniform vec2 EyeToSourceUVScale;\n"
269 "uniform vec2 EyeToSourceUVOffset;\n"
271 "_VS_IN vec2 Position;\n"
272 "_VS_IN vec4 Color;\n"
273 "_VS_IN vec2 TexCoord0;\n"
274 "_VS_IN vec2 TexCoord1;\n"
275 "_VS_IN vec2 TexCoord2;\n"
277 "_VS_OUT vec4 oColor;\n"
278 "_VS_OUT vec2 oTexCoord0;\n"
279 "_VS_OUT vec2 oTexCoord1;\n"
280 "_VS_OUT vec2 oTexCoord2;\n"
282 "void main()\n"
283 "{\n"
284 " gl_Position.x = Position.x;\n"
285 " gl_Position.y = Position.y;\n"
286 " gl_Position.z = 0.5;\n"
287 " gl_Position.w = 1.0;\n"
289 // Vertex inputs are in TanEyeAngle space for the R,G,B channels (i.e. after chromatic aberration and distortion).
290 // Scale them into the correct [0-1],[0-1] UV lookup space (depending on eye)
291 " oTexCoord0 = TexCoord0 * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
292 " oTexCoord1 = TexCoord1 * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
293 " oTexCoord2 = TexCoord2 * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
295 " oColor = Color;\n" // Used for vignette fade.
296 "}\n";
298 const OVR::CAPI::GL::ShaderBase::Uniform DistortionChroma_vs_refl[] =
299 {
300 { "EyeToSourceUVScale", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
301 { "EyeToSourceUVOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
302 };
304 static const char DistortionChroma_fs[] =
305 "uniform sampler2D Texture0;\n"
306 "uniform sampler2D Texture1;\n"
307 "uniform vec3 OverdriveScales_IsSrgb;\n"
309 "_FS_IN vec4 oColor;\n"
310 "_FS_IN vec2 oTexCoord0;\n"
311 "_FS_IN vec2 oTexCoord1;\n"
312 "_FS_IN vec2 oTexCoord2;\n"
314 "_MRTFRAGCOLOR0_DECLARATION\n" // Desired color (next frame's "PrevTexture")
315 "_MRTFRAGCOLOR1_DECLARATION\n" // Overdriven color (Back-buffer)
316 "_GLFRAGCOORD_DECLARATION\n"
318 "#ifdef _TEXELFETCHDECL\n"
319 "_TEXELFETCHDECL\n"
320 "#endif\n"
322 "void main()\n"
323 "{\n"
324 " float ResultR = _TEXTURE(Texture0, oTexCoord0, 0.0).r;\n"
325 " float ResultG = _TEXTURE(Texture0, oTexCoord1, 0.0).g;\n"
326 " float ResultB = _TEXTURE(Texture0, oTexCoord2, 0.0).b;\n"
327 " vec3 newColor = vec3(ResultR * oColor.r, ResultG * oColor.g, ResultB * oColor.b);\n"
329 " _MRTFRAGCOLOR0 = vec4(newColor, 1);\n"
330 " _MRTFRAGCOLOR1 = _MRTFRAGCOLOR0;\n"
332 " #ifdef _TEXELFETCHDECL\n"
333 // pixel luminance overdrive
334 " if(OverdriveScales_IsSrgb.x > 0.0)\n"
335 " {\n"
336 " ivec2 pixelCoord = ivec2(gl_FragCoord.x, gl_FragCoord.y);\n"
337 " vec3 oldColor = texelFetch(Texture1, pixelCoord, 0).rgb;\n"
339 " vec3 adjustedScales;\n"
340 " adjustedScales.x = newColor.x > oldColor.x ? OverdriveScales_IsSrgb.x : OverdriveScales_IsSrgb.y;\n"
341 " adjustedScales.y = newColor.y > oldColor.y ? OverdriveScales_IsSrgb.x : OverdriveScales_IsSrgb.y;\n"
342 " adjustedScales.z = newColor.z > oldColor.z ? OverdriveScales_IsSrgb.x : OverdriveScales_IsSrgb.y;\n"
344 // overdrive is tuned for gamma space so if we're in linear space fix gamma before doing the calculation
345 " vec3 overdriveColor;\n"
346 " if(OverdriveScales_IsSrgb.z > 0.0)\n"
347 " {\n"
348 " oldColor = pow(oldColor, vec3(1.0/2.2, 1.0/2.2, 1.0/2.2));\n"
349 " newColor = pow(newColor, vec3(1.0/2.2, 1.0/2.2, 1.0/2.2));\n"
350 " overdriveColor = clamp(newColor + (newColor - oldColor) * adjustedScales, 0.0, 1.0);\n"
351 " overdriveColor = pow(overdriveColor, vec3(2.2, 2.2, 2.2));\n"
352 " }\n"
353 " else\n"
354 " overdriveColor = clamp(newColor + (newColor - oldColor) * adjustedScales, 0.0, 1.0);\n"
356 " _MRTFRAGCOLOR1 = vec4(overdriveColor, 1.0);\n"
357 " }\n"
358 " #else\n"
359 // If statement to keep OverdriveScales_IsSrgb from being optimized out.
360 " if(OverdriveScales_IsSrgb.x > 0.0)\n"
361 " _MRTFRAGCOLOR1 = vec4(newColor, 1);\n"
362 " #endif\n"
363 "}\n";
365 const OVR::CAPI::GL::ShaderBase::Uniform DistortionChroma_ps_refl[] =
366 {
367 { "OverdriveScales_IsSrgb", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 12 },
368 };
370 static const char DistortionTimewarpChroma_vs[] =
371 "uniform vec2 EyeToSourceUVScale;\n"
372 "uniform vec2 EyeToSourceUVOffset;\n"
373 "uniform mat4 EyeRotationStart;\n"
374 "uniform mat4 EyeRotationEnd;\n"
376 "_VS_IN vec2 Position;\n"
377 "_VS_IN vec4 Color;\n"
378 "_VS_IN vec2 TexCoord0;\n"
379 "_VS_IN vec2 TexCoord1;\n"
380 "_VS_IN vec2 TexCoord2;\n"
382 "_VS_OUT vec4 oColor;\n"
383 "_VS_OUT vec2 oTexCoord0;\n"
384 "_VS_OUT vec2 oTexCoord1;\n"
385 "_VS_OUT vec2 oTexCoord2;\n"
387 "void main()\n"
388 "{\n"
389 " gl_Position.x = Position.x;\n"
390 " gl_Position.y = Position.y;\n"
391 " gl_Position.z = 0.0;\n"
392 " gl_Position.w = 1.0;\n"
394 // Vertex inputs are in TanEyeAngle space for the R,G,B channels (i.e. after chromatic aberration and distortion).
395 // These are now "real world" vectors in direction (x,y,1) relative to the eye of the HMD.
396 " vec3 TanEyeAngleR = vec3 ( TexCoord0.x, TexCoord0.y, 1.0 );\n"
397 " vec3 TanEyeAngleG = vec3 ( TexCoord1.x, TexCoord1.y, 1.0 );\n"
398 " vec3 TanEyeAngleB = vec3 ( TexCoord2.x, TexCoord2.y, 1.0 );\n"
400 // Accurate time warp lerp vs. faster
401 #if 1
402 // Apply the two 3x3 timewarp rotations to these vectors.
403 " vec3 TransformedRStart = (EyeRotationStart * vec4(TanEyeAngleR, 0)).xyz;\n"
404 " vec3 TransformedGStart = (EyeRotationStart * vec4(TanEyeAngleG, 0)).xyz;\n"
405 " vec3 TransformedBStart = (EyeRotationStart * vec4(TanEyeAngleB, 0)).xyz;\n"
406 " vec3 TransformedREnd = (EyeRotationEnd * vec4(TanEyeAngleR, 0)).xyz;\n"
407 " vec3 TransformedGEnd = (EyeRotationEnd * vec4(TanEyeAngleG, 0)).xyz;\n"
408 " vec3 TransformedBEnd = (EyeRotationEnd * vec4(TanEyeAngleB, 0)).xyz;\n"
410 // And blend between them.
411 " vec3 TransformedR = mix ( TransformedRStart, TransformedREnd, Color.a );\n"
412 " vec3 TransformedG = mix ( TransformedGStart, TransformedGEnd, Color.a );\n"
413 " vec3 TransformedB = mix ( TransformedBStart, TransformedBEnd, Color.a );\n"
414 #else
415 " mat3 EyeRotation;\n"
416 " EyeRotation[0] = mix ( EyeRotationStart[0], EyeRotationEnd[0], Color.a ).xyz;\n"
417 " EyeRotation[1] = mix ( EyeRotationStart[1], EyeRotationEnd[1], Color.a ).xyz;\n"
418 " EyeRotation[2] = mix ( EyeRotationStart[2], EyeRotationEnd[2], Color.a ).xyz;\n"
419 " vec3 TransformedR = EyeRotation * TanEyeAngleR;\n"
420 " vec3 TransformedG = EyeRotation * TanEyeAngleG;\n"
421 " vec3 TransformedB = EyeRotation * TanEyeAngleB;\n"
422 #endif
424 // Project them back onto the Z=1 plane of the rendered images.
425 " float RecipZR = 1.0 / TransformedR.z;\n"
426 " float RecipZG = 1.0 / TransformedG.z;\n"
427 " float RecipZB = 1.0 / TransformedB.z;\n"
428 " vec2 FlattenedR = vec2 ( TransformedR.x * RecipZR, TransformedR.y * RecipZR );\n"
429 " vec2 FlattenedG = vec2 ( TransformedG.x * RecipZG, TransformedG.y * RecipZG );\n"
430 " vec2 FlattenedB = vec2 ( TransformedB.x * RecipZB, TransformedB.y * RecipZB );\n"
432 // These are now still in TanEyeAngle space.
433 // Scale them into the correct [0-1],[0-1] UV lookup space (depending on eye)
434 " vec2 SrcCoordR = FlattenedR * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
435 " vec2 SrcCoordG = FlattenedG * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
436 " vec2 SrcCoordB = FlattenedB * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
438 " oTexCoord0 = SrcCoordR;\n"
439 " oTexCoord1 = SrcCoordG;\n"
440 " oTexCoord2 = SrcCoordB;\n"
442 " oColor = vec4(Color.r, Color.r, Color.r, Color.r);\n" // Used for vignette fade.
443 "}\n";
446 const OVR::CAPI::GL::ShaderBase::Uniform DistortionTimewarpChroma_vs_refl[] =
447 {
448 { "EyeToSourceUVScale", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 0, 8 },
449 { "EyeToSourceUVOffset", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 8, 8 },
450 { "EyeRotationStart", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 16, 64 },
451 { "EyeRotationEnd", OVR::CAPI::GL::ShaderBase::VARTYPE_FLOAT, 80, 64 },
452 };
454 }}} // OVR::CAPI::GL
456 #endif // OVR_CAPI_GL_Shaders_h