Skip to content

Commit

Permalink
#150 Add MetallicRoughness instancing shader
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Jul 15, 2022
1 parent 3016e93 commit 12f5028
Show file tree
Hide file tree
Showing 16 changed files with 486 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// File Generated by Assets/BuildShader.py - source: [MetallicRoughnessFS.glsl : INSTANCING]
precision highp float;
uniform sampler2D uTexAlbedo;
uniform sampler2D uTexNormal;
uniform sampler2D uTexRoughMetal;
in vec2 vTexCoord0;
in vec4 vWorldPosition;
in vec3 vWorldNormal;
in vec3 vWorldTangent;
in vec3 vWorldBinormal;
in float vTangentW;
uniform vec4 uColor;
layout(location = 0) out vec4 Diffuse;
layout(location = 1) out vec4 Position;
layout(location = 2) out vec4 Normal;
layout(location = 3) out vec4 SG;
void main(void)
{
vec3 baseMap = texture(uTexAlbedo, vTexCoord0.xy).rgb * uColor.rgb;
vec3 normalMap = texture(uTexNormal, vTexCoord0.xy).xyz;
vec3 rmMap = texture(uTexRoughMetal, vTexCoord0.xy).rgb;
mat3 rotation = mat3(vWorldTangent, vWorldBinormal, vWorldNormal);
vec3 localCoords = normalMap * 2.0 - vec3(1.0, 1.0, 1.0);
localCoords.y = localCoords.y * vTangentW;
vec3 n = rotation * localCoords;
n = normalize(n);
float roughness = rmMap.r;
float metallic = rmMap.g;
vec3 f0 = vec3(0.04);
vec3 diffuseColor = baseMap.rgb * (vec3(1.0) - f0);
diffuseColor *= 1.0 - metallic;
vec3 specularColor = mix(f0, baseMap.rgb, max(roughness, metallic));
Diffuse = vec4(diffuseColor, 1.0);
Position = vWorldPosition;
Normal = vec4(n, 1.0);
SG = vec4(max(specularColor.r, specularColor.g), 1.0 - roughness, 1.0, 1.0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// File Generated by Assets/BuildShader.py - source: [SpecularGlossinessVS.glsl : INSTANCING]
in vec4 inPosition;
in vec3 inNormal;
in vec4 inColor;
in vec2 inTexCoord0;
in vec3 inTangent;
in vec3 inBinormal;
in vec2 inData;
uniform mat4 uMvpMatrix;
uniform mat4 uWorldMatrix;
uniform mat4 uView;
uniform vec4 uUVScale;
out vec3 vWorldNormal;
out vec4 vWorldPosition;
out vec3 vWorldTangent;
out vec3 vWorldBinormal;
out vec2 vTexCoord0;
out float vTangentW;
void main(void)
{
vWorldPosition = uWorldMatrix*inPosition;
vec4 sampleFragPos = uView * vWorldPosition;
vWorldPosition.w = sampleFragPos.z;
vec4 worldNormal = uWorldMatrix * vec4(inNormal, 0.0);
vec4 worldTangent = uWorldMatrix * vec4(inTangent, 0.0);
vWorldNormal = normalize(worldNormal.xyz);
vWorldTangent = normalize(worldTangent.xyz);
vWorldBinormal = normalize(cross(vWorldNormal.xyz, vWorldTangent.xyz));
vTexCoord0 = inTexCoord0 * uUVScale.xy + uUVScale.zw;
vTangentW = inData.x;
gl_Position = uMvpMatrix * inPosition;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ struct PS_INPUT
float3 worldTangent: WORLDTANGENT;
float3 worldBinormal: WORLDBINORMAL;
float tangentw : TANGENTW;
#ifdef INSTANCING
float4 color: COLOR;
float2 specGloss: SPECGLOSS;
#endif
};

struct PS_OUTPUT
Expand All @@ -26,16 +30,25 @@ struct PS_OUTPUT
float4 SG: SV_TARGET3;
};

#ifndef INSTANCING
cbuffer cbPerFrame
{
float4 uColor;
};
#endif

PS_OUTPUT main(PS_INPUT input)
{
PS_OUTPUT output;

float3 baseMap = uTexAlbedo.Sample(uTexAlbedoSampler, input.tex0).rgb * uColor.rgb;
float3 baseMap = uTexAlbedo.Sample(uTexAlbedoSampler, input.tex0).rgb;

#ifdef INSTANCING
baseMap *= input.color.rgb;
#else
baseMap *= uColor.rgb;
#endif

float3 normalMap = uTexNormal.Sample(uTexNormalSampler, input.tex0).xyz;
float3 rmMap = uTexRoughMetal.Sample(uTexRoughMetalSampler, input.tex0).xyz;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// File Generated by Assets/BuildShader.py - source: [MetallicRoughnessFS.hlsl : INSTANCING]
Texture2D uTexAlbedo : register(t0);
SamplerState uTexAlbedoSampler : register(s0);
Texture2D uTexNormal : register(t1);
SamplerState uTexNormalSampler : register(s1);
Texture2D uTexRoughMetal : register(t2);
SamplerState uTexRoughMetalSampler : register(s2);
struct PS_INPUT
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
float4 worldPosition: WORLDPOSITION;
float3 worldNormal: WORLDNORMAL;
float3 worldTangent: WORLDTANGENT;
float3 worldBinormal: WORLDBINORMAL;
float tangentw : TANGENTW;
float4 color: COLOR;
float2 specGloss: SPECGLOSS;
};
struct PS_OUTPUT
{
float4 Diffuse: SV_TARGET0;
float4 Position: SV_TARGET1;
float4 Normal: SV_TARGET2;
float4 SG: SV_TARGET3;
};
PS_OUTPUT main(PS_INPUT input)
{
PS_OUTPUT output;
float3 baseMap = uTexAlbedo.Sample(uTexAlbedoSampler, input.tex0).rgb;
baseMap *= input.color.rgb;
float3 normalMap = uTexNormal.Sample(uTexNormalSampler, input.tex0).xyz;
float3 rmMap = uTexRoughMetal.Sample(uTexRoughMetalSampler, input.tex0).xyz;
float3x3 rotation = float3x3(input.worldTangent, input.worldBinormal, input.worldNormal);
float3 localCoords = normalMap * 2.0 - float3(1.0, 1.0, 1.0);
localCoords.y *= input.tangentw;
float3 n = mul(localCoords, rotation);
n = normalize(n);
float roughness = rmMap.r;
float metallic = rmMap.g;
float3 f0 = float3(0.04, 0.04, 0.04);
float3 diffuseColor = baseMap.rgb * (float3(1.0, 1.0, 1.0) - f0);
diffuseColor *= (1.0 - metallic);
float3 specularColor = lerp(f0, baseMap.rgb, max(roughness, metallic));
output.Diffuse = float4(diffuseColor, 1.0);
output.Position = input.worldPosition;
output.Normal = float4(n, 1.0);
output.SG = float4(max(specularColor.r, specularColor.g), 1.0 - roughness, 1.0, 1.0);
return output;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ struct VS_INPUT
float3 tangent: TANGENT;
float3 binormal: BINORMAL;
float2 data: DATA;
#ifdef INSTANCING
float4 uvScale: TEXCOORD1;
float4 uColor: TEXCOORD2;
float2 uSpecGloss: TEXCOORD3;
float3 sh0: TEXCOORD4;
float3 sh1: TEXCOORD5;
float3 sh2: TEXCOORD6;
float3 sh3: TEXCOORD7;
float4x4 worldMatrix: TEXCOORD8;
#endif
};

struct VS_OUTPUT
Expand All @@ -18,19 +28,40 @@ struct VS_OUTPUT
float3 worldTangent: WORLDTANGENT;
float3 worldBinormal: WORLDBINORMAL;
float tangentw : TANGENTW;
#ifdef INSTANCING
float4 color: COLOR;
float2 specGloss: SPECGLOSS;
#endif
};

