Skip to content

Commit

Permalink
Some code refactoring and added DrawShadows
Browse files Browse the repository at this point in the history
  • Loading branch information
Srlion committed Feb 6, 2025
1 parent 1f7767b commit cdad46a
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 96 deletions.
13 changes: 9 additions & 4 deletions compile_shader_list.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
rndx_r_shaders1_psxx.hlsl -v-20b -v-30
rndx_bh_shaders1_psxx.hlsl -v-30
rndx_bv_shaders1_psxx.hlsl -v-30
rndx_vertex_shaders1_vsxx.hlsl -v-30
rndx_vertex_1_vsxx.hlsl -v-30

rndx_rounded_1_psxx.hlsl -v-20b -v-30
rndx_rounded_bh_1_psxx.hlsl -v-30
rndx_rounded_bv_1_psxx.hlsl -v-30

rndx_shadows_1_psxx.hlsl -v-20b
rndx_shadows_bh_1_psxx.hlsl -v-30
rndx_shadows_bv_1_psxx.hlsl -v-30
37 changes: 27 additions & 10 deletions src/rndx_bh_shaders1_psxx.hlsl → src/blur.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,49 @@
// Copyright © 2015 Inigo Quilez
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// https://www.shadertoy.com/view/Xd33Rf

#include "common_rounded.hlsl"

static const float2 TEXTURE_DIMENSIONS = float2(1920, 1080);

static const float w[8] = {0.026109, 0.034202, 0.043219, 0.052683, 0.061948, 0.070266, 0.076883, 0.081149};
static const float o[8] = {15.5, 13.5, 11.5, 9.5, 7.5, 5.5, 3.5, 1.5};

float4 main(PS_INPUT i) : COLOR
float3 horizontal_blur(float2 uv)
{
float2 texture_dimensions = 1.0 / TexBaseSize;

float3 blr = float3(0.0, 0.0, 0.0);

[unroll] for(int j=0; j<8; j++) {
blr += w[j] * tex2D(TexBase, (uv + float2(-o[j], 0.0)) / texture_dimensions).rgb;
}

blr += 0.041312 * tex2D(TexBase, (uv + float2(0.0, 0.0)) / texture_dimensions).rgb;

[unroll] for(j=7; j>=0; j--) {
blr += w[j] * tex2D(TexBase, (uv + float2(o[j], 0.0)) / texture_dimensions).rgb;
}

blr /= 0.93423; // Normalize

return blr;
}

float3 vertical_blur(float2 uv)
{
float rounded_alpha = calculate_rounded_alpha(i);
float2 texture_dimensions = 1.0 / TexBaseSize;

float2 uv = i.pos.xy;
float3 blr = float3(0.0, 0.0, 0.0);

[unroll] for(int j=0; j<8; j++) {
blr += w[j] * tex2D(TexBase, (uv + float2(-o[j], 0.0)) / TEXTURE_DIMENSIONS).rgb;
blr += w[j] * tex2D(TexBase, (uv + float2(0.0, -o[j])) / texture_dimensions).rgb;
}

blr += 0.041312 * tex2D(TexBase, (uv + float2(0.0, 0.0)) / TEXTURE_DIMENSIONS).rgb;
blr += 0.041312 * tex2D(TexBase, (uv + float2(0.0, 0.0)) / texture_dimensions).rgb;

[unroll] for(j=7; j>=0; j--) {
blr += w[j] * tex2D(TexBase, (uv + float2(o[j], 0.0)) / TEXTURE_DIMENSIONS).rgb;
blr += w[j] * tex2D(TexBase, (uv + float2(0.0, o[j])) / texture_dimensions).rgb;
}

blr /= 0.93423; // Normalize

return float4(blr, rounded_alpha);
return blr;
}
4 changes: 4 additions & 0 deletions src/common.hlsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// common data shared among all screenspace shaders
#ifndef __COMMON_HLSL__
#define __COMMON_HLSL__

// up to four textures available for sampling
sampler TexBase : register( s0 ); // $basetexture
Expand Down Expand Up @@ -53,3 +55,5 @@ struct PS_INPUT
// screenspace position
float2 pos : VPOS;
};

#endif // __COMMON_HLSL__
77 changes: 61 additions & 16 deletions src/common_rounded.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
// but was mainly using https://www.shadertoy.com/view/fsdyzB
// then some help came from Svetov/Jaffies (https://github.com/Jaffies)
// and some help from AI lol

#ifndef __COMMON_ROUNDED_HLSL__
#define __COMMON_ROUNDED_HLSL__

#include "common.hlsl"

#define RADIUS Constants0
#define SIZE Constants1.xy
#define POWER_PARAMETER Constants1.z
#define USE_TEXTURE Constants1.w
#define OUTLINE_THICKNESS Constants2.x
#define AA Constants2.y // Anti-aliasing smoothness (pixels)

