-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathrules.cpp
More file actions
481 lines (440 loc) · 15.9 KB
/
rules.cpp
File metadata and controls
481 lines (440 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run
#include "polyscope/render/opengl/shaders/rules.h"
namespace polyscope {
namespace render {
namespace backend_openGL3 {
// clang-format off
// possibly discards a fragment due to global rules
const ShaderReplacementRule GLSL_VERSION(
/* rule name */ "GLSL_VERSION",
/* replacement sources */
{
{"GLSL_VERSION", "#version 330 core"},
}
);
// possibly discards a fragment due to global rules
const ShaderReplacementRule GLOBAL_FRAGMENT_FILTER(
/* rule name */ "GLOBAL_FRAGMENT_FILTER",
/* replacement sources */
{
{"GLOBAL_FRAGMENT_FILTER", "// do nothing, for now"},
}
);
// light using a matcap texture
// input: vec3 albedoColor;
// output: vec3 litColor after lighting
const ShaderReplacementRule LIGHT_MATCAP (
/* rule name */ "LIGHT_MATCAP",
{ /* replacement sources */
{"FRAG_DECLARATIONS", R"(
uniform sampler2D t_mat_r;
uniform sampler2D t_mat_g;
uniform sampler2D t_mat_b;
uniform sampler2D t_mat_k;
vec3 lightSurfaceMat(vec3 normal, vec3 color, sampler2D t_mat_r, sampler2D t_mat_g, sampler2D t_mat_b, sampler2D t_mat_k);
)"},
{"GENERATE_LIT_COLOR", R"(
vec3 litColor = lightSurfaceMat(shadeNormal, albedoColor, t_mat_r, t_mat_g, t_mat_b, t_mat_k);
)"}
},
/* uniforms */ {},
/* attributes */ {},
/* textures */ {
{"t_mat_r", 2},
{"t_mat_g", 2},
{"t_mat_b", 2},
{"t_mat_k", 2},
}
);
// "light" by just copying the value
// input: vec3 albedoColor;
// output: vec3 litColor after lighting
const ShaderReplacementRule LIGHT_PASSTHRU (
/* rule name */ "LIGHT_PASSTHRU",
{ /* replacement sources */
{"GENERATE_LIT_COLOR", "vec3 litColor = albedoColor;"}
},
/* uniforms */ {},
/* attributes */ {},
/* textures */ {}
);
// input: uniform
// output: vec3 albedoColor
const ShaderReplacementRule SHADE_BASECOLOR (
/* rule name */ "SHADE_BASECOLOR",
{ /* replacement sources */
{"FRAG_DECLARATIONS", R"(
uniform vec3 u_baseColor;
)"},
{"GENERATE_SHADE_COLOR", "vec3 albedoColor = u_baseColor;"}
},
/* uniforms */ {
{"u_baseColor", RenderDataType::Vector3Float},
},
/* attributes */ {},
/* textures */ {}
);
// input: uniform output: vec3 shadeColor
const ShaderReplacementRule SHADECOLOR_FROM_UNIFORM (
/* rule name */ "SHADECOLOR_FROM_UNIFORM",
{ /* replacement sources */
{"FRAG_DECLARATIONS", R"(
uniform vec3 u_color;
)"},
{"GENERATE_SHADE_COLOR", "vec3 shadeColor = u_color;"}
},
/* uniforms */ {
{"u_color", RenderDataType::Vector3Float},
},
/* attributes */ {},
/* textures */ {}
);
// input: vec3 shadeColor
// output: vec3 albedoColor
const ShaderReplacementRule SHADE_COLOR(
/* rule name */ "SHADE_COLOR",
{ /* replacement sources */
{"GENERATE_SHADE_COLOR", "vec3 albedoColor = shadeColor;"}
},
/* uniforms */ {},
/* attributes */ {},
/* textures */ {}
);
// input: attribute float shadeValue
// output: vec3 albedoColor
const ShaderReplacementRule SHADE_COLORMAP_VALUE(
/* rule name */ "SHADE_COLORMAP_VALUE",
{ /* replacement sources */
{"FRAG_DECLARATIONS", R"(
uniform float u_rangeHigh;
uniform float u_rangeLow;
uniform sampler1D t_colormap;
uniform vec3 u_infColor;
uniform vec3 u_nanColor;
)"},
{"GENERATE_SHADE_COLOR", R"(
vec3 albedoColor = vec3(0., 0., 0.);
if (isnan(shadeValue)) {
albedoColor = u_nanColor;
} else if(isinf(shadeValue)) {
albedoColor = u_infColor;
} else {
float rangeTVal = (shadeValue - u_rangeLow) / (u_rangeHigh - u_rangeLow);
rangeTVal = clamp(rangeTVal, 0.f, 1.f);
albedoColor = texture(t_colormap, rangeTVal).rgb;
}
)"}
},
/* uniforms */ {
{"u_rangeLow", RenderDataType::Float},
{"u_rangeHigh", RenderDataType::Float},
{"u_infColor", RenderDataType::Vector3Float},
{"u_nanColor", RenderDataType::Vector3Float},
},
/* attributes */ {},
/* textures */ {
{"t_colormap", 1}
}
);
// input: attribute vec2 shadeValue2
// output: vec3 albedoColor
const ShaderReplacementRule SHADE_COLORMAP_ANGULAR2(
/* rule name */ "SHADE_COLORMAP_ANGULAR2",
{ /* replacement sources */
{"FRAG_DECLARATIONS", R"(
uniform float u_angle;
uniform sampler1D t_colormap;
)"},
{"GENERATE_SHADE_COLOR", R"(
float pi = 3.14159265359;
float angle = atan(shadeValue2.y, shadeValue2.x) / (2. * pi) + 0.5; // in [0,1]
float shiftedAngle = mod(angle + u_angle/(2. * pi), 1.);
vec3 albedoColor = texture(t_colormap, shiftedAngle).rgb;
)"}
},
/* uniforms */ {
{"u_angle", RenderDataType::Float},
},
/* attributes */ {},
/* textures */ {
{"t_colormap", 1}
}
);
const ShaderReplacementRule SHADE_GRID_VALUE2 (
/* rule name */ "SHADE_GRID_VALUE2",
{ /* replacement sources */
{"FRAG_DECLARATIONS", R"(
uniform float u_modLen;
uniform vec3 u_gridLineColor;
uniform vec3 u_gridBackgroundColor;
)"},
{"GENERATE_SHADE_COLOR", R"(
float mX = mod(shadeValue2.x, 2.0 * u_modLen) / u_modLen - 1.f; // in [-1, 1]
float mY = mod(shadeValue2.y, 2.0 * u_modLen) / u_modLen - 1.f;
float minD = min(min(abs(mX), 1.0 - abs(mX)), min(abs(mY), 1.0 - abs(mY))) * 2.; // rect distace from flipping sign in [0,1]
float width = 0.05;
float slopeWidthPix = 5.;
vec2 fw = fwidth(shadeValue2);
float scale = max(fw.x, fw.y);
float pWidth = slopeWidthPix * scale;
float s = smoothstep(width, width + pWidth, minD);
vec3 albedoColor = mix(u_gridLineColor, u_gridBackgroundColor, s);
)"}
},
/* uniforms */ {
{"u_modLen", RenderDataType::Float},
{"u_gridLineColor", RenderDataType::Vector3Float},
{"u_gridBackgroundColor", RenderDataType::Vector3Float},
},
/* attributes */ {},
/* textures */ {}
);
const ShaderReplacementRule SHADE_CHECKER_VALUE2 (
/* rule name */ "SHADE_CHECKER_VALUE2",
{ /* replacement sources */
{"FRAG_DECLARATIONS", R"(
uniform float u_modLen;
uniform vec3 u_color1;
uniform vec3 u_color2;
)"},
{"GENERATE_SHADE_COLOR", R"(
// NOTE checker math shared with other shaders
float mX = mod(shadeValue2.x, 2.0 * u_modLen) / u_modLen - 1.f; // in [-1, 1]
float mY = mod(shadeValue2.y, 2.0 * u_modLen) / u_modLen - 1.f;
float minD = min( min(abs(mX), 1.0 - abs(mX)), min(abs(mY), 1.0 - abs(mY))) * 2.; // rect distace from flipping sign in [0,1]
float p = 6;
float minDSmooth = pow(minD, 1. / p);
// TODO do some clever screen space derivative thing to prevent aliasing
float v = (mX * mY); // in [-1, 1], color switches at 0
float adjV = sign(v) * minDSmooth;
float s = smoothstep(-1.f, 1.f, adjV);
vec3 albedoColor = mix(u_color1, u_color2, s);
)"}
},
/* uniforms */ {
{"u_modLen", RenderDataType::Float},
{"u_color1", RenderDataType::Vector3Float},
{"u_color2", RenderDataType::Vector3Float},
},
/* attributes */ {},
/* textures */ {}
);
const ShaderReplacementRule SHADE_CHECKER_CATEGORY(
/* rule name */ "SHADE_CHECKER_CATEGORY",
{ /* replacement sources */
{"FRAG_DECLARATIONS", R"(
uniform float u_modLen;
uniform float u_modDarkness;
uniform sampler1D t_colormap;
float intToDistinctReal(float start, int index);
)"},
{"GENERATE_SHADE_COLOR", R"(
// sample the categorical color
float catVal = intToDistinctReal(0., int(shadeValue));
float scaleFac = 1.2f; // pump up the brightness a bit, so the modDarkness doesn't make it too dark
vec3 catColor = scaleFac * texture(t_colormap, catVal).rgb;
vec3 catColorDark = catColor * u_modDarkness;
// NOTE checker math shared with other shaders
float mX = mod(shadeValue2.x, 2.0 * u_modLen) / u_modLen - 1.f; // in [-1, 1]
float mY = mod(shadeValue2.y, 2.0 * u_modLen) / u_modLen - 1.f;
float minD = min( min(abs(mX), 1.0 - abs(mX)), min(abs(mY), 1.0 - abs(mY))) * 2.; // rect distace from flipping sign in [0,1]
float p = 6;
float minDSmooth = pow(minD, 1. / p);
// TODO do some clever screen space derivative thing to prevent aliasing
float v = (mX * mY); // in [-1, 1], color switches at 0
float adjV = sign(v) * minDSmooth;
float s = smoothstep(-1.f, 1.f, adjV);
vec3 albedoColor = mix(catColor, catColorDark, s);
)"}
},
/* uniforms */ {
{"u_modLen", RenderDataType::Float},
{"u_modDarkness", RenderDataType::Float},
},
/* attributes */ {},
/* textures */ {
{"t_colormap", 1}
}
);
// input vec2 shadeValue2
// output: float shadeValue
const ShaderReplacementRule SHADEVALUE_MAG_VALUE2(
/* rule name */ "SHADEVALUE_MAG_VALUE2",
{ /* replacement sources */
{"GENERATE_SHADE_COLOR", "float shadeValue = length(shadeValue2);"}
},
/* uniforms */ {},
/* attributes */ {},
/* textures */ {}
);
const ShaderReplacementRule ISOLINE_STRIPE_VALUECOLOR (
/* rule name */ "ISOLINE_STRIPE_VALUECOLOR",
{ /* replacement sources */
{"FRAG_DECLARATIONS", R"(
uniform float u_modLen;
uniform float u_modDarkness;
)"},
{"GENERATE_SHADE_COLOR", R"(
float modVal = mod(shadeValue, 2.0 * u_modLen);
if(modVal > u_modLen) {
albedoColor *= u_modDarkness;
}
)"}
},
/* uniforms */ {
{"u_modLen", RenderDataType::Float},
{"u_modDarkness", RenderDataType::Float},
},
/* attributes */ {},
/* textures */ {}
);
const ShaderReplacementRule CHECKER_VALUE2COLOR (
/* rule name */ "CHECKER_VALUE2COLOR",
{ /* replacement sources */
{"FRAG_DECLARATIONS", R"(
uniform float u_modLen;
uniform float u_modDarkness;
)"},
{"GENERATE_SHADE_COLOR", R"(
vec3 albedoColorDark = albedoColor * u_modDarkness;
float mX = mod(shadeValue2.x, 2.0 * u_modLen) / u_modLen - 1.f; // in [-1, 1]
float mY = mod(shadeValue2.y, 2.0 * u_modLen) / u_modLen - 1.f;
float minD = min( min(abs(mX), 1.0 - abs(mX)), min(abs(mY), 1.0 - abs(mY))) * 2.; // rect distace from flipping sign in [0,1]
float p = 6;
float minDSmooth = pow(minD, 1. / p);
float v = (mX * mY); // in [-1, 1], color switches at 0
float adjV = sign(v) * minDSmooth;
float s = smoothstep(-1.f, 1.f, adjV);
albedoColor = mix(albedoColor, albedoColorDark, s);
)"}
},
/* uniforms */ {
{"u_modLen", RenderDataType::Float},
{"u_modDarkness", RenderDataType::Float},
},
/* attributes */ {},
/* textures */ {}
);
const ShaderReplacementRule GENERATE_VIEW_POS (
/* rule name */ "GENERATE_VIEW_POS",
{ /* replacement sources */
{"FRAG_DECLARATIONS", R"(
uniform mat4 u_invProjMatrix_viewPos; // weird names are to unique-ify because we have multiple....
uniform vec4 u_viewport_viewPos;
vec3 fragmentViewPosition(vec4 viewport, vec2 depthRange, mat4 invProjMat, vec4 fragCoord);
)"},
{"GLOBAL_FRAGMENT_FILTER_PREP", R"(
vec2 depthRange_viewPos = vec2(gl_DepthRange.near, gl_DepthRange.far);
vec4 fragCoord_viewPos = gl_FragCoord;
fragCoord_viewPos.z = depth;
vec3 viewPos = fragmentViewPosition(u_viewport_viewPos, depthRange_viewPos, u_invProjMatrix_viewPos, fragCoord_viewPos);
)"}
},
/* uniforms */ {
{"u_invProjMatrix_viewPos", RenderDataType::Matrix44Float},
{"u_viewport_viewPos", RenderDataType::Vector4Float},
},
/* attributes */ {},
/* textures */ {}
);
const ShaderReplacementRule PROJ_AND_INV_PROJ_MAT (
/* rule name */ "PROJ_AND_INV_PROJ_MAT",
{ /* replacement sources */
{"FRAG_DECLARATIONS", R"(
uniform mat4 u_invProjMatrix;
uniform vec4 u_viewport;
)"}
},
/* uniforms */ {
{"u_invProjMatrix", RenderDataType::Matrix44Float},
{"u_viewport", RenderDataType::Vector4Float},
},
/* attributes */ {},
/* textures */ {}
);
const ShaderReplacementRule COMPUTE_SHADE_NORMAL_FROM_POSITION (
/* rule name */ "COMPUTE_SHADE_NORMAL_FROM_POSITION",
{ /* replacement sources */
{"FRAG_DECLARATIONS", R"(
vec3 fragmentViewPosition(vec4 viewport, vec2 depthRange, mat4 invProjMat, vec4 fragCoord);
)"},
{"GENERATE_SHADE_VALUE", R"(
vec2 depthRange_fornormal = vec2(gl_DepthRange.near, gl_DepthRange.far);
vec3 viewPos_fornormal = fragmentViewPosition(u_viewport, depthRange_fornormal, u_invProjMatrix, gl_FragCoord);
shadeNormal = normalize(cross(dFdx(viewPos_fornormal),dFdy(viewPos_fornormal)));
)"}
},
/* uniforms */ {},
/* attributes */ {},
/* textures */ {}
);
const ShaderReplacementRule PREMULTIPLY_LIT_COLOR(
/* rule name */ "PREMULTIPLY_LIT_COLOR",
{ /* replacement sources */
{"PERTURB_LIT_COLOR", R"(
litColor *= alphaOut; // premultiplied alpha
)"}
},
/* uniforms */ {},
/* attributes */ {},
/* textures */ {}
);
// TODO delete me
const ShaderReplacementRule CULL_POS_FROM_VIEW (
/* rule name */ "CULL_POS_FROM_VIEW",
{ /* replacement sources */
{"GLOBAL_FRAGMENT_FILTER_PREP", R"(
vec3 cullPos = viewPos;
)"},
},
/* uniforms */ {
},
/* attributes */ {},
/* textures */ {}
);
ShaderReplacementRule generateSlicePlaneRule(std::string uniquePostfix) {
std::string centerUniformName = "u_slicePlaneCenter_" + uniquePostfix;
std::string normalUniformName = "u_slicePlaneNormal_" + uniquePostfix;
// This takes what is otherwise a simple rule, and substitues uniquely named uniforms so that we can have multiple slice planes
ShaderReplacementRule slicePlaneRule (
/* rule name */ "SLICE_PLANE_CULL_" + uniquePostfix,
{ /* replacement sources */
{"FRAG_DECLARATIONS", "uniform vec3 " + centerUniformName + "; uniform vec3 " + normalUniformName + ";"},
{"GLOBAL_FRAGMENT_FILTER",
"if(dot(cullPos, " + normalUniformName + ") < dot( " + centerUniformName + " , " + normalUniformName + ")) { discard; }"}
},
/* uniforms */ {
{centerUniformName, RenderDataType::Vector3Float},
{normalUniformName, RenderDataType::Vector3Float},
},
/* attributes */ {},
/* textures */ {}
);
return slicePlaneRule;
}
ShaderReplacementRule generateVolumeGridSlicePlaneRule(std::string uniquePostfix) {
std::string centerUniformName = "u_slicePlaneCenter_" + uniquePostfix;
std::string normalUniformName = "u_slicePlaneNormal_" + uniquePostfix;
// This takes what is otherwise a simple rule, and substitues uniquely named uniforms so that we can have multiple slice planes
ShaderReplacementRule slicePlaneRule (
/* rule name */ "SLICE_PLANE_VOLUMEGRID_CULL_" + uniquePostfix,
{ /* replacement sources */
// skip the frag declarations, we will already have them from the other rule
// {"FRAG_DECLARATIONS", "uniform vec3 " + centerUniformName + "; uniform vec3 " + normalUniformName + ";"},
{"GRID_PLANE_NEIGHBOR_FILTER",
"if(dot(neighCullPos, " + normalUniformName + ") < dot( " + centerUniformName + " , " + normalUniformName + ")) { neighIsVisible = false; }"
}
},
/* uniforms */ {
{centerUniformName, RenderDataType::Vector3Float},
{normalUniformName, RenderDataType::Vector3Float},
},
/* attributes */ {},
/* textures */ {}
);
return slicePlaneRule;
}
// clang-format on
} // namespace backend_openGL3
} // namespace render
} // namespace polyscope