Skip to content

Commit

Permalink
improve GLSL classification (#4692)
Browse files Browse the repository at this point in the history
* added extensions .gs, .comp and .glslf.
  See: https://stackoverflow.com/q/6432838

* add disambiguation rule for .gs files:
  if it starts with `#version <number>`, it's GLSL.
  This seems to work for most GLSL files with few exceptions
  that are catched by the classifier.

* added sample extrude_normal.gs from kyle-piddington/ShaderTool
  (licensed under MIT):
  https://github.com/kyle-piddington/ShaderTool/blob/c753a53bde6eab942da617adab9483c945f27f51/assets/shaders/extrude_normals.gs

* added sample blend_120.glslf from gfx-rs/gfx (licensed under
  Apache 2.0):
  https://github.com/gfx-rs/gfx/blob/7b084cfbb95e3e4d1ea51e91746fc75b2efb3be3/examples/blend/shader/blend_120.glslf

* test_heuristics.rb now supports alt_name to ake a different file name
for a fixture.
  • Loading branch information
smola authored and lildude committed Jan 27, 2020
1 parent 9e70f4d commit 857bbfb
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/linguist/heuristics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ disambiguations:
- language: Game Maker Language
- extensions: ['.gs']
rules:
- language: GLSL
pattern: '^#version\s+[0-9]+\b'
- language: Gosu
pattern: '^uses java\.'
- extensions: ['.h']
Expand Down
2 changes: 2 additions & 0 deletions lib/linguist/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,9 @@ GLSL:
- ".fshader"
- ".geo"
- ".geom"
- ".glslf"
- ".glslv"
- ".gs"
- ".gshader"
- ".shader"
- ".tesc"
Expand Down
53 changes: 53 additions & 0 deletions samples/GLSL/blend_120.glslf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#version 120

#define SCREEN 0
#define DODGE 1
#define BURN 2
#define OVERLAY 3
#define MULTIPLY 4
#define ADD 5
#define DIVIDE 6
#define GRAIN_EXTRACT 7
#define GRAIN_MERGE 8

// grayscale
uniform sampler2D t_Lena;
// rgba
uniform sampler2D t_Tint;

uniform int i_Blend;

varying vec2 v_Uv;

void main() {
// we sample from both textures using the same uv coordinates. since our
// lena image is grayscale, we only get the first component.
vec3 lena = vec3(texture2D(t_Lena, v_Uv).r);
vec3 tint = texture2D(t_Tint, v_Uv).rgb;

vec3 result = vec3(0.0);

// normally you'd have a shader program per technique, but for the sake of
// simplicity we'll just branch on it here.
if (i_Blend == SCREEN) {
result = vec3(1.0) - ((vec3(1.0) - lena) * (vec3(1.0) - tint));
} else if (i_Blend == DODGE) {
result = lena / (vec3(1.0) - tint);
} else if (i_Blend == BURN) {
result = vec3(1.0) - ((vec3(1.0) - lena) / lena);
} else if (i_Blend == OVERLAY) {
result = lena * (lena + (tint * 2) * (vec3(1.0) - lena));
} else if (i_Blend == MULTIPLY) {
result = lena * tint;
} else if (i_Blend == ADD) {
result = lena + tint;
} else if (i_Blend == DIVIDE) {
result = lena / tint;
} else if (i_Blend == GRAIN_EXTRACT) {
result = lena - tint + 0.5;
} else if (i_Blend == GRAIN_MERGE) {
result = lena + tint - 0.5;
}

gl_FragColor = vec4(result, 1.0);
}
27 changes: 27 additions & 0 deletions samples/GLSL/extrude_normals.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#version 330 core
layout(triangles) in;
layout (line_strip, max_vertices = 6) out;

in VS_OUT
{
vec3 normal;
}gs_in[];

const float MAGNITUDE = 0.025f;

void GenerateLine(int index)
{
gl_Position = gl_in[index].gl_Position;
EmitVertex();
gl_Position = gl_in[index].gl_Position + vec4(gs_in[index].normal, 0.0f) * MAGNITUDE;
EmitVertex();
EndPrimitive();
}

void main()
{
GenerateLine(0);
GenerateLine(1);
GenerateLine(2);
}

7 changes: 6 additions & 1 deletion test/test_heuristics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,15 @@ def test_gml_by_heuristics
})
end

# Candidate languages = ["Genie", "GLSL", "Gosu", "JavaScript"]
def test_gs_by_heuristics
assert_heuristics({
"Gosu" => all_fixtures("Gosu", "*.gs")
"GLSL" => all_fixtures("GLSL", "*.gs"),
"Gosu" => all_fixtures("Gosu", "*.gs"),
})
assert_heuristics({
nil => all_fixtures("Genie", "*.gs") + all_fixtures("JavaScript")
}, alt_name="test.gs")
end

# Candidate languages = ["C++", "Objective-C"]
Expand Down

0 comments on commit 857bbfb

Please sign in to comment.