butterw / bshaders Goto Github PK
View Code? Open in Web Editor NEWVideo playback Effects/Filters (DirectX .hlsl pixel shaders, mpv .hook)
Video playback Effects/Filters (DirectX .hlsl pixel shaders, mpv .hook)
Hi,
I'm looking for a shader that desaturates only the dark colors (0-40 rgb value) by amount x (so it's customizable)
Could you help me out with that?
Thank you
I get a blue screen and my output log is
barrett@pissbox ~/Downloads [4]> mpv jennifer-kalenberg-SLRCI45JlZU-unsplash.jpg --glsl-shader=./grain.glsl
(+) Video --vid=1 (mjpeg 4480x6720 1.000fps)
VO: [gpu] 4480x6720 yuv420p
[vo/gpu/opengl] fragment shader source:
[vo/gpu/opengl] [ 1] #version 440
[vo/gpu/opengl] [ 2] #define tex1D texture
[vo/gpu/opengl] [ 3] #define tex3D texture
[vo/gpu/opengl] [ 4] #define LUT_POS(x, lut_size) mix(0.5 / (lut_size), 1.0 - 0.5 / (lut_size), (x))
[vo/gpu/opengl] [ 5] out vec4 out_color;
[vo/gpu/opengl] [ 6] in vec2 texcoord0;
[vo/gpu/opengl] [ 7] layout(std140, binding=0) uniform UBO {
[vo/gpu/opengl] [ 8] layout(offset=0) vec2 input_size;
[vo/gpu/opengl] [ 9] layout(offset=8) vec2 target_size;
[vo/gpu/opengl] [ 10] layout(offset=16) vec2 tex_offset;
[vo/gpu/opengl] [ 11] layout(offset=24) vec2 texture_size0;
[vo/gpu/opengl] [ 12] layout(offset=32) mat2 texture_rot0;
[vo/gpu/opengl] [ 13] layout(offset=64) vec2 texture_off0;
[vo/gpu/opengl] [ 14] layout(offset=72) vec2 pixel_size0;
[vo/gpu/opengl] [ 15] };
[vo/gpu/opengl] [ 16] uniform float random;
[vo/gpu/opengl] [ 17] uniform int frame;
[vo/gpu/opengl] [ 18] uniform sampler2D texture0;
[vo/gpu/opengl] [ 19] #define HOOKED_raw texture0
[vo/gpu/opengl] [ 20] #define HOOKED_pos texcoord0
[vo/gpu/opengl] [ 21] #define HOOKED_size texture_size0
[vo/gpu/opengl] [ 22] #define HOOKED_rot texture_rot0
[vo/gpu/opengl] [ 23] #define HOOKED_off texture_off0
[vo/gpu/opengl] [ 24] #define HOOKED_pt pixel_size0
[vo/gpu/opengl] [ 25] #define HOOKED_map texmap0
[vo/gpu/opengl] [ 26] #define HOOKED_mul 1.000000
[vo/gpu/opengl] [ 27] #define HOOKED_tex(pos) (HOOKED_mul * vec4(texture(HOOKED_raw, pos)).rgba)
[vo/gpu/opengl] [ 28] #define HOOKED_gather(pos, c) (HOOKED_mul * vec4(textureGather(HOOKED_raw, pos, c)))
[vo/gpu/opengl] [ 29] #define HOOKED_texOff(off) HOOKED_tex(HOOKED_pos + HOOKED_pt * vec2(off))
[vo/gpu/opengl] [ 30] #define OUTPUT_raw texture0
[vo/gpu/opengl] [ 31] #define OUTPUT_pos texcoord0
[vo/gpu/opengl] [ 32] #define OUTPUT_size texture_size0
[vo/gpu/opengl] [ 33] #define OUTPUT_rot texture_rot0
[vo/gpu/opengl] [ 34] #define OUTPUT_off texture_off0
[vo/gpu/opengl] [ 35] #define OUTPUT_pt pixel_size0
[vo/gpu/opengl] [ 36] #define OUTPUT_map texmap0
[vo/gpu/opengl] [ 37] #define OUTPUT_mul 1.000000
[vo/gpu/opengl] [ 38] #define OUTPUT_tex(pos) (OUTPUT_mul * vec4(texture(OUTPUT_raw, pos)).rgba)
[vo/gpu/opengl] [ 39] #define OUTPUT_gather(pos, c) (OUTPUT_mul * vec4(textureGather(OUTPUT_raw, pos, c)))
[vo/gpu/opengl] [ 40] #define OUTPUT_texOff(off) OUTPUT_tex(OUTPUT_pos + OUTPUT_pt * vec2(off))
[vo/gpu/opengl] [ 41]
[vo/gpu/opengl] [ 42] #define G 50 // default is 16
[vo/gpu/opengl] [ 43] #define Curve01 0.2 //default is 0.25
[vo/gpu/opengl] [ 44] #define Show_Grain 0
[vo/gpu/opengl] [ 45]
[vo/gpu/opengl] [ 46] /* --- grain (mpv glsl .hook) --- /
[vo/gpu/opengl] [ 47] / v1.01 (2023-11) released by butterw under GPLv3
[vo/gpu/opengl] [ 48] Gaussian noise generation is from "semi-random grayscale noise.hlsl", (C) 2011 Jan-Willem Krans (janwillem32 hotmail.com), license: GPLv2.
[vo/gpu/opengl] [ 49] (1 texture, 42 arithmetic) >> (1 texture, 38 arithmetic with Curve01:1).
[vo/gpu/opengl] [ 50]
[vo/gpu/opengl] [ 51] Filmgrain is a physical characteric of film media, digital camera sensors don't have grain, only noise. A small amount of grain paradoxically increases perceived sharpness and quality (ex: sources that have been denoised prior to encoding). But adding too much just degrades the signal-over-noise ratio. It is recommended to adjust the Strength down so the grain is barely visible.
[vo/gpu/opengl] [ 52] Grain shaders typically generate a dynamic luma noise pattern and blend it with the original image. Grain is calculated for each xy pixel of the image using a pseudo-random hash function with time and counter variables used as seed, leading to a dynamic grain pattern updated every frame. Additive grain blending does not significantly alter the original brightness and contrast of the image.
[vo/gpu/opengl] [ 53] This shader allows to apply less grain to dark and bright areas vs midtones by multiplying grain with a shaping function based on luma. The shaping function is a tunable parabola centered at midgray. The values in x:0 and x:1.0 are set by parameter Curve01.
[vo/gpu/opengl] [ 54] out = c0.rgb + noiseStrengthgrain
[vo/gpu/opengl] [ 55] with c0 and out pixel.rgb values in [0, 1.0].
[vo/gpu/opengl] [ 56] and noiseStrength = 0.01G.
[vo/gpu/opengl] [ 57]
[vo/gpu/opengl] [ 58] grain = gShape(luma, Curve01) grain
[vo/gpu/opengl] [ 59] with grain, positive or negative grayscale value in [-0.125, 0.125], random variable with gaussian distribution.
[vo/gpu/opengl] [ 60] and gShape in [0, 1.0].
[vo/gpu/opengl] [ 61]
[vo/gpu/opengl] [ 62] ## Parameters ##
[vo/gpu/opengl] [ 63] - G, Strength percentage, ex: 16. [0 to 35.0], 0: no effect.
[vo/gpu/opengl] [ 64] - Curve01, curve shaping parameter, ex:0.25, typ [0 to 1.0], 1.0: uniform grain , 0: parabola(luma).
[vo/gpu/opengl] [ 65] gshape(luma:0) = gshape(luma:1.0) = Curve
[vo/gpu/opengl] [ 66] - Show_Grain, 0: default, [0 or 1] 1: display only the noise pattern.
[vo/gpu/opengl] [ 67]
[vo/gpu/opengl] [ 68] Grain should only be added after any sharpening operations are performed (sharpening generated grain is undesirable).
[vo/gpu/opengl] [ 69] When upscaling with no post-resize sharpening, grain can be added pre-resize with some advantages:
[vo/gpu/opengl] [ 70] - less pixels to process: faster.
[vo/gpu/opengl] [ 71] - any resizing (ex bicubic A=-0.60, scaling ratio=1.33x) smooths the shape of the distribution and results in a clean gaussian.
[vo/gpu/opengl] [ 72] - the grain size is scaled. This may achieve a more pleasing result for scaling ratios up to 2x, as single pixels are fairly small at 1080p or higher.
[vo/gpu/opengl] [ 73] Possible HOOK: MAIN, OUTPUT. use grain_luma.glsl for LUMA hook.
[vo/gpu/opengl] [ 74]
[vo/gpu/opengl] [ 75] /
[vo/gpu/opengl] [ 76] #define noiseStrength G0.01 //scaling factor.
[vo/gpu/opengl] [ 77] #define n 40 //number of randomization iterations, ex:4, ! a lower RunCount will cause patterned noise. //
[vo/gpu/opengl] [ 78] #define PI 3.14159265 //acos(-1)
[vo/gpu/opengl] [ 79] const vec4 RandomFactors = { PIPIPIPI, exp(5), pow(13, 0.5PI), sqrt(1997) };
[vo/gpu/opengl] [ 80] #define rnd(u) r_in.u = fract( dot(r_in, RandomFactors) );
[vo/gpu/opengl] [ 81] #define CoefLuma vec4(0.24, 0.69, 0.07, 0) //sRGB, HDTV, rec709
[vo/gpu/opengl] [ 82] #define pow2(u) (u)(u)
[vo/gpu/opengl] [ 83]
[vo/gpu/opengl] [ 84] float gshape(float x) {
[vo/gpu/opengl] [ 85] // C + (1-C)(1 -(2x-1)^2) = 1 + (C-1)(2x-1)^2 //for negative C, use: max(gshape(luma), 0)
[vo/gpu/opengl] [ 86] return mix(1 -pow2(2x -1), 1, Curve01);
[vo/gpu/opengl] [ 87] }
[vo/gpu/opengl] [ 88]
[vo/gpu/opengl] [ 89] #define counter frame
[vo/gpu/opengl] [ 90] #define clock frame+random
[vo/gpu/opengl] [ 91] vec4 hook() {
[vo/gpu/opengl] [ 92] vec2 seed = vec2(counter, clock) 1.0/65536 + exp(5);
[vo/gpu/opengl] [ 93] vec4 r_in = vec4(HOOKED_pos.xy, seed);
[vo/gpu/opengl] [ 94] for(int i=0; i< n; i++) { rnd(x) rnd(y) rnd(z) rnd(w) }; // randomize
[vo/gpu/opengl] [ 95] float grain;
[vo/gpu/opengl] [ 96] #if Show_Grain == 1
[vo/gpu/opengl] [ 97] grain = 0.25 -dot(r_in, vec4(0.125)); //in [-0.125, 0.125]
[vo/gpu/opengl] [ 98] return 0.5 + grain;
[vo/gpu/opengl] [ 99] #endif
[vo/gpu/opengl] [100] grain = 0.2noiseStrength -dot(r_in, vec4(0.2noiseStrength)); // noiseStrength*grain
[vo/gpu/opengl] [101] vec4 c0 = HOOKED_texOff(0);
[vo/gpu/opengl] [102] float luma = dot(c0, CoefLuma);
[vo/gpu/opengl] [103] grain = gshape(luma)*grain;
[vo/gpu/opengl] [104] c0.rgb+= grain;
[vo/gpu/opengl] [105] return c0;
[vo/gpu/opengl] [106] }void main() {
[vo/gpu/opengl] [107] vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
[vo/gpu/opengl] [108] color = hook();
[vo/gpu/opengl] [109] color.a = 1.000000;
[vo/gpu/opengl] [110] out_color = color;
[vo/gpu/opengl] [111] }
[vo/gpu/opengl] fragment shader compile log (status=0):
[vo/gpu/opengl] 0(86) : error C1101: ambiguous overloaded function reference "mix(float, int, float)"
[vo/gpu/opengl] (0) : gp5 float64_t mix(float64_t, float64_t, float64_t)
[vo/gpu/opengl] (0) : float mix(float, float, float)
[vo/gpu/opengl]
[vo/gpu/opengl] shader link log (status=0): Fragment info
[vo/gpu/opengl] -------------
[vo/gpu/opengl] 0(86) : error C1101: ambiguous overloaded function reference "mix(float, int, float)"
[vo/gpu/opengl] (0) : gp5 float64_t mix(float64_t, float64_t, float64_t)
[vo/gpu/opengl] (0) : float mix(float, float, float)
[vo/gpu/opengl] (0) : error C2003: incompatible options for link
[vo/gpu/opengl]
V: 00:00:00 / 00:00:00 (0%)
Exiting... (End of file)
barrett@pissbox ~/Downloads>
please close
Hi. Do you know why this works on SDR but isn't on HDR? The original shader worked on both and only 3 lines of code have changed... I marked them at the end
//!PARAM vibr
//!DESC Saturates/deSaturates
//!TYPE float
//!MINIMUM -1
-0.5
//!HOOK MAIN
//!BIND HOOKED
//!DESC Vibrance
#define CoefLuma vec4(0.2126, 0.7152, 0.0722, 0) //sRGB, HDTV
float Thresh = 200.0/255.0;
float w = 10.0/255.0;
vec4 hook() {
vec4 c0 = HOOKED_texOff(0);
float lum = (c0.r + c0.g + c0.b)/3.0;
float colorSat = max(max(c0.r, c0.g), c0.b) - min(min(c0.r, c0.g), c0.b);
vec3 sat = mix(vec3(dot(c0, CoefLuma)), c0.rgb, 1+vibr - colorSat*abs(vibr));
float delta = smoothstep(Thresh-w, Thresh+w, lum);
1 sat = mix(sat, c0.rgb, delta);
2 delta = smoothstep(0.8, 0.9, colorSat);
3 c0.rgb = mix(c0.rgb, sat, delta);
return c0;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.