diff --git a/components.js b/components.js index 493e11862b..c0edbde9f3 100644 --- a/components.js +++ b/components.js @@ -161,6 +161,11 @@ var components = { "title": "Git", "owner": "lgiraudel" }, + "glsl": { + "title": "GLSL", + "require": "clike", + "owner": "Golmote" + }, "go": { "title": "Go", "require": "clike", diff --git a/components/prism-glsl.js b/components/prism-glsl.js new file mode 100644 index 0000000000..e9cbfacb32 --- /dev/null +++ b/components/prism-glsl.js @@ -0,0 +1,16 @@ +Prism.languages.glsl = Prism.languages.extend('clike', { + 'comment': [ + /\/\*[\w\W]*?\*\//, + /\/\/(?:\\(?:\r\n|[\s\S])|.)*/ + ], + 'number': /\b(?:0x[\da-f]+|(?:\.\d+|\d+\.?\d*)(?:e[+-]?\d+)?)[ulf]*\b/i, + 'keyword': /\b(?:attribute|const|uniform|varying|buffer|shared|coherent|volatile|restrict|readonly|writeonly|atomic_uint|layout|centroid|flat|smooth|noperspective|patch|sample|break|continue|do|for|while|switch|case|default|if|else|subroutine|in|out|inout|float|double|int|void|bool|true|false|invariant|precise|discard|return|d?mat[234](?:x[234])?|[ibdu]?vec[234]|uint|lowp|mediump|highp|precision|[iu]?sampler[123]D|[iu]?samplerCube|sampler[12]DShadow|samplerCubeShadow|[iu]?sampler[12]DArray|sampler[12]DArrayShadow|[iu]?sampler2DRect|sampler2DRectShadow|[iu]?samplerBuffer|[iu]?sampler2DMS(?:Array)?|[iu]?samplerCubeArray|samplerCubeArrayShadow|[iu]?image[123]D|[iu]?image2DRect|[iu]?imageCube|[iu]?imageBuffer|[iu]?image[12]DArray|[iu]?imageCubeArray|[iu]?image2DMS(?:Array)?|struct|common|partition|active|asm|class|union|enum|typedef|template|this|resource|goto|inline|noinline|public|static|extern|external|interface|long|short|half|fixed|unsigned|superp|input|output|hvec[234]|fvec[234]|sampler3DRect|filter|sizeof|cast|namespace|using)\b/ +}); + +Prism.languages.insertBefore('glsl', 'comment', { + 'preprocessor': { + pattern: /(^[ \t]*)#(?:(?:define|undef|if|ifdef|ifndef|else|elif|endif|error|pragma|extension|version|line)\b)?/m, + lookbehind: true, + alias: 'builtin' + } +}); \ No newline at end of file diff --git a/components/prism-glsl.min.js b/components/prism-glsl.min.js new file mode 100644 index 0000000000..ad901c4ca3 --- /dev/null +++ b/components/prism-glsl.min.js @@ -0,0 +1 @@ +Prism.languages.glsl=Prism.languages.extend("clike",{comment:[/\/\*[\w\W]*?\*\//,/\/\/(?:\\(?:\r\n|[\s\S])|.)*/],number:/\b(?:0x[\da-f]+|(?:\.\d+|\d+\.?\d*)(?:e[+-]?\d+)?)[ulf]*\b/i,keyword:/\b(?:attribute|const|uniform|varying|buffer|shared|coherent|volatile|restrict|readonly|writeonly|atomic_uint|layout|centroid|flat|smooth|noperspective|patch|sample|break|continue|do|for|while|switch|case|default|if|else|subroutine|in|out|inout|float|double|int|void|bool|true|false|invariant|precise|discard|return|d?mat[234](?:x[234])?|[ibdu]?vec[234]|uint|lowp|mediump|highp|precision|[iu]?sampler[123]D|[iu]?samplerCube|sampler[12]DShadow|samplerCubeShadow|[iu]?sampler[12]DArray|sampler[12]DArrayShadow|[iu]?sampler2DRect|sampler2DRectShadow|[iu]?samplerBuffer|[iu]?sampler2DMS(?:Array)?|[iu]?samplerCubeArray|samplerCubeArrayShadow|[iu]?image[123]D|[iu]?image2DRect|[iu]?imageCube|[iu]?imageBuffer|[iu]?image[12]DArray|[iu]?imageCubeArray|[iu]?image2DMS(?:Array)?|struct|common|partition|active|asm|class|union|enum|typedef|template|this|resource|goto|inline|noinline|public|static|extern|external|interface|long|short|half|fixed|unsigned|superp|input|output|hvec[234]|fvec[234]|sampler3DRect|filter|sizeof|cast|namespace|using)\b/}),Prism.languages.insertBefore("glsl","comment",{preprocessor:{pattern:/(^[ \t]*)#(?:(?:define|undef|if|ifdef|ifndef|else|elif|endif|error|pragma|extension|version|line)\b)?/m,lookbehind:!0,alias:"builtin"}}); \ No newline at end of file diff --git a/examples/prism-glsl.html b/examples/prism-glsl.html new file mode 100644 index 0000000000..ab9d8d704d --- /dev/null +++ b/examples/prism-glsl.html @@ -0,0 +1,68 @@ +

GLSL (OpenGL Shading Language)

+

To use this language, use the class "language-glsl".

+ +

Vertex shader example

+
attribute vec3 vertex;
+attribute vec3 normal;
+
+uniform mat4 _mvProj;
+uniform mat3 _norm;
+
+varying vec3 vColor;
+varying vec3 localPos;
+
+#pragma include "light.glsl"
+
+// constants
+vec3 materialColor = vec3(1.0,0.7,0.8);
+vec3 specularColor = vec3(1.0,1.0,1.0);
+
+void main(void) {
+    // compute position
+    gl_Position = _mvProj * vec4(vertex, 1.0);
+    
+    localPos = vertex;
+    
+    // compute light info
+    vec3 n = normalize(_norm * normal);
+    vec3 diffuse;
+    float specular;
+    float glowingSpecular = 50.0;
+    getDirectionalLight(n, _dLight, glowingSpecular, diffuse, specular);
+    vColor = max(diffuse,_ambient.xyz)*materialColor+specular*specularColor+_ambient;
+}
+ +

Fragment shader example

+
#ifdef GL_ES
+precision highp float;
+#endif
+
+uniform vec3 BrickColor, MortarColor;
+uniform vec3 BrickSize;
+uniform vec3 BrickPct;
+
+varying vec3 vColor;
+varying vec3 localPos;
+void main()
+{
+    vec3 color;
+	vec3 position, useBrick;
+	
+
+	position = localPos / BrickSize.xyz;
+
+	if (fract(position.y * 0.5) > 0.5){
+		position.x += 0.5;
+        position.z += 0.5;
+	}
+    
+	position = fract(position);
+
+	useBrick = step(position, BrickPct.xyz);
+
+	color = mix(MortarColor, BrickColor, useBrick.x * useBrick.y * useBrick.z);
+	color *= vColor;
+
+	gl_FragColor = vec4(color, 1.0);
+}
+
\ No newline at end of file diff --git a/plugins/show-language/prism-show-language.js b/plugins/show-language/prism-show-language.js index 449c9e046b..b5e37f4ebb 100644 --- a/plugins/show-language/prism-show-language.js +++ b/plugins/show-language/prism-show-language.js @@ -5,7 +5,7 @@ if (typeof self === 'undefined' || !self.Prism || !self.document) { } // The languages map is built automatically with gulp -var Languages = /*languages_placeholder[*/{"css":"CSS","clike":"C-like","javascript":"JavaScript","actionscript":"ActionScript","apacheconf":"Apache Configuration","apl":"APL","applescript":"AppleScript","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","csharp":"C#","cpp":"C++","coffeescript":"CoffeeScript","css-extras":"CSS Extras","fsharp":"F#","http":"HTTP","latex":"LaTeX","lolcode":"LOLCODE","matlab":"MATLAB","nasm":"NASM","nsis":"NSIS","objectivec":"Objective-C","php":"PHP","php-extras":"PHP Extras","powershell":"PowerShell","jsx":"React JSX","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","typescript":"TypeScript","vhdl":"VHDL","wiki":"Wiki markup","yaml":"YAML"}/*]*/; +var Languages = /*languages_placeholder[*/{"css":"CSS","clike":"C-like","javascript":"JavaScript","actionscript":"ActionScript","apacheconf":"Apache Configuration","apl":"APL","applescript":"AppleScript","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","csharp":"C#","cpp":"C++","coffeescript":"CoffeeScript","css-extras":"CSS Extras","fsharp":"F#","glsl":"GLSL","http":"HTTP","latex":"LaTeX","lolcode":"LOLCODE","matlab":"MATLAB","nasm":"NASM","nsis":"NSIS","objectivec":"Objective-C","php":"PHP","php-extras":"PHP Extras","powershell":"PowerShell","jsx":"React JSX","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","typescript":"TypeScript","vhdl":"VHDL","wiki":"Wiki markup","yaml":"YAML"}/*]*/; Prism.hooks.add('before-highlight', function(env) { var pre = env.element.parentNode; if (!pre || !/pre/i.test(pre.nodeName)) { diff --git a/plugins/show-language/prism-show-language.min.js b/plugins/show-language/prism-show-language.min.js index 3b12881eb1..9e63d33adf 100644 --- a/plugins/show-language/prism-show-language.min.js +++ b/plugins/show-language/prism-show-language.min.js @@ -1 +1 @@ -!function(){if("undefined"!=typeof self&&self.Prism&&self.document){var e={css:"CSS",clike:"C-like",javascript:"JavaScript",actionscript:"ActionScript",apacheconf:"Apache Configuration",apl:"APL",applescript:"AppleScript",aspnet:"ASP.NET (C#)",autohotkey:"AutoHotkey",csharp:"C#",cpp:"C++",coffeescript:"CoffeeScript","css-extras":"CSS Extras",fsharp:"F#",http:"HTTP",latex:"LaTeX",lolcode:"LOLCODE",matlab:"MATLAB",nasm:"NASM",nsis:"NSIS",objectivec:"Objective-C",php:"PHP","php-extras":"PHP Extras",powershell:"PowerShell",jsx:"React JSX",rest:"reST (reStructuredText)",sas:"SAS",sass:"Sass (Sass)",scss:"Sass (Scss)",sql:"SQL",typescript:"TypeScript",vhdl:"VHDL",wiki:"Wiki markup",yaml:"YAML"};Prism.hooks.add("before-highlight",function(s){var t=s.element.parentNode;if(t&&/pre/i.test(t.nodeName)){var a=e[s.language]||s.language.substring(0,1).toUpperCase()+s.language.substring(1);t.setAttribute("data-language",a)}})}}(); \ No newline at end of file +!function(){if("undefined"!=typeof self&&self.Prism&&self.document){var e={css:"CSS",clike:"C-like",javascript:"JavaScript",actionscript:"ActionScript",apacheconf:"Apache Configuration",apl:"APL",applescript:"AppleScript",aspnet:"ASP.NET (C#)",autohotkey:"AutoHotkey",csharp:"C#",cpp:"C++",coffeescript:"CoffeeScript","css-extras":"CSS Extras",fsharp:"F#",glsl:"GLSL",http:"HTTP",latex:"LaTeX",lolcode:"LOLCODE",matlab:"MATLAB",nasm:"NASM",nsis:"NSIS",objectivec:"Objective-C",php:"PHP","php-extras":"PHP Extras",powershell:"PowerShell",jsx:"React JSX",rest:"reST (reStructuredText)",sas:"SAS",sass:"Sass (Sass)",scss:"Sass (Scss)",sql:"SQL",typescript:"TypeScript",vhdl:"VHDL",wiki:"Wiki markup",yaml:"YAML"};Prism.hooks.add("before-highlight",function(s){var t=s.element.parentNode;if(t&&/pre/i.test(t.nodeName)){var a=e[s.language]||s.language.substring(0,1).toUpperCase()+s.language.substring(1);t.setAttribute("data-language",a)}})}}(); \ No newline at end of file diff --git a/tests/languages/glsl/comment_feature.test b/tests/languages/glsl/comment_feature.test new file mode 100644 index 0000000000..b45a6bd5c8 --- /dev/null +++ b/tests/languages/glsl/comment_feature.test @@ -0,0 +1,21 @@ +/**/ +/* foo +bar */ +// +// foo +// foo\ +bar + +---------------------------------------------------- + +[ + ["comment", "/**/"], + ["comment", "/* foo\r\nbar */"], + ["comment", "//"], + ["comment", "// foo"], + ["comment", "// foo\\\r\nbar"] +] + +---------------------------------------------------- + +Checks for comments. \ No newline at end of file diff --git a/tests/languages/glsl/keyword_feature.test b/tests/languages/glsl/keyword_feature.test new file mode 100644 index 0000000000..e07768ca17 --- /dev/null +++ b/tests/languages/glsl/keyword_feature.test @@ -0,0 +1,263 @@ +attribute +const +uniform +varying +buffer +shared +coherent +volatile +restrict +readonly +writeonly +atomic_uint +layout +centroid +flat +smooth +noperspective +patch +sample +break +continue +do +for +while +switch +case +default +if +else +subroutine +in +out +inout +float +double +int +void +bool +true +false +invariant +precise +discard +return +mat2 mat3 mat4 +mat2x2 mat2x3 mat2x4 +mat3x2 mat3x3 mat3x4 +mat4x2 mat4x3 mat4x4 +dmat2 dmat3 dmat4 +dmat2x2 dmat2x3 dmat2x4 +dmat3x2 dmat3x3 dmat3x4 +dmat4x2 dmat4x3 dmat4x4 +vec2 vec3 vec4 +ivec2 ivec3 ivec4 +bvec2 bvec3 bvec4 +dvec2 dvec3 dvec4 +uvec2 uvec3 uvec4 +uint +lowp +mediump +highp +precision +sampler1D sampler2D sampler3D +isampler1D isampler2D isampler3D +usampler1D usampler2D usampler3D +samplerCube isamplerCube usamplerCube +sampler1DShadow sampler2DShadow +samplerCubeShadow +sampler1DArray sampler2DArray +isampler1DArray isampler2DArray +usampler1DArray usampler2DArray +sampler1DArrayShadow sampler2DArrayShadow +sampler2DRect isampler2DRect usampler2DRect +sampler2DRectShadow +samplerBuffer isamplerBuffer usamplerBuffer +sampler2DMS isampler2DMS usampler2DMS +sampler2DMSArray isampler2DMSArray usampler2DMSArray +samplerCubeArray isamplerCubeArray usamplerCubeArray +samplerCubeArrayShadow +image1D image2D image3D +iimage1D iimage2D iimage3D +uimage1D uimage2D uimage3D +image2DRect iimage2DRect uimage2DRect +imageCube iimageCube uimageCube +imageBuffer iimageBuffer uimageBuffer +image1DArray image2DArray +iimage1DArray iimage2DArray +uimage1DArray uimage2DArray +imageCubeArray iimageCubeArray uimageCubeArray +image2DMS iimage2DMS uimage2DMS +image2DMSArray iimage2DMSArray uimage2DMSArray +struct +common +partition +active +asm +class; +union +enum +typedef +template +this +resource +goto +inline +noinline +public +static +extern +external +interface; +long +short +half +fixed +unsigned +superp +input +output +hvec2 hvec3 hvec4 +fvec2 fvec3 fvec4 +sampler3DRect +filter +sizeof +cast +namespace +using + +---------------------------------------------------- + +[ + ["keyword", "attribute"], + ["keyword", "const"], + ["keyword", "uniform"], + ["keyword", "varying"], + ["keyword", "buffer"], + ["keyword", "shared"], + ["keyword", "coherent"], + ["keyword", "volatile"], + ["keyword", "restrict"], + ["keyword", "readonly"], + ["keyword", "writeonly"], + ["keyword", "atomic_uint"], + ["keyword", "layout"], + ["keyword", "centroid"], + ["keyword", "flat"], + ["keyword", "smooth"], + ["keyword", "noperspective"], + ["keyword", "patch"], + ["keyword", "sample"], + ["keyword", "break"], + ["keyword", "continue"], + ["keyword", "do"], + ["keyword", "for"], + ["keyword", "while"], + ["keyword", "switch"], + ["keyword", "case"], + ["keyword", "default"], + ["keyword", "if"], + ["keyword", "else"], + ["keyword", "subroutine"], + ["keyword", "in"], + ["keyword", "out"], + ["keyword", "inout"], + ["keyword", "float"], + ["keyword", "double"], + ["keyword", "int"], + ["keyword", "void"], + ["keyword", "bool"], + ["keyword", "true"], + ["keyword", "false"], + ["keyword", "invariant"], + ["keyword", "precise"], + ["keyword", "discard"], + ["keyword", "return"], + ["keyword", "mat2"], ["keyword", "mat3"], ["keyword", "mat4"], + ["keyword", "mat2x2"], ["keyword", "mat2x3"], ["keyword", "mat2x4"], + ["keyword", "mat3x2"], ["keyword", "mat3x3"], ["keyword", "mat3x4"], + ["keyword", "mat4x2"], ["keyword", "mat4x3"], ["keyword", "mat4x4"], + ["keyword", "dmat2"], ["keyword", "dmat3"], ["keyword", "dmat4"], + ["keyword", "dmat2x2"], ["keyword", "dmat2x3"], ["keyword", "dmat2x4"], + ["keyword", "dmat3x2"], ["keyword", "dmat3x3"], ["keyword", "dmat3x4"], + ["keyword", "dmat4x2"], ["keyword", "dmat4x3"], ["keyword", "dmat4x4"], + ["keyword", "vec2"], ["keyword", "vec3"], ["keyword", "vec4"], + ["keyword", "ivec2"], ["keyword", "ivec3"], ["keyword", "ivec4"], + ["keyword", "bvec2"], ["keyword", "bvec3"], ["keyword", "bvec4"], + ["keyword", "dvec2"], ["keyword", "dvec3"], ["keyword", "dvec4"], + ["keyword", "uvec2"], ["keyword", "uvec3"], ["keyword", "uvec4"], + ["keyword", "uint"], + ["keyword", "lowp"], + ["keyword", "mediump"], + ["keyword", "highp"], + ["keyword", "precision"], + ["keyword", "sampler1D"], ["keyword", "sampler2D"], ["keyword", "sampler3D"], + ["keyword", "isampler1D"], ["keyword", "isampler2D"], ["keyword", "isampler3D"], + ["keyword", "usampler1D"], ["keyword", "usampler2D"], ["keyword", "usampler3D"], + ["keyword", "samplerCube"], ["keyword", "isamplerCube"], ["keyword", "usamplerCube"], + ["keyword", "sampler1DShadow"], ["keyword", "sampler2DShadow"], + ["keyword", "samplerCubeShadow"], + ["keyword", "sampler1DArray"], ["keyword", "sampler2DArray"], + ["keyword", "isampler1DArray"], ["keyword", "isampler2DArray"], + ["keyword", "usampler1DArray"], ["keyword", "usampler2DArray"], + ["keyword", "sampler1DArrayShadow"], ["keyword", "sampler2DArrayShadow"], + ["keyword", "sampler2DRect"], ["keyword", "isampler2DRect"], ["keyword", "usampler2DRect"], + ["keyword", "sampler2DRectShadow"], + ["keyword", "samplerBuffer"], ["keyword", "isamplerBuffer"], ["keyword", "usamplerBuffer"], + ["keyword", "sampler2DMS"], ["keyword", "isampler2DMS"], ["keyword", "usampler2DMS"], + ["keyword", "sampler2DMSArray"], ["keyword", "isampler2DMSArray"], ["keyword", "usampler2DMSArray"], + ["keyword", "samplerCubeArray"], ["keyword", "isamplerCubeArray"], ["keyword", "usamplerCubeArray"], + ["keyword", "samplerCubeArrayShadow"], + ["keyword", "image1D"], ["keyword", "image2D"], ["keyword", "image3D"], + ["keyword", "iimage1D"], ["keyword", "iimage2D"], ["keyword", "iimage3D"], + ["keyword", "uimage1D"], ["keyword", "uimage2D"], ["keyword", "uimage3D"], + ["keyword", "image2DRect"], ["keyword", "iimage2DRect"], ["keyword", "uimage2DRect"], + ["keyword", "imageCube"], ["keyword", "iimageCube"], ["keyword", "uimageCube"], + ["keyword", "imageBuffer"], ["keyword", "iimageBuffer"], ["keyword", "uimageBuffer"], + ["keyword", "image1DArray"], ["keyword", "image2DArray"], + ["keyword", "iimage1DArray"], ["keyword", "iimage2DArray"], + ["keyword", "uimage1DArray"], ["keyword", "uimage2DArray"], + ["keyword", "imageCubeArray"], ["keyword", "iimageCubeArray"], ["keyword", "uimageCubeArray"], + ["keyword", "image2DMS"], ["keyword", "iimage2DMS"], ["keyword", "uimage2DMS"], + ["keyword", "image2DMSArray"], ["keyword", "iimage2DMSArray"], ["keyword", "uimage2DMSArray"], + ["keyword", "struct"], + ["keyword", "common"], + ["keyword", "partition"], + ["keyword", "active"], + ["keyword", "asm"], + ["keyword", "class"], ["punctuation", ";"], + ["keyword", "union"], + ["keyword", "enum"], + ["keyword", "typedef"], + ["keyword", "template"], + ["keyword", "this"], + ["keyword", "resource"], + ["keyword", "goto"], + ["keyword", "inline"], + ["keyword", "noinline"], + ["keyword", "public"], + ["keyword", "static"], + ["keyword", "extern"], + ["keyword", "external"], + ["keyword", "interface"], ["punctuation", ";"], + ["keyword", "long"], + ["keyword", "short"], + ["keyword", "half"], + ["keyword", "fixed"], + ["keyword", "unsigned"], + ["keyword", "superp"], + ["keyword", "input"], + ["keyword", "output"], + ["keyword", "hvec2"], ["keyword", "hvec3"], ["keyword", "hvec4"], + ["keyword", "fvec2"], ["keyword", "fvec3"], ["keyword", "fvec4"], + ["keyword", "sampler3DRect"], + ["keyword", "filter"], + ["keyword", "sizeof"], + ["keyword", "cast"], + ["keyword", "namespace"], + ["keyword", "using"] +] + +---------------------------------------------------- + +Checks for keywords. \ No newline at end of file diff --git a/tests/languages/glsl/number_feature.test b/tests/languages/glsl/number_feature.test new file mode 100644 index 0000000000..20ac4d1109 --- /dev/null +++ b/tests/languages/glsl/number_feature.test @@ -0,0 +1,31 @@ +0xBadFace +42 +3.14159 +3e8 +3.6e-7 +4.7E+12 +4u +42U +3.1l +42f +2.0LF + +---------------------------------------------------- + +[ + ["number", "0xBadFace"], + ["number", "42"], + ["number", "3.14159"], + ["number", "3e8"], + ["number", "3.6e-7"], + ["number", "4.7E+12"], + ["number", "4u"], + ["number", "42U"], + ["number", "3.1l"], + ["number", "42f"], + ["number", "2.0LF"] +] + +---------------------------------------------------- + +Checks for numbers. \ No newline at end of file diff --git a/tests/languages/glsl/preprocessor_feature.test b/tests/languages/glsl/preprocessor_feature.test new file mode 100644 index 0000000000..a720d35063 --- /dev/null +++ b/tests/languages/glsl/preprocessor_feature.test @@ -0,0 +1,35 @@ +#define +#undef +#if +#ifdef +#ifndef +#else +#elif +#endif +#error +#pragma +#extension +#version +#line + +---------------------------------------------------- + +[ + ["preprocessor", "#define"], + ["preprocessor", "#undef"], + ["preprocessor", "#if"], + ["preprocessor", "#ifdef"], + ["preprocessor", "#ifndef"], + ["preprocessor", "#else"], + ["preprocessor", "#elif"], + ["preprocessor", "#endif"], + ["preprocessor", "#error"], + ["preprocessor", "#pragma"], + ["preprocessor", "#extension"], + ["preprocessor", "#version"], + ["preprocessor", "#line"] +] + +---------------------------------------------------- + +Checks for preprocessor instructions. \ No newline at end of file