urclib.ui_qt.visualizer.shaders
shaders.py
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= “Shaders” are programs that tell a GPU what to do at various rendering steps. Currently, this module uses shaders for the vertex placement, geometry rendering, and fragment coloration steps of the rendering pipeline. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Shaders are expecting glsl version 430. Would use 450, but NETL is still providing hardware with ancient drivers. Eventually may need smarter check to detect on startup, but this should work.
Each of the shaders in this file can be overridden, but don’t do this unless you know what you are doing.
Module Contents
Classes
Convenience manager for multiple shader programs. |
Functions
|
Find the index location of all uniform variables in each shader program. |
|
Take the supplied shader source codes, compile, and combine into shader programs. |
|
Assign a uniform block to any shader program which explicitly calls for it. |
|
Build file name list of shaders to export based on names provided. |
|
Convenience method for exporting all shaders to external files. |
Attributes
- urclib.ui_qt.visualizer.shaders._defines = Multiline-String
Show Value
"""#version 430 #define PI 3.1415926535897932384626433832795 #define HALF_PI PI*0.5 #define QUART_PI PI*0.25 """
- urclib.ui_qt.visualizer.shaders._pointFns = Multiline-String
Show Value
""" //Charcodes for point glyphs #define ORD_CIRCLE 46u //'.' #define ORD_DOWNTRI 118u //'v' #define ORD_UPTRI 94u //'^' #define ORD_SQUARE 115u //'s' #define ORD_DIAMND 100u //'d' #define ORD_X 120u //'x' #define ORD_CROSS 43u //'+' #define ORD_STAR 42u //'*' vec4 applySD(vec4 ptColor,float sDist,const float bordThick) { //vec4 outColor =vec4(edgeColor.xyz,0.); vec4 outColor =vec4(ptColor.xyz,0.); if (sDist<0.) outColor= fSelected==0 ? ptColor : selectColor; vec4 useEdgeColor = fSelected == 0 ? ptColor : selectColor; vec2 gradient = vec2(dFdx(sDist),dFdy(sDist)); float fromLine = abs(sDist/length(gradient)); float lineweight = clamp(bordThick - fromLine,0.f,1.f); return mix(outColor,useEdgeColor,lineweight); } vec4 pointCircle(vec4 ptColor) { const float THICKNESS = ptScale <= 4 ? 0. : 1.; const float TEST_RAD = ptScale <= 4 ? 1. : .85; vec2 signCoord = (2.*gl_PointCoord) - 1.; float radius = length(signCoord); float sDist = radius - TEST_RAD; return applySD(ptColor,sDist, THICKNESS); } vec4 pointSquare(vec4 ptColor) { //todo add border return ptColor; } vec4 pointDiamond(vec4 ptColor) { const float THICKNESS = ptScale <= 4 ? 0. : 1.; vec2 adj = (2.*gl_PointCoord.xy) - 1.0; float sDist= (abs(adj.x)+abs(adj.y))-1.; return applySD(ptColor,sDist, THICKNESS); } vec4 pointX(vec4 ptColor) { const float THICKNESS = ptScale <= 4 ? 0. : 1.; vec2 adj = (2.*gl_PointCoord.xy) - 1.0; float sDist= abs(abs(adj.x)-abs(adj.y))-0.2; // if(sDist>=0) // discard; return applySD(ptColor,sDist, THICKNESS); } vec4 pointCross(vec4 ptColor) { const float THICKNESS = ptScale <= 4 ? 0. : 1.; vec2 adj = (2.*gl_PointCoord.xy) - 1.0; float trg =min(abs(adj.x),abs(adj.y)); float sDist= trg-0.2; if(sDist>=0) discard; return ptColor; // return applySD(ptColor,sDist, THICKNESS); } vec4 pointTriangle(vec4 ptColor, bool pointDown) { const float THICKNESS = ptScale <= 4 ? 0. : 1.; vec2 adj = (2.*gl_PointCoord.xy) - 1.0; float height=gl_PointCoord.y; if (pointDown) height = 1.-height; float sDist= abs(adj.x)-height; return applySD(ptColor,sDist, THICKNESS); } vec3 barycentric(vec2 p, vec2 p0, vec2 p1, vec2 p2) { mat2 T = mat2(p0-p2,p1-p2); vec3 results = vec3(0); results.xy = inverse(T)*(p-p2); results.z = 1. - results.x-results.y; return results; } bool inBarycentric(vec3 p) { return all(greaterThan(p,vec3(0.))); } bool inTriangle(vec2 p, vec2 p0, vec2 p1, vec2 p2) { vec3 bc = barycentric(p,p0,p1,p2); return inBarycentric(bc); } vec4 pointStar(vec4 ptColor, bool pointDown) { const float THICKNESS = ptScale <= 4 ? 0. : 1.; vec2 adj=(2.*gl_PointCoord.xy) - 1.0; if (pointDown) adj.y*=-1.; //const float INNER_RAD_2 = 0.25; //if (pow(adj.x,2)+pow(adj.y,2) < INNER_RAD_2) // return ptColor; //TODO: add pre-calculated tris here vec2 tris[5][3] = { {vec2( 0. , 1. ),vec2(-.3804,-.1236),vec2( .3804,-.1236)}, {vec2( .9511, .3090),vec2(-.2351, .3236),vec2( .0 ,-.4 )}, {vec2( .5878,-.8090),vec2( .2351, .3236),vec2(-.3804,-.1236)}, {vec2(-.5878,-.8090),vec2( .3804,-.1236),vec2(-.2351, .3236)}, {vec2(-.9511, .3090),vec2( 0. ,-.4 ),vec2( .2351, .3236)}, }; bool hit = false; vec3 baryCoord=vec3(0); for (int i=0; !hit && i<5; ++i) { baryCoord = barycentric(adj,tris[i][0],tris[i][1],tris[i][2]); hit = inBarycentric(baryCoord); } if (!hit) discard; return ptColor; // todo: fix border // float sDist = 1 - min(baryCoord.x,min(baryCoord.y,baryCoord.z)); // return applySD(ptColor,sDist, THICKNESS); } vec4 doPoint(vec4 ptColor,uint typeCode) { vec4 outColor; switch(typeCode) { case ORD_DOWNTRI: outColor = pointTriangle(ptColor,true); break; case ORD_UPTRI: outColor = pointTriangle(ptColor,false); break; case ORD_SQUARE: outColor = pointSquare(ptColor); break; case ORD_DIAMND: outColor = pointDiamond(ptColor); break; case ORD_X: outColor = pointX(ptColor); break; case ORD_CROSS: outColor = pointCross(ptColor); break; case ORD_STAR: outColor = pointStar(ptColor,true); break; case ORD_CIRCLE: default: outColor=pointCircle(ptColor); }; return outColor; } """
- urclib.ui_qt.visualizer.shaders.passthru_vert
- urclib.ui_qt.visualizer.shaders.thickline_vert
- urclib.ui_qt.visualizer.shaders.refcolortex_vert
- urclib.ui_qt.visualizer.shaders.point_vert
- urclib.ui_qt.visualizer.shaders.point_ref_vert
- urclib.ui_qt.visualizer.shaders.text_vert
- urclib.ui_qt.visualizer.shaders.rubberband_vert
- urclib.ui_qt.visualizer.shaders.fbBlit_vert
- urclib.ui_qt.visualizer.shaders.thickline_geom
- urclib.ui_qt.visualizer.shaders.simple_frag
- urclib.ui_qt.visualizer.shaders.point_frag
- urclib.ui_qt.visualizer.shaders.point_ref_frag
- urclib.ui_qt.visualizer.shaders.line_ref_frag
- urclib.ui_qt.visualizer.shaders.select_frag
- urclib.ui_qt.visualizer.shaders.select_line_frag
- urclib.ui_qt.visualizer.shaders.refcolortex_frag
- urclib.ui_qt.visualizer.shaders.colortex_frag
- urclib.ui_qt.visualizer.shaders.refcolorval_frag
- urclib.ui_qt.visualizer.shaders.rubberband_frag
- urclib.ui_qt.visualizer.shaders.text_frag
- urclib.ui_qt.visualizer.shaders.fbBlit_frag
- urclib.ui_qt.visualizer.shaders.shader_recipes
- urclib.ui_qt.visualizer.shaders.fieldMappings
- urclib.ui_qt.visualizer.shaders.findUniformLocations(progDict, mappings)
Find the index location of all uniform variables in each shader program.
- Parameters:
progDict (dict) – A dictionary that matches the output of buildShaders().
mappings (dict) – A dict of dicts which maps the names of uniform variables to locate.
- Returns:
Uniform name (key) and location (value).
- Return type:
dict
- urclib.ui_qt.visualizer.shaders.buildShaders(recipes)
Take the supplied shader source codes, compile, and combine into shader programs.
- Parameters:
recipes (dict) – Lists of shaders to build shader pipelines.
- Returns:
- The OpenGL identifiers for the newly built shaders, stored under the key used for the
equivalent recipe entry.
- Return type:
dict
- urclib.ui_qt.visualizer.shaders.assignUniformBlock(progs, bindPt, lbl)
Assign a uniform block to any shader program which explicitly calls for it.
- Parameters:
progs (list) – The programs to parse for the designated uniform block.
bindPt (int) – The uniform array binding index.
lbl (str) – The dlg_label of the uniform block declared within the shader program.
- urclib.ui_qt.visualizer.shaders._grabSubShaderList(names, suffix)
Build file name list of shaders to export based on names provided.
- Parameters:
names (list) – The list of names to transform.
suffix (str) – The suffix of names to include.
- Returns:
the filenames to apply.
- Return type:
list
- urclib.ui_qt.visualizer.shaders.ExportShadersToFiles(outDir)
Convenience method for exporting all shaders to external files.
- Parameters:
outDir (str or Path) – path to parent directory
- class urclib.ui_qt.visualizer.shaders.ShaderProgMgr(progRecipes=None, mappings=None)
Bases:
objectConvenience manager for multiple shader programs.
- Parameters:
progRecipes (dict,optional) – The shader program recipes to include. If None, the module variable shader_recipes is used.
mappings (dict,optional) – The uniform field mappings to shader programs. If None, the module variable fieldMappings is used.
- property shaderProgram
OpenGL identifier of active shader program.
- Type:
int
- cleanup()
Delete all the programs managed by this manager.
- useProgram(progName=None)
Activate a shader prograam by name.
- Parameters:
progName (str,optional) – The name of the program to activate; all programs are deactivated if omitted.
- useProgramDirectly(prog)
Activate a shader prograam by OpenGL identifier.
- Parameters:
prog (int) – The OpenGL shader program identifier.
- __getitem__(item)
- progLookup(progName)
Find an OpenGL program identifier by program name.
- Parameters:
progName (str) – The program to search for.
- Returns:
The identifier for the program, or 0 if progName does not map to a known shader program.
- Return type:
int