-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the point shaders so that common code is in shared files.
The shader-loader allows importing chunks into other files. This allows common code to be placed in a shared file and imported by other files. There are some restrictions: the common chunks must have filenames ending in `glsl`, and the glslangValidator doesn't handle these inclusions on its own. However, sharing code is of substantial benefit for reducing maintenance issues, so it is worth this change. Note that there are other webpack shader loaders (e.g., webpack-glsl-loader) that provide similar include functionality but with a different syntax, as glsl doesn't have its own include statements. As such, if we ever switch the shader loader to another one, the import statements would have to change.
- Loading branch information
Showing
9 changed files
with
130 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import argparse | ||
import os | ||
import re | ||
import sys | ||
|
||
|
||
def readSource(source): | ||
data = open(source).read() | ||
parts = re.split('(\\$[-.\\w]+)', data) | ||
for idx, chunk in enumerate(parts): | ||
if chunk.startswith('$') and len(chunk) > 1: | ||
parts[idx] = readSource(os.path.join(os.path.dirname(source), chunk[1:] + '.glsl')) | ||
return ''.join(parts) | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
description='Preprocess glsl files to handle includes in the same way ' | ||
'as shader-loader. The output of this can sent to glslangValidator.') | ||
parser.add_argument('source', help='Source file') | ||
args = parser.parse_args() | ||
data = readSource(args.source) | ||
sys.stdout.write(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* pointFeature common fragment shader */ | ||
|
||
#ifdef GL_ES | ||
precision highp float; | ||
#endif | ||
varying vec4 fillColorVar; | ||
varying vec4 strokeColorVar; | ||
varying float radiusVar; | ||
varying float strokeWidthVar; | ||
|
||
void pointFeatureFragment(float rad) { | ||
vec4 strokeColor, fillColor; | ||
float endStep; | ||
// No stroke or fill implies nothing to draw | ||
if (rad > 1.0) | ||
discard; | ||
// If there is no stroke, the fill region should transition to nothing | ||
if (strokeColorVar.a == 0.0) { | ||
strokeColor = vec4(fillColorVar.rgb, 0.0); | ||
endStep = 1.0; | ||
} else { | ||
strokeColor = strokeColorVar; | ||
endStep = radiusVar / (radiusVar + strokeWidthVar); | ||
} | ||
// Likewise, if there is no fill, the stroke should transition to nothing | ||
if (fillColorVar.a == 0.0) | ||
fillColor = vec4(strokeColor.rgb, 0.0); | ||
else | ||
fillColor = fillColorVar; | ||
// Distance to antialias over. First number is in pixels | ||
float antialiasDist = 1.5 / (radiusVar + strokeWidthVar); | ||
if (rad < endStep) { | ||
float step = smoothstep(max(0.0, endStep - antialiasDist), endStep, rad); | ||
vec4 color = mix(fillColor, strokeColor, step); | ||
float step2 = smoothstep(max(0.0, 1.0 - antialiasDist), 1.0, rad); | ||
gl_FragColor = mix(color, vec4(color.rgb, 0.0), step2); | ||
} else { | ||
float step = smoothstep(max(0.0, 1.0 - antialiasDist), 1.0, rad); | ||
gl_FragColor = mix(strokeColor, vec4(strokeColor.rgb, 0.0), step); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,12 @@ | ||
/* pointFeature square/triangle fragment shader */ | ||
|
||
#ifdef GL_ES | ||
precision highp float; | ||
#endif | ||
uniform float aspect; | ||
varying vec4 fillColorVar; | ||
varying vec4 strokeColorVar; | ||
varying float radiusVar; | ||
varying float strokeWidthVar; | ||
$pointFeatureFS | ||
|
||
varying vec3 unitVar; // distinct for square/triangle | ||
|
||
void main () { | ||
vec4 strokeColor, fillColor; | ||
float endStep; | ||
// No stroke or fill implies nothing to draw | ||
if (fillColorVar.a == 0.0 && strokeColorVar.a == 0.0) | ||
discard; | ||
float rad = length(unitVar.xy); // distinct for square/triangle | ||
if (rad > 1.0) | ||
discard; | ||
// If there is no stroke, the fill region should transition to nothing | ||
if (strokeColorVar.a == 0.0) { | ||
strokeColor = vec4(fillColorVar.rgb, 0.0); | ||
endStep = 1.0; | ||
} else { | ||
strokeColor = strokeColorVar; | ||
endStep = radiusVar / (radiusVar + strokeWidthVar); | ||
} | ||
// Likewise, if there is no fill, the stroke should transition to nothing | ||
if (fillColorVar.a == 0.0) | ||
fillColor = vec4(strokeColor.rgb, 0.0); | ||
else | ||
fillColor = fillColorVar; | ||
// Distance to antialias over. First number is in pixels | ||
float antialiasDist = 1.5 / (radiusVar + strokeWidthVar); | ||
if (rad < endStep) { | ||
float step = smoothstep(max(0.0, endStep - antialiasDist), endStep, rad); | ||
vec4 color = mix(fillColor, strokeColor, step); | ||
float step2 = smoothstep(max(0.0, 1.0 - antialiasDist), 1.0, rad); | ||
gl_FragColor = mix(color, vec4(color.rgb, 0.0), step2); | ||
} else { | ||
float step = smoothstep(max(0.0, 1.0 - antialiasDist), 1.0, rad); | ||
gl_FragColor = mix(strokeColor, vec4(strokeColor.rgb, 0.0), step); | ||
} | ||
pointFeatureFragment(rad); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,13 @@ | ||
/* pointFeature sprite fragment shader */ | ||
|
||
#ifdef GL_ES | ||
precision highp float; | ||
#endif | ||
uniform float aspect; | ||
varying vec4 fillColorVar; | ||
varying vec4 strokeColorVar; | ||
varying float radiusVar; | ||
varying float strokeWidthVar; | ||
// the square/triangle shade defines unitVar | ||
$pointFeatureFS | ||
|
||
// the square/triangle shader defines unitVar | ||
|
||
void main () { | ||
vec4 strokeColor, fillColor; | ||
float endStep; | ||
// No stroke or fill implies nothing to draw | ||
if (fillColorVar.a == 0.0 && strokeColorVar.a == 0.0) | ||
discard; | ||
float rad = 2.0 * length(gl_PointCoord - vec2(0.5)); // distinct for sprite | ||
if (rad > 1.0) | ||
discard; | ||
// If there is no stroke, the fill region should transition to nothing | ||
if (strokeColorVar.a == 0.0) { | ||
strokeColor = vec4(fillColorVar.rgb, 0.0); | ||
endStep = 1.0; | ||
} else { | ||
strokeColor = strokeColorVar; | ||
endStep = radiusVar / (radiusVar + strokeWidthVar); | ||
} | ||
// Likewise, if there is no fill, the stroke should transition to nothing | ||
if (fillColorVar.a == 0.0) | ||
fillColor = vec4(strokeColor.rgb, 0.0); | ||
else | ||
fillColor = fillColorVar; | ||
// Distance to antialias over. First number is in pixels | ||
float antialiasDist = 1.5 / (radiusVar + strokeWidthVar); | ||
if (rad < endStep) { | ||
float step = smoothstep(max(0.0, endStep - antialiasDist), endStep, rad); | ||
vec4 color = mix(fillColor, strokeColor, step); | ||
float step2 = smoothstep(max(0.0, 1.0 - antialiasDist), 1.0, rad); | ||
gl_FragColor = mix(color, vec4(color.rgb, 0.0), step2); | ||
} else { | ||
float step = smoothstep(max(0.0, 1.0 - antialiasDist), 1.0, rad); | ||
gl_FragColor = mix(strokeColor, vec4(strokeColor.rgb, 0.0), step); | ||
} | ||
pointFeatureFragment(rad); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* pointFeature common vertex shader */ | ||
|
||
#ifdef GL_ES | ||
precision highp float; | ||
#endif | ||
attribute vec3 pos; | ||
attribute float radius; | ||
attribute vec3 fillColor; | ||
attribute vec3 strokeColor; | ||
attribute float fillOpacity; | ||
attribute float strokeWidth; | ||
attribute float strokeOpacity; | ||
attribute float fill; | ||
attribute float stroke; | ||
uniform mat4 modelViewMatrix; | ||
uniform mat4 projectionMatrix; | ||
varying vec4 fillColorVar; | ||
varying vec4 strokeColorVar; | ||
varying float radiusVar; | ||
varying float strokeWidthVar; | ||
|
||
float pointFeaturePrep() { | ||
strokeWidthVar = strokeWidth; | ||
fillColorVar = vec4(fillColor, fillOpacity); | ||
strokeColorVar = vec4(strokeColor, strokeOpacity); | ||
// No stroke or fill implies nothing to draw | ||
if (stroke < 1.0 || strokeWidth <= 0.0 || strokeOpacity <= 0.0) { | ||
strokeColorVar.a = 0.0; | ||
strokeWidthVar = 0.0; | ||
} | ||
if (fill < 1.0 || radius <= 0.0 || fillOpacity <= 0.0) | ||
fillColorVar.a = 0.0; | ||
/* If the point has no visible pixels, skip doing computations on it. */ | ||
if (fillColorVar.a == 0.0 && strokeColorVar.a == 0.0) { | ||
gl_Position = vec4(2, 2, 0, 1); | ||
return 0.0; | ||
} | ||
return radius; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters