Skip to content

Commit

Permalink
feat: add water translucency water
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Pollind <[email protected]>
  • Loading branch information
pollend committed Jan 4, 2024
1 parent a2fcd34 commit 8f288b5
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 35 deletions.
12 changes: 4 additions & 8 deletions HPL2/include/graphics/RendererDeferred2.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,8 @@ namespace hpl {
SharedShader m_translucencyRefractionShaderPremulAlpha;

SharedShader m_translucencyIlluminationShaderAdd;
SharedShader m_translucencyIlluminationShaderMul;
SharedShader m_translucencyIlluminationShaderMulX2;
SharedShader m_translucencyIlluminationShaderAlpha;
SharedShader m_translucencyIlluminationShaderPremulAlpha;

SharedShader m_translucencyWaterShader;

struct BlendPipelines {
SharedPipeline m_pipelineBlendAdd;
Expand Down Expand Up @@ -309,13 +307,11 @@ namespace hpl {

SharedPipeline m_translucencyIlluminationPipline;
SharedPipeline m_translucencyIlluminationPiplineNoDepth;

BlendPipelines m_translucencyIlluminationPipline;
BlendPipelines m_translucencyIlluminationPiplineNoDepth;
SharedPipeline m_translucencyWaterPipeline;
SharedPipeline m_translucencyWaterPipelineNoDepth;

CopyTextureSubpass4 m_copySubpass;


// variables that are persistent for the frame
struct TransientFrameVars {
folly::F14ValueMap<iRenderable*, uint32_t> m_objectSlotIndex;
Expand Down
6 changes: 6 additions & 0 deletions HPL2/resource/ShaderList.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,9 @@
#include "translucency_shade.frag.fsl"
#end

#frag translucency_water_shade.frag
#define WAVE_AMPLITUDE_SCALE 0.04
#define WAVE_FREQUENCY_SCALE 10.0
#include "translucency_water_shade.frag.fsl"
#end

4 changes: 0 additions & 4 deletions HPL2/resource/parallax_bindless.h.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#define _PARALLAX_BINDLESS_H

INLINE float GetNormalizedDepth(float currentSample, float startDepth, float stopDepth, float inverseDepthRange) {
//float currentSample = 0.0;
// BeginNonUniformResourceIndex(heightIdx);
// EndNonUniformResourceIndex();

float normalizedDepth = 0.0;
if(stopDepth - startDepth > 0.0001)
{
Expand Down
12 changes: 5 additions & 7 deletions HPL2/resource/scene_defs.h.fsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef _SCENE_RESOURCE_DEFS_H_
#define _SCENE_RESOURCE_DEFS_H_

#define VK_EXT_DESCRIPTOR_INDEXING_ENABLED 1

#define PARALLAX_MULTIPLIER 0.7

#define POINT_LIGHT_MAX_COUNT (256)
Expand All @@ -20,7 +18,7 @@
#define LIGHT_CLUSTER_DATA_POS(il, ix, iy) ( LIGHT_CLUSTER_COUNT_POS(ix, iy)*LIGHT_CLUSTER_COUNT + (il) )

#define PRIMARY_VIEWPORT_INDEX 0

#define SCENE_MAX_REFLECTION_COUNT 10000
#define SCENE_MAX_TEXTURE_COUNT 10000
#define SCENE_MAX_TEXTURE_CUBE_COUNT 5000

Expand Down Expand Up @@ -53,15 +51,13 @@ STRUCT(WaterMaterial) {

DATA(float, refractionScale, None);
DATA(float, frenselBias, None);
DATA(float, frenselPos, None);
DATA(float, frenselPow, None);
DATA(float, reflectionFadeStart, None);


DATA(float, reflectionFadeEnd, None);
DATA(float, waveSpeed, None);
DATA(float, waveAmplitude, None);
DATA(float, waveFreq, None);

};


Expand Down Expand Up @@ -138,6 +134,8 @@ STRUCT(UniformObject)
#define MATERIAL_IS_ALPHA_SINGLE_CHANNEL (0x1)
#define MATERIAL_IS_HEIGHT_SINGLE_CHANNEL (0x2)
#define MATERIAL_USE_ALPHA_DISSOLVE_FILTER (0x4)
#define MATERIAL_USE_REFLECTION (0x8)
#define MATERIAL_USE_REFRACTION (0x16)

#define MATERIAL_TYPE_SOLID_DIFFUSE 1
#define MATERIAL_TYPE_SOLID_TRANSLUCENT 2
Expand Down Expand Up @@ -171,7 +169,7 @@ STRUCT(WorldInfo) {
DATA(float, worldFogLength, None);
DATA(float, oneMinusFogAlpha, None);
DATA(float, fogFalloffExp, None);
DATA(uint, __pad0, NONE);
DATA(float, afT, None);
DATA(uint, __pad1, NONE);
DATA(uint, __pad2, NONE);
DATA(float4, worldFogColor, None);
Expand Down
11 changes: 6 additions & 5 deletions HPL2/resource/scene_resource.h.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ RES(Buffer(WaterMaterial), sceneWaterMat, UPDATE_FREQ_NONE, t12, binding = 17);
RES(Tex2D(float4), visibilityTexture, UPDATE_FREQ_PER_FRAME, t13, binding = 18);
RES(Tex2D(float4), albedoTexture, UPDATE_FREQ_PER_FRAME, t14, binding = 19);
RES(Tex2D(float4), refractionTexture, UPDATE_FREQ_PER_FRAME, t15, binding = 20);
RES(Tex2D(float4), reflectionTexture[SCENE_MAX_REFLECTION_COUNT], UPDATE_FREQ_PER_FRAME, t16, binding = 20);

RES(Tex2D(float4), dissolveTexture, UPDATE_FREQ_NONE, t16, binding = 21);
RES(Tex2D(float4), dissolveTexture, UPDATE_FREQ_NONE, t21, binding = 21);

RES(ByteBuffer, lightClustersCount, UPDATE_FREQ_PER_FRAME, t17, binding = 22);
RES(ByteBuffer, lightClusters, UPDATE_FREQ_PER_FRAME, t18, binding = 23);
RES(ByteBuffer, lightClustersCount, UPDATE_FREQ_PER_FRAME, t22, binding = 22);
RES(ByteBuffer, lightClusters, UPDATE_FREQ_PER_FRAME, t23, binding = 23);

RES(Tex2D(float4), sceneTextures[SCENE_MAX_TEXTURE_COUNT], UPDATE_FREQ_PER_FRAME, t19, binding = 24);
RES(TexCube(float4), sceneCubeTextures[SCENE_MAX_TEXTURE_CUBE_COUNT], UPDATE_FREQ_PER_FRAME, t10020, binding = 25);
RES(Tex2D(float4), sceneTextures[SCENE_MAX_TEXTURE_COUNT], UPDATE_FREQ_PER_FRAME, t24, binding = 24);
RES(TexCube(float4), sceneCubeTextures[SCENE_MAX_TEXTURE_CUBE_COUNT], UPDATE_FREQ_PER_FRAME, t10024, binding = 25);

#define SCENE_OBJECT_ID_BIT 19 // 13 bits for ObjectID
#define SCENE_PRIM_ID_BIT 0 // 19 bits for PrimitiveID
Expand Down
149 changes: 149 additions & 0 deletions HPL2/resource/translucency_water_shade.frag.fsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#include "scene_resource.h.fsl"
#include "packing.h.fsl"
#include "parallax_bindless.h.fsl"

STRUCT(PsIn)
{
DATA(float4, Position, SV_Position);
DATA(float3, pos, POSITION);
DATA(float2, uv, TEXCOORD0);
#if !defined(INDIRECT_ROOT_CONSTANT)
DATA(FLAT(uint), drawID, TEXCOORD1);
#endif
DATA(float3, normal, NORMAL);
DATA(float3, tangent, TANGENT);
DATA(float3, bitangent, BITANGENT);
DATA(float4, Color, COLOR);
};

STRUCT(PsOut)
{
DATA(float4, diffuse, SV_Target0);
};

PsOut PS_MAIN(PsIn In, SV_PrimitiveID(uint) primitiveID)
{
INIT_MAIN;
PsOut Out;
#if !defined(INDIRECT_ROOT_CONSTANT)
UniformObject obj = Get(sceneObjects)[In.drawID];
#else
UniformObject obj = Get(sceneObjects)[Get(indirectDrawId)];
#endif
ViewportInfo viewInfo = Get(viewports)[PRIMARY_VIEWPORT_INDEX];

WaterMaterial material = Get(sceneWaterMat)[obj.materialIndex];

float waveAft = Get(worldInfo).afT * material.waveSpeed;
float waveAmplitude = material.waveAmplitude * WAVE_AMPLITUDE_SCALE;
float waveFrequency = material.waveFreq * WAVE_FREQUENCY_SCALE;

//Get the two uv coords
float fT1 = waveAft * 0.8;
float2 vUv1 = In.uv + waveAft * 0.01f;
vUv1.x += sin(fT1 + vUv1.y * waveFrequency) * waveAmplitude;
vUv1.y += sin(fT1 + vUv1.x * waveFrequency) * waveAmplitude;

float fT2 = waveAft * -2.6;
float2 vUv2 = In.uv + waveAft * -0.012f;
vUv2.x += sin(fT2 + vUv2.y * waveFrequency * 1.2) * waveAmplitude * 0.75;
vUv2.y += sin(fT2 + vUv2.x * waveFrequency * 1.2) * waveAmplitude * 0.75;

//Get the normals and combine into final normal
// (No need for full unpack since there is a normalize later)
float3 vNormal1;// = SampleTex2D(Get(normalMap), Get(materialSampler), vUv1).xyz - 0.5;
float3 vNormal2;// = SampleTex2D(Get(normalMap), Get(materialSampler), vUv2).xyz - 0.5;

const uint normalTextureIndex = material.normalTextureIndex;
BeginNonUniformResourceIndex(normalTextureIndex);
vNormal1 = SampleTex2D(Get(sceneTextures)[normalTextureIndex], Get(sceneSampler), vUv1).xyz - 0.5;
vNormal2 = SampleTex2D(Get(sceneTextures)[normalTextureIndex], Get(sceneSampler), vUv2).xyz - 0.5;
EndNonUniformResourceIndex();

float3 vFinalNormal = normalize(vNormal1*0.7 + vNormal2*0.3);

//Get the diffuse color
const uint diffuseTextureIndex = material.normalTextureIndex;
float4 surfaceColor; //= SampleTex2D(Get(diffuseMap), Get(materialSampler), vUv1);
BeginNonUniformResourceIndex(diffuseTextureIndex);
surfaceColor = SampleTex2D(Get(sceneTextures)[diffuseTextureIndex], Get(sceneSampler), vUv1);
EndNonUniformResourceIndex();

float4 vRefractionColor = float4(1,1,1,1);
float2 vDistortedScreenPos = float2(0.0,0.0);
if((material.config & MATERIAL_USE_REFRACTION) > 0) {
float fInvDist = min(1.0 / In.pos.z, 10.0);
vDistortedScreenPos = In.Position.xy + (viewInfo.rect.z * vFinalNormal.xy * material.refractionScale * fInvDist);
vRefractionColor = SampleTex2D(Get(refractionTexture), Get(linearBorderSampler), vDistortedScreenPos * (1.0 / viewInfo.rect.zw));
// if(vRefractionColor.a < 0.5) {
// vRefractionColor = SampleTex2D(Get(refractionMap), Get(nearestSampler), In.Position.xy * Get(viewTexel));
// }
}


float4 vReflectionColor = float4(1.0,1.0,1.0,1.0);
float fFresnel = 1.0;
if((material.config & MATERIAL_USE_REFLECTION ) > 0) {
//////////////////
//Fresnel
float3 vScreenNormal = normalize(vFinalNormal.x * In.tangent + vFinalNormal.y * In.bitangent + vFinalNormal.z * In.normal);
float3 vEyeVec = normalize(In.pos);

float afEDotN = max(dot(-vEyeVec, vScreenNormal),0.0);
fFresnel = Fresnel(afEDotN, material.frenselBias, material.frenselPow);

if(material.reflectionFadeEnd > 0) {
const float refractionFadeLength = material.reflectionFadeEnd - material.reflectionFadeStart;
fFresnel *= 1.0 - clamp( (In.pos.z - material.reflectionFadeStart) / refractionFadeLength, 0.0, 1.0);
}

//////////////////
//Cubemap
if(isTextureIndexValid(material.cubeMapTextureIndex)) {
float3 vEnvUv = reflect(vEyeVec, vScreenNormal);

vEnvUv = mul(transpose(float3x3(
viewInfo.viewMat[0].xyz,
viewInfo.viewMat[1].xyz,
viewInfo.viewMat[2].xyz)), vEnvUv);
// vEnvUv = mul(Get(invViewRotationMat), float4(vEnvUv.x, vEnvUv.y, vEnvUv.z, 1)).xyz;
//vReflectionColor = SampleTexCube(Get(cubeMap), Get(materialSampler), vEnvUv);

const uint cubeMapTextureIndex = material.cubeMapTextureIndex;
BeginNonUniformResourceIndex(cubeMapTextureIndex);
vReflectionColor = SampleTexCube(Get(sceneCubeTextures)[cubeMapTextureIndex], Get(sceneSampler), vEnvUv);
EndNonUniformResourceIndex();
} else {
//vReflectionColor = SampleTex2D(Get(reflectionMap)[ReflectionBuffer(Get(options))], Get(nearestSampler), vDistortedScreenPos * Get(viewTexel));
}
}


float3 vLightDir = normalize(float3(0.5, 0.5, 0.5));
float fLDotN = max(dot(vLightDir, vFinalNormal),0.0);
float fDiffuse = fLDotN * 0.5 + 0.5;
float fSpecular = pow(fLDotN,16.0);

float fFogAmount = 0.0;
// if (UseFog(Get(options))) {
// fFogAmount = (-In.pos.z - Get(worldFogStart)) /Get(worldFogLength);
// fFogAmount = clamp(fFogAmount, 0.0, 1.0);
// fFogAmount = pow(fFogAmount, Get(fogFalloffExp)) * Get(worldFogColor).a;
// }
if ((Get(worldInfo).worldFlags & WORLD_FLAG_IS_FOG_ENABLED) > 0) {
fFogAmount = pow(clamp((-In.pos.z - Get(worldInfo).worldFogStart) / Get(worldInfo).worldFogLength, 0.0, 1.0), Get(worldInfo).fogFalloffExp) * Get(worldInfo).worldFogColor.a;
}

if ((material.config & MATERIAL_USE_REFLECTION ) > 0) {
Out.diffuse.rgb = (surfaceColor.xyz * vRefractionColor.xyz + vReflectionColor.xyz * fFresnel) * (1.0-fFogAmount) + Get(worldInfo).worldFogColor.xyz*fFogAmount;
} else {
Out.diffuse.rgb = (surfaceColor.xyz * vRefractionColor.xyz * fDiffuse + float3(fSpecular, fSpecular, fSpecular)) * (1.0-fFogAmount) + Get(worldInfo).worldFogColor.xyz*fFogAmount;
}

Out.diffuse.w = 1.0;
RETURN(Out);
}




Loading

0 comments on commit 8f288b5

Please sign in to comment.