float length_custom(float2 vec) {
float2 powered = pow(vec, POWER_PARAMETER);
return pow(dot(powered, 1.0), 1.0 / POWER_PARAMETER);
float2 powered = pow(vec, POWER_PARAMETER);
return pow(dot(powered, 1.0), 1.0 / POWER_PARAMETER);
}

float rounded_box_sdf(float2 p, float2 b, float4 r) {
Expand All @@ -28,30 +33,70 @@ float rounded_box_sdf(float2 p, float2 b, float4 r) {
return min(max(q.x, q.y), 0.0) + len - radius;
}

// Thanks to https://bohdon.com/docs/smooth-sdf-shape-edges/ awesome article
float uv_filter_width_bias(float dist, float2 uv) {
float2 dpos = fwidth(uv);
float fw = max(dpos.x, dpos.y);
float biasedSDF = dist + 0.5 * fw;
return saturate(1.0 - biasedSDF / fw);
}

// float blended_AA(float dist, float2 uv) {
// float linear_cov = uv_filter_width_bias(dist, uv);
// float smooth_cov = 1.0 - smoothstep(0.0, 1, dist + 1);
// return lerp(linear_cov, smooth_cov, 0.06);
// }
//

float calculate_rounded_alpha(PS_INPUT i) {
float2 screen_pos = i.uv.xy * SIZE;
float2 rect_half_size = SIZE * 0.5;

// Compute outer SDF distance (original radii)
float distance_outer = rounded_box_sdf(screen_pos - rect_half_size, rect_half_size, RADIUS);
float dist_outer = rounded_box_sdf(screen_pos - rect_half_size, rect_half_size, RADIUS);
float aa_outer = uv_filter_width_bias(dist_outer, screen_pos);
if (OUTLINE_THICKNESS < 0)
return aa_outer;

// Adjust inner radii and size for outline
float2 inner_half_size = max(rect_half_size - OUTLINE_THICKNESS, 0.0);
float4 inner_radius = max(RADIUS - OUTLINE_THICKNESS, 0.0);
float distance_inner = rounded_box_sdf(screen_pos - rect_half_size, inner_half_size, inner_radius);

// Determine if AA should be applied (when both dimensions are >= 3 pixels)
float aa_enabled = step(3.0, SIZE.x) * step(3.0, SIZE.y);
float effective_AA = AA * aa_enabled;
// Check if inner shape would collapse to a point, some filled shapes if using max outline thickness would have a small dot in the center
// if (all(inner_half_size <= 0.0) && all(inner_radius <= 0.0))
// return alpha_outer; // Fallback to filled when outline is too thick

// Offset SDF distances by AA
float adjusted_distance_outer = distance_outer + effective_AA;
float adjusted_distance_inner = distance_inner + effective_AA;
float dist_inner = rounded_box_sdf(screen_pos - rect_half_size, inner_half_size, inner_radius);
float aa_inner = uv_filter_width_bias(dist_inner, screen_pos);
return aa_outer * (1.0 - aa_inner);
}

// Compute alpha with smoothstep (like Shadertoy's edge softness)
float alpha_outer = 1.0 - smoothstep(0.0, effective_AA, adjusted_distance_outer);
float alpha_inner = 1.0 - smoothstep(0.0, effective_AA, adjusted_distance_inner);
float calculate_smooth_rounded_alpha(PS_INPUT i) {
float2 screen_pos = i.uv.xy * SIZE;
float2 rect_half_size = SIZE * 0.5;

// Combine results (outer alpha minus inner alpha)
return alpha_outer * (1.0 - alpha_inner);
// Compute outer SDF distance (original radii)
float dist_outer = rounded_box_sdf(screen_pos - rect_half_size, rect_half_size, RADIUS);

float aa_outer = 1.0 - smoothstep(0.0, AA, dist_outer + AA);
if (OUTLINE_THICKNESS < 0)
return aa_outer;

// Adjust inner radii and size for outline
float2 inner_half_size = max(rect_half_size - OUTLINE_THICKNESS, 0.0);
float4 inner_radius = max(RADIUS - OUTLINE_THICKNESS, 0.0);
float dist_inner = rounded_box_sdf(screen_pos - rect_half_size, inner_half_size, inner_radius);

float aa_inner = 1.0 - smoothstep(0.0, AA, dist_inner + AA);
return aa_outer * (1.0 - aa_inner);
}

/* filter width bias, we could use it later
float apply_AA(float dist) {
float fw = fwidth(dist);
dist += 0.5 * fw; // bias
float aa_linear = saturate(1.0 - dist / fw);
return aa_linear;
}
*/

#endif // __COMMON_ROUNDED_HLSL__
Loading

0 comments on commit cdad46a

Please sign in to comment.