Skip to content

Commit

Permalink
shader refactor wpos -> pos_ws
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Sep 20, 2024
1 parent ef45038 commit c0c11cb
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 72 deletions.
16 changes: 8 additions & 8 deletions data/pipelines/common.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ struct Surface {
float shadow;
float3 N;
float3 V;
float3 wpos;
float3 pos_ws;
float2 motion;
};

Expand Down Expand Up @@ -250,8 +250,8 @@ Surface unpackSurface(float2 uv, uint gbuffer0, uint gbuffer1, uint gbuffer2, ui
surface.roughness = gb0.a;
surface.metallic = gb2.z;
surface.emission = unpackEmission(gb2.x);
surface.wpos = getPositionWS(gbuffer_depth, uv, ndc_depth);
surface.V = normalize(-surface.wpos);
surface.pos_ws = getPositionWS(gbuffer_depth, uv, ndc_depth);
surface.V = normalize(-surface.pos_ws);
surface.translucency = gb2.y;
surface.ao = gb1.w;
surface.shadow = gb2.w;
Expand Down Expand Up @@ -491,7 +491,7 @@ float3 pointLightsLighting(Cluster cluster, Surface surface, uint shadow_atlas,
float3 res = 0;
for (int i = cluster.offset; i < cluster.offset + cluster.lights_count; ++i) {
Light light = b_lights[b_cluster_map[i]];
float3 lpos = surface.wpos.xyz - light.pos_radius.xyz;
float3 lpos = surface.pos_ws.xyz - light.pos_radius.xyz;
float dist = length(lpos);
float attn = pow(max(0, 1 - dist / light.pos_radius.w), light.color_attn.w);
float3 L = -lpos / dist;
Expand Down Expand Up @@ -569,7 +569,7 @@ float3 envProbesLighting(Cluster cluster, Surface surface) {
int to = from + cluster.env_probes_count;
for (int i = from; i < to; ++i) {
int probe_idx = b_cluster_map[i];
float3 lpos = b_env_probes[probe_idx].pos.xyz - surface.wpos.xyz;
float3 lpos = b_env_probes[probe_idx].pos.xyz - surface.pos_ws.xyz;
float4 rot = b_env_probes[probe_idx].rot;
float3 outer_range = b_env_probes[probe_idx].outer_range.xyz;
float3 inner_range = b_env_probes[probe_idx].inner_range.xyz;
Expand Down Expand Up @@ -618,7 +618,7 @@ float3 reflProbesLighting(Cluster cluster, Surface surface, uint reflection_prob
int probe_idx = b_cluster_map[i];
ReflectionProbe probe = b_refl_probes[probe_idx];
float4 rot = probe.rot;
float3 lpos = probe.pos_layer.xyz - surface.wpos;
float3 lpos = probe.pos_layer.xyz - surface.pos_ws;
uint layer = asuint(probe.pos_layer.w);
float4 radiance_rgbm = sampleCubeArrayBindlessLod(LinearSamplerClamp, reflection_probes, float4(RV, layer), lod);
float3 radiance = radiance_rgbm.rgb * radiance_rgbm.a * 4;
Expand Down Expand Up @@ -656,9 +656,9 @@ float distanceInFog(float3 a, float3 b) {
}

float3 computeLighting(Cluster cluster, Surface surface, float3 light_direction, float3 light, uint shadowmap, uint shadow_atlas, uint reflection_probes, float2 frag_coord) {
float shadow = min(surface.shadow, getShadow(shadowmap, surface.wpos, surface.N, frag_coord));
float shadow = min(surface.shadow, getShadow(shadowmap, surface.pos_ws, surface.N, frag_coord));

float dist = max(0, (Global_fog_top - surface.wpos.y - Global_camera_world_pos.y) * Global_light_dir.y);
float dist = max(0, (Global_fog_top - surface.pos_ws.y - Global_camera_world_pos.y) * Global_light_dir.y);
float3 fog_transmittance = Global_fog_enabled > 0 ? exp(-dist * Global_fog_scattering.rgb * 10) : 1;

float3 res = computeDirectLight(surface, light_direction, light * shadow * fog_transmittance);
Expand Down
4 changes: 2 additions & 2 deletions data/pipelines/curve_decal.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ cbuffer Dc : register(b4) {

GBufferOutput mainPS(VSOutput input) {
float2 screen_uv = input.position.xy / Global_framebuffer_size;
float3 wpos = getPositionWS(u_gbuffer_depth, screen_uv);
float3 pos_ws = getPositionWS(u_gbuffer_depth, screen_uv);

float4 r = input.rot;
r.w = -r.w;
float3 lpos = rotateByQuat(r, wpos - input.pos);
float3 lpos = rotateByQuat(r, pos_ws - input.pos);
if (any(abs(lpos) > input.half_extents)) discard;

float2 bezier_dist = sdBezier(lpos.xz, input.bezier.xy, 0, input.bezier.zw);
Expand Down
8 changes: 4 additions & 4 deletions data/pipelines/editor_icon.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

struct VSOutput {
float2 uv : TEXCOORD0;
float3 wpos : TEXCOORD1;
float3 pos_ws : TEXCOORD1;
float3 normal : TEXCOORD2;
float4 position : SV_POSITION;
};
Expand Down Expand Up @@ -34,15 +34,15 @@ VSOutput mainVS(Input input) {
output.uv = 0;
#endif
float4 p = mul(float4(input.position, 1), mul(u_model, Global_view));
output.wpos = p.xyz;
output.pos_ws = p.xyz;
output.normal = input.normal;
output.position = mul(p, Global_projection_no_jitter);
return output;
}

float4 mainPS(VSOutput input) : SV_TARGET {
float2 screen_uv = input.position.xy / Global_framebuffer_size;
float3 wpos = getPositionWS(u_gbuffer_depth, screen_uv);
float3 pos_ws = getPositionWS(u_gbuffer_depth, screen_uv);
float4 albedo = sampleBindless(LinearSampler, t_albedo, input.uv);
#ifdef ALPHA_CUTOUT
if (albedo.a < 0.5) discard;
Expand All @@ -51,6 +51,6 @@ float4 mainPS(VSOutput input) : SV_TARGET {
float4 o_color;
o_color.rgb = albedo.rgb * saturate(max(0, -d) + 0.25 * max(0, d) + 0.25);
o_color.a = 1;
if(length(wpos) < length(input.wpos)) o_color.rgb *= 0.25;
if(length(pos_ws) < length(input.pos_ws)) o_color.rgb *= 0.25;
return o_color;
}
18 changes: 9 additions & 9 deletions data/pipelines/impostor.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct VSOutput {
float2 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
float3 tangent : TEXCOORD2;
float4 wpos : TEXCOORD3;
float4 pos_ws : TEXCOORD3;
#if !defined DEPTH && defined HAS_SELFSHADOW
float4 shadow_coefs : TEXCOORD4;
#endif
Expand Down Expand Up @@ -95,9 +95,9 @@ float2 dirToGrid(float3 vec) {
output.lod = 1;
output.tangent = tangent_space[0];
output.normal = tangent_space[2];
output.wpos = float4(p, 1);
output.pos_ws = float4(p, 1);

output.position = mul(output.wpos, Pass_view_projection);
output.position = mul(output.pos_ws, Pass_view_projection);
return output;
}
#else
Expand Down Expand Up @@ -145,9 +145,9 @@ float2 dirToGrid(float3 vec) {
p *= scale;
output.tangent = tangent_space[0];
output.normal = tangent_space[2];
output.wpos = float4(instance_pos + p, 1);
output.pos_ws = float4(instance_pos + p, 1);

output.position = mul(output.wpos, Pass_view_projection);
output.position = mul(output.pos_ws, Pass_view_projection);
return output;
}
#endif
Expand All @@ -166,8 +166,8 @@ Surface getSurface(VSOutput input) {
normalize(cross(input.normal, input.tangent))
);

data.wpos = input.wpos.xyz;
data.V = normalize(-data.wpos);
data.pos_ws = input.pos_ws.xyz;
data.V = normalize(-data.pos_ws);
data.roughness = u_roughness;
data.metallic = u_metallic;
#ifdef HAS_NORMAL
Expand All @@ -180,7 +180,7 @@ Surface getSurface(VSOutput input) {
data.emission = u_emission;
data.translucency = u_translucency;
data.ao = 1;
data.motion = computeStaticObjectMotionVector(input.wpos.xyz);
data.motion = computeStaticObjectMotionVector(input.pos_ws.xyz);

#if !defined DEPTH && defined HAS_SELFSHADOW
float4 self_shadow = sampleBindless(LinearSampler, t_self_shadow, input.uv);
Expand Down Expand Up @@ -242,7 +242,7 @@ Surface getSurface(VSOutput input) {
, Global_light_color.rgb * Global_light_intensity * surface.shadow);
res += surface.emission * surface.albedo;

float linear_depth = dot(surface.wpos.xyz, Pass_view_dir.xyz);
float linear_depth = dot(surface.pos_ws.xyz, Pass_view_dir.xyz);
Cluster cluster = getClusterLinearDepth(linear_depth);
//res += pointLightsLighting(cluster, surface, shadow_atlas);
res += envProbesLighting(cluster, surface);
Expand Down
4 changes: 2 additions & 2 deletions data/pipelines/particles.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ float4 mainPS(VSOutput input) : SV_TARGET {
float4 c = sampleBindless(LinearSampler, t_texture, input.uv) * saturate(input.color);
data.N = 0;
data.V = 0;
data.wpos = 0;
data.pos_ws = 0;
data.albedo = c.rgb;
data.alpha = c.a;
data.emission = input.emission;
Expand All @@ -58,7 +58,7 @@ float4 mainPS(VSOutput input) : SV_TARGET {
data.metallic = 0;
data.translucency = 0;

float linear_depth = dot(data.wpos.xyz, Pass_view_dir.xyz);
float linear_depth = dot(data.pos_ws.xyz, Pass_view_dir.xyz);
Cluster cluster = getClusterLinearDepth(linear_depth, input.position.xy);
float4 o_color;
o_color.rgb = computeLighting(cluster, data, Global_light_dir.xyz, Global_light_color.rgb * Global_light_intensity, Global_shadowmap, Global_shadow_atlas, Global_reflection_probes, input.position.xy);
Expand Down
6 changes: 3 additions & 3 deletions data/pipelines/procedural_geom.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ Surface getSurface(VSOutput input) {
surface.alpha = 1;
surface.emission = u_emission;
surface.translucency = u_translucency;
surface.wpos = input.pos_ws.xyz;
surface.V = normalize(-surface.wpos);
surface.pos_ws = input.pos_ws.xyz;
surface.V = normalize(-surface.pos_ws);
surface.motion = computeStaticObjectMotionVector(input.pos_ws.xyz);
return surface;
}
Expand All @@ -85,7 +85,7 @@ Surface getSurface(VSOutput input) {
float4 mainPS(VSOutput input) : SV_TARGET {
Surface surface = getSurface();

float linear_depth = dot(surface.wpos.xyz, Pass_view_dir.xyz);
float linear_depth = dot(surface.pos_ws.xyz, Pass_view_dir.xyz);
Cluster cluster = getClusterLinearDepth(linear_depth, input.position.xy);
float4 result;
result.rgb = computeLighting(cluster, surface, Global_light_dir.xyz, Global_light_color.rgb * Global_light_intensity, u_shadowmap, u_shadow_atlas, u_reflection_probes, input.position.xy);
Expand Down
36 changes: 18 additions & 18 deletions data/pipelines/surface_base.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#endif

struct VSOutput {
float4 wpos : TEXCOORD0;
float4 pos_ws : TEXCOORD0;
float3 normal : TEXCOORD1;
#ifdef UV0_ATTR
float2 uv : TEXCOORD2;
Expand Down Expand Up @@ -109,8 +109,8 @@ VSOutput mainVS(VSInput input) {
#endif
#ifdef AUTOINSTANCED
float3 p = input.position.xyz * input.i_scale.xyz;
output.wpos = float4(input.i_pos_lod.xyz + rotateByQuat(input.i_rot, p), 1);
output.position = mul(output.wpos, Pass_view_projection);
output.pos_ws = float4(input.i_pos_lod.xyz + rotateByQuat(input.i_rot, p), 1);
output.position = mul(output.pos_ws, Pass_view_projection);
output.normal = rotateByQuat(input.i_rot, input.normal);
#ifdef HAS_LOD
output.lod = input.i_pos_lod.w;
Expand All @@ -123,28 +123,28 @@ VSOutput mainVS(VSInput input) {
output.tangent = rotateByQuat(rot_quat, input.tangent);
#endif
float3 p = input.position * input.i_pos_scale.w;
output.wpos = float4(input.i_pos_scale.xyz + rotateByQuat(rot_quat, p), 1);
output.position = mul(output.wpos, Pass_view_projection);
output.pos_ws = float4(input.i_pos_scale.xyz + rotateByQuat(rot_quat, p), 1);
output.position = mul(output.pos_ws, Pass_view_projection);
#elif defined GRASS
output.normal = rotateByQuat(input.i_rot, input.normal);
#ifdef TANGENT_ATTR
output.tangent = rotateByQuat(input.i_rot, input.tangent);
#endif
float3 p = input.position;
output.pos_y = p.y;
output.wpos = float4(input.i_pos_scale.xyz + rotateByQuat(input.i_rot, input.position * input.i_pos_scale.w), 1);
output.wpos.xyz += u_grass_origin;
output.pos_ws = float4(input.i_pos_scale.xyz + rotateByQuat(input.i_rot, input.position * input.i_pos_scale.w), 1);
output.pos_ws.xyz += u_grass_origin;
#ifdef COLOR0_ATTR
//output.color = input.color;
#endif
output.position = mul(output.wpos, Pass_view_projection);
output.position = mul(output.pos_ws, Pass_view_projection);
#elif defined DYNAMIC
output.normal = rotateByQuat(input.i_rot, input.normal);
#ifdef TANGENT_ATTR
output.tangent = rotateByQuat(input.i_rot, input.tangent);
#endif
output.wpos = float4(input.i_pos_lod.xyz + rotateByQuat(input.i_rot, input.position * input.i_scale.xyz), 1);
output.position = mul(output.wpos, Pass_view_projection);
output.pos_ws = float4(input.i_pos_lod.xyz + rotateByQuat(input.i_rot, input.position * input.i_scale.xyz), 1);
output.position = mul(output.pos_ws, Pass_view_projection);
output.prev_ndcpos_no_jitter = float4(input.i_prev_pos_lod.xyz + rotateByQuat(input.i_prev_rot, input.position * input.i_prev_scale.xyz), 1);
output.prev_ndcpos_no_jitter = mul(output.prev_ndcpos_no_jitter, mul(Global_view_projection_no_jitter, Global_reprojection));
#elif defined SKINNED
Expand All @@ -170,8 +170,8 @@ VSOutput mainVS(VSInput input) {
#else
mpos = input.position;
#endif
output.wpos = mul(float4(transformByDualQuat(dq, mpos), 1), mtx);
output.position = mul(output.wpos, Pass_view_projection);
output.pos_ws = mul(float4(transformByDualQuat(dq, mpos), 1), mtx);
output.position = mul(output.pos_ws, Pass_view_projection);
// TODO previous frame bone positions
output.prev_ndcpos_no_jitter = mul(float4(transformByDualQuat(dq, mpos), 1), prev_matrix);
output.prev_ndcpos_no_jitter = mul(output.prev_ndcpos_no_jitter, mul(Global_view_projection_no_jitter, Global_reprojection));
Expand All @@ -181,22 +181,22 @@ VSOutput mainVS(VSInput input) {
#ifdef TANGENT_ATTR
output.tangent = mul(input.tangent, rot_mtx);
#endif
output.wpos = mul(float4(input.position, 1), model_mtx);
output.position = mul(output.wpos, Pass_view_projection);
output.pos_ws = mul(float4(input.position, 1), model_mtx);
output.position = mul(output.pos_ws, Pass_view_projection);
#endif
return output;
}

Surface getSurfaceEx(VSOutput input) {
Surface data = getSurface(input);
float4 p = mul(input.wpos, Global_view_projection_no_jitter);
float4 p = mul(input.pos_ws, Global_view_projection_no_jitter);
#if defined DYNAMIC || defined SKINNED
float2 prev_pos_projected = input.prev_ndcpos_no_jitter.xy / input.prev_ndcpos_no_jitter.w;
data.motion = prev_pos_projected.xy - p.xy / p.w;
#else
data.motion = computeStaticObjectMotionVector(input.wpos.xyz);
data.motion = computeStaticObjectMotionVector(input.pos_ws.xyz);
#endif
data.V = normalize(-data.wpos);
data.V = normalize(-data.pos_ws);
return data;
}

Expand Down Expand Up @@ -228,7 +228,7 @@ Surface getSurfaceEx(VSOutput input) {

Surface data = getSurfaceEx(input);

float linear_depth = dot(data.wpos.xyz, Pass_view_dir.xyz);
float linear_depth = dot(data.pos_ws.xyz, Pass_view_dir.xyz);
Cluster cluster = getClusterLinearDepth(linear_depth, frag_coord.xy);
float4 result;
result.rgb = computeLighting(cluster, data, Global_light_dir.xyz, Global_light_color.rgb * Global_light_intensity, u_shadowmap, u_shadow_atlas, u_reflection_probes, frag_coord);
Expand Down
12 changes: 6 additions & 6 deletions data/pipelines/terrain.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct VSOutput {
#ifndef DEPTH
float2 uv : TEXCOORD0;
float dist2 : TEXCOORD1;
float3 wpos : TEXCOORD2;
float3 pos_ws : TEXCOORD2;
#endif
float4 position : SV_POSITION;
};
Expand Down Expand Up @@ -61,11 +61,11 @@ VSOutput mainVS(uint vertex_id : SV_VertexID, uint instance_id : SV_InstanceID)
float h = sampleBindlessLod(LinearSamplerClamp, t_heightmap, hm_uv, 0).x * u_terrain_scale.y;
#endif

float3 wpos = u_position.xyz + v + float3(0, h, 0);
float3 pos_ws = u_position.xyz + v + float3(0, h, 0);
#ifndef DEPTH
output.wpos = wpos;
output.pos_ws = pos_ws;
#endif
float4 p = mul(float4(wpos, 1), Pass_view);
float4 p = mul(float4(pos_ws, 1), Pass_view);
#ifndef DEPTH
output.dist2 = dot(p.xyz, p.xyz);
#endif
Expand Down Expand Up @@ -202,10 +202,10 @@ VSOutput mainVS(uint vertex_id : SV_VertexID, uint instance_id : SV_InstanceID)
}

#ifndef DEPTH
surface.motion = computeStaticObjectMotionVector(input.wpos);
surface.motion = computeStaticObjectMotionVector(input.pos_ws);
#endif

surface.wpos = 0;
surface.pos_ws = 0;
surface.roughness = u_roughness;
surface.metallic = u_metallic;
surface.emission = u_emission;
Expand Down
Loading

0 comments on commit c0c11cb

Please sign in to comment.