#ifdef INSTANCING
cbuffer cbPerObject
{
float4x4 uVPMatrix;
float4x4 uView;
};
#else
cbuffer cbPerObject
{
float4x4 uMvpMatrix;
float4x4 uWorldMatrix;
float4x4 uView;
float4 uUVScale;
};
#endif

VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT output;

#ifdef INSTANCING
output.color = input.uColor;
output.specGloss = input.uSpecGloss;
float4x4 uWorldMatrix = transpose(input.worldMatrix);
float4 uUVScale = input.uvScale;
float4x4 uMvpMatrix = mul(uWorldMatrix, uVPMatrix);
#endif

output.pos = mul(input.pos, uMvpMatrix);
output.tex0 = input.tex0 * uUVScale.xy + uUVScale.zw;
output.tangentw = input.data.x;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// File Generated by Assets/BuildShader.py - source: [SpecularGlossinessVS.hlsl : INSTANCING]
struct VS_INPUT
{
float4 pos: POSITION;
float3 norm: NORMAL;
float4 color: COLOR;
float2 tex0: TEXCOORD0;
float3 tangent: TANGENT;
float3 binormal: BINORMAL;
float2 data: DATA;
float4 uvScale: TEXCOORD1;
float4 uColor: TEXCOORD2;
float2 uSpecGloss: TEXCOORD3;
float3 sh0: TEXCOORD4;
float3 sh1: TEXCOORD5;
float3 sh2: TEXCOORD6;
float3 sh3: TEXCOORD7;
float4x4 worldMatrix: TEXCOORD8;
};
struct VS_OUTPUT
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
float4 worldPosition: WORLDPOSITION;
float3 worldNormal: WORLDNORMAL;
float3 worldTangent: WORLDTANGENT;
float3 worldBinormal: WORLDBINORMAL;
float tangentw : TANGENTW;
float4 color: COLOR;
float2 specGloss: SPECGLOSS;
};
cbuffer cbPerObject
{
float4x4 uVPMatrix;
float4x4 uView;
};
VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT output;
output.color = input.uColor;
output.specGloss = input.uSpecGloss;
float4x4 uWorldMatrix = transpose(input.worldMatrix);
float4 uUVScale = input.uvScale;
float4x4 uMvpMatrix = mul(uWorldMatrix, uVPMatrix);
output.pos = mul(input.pos, uMvpMatrix);
output.tex0 = input.tex0 * uUVScale.xy + uUVScale.zw;
output.tangentw = input.data.x;
float4 worldPos = mul(input.pos, uWorldMatrix);
float4 worldNormal = mul(float4(input.norm, 0.0), uWorldMatrix);
float4 worldTangent = mul(float4(input.tangent, 0.0), uWorldMatrix);
float4 sampleFragPos = mul(worldPos, uView);
float sampleDepth = sampleFragPos.z;
output.worldPosition = float4(worldPos.xyz, sampleDepth);
output.worldNormal = normalize(worldNormal.xyz);
output.worldTangent = normalize(worldTangent.xyz);
output.worldBinormal = normalize(cross(worldNormal.xyz, worldTangent.xyz));
return output;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<shaderConfig name="MetallicRoughness" baseShader="SOLID" deferred="true">
<shaderConfig name="MetallicRoughness" baseShader="SOLID" deferred="true" instancing="MetallicRoughnessInstancing">
<uniforms>
<vs>
<uniform name="uMvpMatrix" type="WORLD_VIEW_PROJECTION" value="0" float="16" matrix="true"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<shaderConfig name="MetallicRoughnessInstancing" baseShader="SOLID" deferred="true">
<uniforms>
<vs>
<uniform name="uVPMatrix" type="VIEW_PROJECTION" value="0" float="16" matrix="true"/>
<uniform name="uView" type="DEFERRED_VIEW" value="0" float="16" matrix="true"/>
</vs>
<fs>
<uniform name="uTexAlbedo" type="DEFAULT_VALUE" value="0" float="1" directX="false"/>
<uniform name="uTexNormal" type="DEFAULT_VALUE" value="1" float="1" directX="false" normal="true"/>
<uniform name="uTexRoughMetal" type="DEFAULT_VALUE" value="2" float="1" directX="false"/>
</fs>
</uniforms>
<resources>
</resources>
<customUI>
</customUI>
<shader type="GLSL"
vs="GLSL/SpecularGlossinessVSInstancing.glsl"
fs="GLSL/MetallicRoughnessFSInstancing.glsl"
vs_source="GLSL/SpecularGlossinessVS.glsl"
fs_source="GLSL/MetallicRoughnessFS.glsl"
define="INSTANCING"/>
<shader type="HLSL"
vs="HLSL/SpecularGlossinessVSInstancing.hlsl"
fs="HLSL/MetallicRoughnessFSInstancing.hlsl"
vs_source="HLSL/SpecularGlossinessVS.hlsl"
fs_source="HLSL/MetallicRoughnessFS.hlsl"
define="INSTANCING"/>
</shaderConfig>
4 changes: 2 additions & 2 deletions Projects/Skylicht/Engine/Source/Material/Shader/CShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,8 @@ namespace Skylicht
{
int matID = getVideoDriver()->getGPUProgrammingServices()->addHighLevelShaderMaterialFromFiles
(
vs.c_str(), "main", video::EVST_VS_4_0,
fs.c_str(), "main", video::EPST_PS_4_0,
vs.c_str(), "main", video::EVST_VS_5_0,
fs.c_str(), "main", video::EPST_PS_5_0,
this,
m_baseShader
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This file is part of the "Skylicht Engine".
#include "Utils/CPath.h"

#include "Instancing/CStandardSGInstancing.h"
#include "Instancing/CTBNSGInstancing.h"

namespace Skylicht
{
Expand Down Expand Up @@ -143,6 +144,7 @@ namespace Skylicht
void CShaderManager::initSGDeferredShader()
{
loadShader("BuiltIn/Shader/SpecularGlossiness/Deferred/ColorInstancing.xml");
loadShader("BuiltIn/Shader/SpecularGlossiness/Deferred/MetallicRoughnessInstancing.xml");

loadShader("BuiltIn/Shader/Lightmap/LMStandardSGInstancing.xml");

Expand All @@ -158,7 +160,9 @@ namespace Skylicht
shader = loadShader("BuiltIn/Shader/SpecularGlossiness/Deferred/Diffuse.xml");
shader = loadShader("BuiltIn/Shader/SpecularGlossiness/Deferred/SpecularGlossiness.xml");
shader = loadShader("BuiltIn/Shader/SpecularGlossiness/Deferred/SpecularGlossinessMask.xml");

shader = loadShader("BuiltIn/Shader/SpecularGlossiness/Deferred/MetallicRoughness.xml");
if (shader) shader->setInstancing(new CTBNSGInstancing());

loadShader("BuiltIn/Shader/SpecularGlossiness/Lighting/SGDirectionalLight.xml");
loadShader("BuiltIn/Shader/SpecularGlossiness/Lighting/SGDirectionalLightSSR.xml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace Skylicht
IMeshBuffer* CStandardSGInstancing::createMeshBuffer(video::E_INDEX_TYPE type)
{
return new CMeshBuffer<S3DVertex>(m_baseVtxDescriptor, type);
}
}

void CStandardSGInstancing::batchIntancing(IVertexBuffer* vtxBuffer,
core::array<CMaterial*>& materials,
Expand All @@ -102,6 +102,8 @@ namespace Skylicht
CMaterial** matData = materials.pointer();
CIndirectLightingData** lightingData = lightings.pointer();

float invColor = 1.111f / 255.0f;

for (u32 i = 0; i < count; i++)
{
SVtxSGInstancing& vtx = instanceBuffer->getVertex(i);
Expand All @@ -115,12 +117,26 @@ namespace Skylicht

// sh lighting
CIndirectLightingData* light = lightingData[i];
for (int j = 0; j < 4; j++)
if (light->Type == CIndirectLightingData::SH9)
{
if (light->SH)
vtx.SH[j].set(light->SH[j]);
else
for (int j = 0; j < 4; j++)
{
if (light->SH)
vtx.SH[j].set(light->SH[j]);
else
vtx.SH[j].set(0.0f, 0.0f, 0.0f);
}
}
else
{
for (int j = 0; j < 4; j++)
vtx.SH[j].set(0.0f, 0.0f, 0.0f);

if (light->Type == CIndirectLightingData::AmbientColor)
{
// save ambient to SH0
vtx.SH[0].set(light->Color.getRed() * invColor, light->Color.getGreen() * invColor, light->Color.getBlue() * invColor);
}
}

// world transform
Expand Down
Loading

0 comments on commit 12f5028

Please sign in to comment.