From 30d52c46ba6b17876f0620ed1c1fee66034fe77b Mon Sep 17 00:00:00 2001 From: luboslenco Date: Sat, 29 Mar 2025 21:52:27 +0100 Subject: [PATCH] kong test --- armorpaint/project.js | 1 + armorpaint/shaders/dilate_pass.frag.glsl | 60 ---------- armorpaint/shaders/dilate_pass.kong | 114 +++++++++++++++++++ base/assets/Scene.arm | Bin 178444 -> 178161 bytes base/assets/shader_datas.arm | Bin 14713 -> 14897 bytes base/shaders/bloom_downsample_pass.frag.glsl | 45 -------- base/shaders/bloom_downsample_pass.kong | 74 ++++++++++++ base/shaders/bloom_upsample_pass.frag.glsl | 37 ------ base/shaders/bloom_upsample_pass.kong | 66 +++++++++++ base/shaders/compositor_pass.frag.glsl | 33 ------ base/shaders/compositor_pass.kong | 70 ++++++++++++ base/shaders/copy_mrt3_pass.frag.glsl | 14 --- base/shaders/copy_mrt3_pass.kong | 48 ++++++++ base/shaders/copy_pass.kong | 33 ++++++ base/shaders/cursor.frag.glsl | 15 --- base/shaders/cursor.kong | 114 +++++++++++++++++++ base/shaders/cursor.vert.glsl | 65 ----------- base/shaders/histogram_pass.frag.glsl | 17 --- base/shaders/histogram_pass.kong | 42 +++++++ base/shaders/mesh_poscol.frag.glsl | 9 -- base/shaders/mesh_poscol.kong | 39 +++++++ base/shaders/mesh_poscol.vert.glsl | 13 --- base/shaders/mesh_posnor.frag.glsl | 24 ---- base/shaders/mesh_posnor.kong | 89 +++++++++++++++ base/shaders/mesh_posnor.vert.glsl | 19 ---- base/shaders/mesh_posnortex.frag.glsl | 25 ---- base/shaders/mesh_posnortex.kong | 95 ++++++++++++++++ base/shaders/mesh_posnortex.vert.glsl | 23 ---- base/sources/ts/iron/uniforms.ts | 13 +++ base/sources/ts/pipes.ts | 60 +++++++++- 30 files changed, 854 insertions(+), 403 deletions(-) delete mode 100644 armorpaint/shaders/dilate_pass.frag.glsl create mode 100644 armorpaint/shaders/dilate_pass.kong delete mode 100644 base/shaders/bloom_downsample_pass.frag.glsl create mode 100644 base/shaders/bloom_downsample_pass.kong delete mode 100644 base/shaders/bloom_upsample_pass.frag.glsl create mode 100644 base/shaders/bloom_upsample_pass.kong delete mode 100644 base/shaders/compositor_pass.frag.glsl create mode 100644 base/shaders/compositor_pass.kong delete mode 100644 base/shaders/copy_mrt3_pass.frag.glsl create mode 100644 base/shaders/copy_mrt3_pass.kong create mode 100644 base/shaders/copy_pass.kong delete mode 100644 base/shaders/cursor.frag.glsl create mode 100644 base/shaders/cursor.kong delete mode 100644 base/shaders/cursor.vert.glsl delete mode 100644 base/shaders/histogram_pass.frag.glsl create mode 100644 base/shaders/histogram_pass.kong delete mode 100644 base/shaders/mesh_poscol.frag.glsl create mode 100644 base/shaders/mesh_poscol.kong delete mode 100644 base/shaders/mesh_poscol.vert.glsl delete mode 100644 base/shaders/mesh_posnor.frag.glsl create mode 100644 base/shaders/mesh_posnor.kong delete mode 100644 base/shaders/mesh_posnor.vert.glsl delete mode 100644 base/shaders/mesh_posnortex.frag.glsl create mode 100644 base/shaders/mesh_posnortex.kong delete mode 100644 base/shaders/mesh_posnortex.vert.glsl diff --git a/armorpaint/project.js b/armorpaint/project.js index 4d29b498..6573d074 100644 --- a/armorpaint/project.js +++ b/armorpaint/project.js @@ -10,6 +10,7 @@ project.add_project("../base"); project.add_tsfiles("sources"); project.add_tsfiles("sources/nodes"); project.add_shaders("shaders/*.glsl"); +project.add_shaders("shaders/*.kong"); project.add_assets("assets/*", { destination: "data/{name}" }); project.add_assets("assets/export_presets/*", { destination: "data/export_presets/{name}" }); project.add_assets("assets/keymap_presets/*", { destination: "data/keymap_presets/{name}" }); diff --git a/armorpaint/shaders/dilate_pass.frag.glsl b/armorpaint/shaders/dilate_pass.frag.glsl deleted file mode 100644 index 75792d2d..00000000 --- a/armorpaint/shaders/dilate_pass.frag.glsl +++ /dev/null @@ -1,60 +0,0 @@ -#version 450 - -uniform sampler2D tex; -uniform sampler2D texdilate; -uniform float dilate_radius; - -in vec2 tex_coord; -out vec4 frag_color; - -#ifdef ESSL -const vec2 offsets[8] = vec2[]( - vec2(-1.0, 0.0), vec2( 1.0, 0.0), vec2( 0.0, 1.0), vec2( 0.0, -1.0), - vec2(-1.0, 1.0), vec2( 1.0, 1.0), vec2( 1.0, -1.0), vec2(-1.0, -1.0) -); -#else -const vec2 offsets[8] = { - vec2(-1.0, 0.0), vec2( 1.0, 0.0), vec2( 0.0, 1.0), vec2( 0.0, -1.0), - vec2(-1.0, 1.0), vec2( 1.0, 1.0), vec2( 1.0, -1.0), vec2(-1.0, -1.0) -}; -#endif - -void main() { - // Based on https://shaderbits.com/blog/uv-dilation by Ryan Brucks - vec2 size = vec2(textureSize(tex, 0).xy); - vec2 texel_size = vec2(1.0, 1.0) / size; - float min_dist = 10000000.0; - ivec2 coord = ivec2(tex_coord * size); - float mask = texelFetch(texdilate, coord, 0).r; - if (mask > 0.0) discard; - - frag_color = texelFetch(tex, coord, 0); - int i = 0; - while (i < int(dilate_radius)) { - i++; - int j = 0; - while (j < 8) { - vec2 cur_uv = tex_coord + offsets[j] * texel_size * vec2(i, i); - coord = ivec2(cur_uv * size); - float offset_mask = texelFetch(texdilate, coord, 0).r; - vec4 offset_col = texelFetch(tex, coord, 0); - - if (offset_mask != 0.0) { - float cur_dist = length(tex_coord - cur_uv); - if (cur_dist < min_dist) { - vec2 project_uv = cur_uv + offsets[j] * texel_size * vec2(i, i) * vec2(0.25, 0.25); - vec4 direction = textureLod(tex, project_uv, 0.0); - min_dist = cur_dist; - if (direction.x != 0.0 || direction.y != 0.0 || direction.z != 0.0) { - vec4 delta = offset_col - direction; - frag_color = offset_col + delta * vec4(4.0, 4.0, 4.0, 4.0); - } - else { - frag_color = offset_col; - } - } - } - j++; - } - } -} diff --git a/armorpaint/shaders/dilate_pass.kong b/armorpaint/shaders/dilate_pass.kong new file mode 100644 index 00000000..e1e52769 --- /dev/null +++ b/armorpaint/shaders/dilate_pass.kong @@ -0,0 +1,114 @@ + +#[set(everything)] +const tex: tex2d; + +#[set(everything)] +const tex_sampler: sampler; + +#[set(everything)] +const texdilate: tex2d; + +#[set(everything)] +const texdilate_sampler: sampler; + +#[set(everything)] +const constants: { + dilate_radius: float; + tex_size: float2; +}; + +// const offsets: float2[8] = { +// float2(-1.0, 0.0), float2( 1.0, 0.0), float2( 0.0, 1.0), float2( 0.0, -1.0), +// float2(-1.0, 1.0), float2( 1.0, 1.0), float2( 1.0, -1.0), float2(-1.0, -1.0) +// }; + +struct vert_in { + pos: float2; +} + +struct vert_out { + pos: float4; + tex: float2; +} + +fun get_offset(i: int): float2 { + if (i == 0) { + return float2(-1.0, 0.0); + } + if (i == 1) { + return float2( 1.0, 0.0); + } + if (i == 2) { + return float2( 0.0, 1.0); + } + if (i == 3) { + return float2( 0.0, -1.0); + } + if (i == 4) { + return float2(-1.0, 1.0); + } + if (i == 5) { + return float2( 1.0, 1.0); + } + if (i == 6) { + return float2( 1.0, -1.0); + } + return float2(-1.0, -1.0); +} + +fun dilate_pass_vert(input: vert_in): vert_out { + var output: vert_out; + output.tex = input.pos.xy * 0.5 + 0.5; + output.tex.y = 1.0 - output.tex.y; + output.pos = float4(input.pos.xy, 0.0, 1.0); + return output; +} + +fun dilate_pass_frag(input: vert_out): float4 { + // Based on https://shaderbits.com/blog/uv-dilation by Ryan Brucks + var texel_size: float2 = float2(1.0, 1.0) / constants.tex_size; + var min_dist: float = 10000000.0; + var coord: uint2 = uint2(input.tex * constants.tex_size); + var mask: float = texdilate[coord].r; + if (mask > 0.0) { + discard; + } + + var color: float4 = tex[coord]; + var i: int = 0; + while (i < int(constants.dilate_radius)) { + i += 1; + var j: int = 0; + while (j < 8) { + var cur_uv: float2 = input.tex + get_offset(j) * texel_size * float2(i, i); + coord = uint2(cur_uv * constants.tex_size); + var offset_mask: float = texdilate[coord].r; + var offset_col: float4 = tex[coord]; + + if (offset_mask != 0.0) { + var cur_dist: float = length(input.tex - cur_uv); + if (cur_dist < min_dist) { + var project_uv: float2 = cur_uv + get_offset(j) * texel_size * float2(i, i) * float2(0.25, 0.25); + var direction: float4 = sample_lod(tex, tex_sampler, project_uv, 0.0); + min_dist = cur_dist; + if (direction.x != 0.0 || direction.y != 0.0 || direction.z != 0.0) { + var delta: float4 = offset_col - direction; + color = offset_col + delta * float4(4.0, 4.0, 4.0, 4.0); + } + else { + color = offset_col; + } + } + } + j += 1; + } + } + + return color; +} + +#[pipe] +struct pipe { + vertex = dilate_pass_vert; + fragment = dilate_pass_frag; +} diff --git a/base/assets/Scene.arm b/base/assets/Scene.arm index dfca562ca59c92e916feb00da0b7b68fbfc3b6bd..2a8d91223406f85d15f2edf554e714902597bdd7 100644 GIT binary patch delta 200 zcmeDA#P#t#S3?Wq7N*H7Sp^vw7~-c-U&*8jVQxRLl1Yn^g$XD;{oqC>pXs;PF}Y6f zS`8Frn!b7y)1>Kh*D&pYYKvV9*VeX{={ysw2-vj94NMvk=JwVNOu7iuUQXY#nrYE= YpUq5G)9-9%dP1J<;@cl?XVPW_0CV_1EdT%j delta 288 zcmezPo~!2*S3?Wq7N*H7Sp*mu7^ZV>VNwONwi|C@(qd#}o*uWANosoTR;KmST{bZ- z;$R1=Dkw@Vn?83FlRT#kkQWc;9@xYrK7IW*AQ8HTX(`08|7&1|X|82D&%`1Ovf=M~ zCJiuayVeFKU1S>^rZ3;fG?$TOy6iTlNz<=yV)7Kf&kWRZn*~VZCFZ8yCS diff --git a/base/assets/shader_datas.arm b/base/assets/shader_datas.arm index a37c9ebeac1ca2f435473af3ee5634f693836699..8eabbe9862ef3b11a2af6115b08b22628e2dbced 100644 GIT binary patch delta 436 zcmexaw6SD^Dx-iX0|P^Ra(-?>esN|=eo_2nMol?Zu;6BIMsYSSeu#WQC0K?N&fOfK zqs+K@J%=PCJ|&uro29ws@hVYfoV=bxZ1Y^+5I!+!kY!0Z`T4o=Df#7j#fiBEIjImQ zfu%N!%WyG^NFr2~79gqNMUj}SC}X*~L%y4tUj*)j+@ca=h;d-C&6djBnD9zlF>dx( zU&qKJ2r?oiGbgbG;v8-y0dPnF{g64?*Ft#m2`vZd`^-S)w^@KhUSe+QZ4Mx#B();G zII}8s@>y|3P7p%_D55#pP)T@l4V&@g3v4WtgS5{>+>()5T#}z&l$eX`RCgUCUI4M7 BbwU6D delta 276 zcmdm3^0R1zDkBRg0|Ue405zq}5scz&FrFMQA5gd;vA8%sIlrKCbE1qKgy0TatQ#Hrex+MmZWZ8z}~|+`JRCAWPNRi$sa}3CLdr{nEXro LGMXX(b&PlczluT} diff --git a/base/shaders/bloom_downsample_pass.frag.glsl b/base/shaders/bloom_downsample_pass.frag.glsl deleted file mode 100644 index 52b5c3c4..00000000 --- a/base/shaders/bloom_downsample_pass.frag.glsl +++ /dev/null @@ -1,45 +0,0 @@ -#version 450 - -uniform sampler2D tex; -uniform vec2 screen_size_inv; -uniform int current_mip_level; - -in vec2 tex_coord; -out vec4 frag_color; - -const float bloom_knee = 0.5; -const float bloom_threshold = 0.8; -const float epsilon = 6.2e-5; - -vec3 downsample_dual_filter(const sampler2D tex, const vec2 tex_coord, const vec2 texel_size) { - vec3 delta = texel_size.xyx * vec3(0.5, 0.5, -0.5); - - vec3 result; - result = textureLod(tex, tex_coord, 0.0).rgb * 4.0; - result += textureLod(tex, tex_coord - delta.xy, 0.0).rgb; - result += textureLod(tex, tex_coord - delta.zy, 0.0).rgb; - result += textureLod(tex, tex_coord + delta.zy, 0.0).rgb; - result += textureLod(tex, tex_coord + delta.xy, 0.0).rgb; - - return result * (1.0 / 8.0); -} - -void main() { - frag_color.rgb = downsample_dual_filter(tex, tex_coord, screen_size_inv); - - if (current_mip_level == 0) { - float brightness = max(frag_color.r, max(frag_color.g, frag_color.b)); - - float softening_curve = brightness - bloom_threshold + bloom_knee; - softening_curve = clamp(softening_curve, 0.0, 2.0 * bloom_knee); - softening_curve = softening_curve * softening_curve / (4 * bloom_knee + epsilon); - - float contribution_factor = max(softening_curve, brightness - bloom_threshold); - - contribution_factor /= max(epsilon, brightness); - - frag_color.rgb *= contribution_factor; - } - - frag_color.a = 1.0; -} diff --git a/base/shaders/bloom_downsample_pass.kong b/base/shaders/bloom_downsample_pass.kong new file mode 100644 index 00000000..57d47d72 --- /dev/null +++ b/base/shaders/bloom_downsample_pass.kong @@ -0,0 +1,74 @@ + +#[set(everything)] +const tex: tex2d; + +#[set(everything)] +const tex_sampler: sampler; + +#[set(everything)] +const constants: { + screen_size_inv: float2; + current_mip_level: int; +}; + +struct vert_in { + pos: float2; +} + +struct vert_out { + pos: float4; + tex: float2; +} + +const bloom_knee: float = 0.5; +const bloom_threshold: float = 0.8; +const epsilon: float = 0.000062; + +fun bloom_downsample_pass_vert(input: vert_in): vert_out { + var output: vert_out; + output.tex = input.pos.xy * 0.5 + 0.5; + output.tex.y = 1.0 - output.tex.y; + output.pos = float4(input.pos.xy, 0.0, 1.0); + return output; +} + +fun downsample_dual_filter(tex: tex2d, tex_coord: float2, texel_size: float2): float3 { + var delta: float3 = float3(texel_size.xy, texel_size.x) * float3(0.5, 0.5, -0.5); + + var result: float3; + result = sample_lod(tex, tex_sampler, tex_coord, 0.0).rgb * 4.0; + result += sample_lod(tex, tex_sampler, tex_coord - delta.xy, 0.0).rgb; + result += sample_lod(tex, tex_sampler, tex_coord - delta.zy, 0.0).rgb; + result += sample_lod(tex, tex_sampler, tex_coord + delta.zy, 0.0).rgb; + result += sample_lod(tex, tex_sampler, tex_coord + delta.xy, 0.0).rgb; + + return result * (1.0 / 8.0); +} + +fun bloom_downsample_pass_frag(input: vert_out): float4 { + var color: float4; + color.rgb = downsample_dual_filter(tex, input.tex, constants.screen_size_inv); + + if (constants.current_mip_level == 0) { + var brightness: float = max(color.r, max(color.g, color.b)); + + var softening_curve: float = brightness - bloom_threshold + bloom_knee; + softening_curve = clamp(softening_curve, 0.0, 2.0 * bloom_knee); + softening_curve = softening_curve * softening_curve / (4 * bloom_knee + epsilon); + + var contribution_factor: float = max(softening_curve, brightness - bloom_threshold); + + contribution_factor /= max(epsilon, brightness); + + color.rgb *= contribution_factor; + } + + color.a = 1.0; + return color; +} + +#[pipe] +struct pipe { + vertex = bloom_downsample_pass_vert; + fragment = bloom_downsample_pass_frag; +} diff --git a/base/shaders/bloom_upsample_pass.frag.glsl b/base/shaders/bloom_upsample_pass.frag.glsl deleted file mode 100644 index e97cd190..00000000 --- a/base/shaders/bloom_upsample_pass.frag.glsl +++ /dev/null @@ -1,37 +0,0 @@ -#version 450 - -uniform sampler2D tex; -uniform vec2 screen_size_inv; -uniform int current_mip_level; -uniform float sample_scale; - -in vec2 tex_coord; -out vec4 frag_color; - -const float bloom_strength = 0.02; - -vec3 upsample_dual_filter(const sampler2D tex, const vec2 tex_coord, const vec2 texel_size) { - vec2 delta = texel_size * sample_scale; - - vec3 result; - result = textureLod(tex, tex_coord + vec2(-delta.x * 2.0, 0.0), 0.0).rgb; - result += textureLod(tex, tex_coord + vec2(-delta.x, delta.y), 0.0).rgb * 2.0; - result += textureLod(tex, tex_coord + vec2(0.0, delta.y * 2.0), 0.0).rgb; - result += textureLod(tex, tex_coord + delta, 0.0).rgb * 2.0; - result += textureLod(tex, tex_coord + vec2(delta.x * 2.0, 0.0), 0.0).rgb; - result += textureLod(tex, tex_coord + vec2(delta.x, -delta.y), 0.0).rgb * 2.0; - result += textureLod(tex, tex_coord + vec2(0.0, -delta.y * 2.0), 0.0).rgb; - result += textureLod(tex, tex_coord - delta, 0.0).rgb * 2.0; - - return result * (1.0 / 12.0); -} - -void main() { - frag_color.rgb = upsample_dual_filter(tex, tex_coord, screen_size_inv); - - if (current_mip_level == 0) { - frag_color.rgb *= bloom_strength; - } - - frag_color.a = 1.0; -} diff --git a/base/shaders/bloom_upsample_pass.kong b/base/shaders/bloom_upsample_pass.kong new file mode 100644 index 00000000..47789618 --- /dev/null +++ b/base/shaders/bloom_upsample_pass.kong @@ -0,0 +1,66 @@ + +#[set(everything)] +const tex: tex2d; + +#[set(everything)] +const tex_sampler: sampler; + +#[set(everything)] +const constants: { + screen_size_inv: float2; + current_mip_level: int; + sample_scale: float; +}; + +struct vert_in { + pos: float2; +} + +struct vert_out { + pos: float4; + tex: float2; +} + +const bloom_strength: float = 0.02; + +fun bloom_upsample_pass_vert(input: vert_in): vert_out { + var output: vert_out; + output.tex = input.pos.xy * 0.5 + 0.5; + output.tex.y = 1.0 - output.tex.y; + output.pos = float4(input.pos.xy, 0.0, 1.0); + return output; +} + +fun upsample_dual_filter(tex: tex2d, tex_coord: float2, texel_size: float2): float3 { + var delta: float2 = texel_size * constants.sample_scale; + + var result: float3; + result = sample_lod(tex, tex_sampler, tex_coord + float2(-delta.x * 2.0, 0.0), 0.0).rgb; + result += sample_lod(tex, tex_sampler, tex_coord + float2(-delta.x, delta.y), 0.0).rgb * 2.0; + result += sample_lod(tex, tex_sampler, tex_coord + float2(0.0, delta.y * 2.0), 0.0).rgb; + result += sample_lod(tex, tex_sampler, tex_coord + delta, 0.0).rgb * 2.0; + result += sample_lod(tex, tex_sampler, tex_coord + float2(delta.x * 2.0, 0.0), 0.0).rgb; + result += sample_lod(tex, tex_sampler, tex_coord + float2(delta.x, -delta.y), 0.0).rgb * 2.0; + result += sample_lod(tex, tex_sampler, tex_coord + float2(0.0, -delta.y * 2.0), 0.0).rgb; + result += sample_lod(tex, tex_sampler, tex_coord - delta, 0.0).rgb * 2.0; + + return result * (1.0 / 12.0); +} + +fun bloom_upsample_pass_frag(input: vert_out): float4 { + var color: float4; + color.rgb = upsample_dual_filter(tex, input.tex, constants.screen_size_inv); + + if (constants.current_mip_level == 0) { + color.rgb = color.rgb * bloom_strength; + } + + color.a = 1.0; + return color; +} + +#[pipe] +struct pipe { + vertex = bloom_upsample_pass_vert; + fragment = bloom_upsample_pass_frag; +} diff --git a/base/shaders/compositor_pass.frag.glsl b/base/shaders/compositor_pass.frag.glsl deleted file mode 100644 index 21b316c1..00000000 --- a/base/shaders/compositor_pass.frag.glsl +++ /dev/null @@ -1,33 +0,0 @@ -#version 450 - -uniform sampler2D tex; -// uniform sampler2D histogram; -uniform float vignette_strength; -uniform float grain_strength; - -in vec2 tex_coord; -out vec4 frag_color; - -// Based on Filmic Tonemapping Operators http://filmicgames.com/archives/75 -vec3 tonemap_filmic(const vec3 color) { - vec3 x = max(vec3(0.0, 0.0, 0.0), color - 0.004); - return (x * (6.2 * x + 0.5)) / (x * (6.2 * x + 1.7) + 0.06); -} - -void main() { - frag_color = textureLod(tex, tex_coord, 0.0); - - // Static grain - float x = (tex_coord.x + 4.0) * (tex_coord.y + 4.0) * 10.0; - float g = mod((mod(x, 13.0) + 1.0) * (mod(x, 123.0) + 1.0), 0.01) - 0.005; - frag_color.rgb += vec3(g, g, g) * grain_strength; - - frag_color.rgb *= (1.0 - vignette_strength) + vignette_strength * pow(16.0 * tex_coord.x * tex_coord.y * (1.0 - tex_coord.x) * (1.0 - tex_coord.y), 0.2); - - // Auto exposure - // const float auto_exposure_strength = 1.0; - // float expo = 2.0 - clamp(length(textureLod(histogram, vec2(0.5, 0.5), 0.0).rgb), 0.0, 1.0); - // frag_color.rgb *= pow(expo, auto_exposure_strength * 2.0); - - frag_color.rgb = tonemap_filmic(frag_color.rgb); // With gamma -} diff --git a/base/shaders/compositor_pass.kong b/base/shaders/compositor_pass.kong new file mode 100644 index 00000000..27652649 --- /dev/null +++ b/base/shaders/compositor_pass.kong @@ -0,0 +1,70 @@ + +#[set(everything)] +const tex: tex2d; + +#[set(everything)] +const tex_sampler: sampler; + +// #[set(everything)] +// const histogram: tex2d; + +// #[set(everything)] +// const histogram_sampler: sampler; + +#[set(everything)] +const constants: { + vignette_strength: float; + grain_strength: float; +}; + +struct vert_in { + pos: float2; +} + +struct vert_out { + pos: float4; + tex: float2; +} + +fun compositor_pass_vert(input: vert_in): vert_out { + var output: vert_out; + output.tex = input.pos.xy * 0.5 + 0.5; + output.tex.y = 1.0 - output.tex.y; + output.pos = float4(input.pos.xy, 0.0, 1.0); + return output; +} + +fun tonemap_filmic(color: float3): float3 { + // Based on Filmic Tonemapping Operators http://filmicgames.com/archives/75 + // var x: float3 = max(float3(0.0, 0.0, 0.0), color - 0.004); + var x: float3; + x.x = max(float3(0.0, 0.0, 0.0), color.x - 0.004); + x.y = max(float3(0.0, 0.0, 0.0), color.y - 0.004); + x.z = max(float3(0.0, 0.0, 0.0), color.z - 0.004); + return (x * (6.2 * x + 0.5)) / (x * (6.2 * x + 1.7) + 0.06); +} + +fun compositor_pass_frag(input: vert_out): float4 { + var color: float4 = sample_lod(tex, tex_sampler, input.tex, 0.0); + + // Static grain + var x: float = (input.tex.x + 4.0) * (input.tex.y + 4.0) * 10.0; + var g: float = (((x % 13.0) + 1.0) * ((x % 123.0) + 1.0) % 0.01) - 0.005; + color.rgb += float3(g, g, g) * constants.grain_strength; + + color.rgb *= (1.0 - constants.vignette_strength) + constants.vignette_strength * pow(16.0 * input.tex.x * input.tex.y * (1.0 - input.tex.x) * (1.0 - input.tex.y), 0.2); + + // Auto exposure + // const float auto_exposure_strength = 1.0; + // float expo = 2.0 - clamp(length(sample_lod(histogram, histogram_sampler, float2(0.5, 0.5), 0.0).rgb), 0.0, 1.0); + // color.rgb *= pow(expo, auto_exposure_strength * 2.0); + + color.rgb = tonemap_filmic(color.rgb); // With gamma + return color; +} + +#[pipe] +struct pipe { + vertex = compositor_pass_vert; + fragment = compositor_pass_frag; +} diff --git a/base/shaders/copy_mrt3_pass.frag.glsl b/base/shaders/copy_mrt3_pass.frag.glsl deleted file mode 100644 index bf942b9d..00000000 --- a/base/shaders/copy_mrt3_pass.frag.glsl +++ /dev/null @@ -1,14 +0,0 @@ -#version 450 - -uniform sampler2D tex0; -uniform sampler2D tex1; -uniform sampler2D tex2; - -in vec2 tex_coord; -out vec4 frag_color[3]; - -void main() { - frag_color[0] = textureLod(tex0, tex_coord, 0.0); - frag_color[1] = textureLod(tex1, tex_coord, 0.0); - frag_color[2] = textureLod(tex2, tex_coord, 0.0); -} diff --git a/base/shaders/copy_mrt3_pass.kong b/base/shaders/copy_mrt3_pass.kong new file mode 100644 index 00000000..2f5dc25c --- /dev/null +++ b/base/shaders/copy_mrt3_pass.kong @@ -0,0 +1,48 @@ +#[set(everything)] +const tex0: tex2d; + +#[set(everything)] +const tex0_sampler: sampler; + +#[set(everything)] +const tex1: tex2d; + +#[set(everything)] +const tex1_sampler: sampler; + +#[set(everything)] +const tex2: tex2d; + +#[set(everything)] +const tex2_sampler: sampler; + +struct vert_in { + pos: float2; +} + +struct vert_out { + pos: float4; + tex: float2; +} + +fun copy_mrt3_pass_vert(input: vert_in): vert_out { + var output: vert_out; + output.tex = input.pos.xy * 0.5 + 0.5; + output.tex.y = 1.0 - output.tex.y; + output.pos = float4(input.pos.xy, 0.0, 1.0); + return output; +} + +fun copy_mrt3_pass_frag(input: vert_out): float4[3] { + var color: float4[3]; + color[0].rgba = sample_lod(tex0, tex0_sampler, input.tex, 0.0); + color[1].rgba = sample_lod(tex1, tex1_sampler, input.tex, 0.0); + color[2].rgba = sample_lod(tex2, tex2_sampler, input.tex, 0.0); + return color; +} + +#[pipe] +struct pipe { + vertex = copy_mrt3_pass_vert; + fragment = copy_mrt3_pass_frag; +} diff --git a/base/shaders/copy_pass.kong b/base/shaders/copy_pass.kong new file mode 100644 index 00000000..cdd731d9 --- /dev/null +++ b/base/shaders/copy_pass.kong @@ -0,0 +1,33 @@ +#[set(everything)] +const tex: tex2d; + +#[set(everything)] +const tex_sampler: sampler; + +struct vert_in { + pos: float2; +} + +struct vert_out { + pos: float4; + tex: float2; +} + +fun copy_pass_vert(input: vert_in): vert_out { + var output: vert_out; + output.tex = input.pos.xy * 0.5 + 0.5; + output.tex.y = 1.0 - output.tex.y; + output.pos = float4(input.pos.xy, 0.0, 1.0); + return output; +} + +fun copy_pass_frag(input: vert_out): float4 { + var color: float4 = sample_lod(tex, tex_sampler, input.tex, 0.0); + return color; +} + +#[pipe] +struct pipe { + vertex = copy_pass_vert; + fragment = copy_pass_frag; +} diff --git a/base/shaders/cursor.frag.glsl b/base/shaders/cursor.frag.glsl deleted file mode 100644 index 2842e526..00000000 --- a/base/shaders/cursor.frag.glsl +++ /dev/null @@ -1,15 +0,0 @@ -#version 450 - -uniform vec3 tint; - -in vec2 tex_coord; -out vec4 frag_color; - -void main() { - float radius = 0.45; - float thickness = 0.03; - float dist = distance(tex_coord, vec2(0.5, 0.5)); - float ring = smoothstep(radius - thickness, radius, dist) - - smoothstep(radius, radius + thickness, dist); - frag_color = vec4(tint, min(ring, 0.6)); -} diff --git a/base/shaders/cursor.kong b/base/shaders/cursor.kong new file mode 100644 index 00000000..eabc3aba --- /dev/null +++ b/base/shaders/cursor.kong @@ -0,0 +1,114 @@ + +#[set(everything)] +const constants: { + VP: float4x4; + invVP: float4x4; + mouse: float2; + tex_step: float2; + radius: float; + camera_right: float3; + tint: float3; +}; + +#[set(everything)] +const gbufferD: tex2d; + +#[set(everything)] +const gbufferD_sampler: sampler; + +struct vert_in { + pos: float4; // hlsl + nor: float2; // hlsl + tex: float2; +} + +struct vert_out { + pos: float4; + tex: float2; +} + +fun get_pos(uv: float2): float3 { + // var depth: float = sample_lod(gbufferD, gbufferD_sampler, uv, 0.0).r; + var depth: float = sample_lod(gbufferD, gbufferD_sampler, float2(uv.x, 1.0 - uv.y), 0.0).r; + var wpos: float4 = float4(uv * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0); + wpos = constants.invVP * wpos; + return wpos.xyz / wpos.w; +} + +fun get_normal(p0: float3, uv: float2): float3 { + var p1: float3 = get_pos(uv + float2(constants.tex_step.x * 4.0, 0.0)); + var p2: float3 = get_pos(uv + float2(0.0, constants.tex_step.y * 4.0)); + return normalize(cross(p2 - p0, p1 - p0)); +} + +// fun create_basis(normal: float3, out tangent: float3, out binormal: float3) { +// tangent = normalize(constants.camera_right - normal * dot(constants.camera_right, normal)); +// binormal = cross(tangent, normal); +// } + +fun cursor_vert(input: vert_in): vert_out { + + var keep: float = input.pos.x + input.nor.x; // hlsl + + var output: vert_out; + output.tex = input.tex; + var wpos: float3 = get_pos(constants.mouse); + var uv1: float2 = constants.mouse + constants.tex_step * float2(4.0, 4.0); + var uv2: float2 = constants.mouse - constants.tex_step * float2(4.0, 4.0); + var wpos1: float3 = get_pos(uv1); + var wpos2: float3 = get_pos(uv2); + var n: float3 = normalize(get_normal(wpos, constants.mouse) + get_normal(wpos1, uv1) + get_normal(wpos2, uv2)); + var n_tan: float3; + var n_bin: float3; + + // create_basis(n, n_tan, n_bin); + n_tan = normalize(constants.camera_right - n * dot(constants.camera_right, n)); + n_bin = cross(n_tan, n); + + var x_bit: float = (input.pos.x + 16383.0) / (2.0 * 16383.0); + var z_bit: float = (input.pos.z + 16383.0) / (2.0 * 16383.0); + var vertex_id: int = int(2.0 * x_bit + z_bit); + + if (input.pos.x < 0 && input.pos.z < 0) { + vertex_id = 0; + } + if (input.pos.x > 0 && input.pos.z < 0) { + vertex_id = 1; + } + if (input.pos.x > 0 && input.pos.z > 0) { + vertex_id = 2; + } + if (input.pos.x < 0 && input.pos.z > 0) { + vertex_id = 3; + } + + if (vertex_id == 0) { + wpos += normalize(-n_tan - n_bin) * 0.7 * constants.radius; + } + /*else */if (vertex_id == 1) { + wpos += normalize( n_tan - n_bin) * 0.7 * constants.radius; + } + /*else */if (vertex_id == 2) { + wpos += normalize( n_tan + n_bin) * 0.7 * constants.radius; + } + /*else */if (vertex_id == 3) { + wpos += normalize(-n_tan + n_bin) * 0.7 * constants.radius; + } + + output.pos = constants.VP * float4(wpos, 1.0); + return output; +} + +fun cursor_frag(input: vert_out): float4 { + var radius: float = 0.45; + var thickness: float = 0.03; + var dist: float = distance(input.tex, float2(0.5, 0.5)); + var ring: float = smoothstep(radius - thickness, radius, dist) - smoothstep(radius, radius + thickness, dist); + return float4(constants.tint, min(ring, 0.6)); +} + +#[pipe] +struct pipe { + vertex = cursor_vert; + fragment = cursor_frag; +} diff --git a/base/shaders/cursor.vert.glsl b/base/shaders/cursor.vert.glsl deleted file mode 100644 index bfe6c05c..00000000 --- a/base/shaders/cursor.vert.glsl +++ /dev/null @@ -1,65 +0,0 @@ -#version 450 - -uniform mat4 VP; -uniform mat4 invVP; -uniform vec2 mouse; -uniform vec2 tex_step; -uniform float radius; -uniform vec3 camera_right; -uniform sampler2D gbufferD; - -#ifdef HLSL -in vec4 pos; -in vec2 nor; -#endif -in vec2 tex; -out vec2 tex_coord; - -vec3 get_pos(vec2 uv) { -#ifdef HLSL - float keep = pos.x + nor.x; -#endif - -#ifdef GLSL - float depth = textureLod(gbufferD, uv, 0.0).r; -#else - float depth = textureLod(gbufferD, vec2(uv.x, 1.0 - uv.y), 0.0).r; -#endif - - vec4 wpos = vec4(uv * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0); - wpos = mul(wpos, invVP); - return wpos.xyz / wpos.w; -} - -vec3 get_normal(vec3 p0, vec2 uv) { - vec3 p1 = get_pos(uv + vec2(tex_step.x * 4.0, 0.0)); - vec3 p2 = get_pos(uv + vec2(0.0, tex_step.y * 4.0)); - return normalize(cross(p2 - p0, p1 - p0)); -} - -void create_basis(vec3 normal, OUT(vec3, tangent), OUT(vec3, binormal)) { - tangent = normalize(camera_right - normal * dot(camera_right, normal)); - binormal = cross(tangent, normal); -} - -void main() { - tex_coord = tex; - vec3 wpos = get_pos(mouse); - vec2 uv1 = mouse + tex_step * vec2(4.0, 4.0); - vec2 uv2 = mouse - tex_step * vec2(4.0, 4.0); - vec3 wpos1 = get_pos(uv1); - vec3 wpos2 = get_pos(uv2); - vec3 n = normalize( - get_normal(wpos, mouse) + - get_normal(wpos1, uv1) + - get_normal(wpos2, uv2) - ); - vec3 n_tan; - vec3 n_bin; - create_basis(n, n_tan, n_bin); - if (gl_VertexID == 0) wpos += normalize(-n_tan - n_bin) * 0.7 * radius; - else if (gl_VertexID == 1) wpos += normalize( n_tan - n_bin) * 0.7 * radius; - else if (gl_VertexID == 2) wpos += normalize( n_tan + n_bin) * 0.7 * radius; - else if (gl_VertexID == 3) wpos += normalize(-n_tan + n_bin) * 0.7 * radius; - gl_Position = mul(vec4(wpos, 1.0), VP); -} diff --git a/base/shaders/histogram_pass.frag.glsl b/base/shaders/histogram_pass.frag.glsl deleted file mode 100644 index 72b2e511..00000000 --- a/base/shaders/histogram_pass.frag.glsl +++ /dev/null @@ -1,17 +0,0 @@ -#version 450 - -uniform sampler2D tex; - -in vec2 tex_coord; -out vec4 frag_color; - -void main() { - const float auto_exposure_speed = 1.0; - frag_color.a = 0.01 * auto_exposure_speed; - frag_color.rgb = textureLod(tex, vec2(0.5, 0.5), 0.0).rgb + - textureLod(tex, vec2(0.2, 0.2), 0.0).rgb + - textureLod(tex, vec2(0.8, 0.2), 0.0).rgb + - textureLod(tex, vec2(0.2, 0.8), 0.0).rgb + - textureLod(tex, vec2(0.8, 0.8), 0.0).rgb; - frag_color.rgb /= 5.0; -} diff --git a/base/shaders/histogram_pass.kong b/base/shaders/histogram_pass.kong new file mode 100644 index 00000000..ea05dcec --- /dev/null +++ b/base/shaders/histogram_pass.kong @@ -0,0 +1,42 @@ +#[set(everything)] +const tex: tex2d; + +#[set(everything)] +const tex_sampler: sampler; + +struct vert_in { + pos: float2; +} + +struct vert_out { + pos: float4; + tex: float2; +} + +const auto_exposure_speed: float = 1.0; + +fun histogram_pass_vert(input: vert_in): vert_out { + var output: vert_out; + output.tex = input.pos.xy * 0.5 + 0.5; + output.tex.y = 1.0 - output.tex.y; + output.pos = float4(input.pos.xy, 0.0, 1.0); + return output; +} + +fun histogram_pass_frag(input: vert_out): float4 { + var color: float4; + color.a = 0.01 * auto_exposure_speed; + color.rgb = sample_lod(tex, tex_sampler, float2(0.5, 0.5), 0.0).rgb + + sample_lod(tex, tex_sampler, float2(0.2, 0.2), 0.0).rgb + + sample_lod(tex, tex_sampler, float2(0.8, 0.2), 0.0).rgb + + sample_lod(tex, tex_sampler, float2(0.2, 0.8), 0.0).rgb + + sample_lod(tex, tex_sampler, float2(0.8, 0.8), 0.0).rgb; + color.rgb /= 5.0; + return color; +} + +#[pipe] +struct pipe { + vertex = histogram_pass_vert; + fragment = histogram_pass_frag; +} diff --git a/base/shaders/mesh_poscol.frag.glsl b/base/shaders/mesh_poscol.frag.glsl deleted file mode 100644 index 25f0ee98..00000000 --- a/base/shaders/mesh_poscol.frag.glsl +++ /dev/null @@ -1,9 +0,0 @@ -#version 450 - -in vec3 vcolor; -out vec4 frag_color; - -void main() { - frag_color = vec4(vcolor, 1.0); - frag_color.rgb = pow(frag_color.rgb, vec3(1.0 / 2.2, 1.0 / 2.2, 1.0 / 2.2)); -} diff --git a/base/shaders/mesh_poscol.kong b/base/shaders/mesh_poscol.kong new file mode 100644 index 00000000..a0ffaa6b --- /dev/null +++ b/base/shaders/mesh_poscol.kong @@ -0,0 +1,39 @@ + +#[set(everything)] +const constants: { + WVP: float4x4; +}; + +struct vert_in { + pos: float4; + col: float4; +} + +struct vert_out { + pos: float4; + vcolor: float3; +} + +fun mesh_poscol_vert(input: vert_in): vert_out { + var output: vert_out; + var spos: float4 = float4(input.pos.xyz, 1.0); + output.vcolor = input.col.rgb; + output.pos = constants.WVP * spos; + return output; +} + +fun mesh_poscol_frag(input: vert_out): float4 { + var color: float4; + color = float4(input.vcolor, 1.0); + // color.rgb = pow(color.rgb, vec3(1.0 / 2.2, 1.0 / 2.2, 1.0 / 2.2)); + color.r = pow(color.r, 1.0 / 2.2); + color.g = pow(color.g, 1.0 / 2.2); + color.b = pow(color.b, 1.0 / 2.2); + return color; +} + +#[pipe] +struct pipe { + vertex = mesh_poscol_vert; + fragment = mesh_poscol_frag; +} diff --git a/base/shaders/mesh_poscol.vert.glsl b/base/shaders/mesh_poscol.vert.glsl deleted file mode 100644 index 6e13d482..00000000 --- a/base/shaders/mesh_poscol.vert.glsl +++ /dev/null @@ -1,13 +0,0 @@ -#version 450 - -uniform mat4 WVP; - -in vec4 pos; -in vec4 col; -out vec3 vcolor; - -void main() { - vec4 spos = vec4(pos.xyz, 1.0); - vcolor = col.rgb; - gl_Position = mul(spos, WVP); -} diff --git a/base/shaders/mesh_posnor.frag.glsl b/base/shaders/mesh_posnor.frag.glsl deleted file mode 100644 index 94af5359..00000000 --- a/base/shaders/mesh_posnor.frag.glsl +++ /dev/null @@ -1,24 +0,0 @@ -#version 450 - -#include "../../base/shaders/std/gbuffer.glsl" - -in vec3 wnormal; -in vec4 wvpposition; -in vec4 prevwvpposition; -out vec4 frag_color[3]; - -void main() { - vec3 n = normalize(wnormal); - vec3 basecol = vec3(0.8, 0.8, 0.8); - float roughness = 0.25; - float metallic = 0.0; - float occlusion = 1.0; - n /= (abs(n.x) + abs(n.y) + abs(n.z)); - n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy); - uint matid = uint(0); - frag_color[0] = vec4(n.xy, roughness, pack_f32_i16(metallic, matid)); - frag_color[1] = vec4(basecol, occlusion); - vec2 posa = (wvpposition.xy / wvpposition.w) * 0.5 + 0.5; - vec2 posb = (prevwvpposition.xy / prevwvpposition.w) * 0.5 + 0.5; - frag_color[2].rg = vec2(posa - posb); -} diff --git a/base/shaders/mesh_posnor.kong b/base/shaders/mesh_posnor.kong new file mode 100644 index 00000000..7ee13b73 --- /dev/null +++ b/base/shaders/mesh_posnor.kong @@ -0,0 +1,89 @@ + +#[set(everything)] +const constants: { + N: float3x3; + WVP: float4x4; +}; + +struct vert_in { + pos: float4; + nor: float2; +} + +struct vert_out { + pos: float4; + wnormal: float3; +} + +fun mesh_posnor_vert(input: vert_in): vert_out { + var output: vert_out; + output.pos = constants.WVP * float4(input.pos.xyz, 1.0); + output.wnormal = normalize(constants.N * float3(input.nor.xy, input.pos.w)); + return output; +} + +fun octahedron_wrap(v: float2): float2 { + var a: float2; + if (v.x >= 0.0) { + a.x = 1.0; + } + else { + a.x = -1.0; + } + + if (v.y >= 0.0) { + a.y = 1.0; + } + else { + a.y = -1.0; + } + + var r: float2; + r.x = abs(v.y); + r.y = abs(v.x); + r.x = 1.0 - r.x; + r.y = 1.0 - r.y; + return r * a; + + // return (1.0 - abs(v.yx)) * (float2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0)); +} + +fun pack_f32_i16(f: float, i: uint): float { + // GBuffer helper - Sebastien Lagarde + // https://seblagarde.wordpress.com/2018/09/02/gbuffer-helper-packing-integer-and-float-together/ + // const num_bit_target: int = 16; + // const num_bit_i: int = 4; + // const prec: float = float(1 << num_bit_target); + // const maxi: float = float(1 << num_bit_i); + // const prec_minus_one: float = prec - 1.0; + // const t1: float = ((prec / maxi) - 1.0) / prec_minus_one; + // const t2: float = (prec / maxi) / prec_minus_one; + // return t1 * f + t2 * float(i); + return 0.062504762 * f + 0.062519999 * float(i); +} + +fun mesh_posnor_frag(input: vert_out): float4[2] { + var basecol: float3 = float3(0.8, 0.8, 0.8); + var roughness: float = 0.25; + var metallic: float = 0.0; + var occlusion: float = 1.0; + var n: float3 = normalize(input.wnormal); + n /= abs(n.x) + abs(n.y) + abs(n.z); + if (n.z >= 0.0) { + n.xy = n.xy; + } + else { + n.xy = octahedron_wrap(n.xy); + } + var matid: uint = 0; + var color: float4[2]; + color[0].rgba = float4(n.xy, roughness, pack_f32_i16(metallic, matid)); + color[1].rgba = float4(basecol, occlusion); + return color; +} + +#[pipe] +struct pipe { + vertex = mesh_posnor_vert; + fragment = mesh_posnor_frag; +} diff --git a/base/shaders/mesh_posnor.vert.glsl b/base/shaders/mesh_posnor.vert.glsl deleted file mode 100644 index 987d2a1b..00000000 --- a/base/shaders/mesh_posnor.vert.glsl +++ /dev/null @@ -1,19 +0,0 @@ -#version 450 - -uniform mat3 N; -uniform mat4 WVP; -uniform mat4 prevWVP; - -in vec4 pos; -in vec2 nor; -out vec3 wnormal; -out vec4 wvpposition; -out vec4 prevwvpposition; - -void main() { - vec4 spos = vec4(pos.xyz, 1.0); - wnormal = normalize(mul(vec3(nor.xy, pos.w), N)); - gl_Position = mul(spos, WVP); - wvpposition = gl_Position; - prevwvpposition = mul(spos, prevWVP); -} diff --git a/base/shaders/mesh_posnortex.frag.glsl b/base/shaders/mesh_posnortex.frag.glsl deleted file mode 100644 index 9315ba5d..00000000 --- a/base/shaders/mesh_posnortex.frag.glsl +++ /dev/null @@ -1,25 +0,0 @@ -#version 450 - -#include "std/gbuffer.glsl" - -in vec2 tex_coord; -in vec3 wnormal; -in vec4 wvpposition; -in vec4 prevwvpposition; -out vec4 frag_color[3]; - -void main() { - vec3 n = normalize(wnormal); - vec3 basecol = vec3(0.2, 0.2, 0.2) + tex_coord.x * 0.0000001; // keep tex_coord - float roughness = 0.4; - float metallic = 0.0; - float occlusion = 1.0; - n /= (abs(n.x) + abs(n.y) + abs(n.z)); - n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy); - uint matid = uint(0); - frag_color[0] = vec4(n.xy, roughness, pack_f32_i16(metallic, matid)); - frag_color[1] = vec4(basecol, occlusion); - vec2 posa = (wvpposition.xy / wvpposition.w) * 0.5 + 0.5; - vec2 posb = (prevwvpposition.xy / prevwvpposition.w) * 0.5 + 0.5; - frag_color[2].rg = vec2(posa - posb); -} diff --git a/base/shaders/mesh_posnortex.kong b/base/shaders/mesh_posnortex.kong new file mode 100644 index 00000000..2fa579e9 --- /dev/null +++ b/base/shaders/mesh_posnortex.kong @@ -0,0 +1,95 @@ + +#[set(everything)] +const constants: { + N: float3x3; + WVP: float4x4; + tex_unpack: float; +}; + +struct vert_in { + pos: float4; + nor: float2; + tex: float2; +} + +struct vert_out { + pos: float4; + tex: float2; + wnormal: float3; +} + +fun mesh_posnortex_vert(input: vert_in): vert_out { + var output: vert_out; + output.pos = constants.WVP * float4(input.pos.xyz, 1.0); + output.tex = input.tex * constants.tex_unpack; + output.wnormal = normalize(constants.N * float3(input.nor.xy, input.pos.w)); + return output; +} + +fun octahedron_wrap(v: float2): float2 { + var a: float2; + if (v.x >= 0.0) { + a.x = 1.0; + } + else { + a.x = -1.0; + } + + if (v.y >= 0.0) { + a.y = 1.0; + } + else { + a.y = -1.0; + } + + var r: float2; + r.x = abs(v.y); + r.y = abs(v.x); + r.x = 1.0 - r.x; + r.y = 1.0 - r.y; + return r * a; + + // return (1.0 - abs(v.yx)) * (float2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0)); +} + +fun pack_f32_i16(f: float, i: uint): float { + // GBuffer helper - Sebastien Lagarde + // https://seblagarde.wordpress.com/2018/09/02/gbuffer-helper-packing-integer-and-float-together/ + // const num_bit_target: int = 16; + // const num_bit_i: int = 4; + // const prec: float = float(1 << num_bit_target); + // const maxi: float = float(1 << num_bit_i); + // const prec_minus_one: float = prec - 1.0; + // const t1: float = ((prec / maxi) - 1.0) / prec_minus_one; + // const t2: float = (prec / maxi) / prec_minus_one; + // return t1 * f + t2 * float(i); + return 0.062504762 * f + 0.062519999 * float(i); +} + +fun mesh_posnortex_frag(input: vert_out): float4[2] { + var n: float3 = normalize(input.wnormal); + var basecol: float3 = float3(0.2, 0.2, 0.2) + input.tex.x * 0.0000001; // keep tex_coord + var roughness: float = 0.4; + var metallic: float = 0.0; + var occlusion: float = 1.0; + + n /= abs(n.x) + abs(n.y) + abs(n.z); + if (n.z >= 0.0) { + n.xy = n.xy; + } + else { + n.xy = octahedron_wrap(n.xy); + } + + var matid: uint = 0; + var color: float4[2]; + color[0].rgba = float4(n.xy, roughness, pack_f32_i16(metallic, matid)); + color[1].rgba = float4(basecol, occlusion); + return color; +} + +#[pipe] +struct pipe { + vertex = mesh_posnortex_vert; + fragment = mesh_posnortex_frag; +} diff --git a/base/shaders/mesh_posnortex.vert.glsl b/base/shaders/mesh_posnortex.vert.glsl deleted file mode 100644 index 4711d9aa..00000000 --- a/base/shaders/mesh_posnortex.vert.glsl +++ /dev/null @@ -1,23 +0,0 @@ -#version 450 - -uniform mat3 N; -uniform mat4 WVP; -uniform float tex_unpack; -uniform mat4 prevWVP; - -in vec4 pos; -in vec2 nor; -in vec2 tex; -out vec2 tex_coord; -out vec3 wnormal; -out vec4 wvpposition; -out vec4 prevwvpposition; - -void main() { - vec4 spos = vec4(pos.xyz, 1.0); - tex_coord = tex * tex_unpack; - wnormal = normalize(mul(vec3(nor.xy, pos.w), N)); - gl_Position = mul(spos, WVP); - wvpposition = gl_Position; - prevwvpposition = mul(spos, prevWVP); -} diff --git a/base/sources/ts/iron/uniforms.ts b/base/sources/ts/iron/uniforms.ts index f3c7bdd0..0a93a537 100644 --- a/base/sources/ts/iron/uniforms.ts +++ b/base/sources/ts/iron/uniforms.ts @@ -335,6 +335,19 @@ function uniforms_set_context_const(location: iron_gpu_constant_location_t, c: s v.x = zfar / (zfar - znear); v.y = (-zfar * znear) / (zfar - znear); } + else if (starts_with(c.link, "_size(")) { + let tex: string = substring(c.link, 6, c.link.length - 1); + for (let i: i32 = 0; i < math_floor(_render_path_bind_params.length / 2); ++i) { + let pos: i32 = i * 2; // bind params = [texture, sampler_id] + let sampler_id: string = _render_path_bind_params[pos + 1]; + if (sampler_id == tex) { + let rt_id: string = _render_path_bind_params[pos]; + let rt: render_target_t = map_get(render_path_render_targets, rt_id); + v.x = rt.width; + v.y = rt.height; + } + } + } else { return false; } diff --git a/base/sources/ts/pipes.ts b/base/sources/ts/pipes.ts index 96414b84..761db430 100644 --- a/base/sources/ts/pipes.ts +++ b/base/sources/ts/pipes.ts @@ -260,26 +260,78 @@ function pipes_init() { pipes_cursor.vertex_shader = sys_get_shader("cursor.vert"); pipes_cursor.fragment_shader = sys_get_shader("cursor.frag"); let vs: iron_gpu_vertex_structure_t = gpu_vertex_struct_create(); - ///if (arm_metal || arm_vulkan) - gpu_vertex_struct_add(vs, "tex", vertex_data_t.I16_2X_NORM); - ///else gpu_vertex_struct_add(vs, "pos", vertex_data_t.I16_4X_NORM); gpu_vertex_struct_add(vs, "nor", vertex_data_t.I16_2X_NORM); gpu_vertex_struct_add(vs, "tex", vertex_data_t.I16_2X_NORM); - ///end pipes_cursor.input_layout = vs; pipes_cursor.blend_source = blend_factor_t.SOURCE_ALPHA; pipes_cursor.blend_destination = blend_factor_t.INV_SOURCE_ALPHA; pipes_cursor.depth_write = false; pipes_cursor.depth_mode = compare_mode_t.ALWAYS; + + pipes_cursor.kong = true; + gpu_compile_pipeline(pipes_cursor); + pipes_cursor_vp = gpu_get_constant_location(pipes_cursor, "VP"); + let ptr: gpu_constant_location_impl_t = ADDRESS(pipes_cursor_vp.impl); + let size: i32 = 0; + let offset: i32 = 0; + size = shader_context_type_size("mat4"); + offset += shader_context_type_pad(offset, size); + ptr.vertexOffset = offset; + ptr.fragmentOffset = offset; + offset += size; + pipes_cursor_inv_vp = gpu_get_constant_location(pipes_cursor, "invVP"); + ptr = ADDRESS(pipes_cursor_inv_vp.impl); + size = shader_context_type_size("mat4"); + offset += shader_context_type_pad(offset, size); + ptr.vertexOffset = offset; + ptr.fragmentOffset = offset; + offset += size; + pipes_cursor_mouse = gpu_get_constant_location(pipes_cursor, "mouse"); + ptr = ADDRESS(pipes_cursor_mouse.impl); + size = shader_context_type_size("vec2"); + offset += shader_context_type_pad(offset, size); + ptr.vertexOffset = offset; + ptr.fragmentOffset = offset; + offset += size; + pipes_cursor_tex_step = gpu_get_constant_location(pipes_cursor, "tex_step"); + ptr = ADDRESS(pipes_cursor_tex_step.impl); + size = shader_context_type_size("vec2"); + offset += shader_context_type_pad(offset, size); + ptr.vertexOffset = offset; + ptr.fragmentOffset = offset; + offset += size; + pipes_cursor_radius = gpu_get_constant_location(pipes_cursor, "radius"); + ptr = ADDRESS(pipes_cursor_radius.impl); + size = shader_context_type_size("float"); + offset += shader_context_type_pad(offset, size); + ptr.vertexOffset = offset; + ptr.fragmentOffset = offset; + offset += size; + pipes_cursor_camera_right = gpu_get_constant_location(pipes_cursor, "camera_right"); + ptr = ADDRESS(pipes_cursor_camera_right.impl); + size = shader_context_type_size("vec3"); + offset += shader_context_type_pad(offset, size); + ptr.vertexOffset = offset; + ptr.fragmentOffset = offset; + offset += size; + pipes_cursor_tint = gpu_get_constant_location(pipes_cursor, "tint"); + ptr = ADDRESS(pipes_cursor_tint.impl); + size = shader_context_type_size("vec3"); + offset += shader_context_type_pad(offset, size); + ptr.vertexOffset = offset; + ptr.fragmentOffset = offset; + offset += size; + pipes_cursor_gbufferd = gpu_get_texture_unit(pipes_cursor, "gbufferD"); + ARRAY_ACCESS(pipes_cursor_gbufferd.stages, IRON_GPU_SHADER_TYPE_VERTEX) = 0; } }