Skip to content

Commit

Permalink
redo shader:
Browse files Browse the repository at this point in the history
  • Loading branch information
mobile-bungalow committed Oct 7, 2024
1 parent 91c6d17 commit 66cd4be
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 58 deletions.
79 changes: 37 additions & 42 deletions example_shaders/ascii.glsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#version 450
#pragma tweak_shader(version="1.0")

// Original Shader by movAX13h, https://www.shadertoy.com/user/movAX13h
// Used unattributed by VidVox. modified by mobile bungalow.
// Used unattributed by VidVox.

#pragma utility_block(ShaderInputs)
layout(push_constant) uniform ShaderInputs {
Expand All @@ -23,67 +22,63 @@ layout(set = 0, binding = 1) uniform sampler default_sampler;
layout(set = 0, binding = 2) uniform texture2D input_image;

#pragma input(float, name=gamma, default=0.5, min=0.0, max=1.0)
#pragma input(float, name=size, default=0.5, min=0.0, max=10.0)
#pragma input(float, name=size, default=1.0, min=0.0, max=10.0)
#pragma input(float, name=tint, default=1.0, min=0.0, max=1.0)
#pragma input(bool, name=alphaMode, default=false)
#pragma input(color, name=tintColor, default=[0.0, 1.0, 0.0, 1.0])
layout(set = 0, binding = 3) uniform custom_inputs {
float gamma;
float size;
float tint;
int alphaMode;
vec4 tintColor;
};

uint getNthBit(uvec4 vector, int n) {
int componentIndex = n / 32; // Each ivec4 component has 32 bits
int bitIndex = n - componentIndex * 32;

return (vector[componentIndex] >> bitIndex) & 1;
int getNthBit(uint value, int n) {
return int((value >> uint(n)) & uint(1));
}

const float char_width = 8.0;
const float char_height = 16.0;
float character(uvec4 n, vec2 p)
float character(uint j, vec2 p) // some compilers have the word "char" reserved
{
if (getNthBit(n, int(p.x * char_width) + int(p.y * char_height) * int(char_width)) == 1) return 1.0;
return 0.0;
p = floor(p * vec2(-4.0, 4.0) + 4.0);
if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y)
{
int index = int(p.x + (p.y * 5.0));
int i = getNthBit(j, index);
return float(i);
} else {
return 0.0;
}
}

void main() {
float _size = floor(size + 1.0);
vec2 size_dim = vec2(_size * char_width, _size * char_height);
float _size = size * 36.0 + 8.0;
//vec2 uv = gl_FragCoord.xy;

vec2 uv = gl_FragCoord.xy;
vec2 grid_v = floor(uv / size_dim) * size_dim / resolution.xy;
vec2 grid_v_right = floor((uv + vec2(size_dim.x / 2.0, 0)) / size_dim) * size_dim / resolution.xy;
vec2 grid_v_down = floor((uv + vec2(0, size_dim.y)) / size_dim) * size_dim / resolution.xy;
vec4 inputColor = texture(sampler2D(input_image, default_sampler), grid_v);
vec4 inputColor_r = texture(sampler2D(input_image, default_sampler), grid_v_right);
vec4 inputColor_d = texture(sampler2D(input_image, default_sampler), grid_v_down);
vec4 inputColor = texture(sampler2D(input_image, default_sampler), (floor(uv / _size) * _size / resolution.xy));
vec3 col = inputColor.rgb;
float gray = (col.r + col.g + col.b) / 3.0;
gray = pow(gray, gamma);
col = mix(tintColor.rgb, col.rgb, 1.0 - tint);

uvec4 n = uvec4(0);
vec2 p = mod(uv, size_dim) / size_dim;
if (length(inputColor - inputColor_d) > 0.5) {
n = uvec4(4294967295, 16777215, 0, 0);
if (character(n, p) == 1) {
col = min(inputColor, inputColor_r).rgb;
}
}
if (length(inputColor - inputColor_r) > 0.5) {
n = uvec4(252645135);
if (character(n, p) == 1) {
col = min(inputColor, inputColor_r).rgb;
}
} else {
if (gray > 0.3) n = uvec4(289673540); // light grid
if (gray > 0.4) n = uvec4(1437226410); // darker grid
if (gray > 0.7) n = uvec4(0xFFFFFFFF); // no hatching
if (character(n, p) == 1) {
col = inputColor.rgb - 0.2;
}
int l = 0;
uint j = 65536;
if (gray > 0.2) j = 65600; // :
if (gray > 0.3) j = 332772; // *
if (gray > 0.4) j = 15255086; // o
if (gray > 0.5) j = 23385164; // &
if (gray > 0.6) j = 15252014; // 8
if (gray > 0.7) j = 13199452; // @
if (gray > 0.8) j = 11512810; // #

vec2 p = mod(uv / (_size / 2.0), 2.0) - vec2(1.0);
col = col * character(j, p);
float alpha = mix(tintColor.a * inputColor.a, inputColor.a, 1.0 - tint);
if (alphaMode != 0) {
alpha = (col.r + col.g + col.b) / 3.0;
alpha = (alpha > 0.01) ? tintColor.a : alpha;
}

out_color = vec4(col, 1.0);
out_color = vec4(col, alpha);
}
6 changes: 4 additions & 2 deletions src/preprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ impl VisitorMut for FormatSwizzler {
}
}

pub fn convert_output_to_ae_format(module: &str) -> Result<String, ()> {
pub fn convert_output_to_ae_format(module: &str) -> Result<String, String> {
let mut swiz = FormatSwizzler::new();
let mut expr = glsl::syntax::TranslationUnit::parse(module).unwrap();
let mut expr = glsl::syntax::TranslationUnit::parse(module)
.map_err(|e| format!("failed to mangle: {e}"))?;

expr.visit_mut(&mut swiz);

let mut output = String::new();
Expand Down
9 changes: 7 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,13 @@ impl LocalInit {
let mut build_error = None;

let ctx = src
.and_then(|src| preprocessing::convert_output_to_ae_format(&src).ok())
.ok_or(|src| tweak_shader::RenderContext::new(src, fmt, device, queue));
.ok_or("No Source in initialization".to_owned())
.and_then(|src| preprocessing::convert_output_to_ae_format(&src))
.and_then(|src| {
tweak_shader::RenderContext::new(src, fmt, device, queue)
.map_err(|e| format!("{e}"))
});

let ctx = match ctx {
Ok(okay) => okay,
Err(e) => {
Expand Down
14 changes: 2 additions & 12 deletions src/u15_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,9 @@ impl U16ConversionContext {
);

// Update resolutions
self.fp_to_u16_ctx
.get_input_mut("height")
.unwrap()
.as_float()
.unwrap()
.current = height as f32;
*self.fp_to_u16_ctx.get_input_as("height").unwrap() = height as f32;

self.fp_to_u16_ctx
.get_input_mut("width")
.unwrap()
.as_float()
.unwrap()
.current = width as f32;
*self.fp_to_u16_ctx.get_input_as("width").unwrap() = width as f32;

self.fp_to_u16_ctx
.load_shared_texture(target_texture, "input_image");
Expand Down

0 comments on commit 66cd4be

Please sign in to comment.