Skip to content

Commit

Permalink
Merge pull request #49 from skylicht-lab/feature/#47-add-deferred-ren…
Browse files Browse the repository at this point in the history
…dering

Feature/#47 add deferred rendering
  • Loading branch information
ducphamhong committed Jan 18, 2020
2 parents f96a23d + 46af989 commit 73be5cf
Show file tree
Hide file tree
Showing 67 changed files with 3,464 additions and 184 deletions.
2 changes: 1 addition & 1 deletion Assets/BuiltIn/Shader/Basic/TextureColor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</uniforms>
<customUI>
<ui control="UIGroup" name="Texture">
<ui control="UITexture" name="uTexDiffuse" autoReplace="_diff.tga"/>
<ui control="UITexture" name="uTexDiffuse" autoReplace="_diff.tga"/>
</ui>
</customUI>
<shader type="GLSL" vs="GLSL/TextureColorVS.glsl" fs="GLSL/TextureColorFS.glsl"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
precision highp float;

uniform sampler2D uTexDiffuse;
uniform sampler2D uTexNormal;
uniform sampler2D uTexSpecGloss;

in vec2 vTexCoord0;
in vec3 vWorldPosition;
in vec3 vWorldNormal;
in vec3 vWorldTangent;
in vec3 vWorldBinormal;
in float vTangentW;

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(uTexDiffuse, vTexCoord0.xy).xyz;
vec3 normalMap = texture(uTexNormal, vTexCoord0.xy).xyz;
vec3 sgMap = texture(uTexSpecGloss, vTexCoord0.xy).xyz;

mat3 rotation = mat3(vWorldTangent, vWorldBinormal, vWorldNormal);
vec3 localCoords = normalMap * 2.0 - vec3(1.0);
localCoords.y = localCoords.y * vTangentW;
vec3 n = rotation * localCoords;
n = normalize(n);

Diffuse = vec4(baseMap, 1.0);
Position = vec4(vWorldPosition, 1.0);
Normal = vec4(n, 1.0);
SG = vec4(sgMap, 1.0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
in vec4 inPosition;
in vec4 inColor;
in vec3 inNormal;
in vec2 inTexCoord0;
in vec3 inTangent;
in vec3 inBinormal;
in vec2 inTangentW;

uniform mat4 uMvpMatrix;
uniform mat4 uWorldMatrix;
uniform vec4 uUVScale;

out vec3 vWorldNormal;
out vec3 vWorldPosition;
out vec3 vWorldTangent;
out vec3 vWorldBinormal;

out vec2 vTexCoord0;
out float vTangentW;

void main(void)
{
vWorldPosition = (uWorldMatrix*inPosition).xyz;

vec4 worldNormal = uWorldMatrix * vec4(inNormal,0.0);
vec4 worldTangent = uWorldMatrix * vec4(inTangent,0.0);

vWorldNormal = normalize(worldNormal.xyz);
vWorldTangent = normalize(worldTangent.xyz);
vWorldBinormal = cross(vWorldNormal.xyz, vWorldTangent.xyz);

vTexCoord0 = inTexCoord0;// * uUVScale.xy + uUVScale.zw;
vTangentW = inTangentW.x;

gl_Position = uMvpMatrix * inPosition;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Texture2D uTexDiffuse : register(t0);
SamplerState uTexDiffuseSampler : register(s0);

Texture2D uTexNormal : register(t1);
SamplerState uTexNormalSampler : register(s1);

Texture2D uTexSpecGloss : register(t2);
SamplerState uTexSpecGlossSampler : register(s2);

struct PS_INPUT
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
float3 worldPosition: WORLDPOSITION;
float3 worldNormal: WORLDNORMAL;
float3 worldTangent: WORLDTANGENT;
float3 worldBinormal: WORLDBINORMAL;
float tangentw : TANGENTW;
};

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 = uTexDiffuse.Sample(uTexDiffuseSampler, input.tex0).xyz;
float3 normalMap = uTexNormal.Sample(uTexNormalSampler, input.tex0).xyz;
float3 sgMap = uTexSpecGloss.Sample(uTexSpecGlossSampler, 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);

output.Diffuse = float4(baseMap, 1.0);
output.Position = float4(input.worldPosition, 1.0);
output.Normal = float4(n, 1.0);
output.SG = float4(sgMap, 1.0);

return output;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
struct VS_INPUT
{
float4 pos: POSITION;
float3 norm: NORMAL;
float4 color: COLOR;
float2 tex0: TEXCOORD0;
float3 tangent: TANGENT;
float3 binormal: BINORMAL;
float2 tangentw: TANGENTW;
};

struct VS_OUTPUT
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
float3 worldPosition: WORLDPOSITION;
float3 worldNormal: WORLDNORMAL;
float3 worldTangent: WORLDTANGENT;
float3 worldBinormal: WORLDBINORMAL;
float tangentw : TANGENTW;
};

cbuffer cbPerObject
{
float4x4 uMvpMatrix;
float4x4 uWorldMatrix;
float4 uUVScale;
};

VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT output;
output.pos = mul(input.pos, uMvpMatrix);
output.tex0 = input.tex0;// * uUVScale.xy + uUVScale.zw;
output.tangentw = input.tangentw.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);

output.worldPosition = worldPos.xyz;
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
@@ -0,0 +1,25 @@
<shaderConfig name="SpecularGlossiness" baseShader="SOLID" deferred="true">
<uniforms>
<vs>
<uniform name="uMvpMatrix" type="WORLD_VIEW_PROJECTION" value="0" float="16" matrix="true"/>
<uniform name="uWorldMatrix" type="WORLD" value="0" float="16" matrix="true"/>
<uniform name="uUVScale" type="NODE_PARAM" valueIndex="0" value="1.0,1.0,0.0,0.0" float="4"/>
</vs>
<fs>
<uniform name="uTexDiffuse" 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="uTexSpecGloss" type="DEFAULT_VALUE" value="2" float="1" directX="false"/>
</fs>
</uniforms>
<resources>
</resources>
<customUI>
<ui control="UIGroup" name="Texture">
<ui control="UITexture" name="uTexDiffuse" autoReplace="_diff."/>
<ui control="UITexture" name="uTexNormal" autoReplace="_norm.;_ddn.;_n."/>
<ui control="UITexture" name="uTexSpecGloss" autoReplace="_spec.;_s."/>
</ui>
</customUI>
<shader type="GLSL" vs="GLSL/SpecularGlossinessVS.glsl" fs="GLSL/SpecularGlossinessFS.glsl"/>
<shader type="HLSL" vs="HLSL/SpecularGlossinessVS.hlsl" fs="HLSL/SpecularGlossinessFS.hlsl"/>
</shaderConfig>
Binary file added Assets/BuiltIn/Textures/NullNormalMap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/BuiltIn/Textures/NullTexture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Projects/Demo/Source/Context/CContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void CContext::releaseScene()

CBaseRP* CContext::initRenderPipeline(int w, int h)
{
m_rendering = new CForwardRP();
m_rendering = new CDeferredRP();
m_rendering->initRender(w, h);
return m_rendering;
}
Expand Down
4 changes: 2 additions & 2 deletions Projects/Demo/Source/View/CViewDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void CViewDemo::onDestroy()
void CViewDemo::onUpdate()
{
CContext *context = CContext::getInstance();
CScene *scene = context->getScene();
CScene *scene = context->getScene();
if (scene != NULL)
scene->update();
}
Expand All @@ -40,6 +40,6 @@ void CViewDemo::onRender()

if (camera != NULL && scene != NULL)
{
context->getRenderPipeline()->render(camera, scene->getEntityManager());
context->getRenderPipeline()->render(NULL, camera, scene->getEntityManager());
}
}
44 changes: 37 additions & 7 deletions Projects/Demo/Source/View/CViewInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

#include "Context/CContext.h"

#include "Material/CShaderManager.h"
#include "Material/Shader/CShaderManager.h"
#include "Material/CMaterialManager.h"

#include "Camera/CEditorCamera.h"
#include "GridPlane/CGridPlane.h"
Expand Down Expand Up @@ -47,7 +48,9 @@ void CViewInit::onInit()
{
getApplication()->getFileSystem()->addFileArchive(getBuiltInPath("BuiltIn.zip"), false, false);

CShaderManager::getInstance()->initBasicShader();
CShaderManager *shaderMgr = CShaderManager::getInstance();
shaderMgr->initBasicShader();
shaderMgr->loadShader("BuiltIn/Shader/SpecularGlossiness/Deferred/SpecularGlossiness.xml");
}

void CViewInit::initScene()
Expand All @@ -60,10 +63,10 @@ void CViewInit::initScene()
// camera
CGameObject *camObj = zone->createEmptyObject();
camObj->addComponent<CCamera>();
camObj->addComponent<CEditorCamera>();
camObj->addComponent<CEditorCamera>()->setMoveSpeed(2.0f);

CCamera *camera = camObj->getComponent<CCamera>();
camera->setPosition(core::vector3df(3.0f, 3.0f, 3.0f));
camera->setPosition(core::vector3df(2.0f, 1.0f, 2.0f));
camera->lookAt(core::vector3df(0.0f, 0.0f, 0.0f), core::vector3df(0.0f, 1.0f, 0.0f));

// sky
Expand All @@ -75,7 +78,35 @@ void CViewInit::initScene()
}

// grid
zone->createEmptyObject()->addComponent<CGridPlane>();
// zone->createEmptyObject()->addComponent<CGridPlane>();

// sponza
/*
CMeshManager *meshManager = CMeshManager::getInstance();
CEntityPrefab *prefab = NULL;
std::vector<std::string> textureFolders;
textureFolders.push_back("Demo/Sponza/Textures");
// load model
prefab = meshManager->loadModel("Demo/Sponza/Sponza.dae", NULL, true);
if (prefab != NULL)
{
// export model material
ArrayMaterial& materials = CMaterialManager::getInstance()->loadMaterial("Demo/Sponza/Sponza.xml", true, textureFolders);
for (CMaterial *&material : materials)
{
material->changeShader("BuiltIn/Shader/SpecularGlossiness/Deferred/SpecularGlossiness.xml");
material->autoDetectLoadTexture();
}
// create render mesh object
CGameObject *sponza = zone->createEmptyObject();
CRenderMesh *renderer = sponza->addComponent<CRenderMesh>();
renderer->initFromPrefab(prefab);
renderer->initMaterial(materials);
}
*/

// test dae model & animation
/*
Expand All @@ -84,8 +115,7 @@ void CViewInit::initScene()
CAnimationClip *animWalkForward = animManager->loadAnimation("Demo/Model3D/Hero@WalkForward.dae");
CAnimationClip *animRunForward = animManager->loadAnimation("Demo/Model3D/Hero@RunForward.dae");
CMeshManager *meshManager = CMeshManager::getInstance();
CEntityPrefab *prefab = meshManager->loadModel("Demo/Model3D/Hero.dae", "Demo/Model3D/Textures", false);
prefab = meshManager->loadModel("Demo/Model3D/Hero.dae", "Demo/Model3D/Textures", false);
if (prefab != NULL)
{
// instance object 1
Expand Down
3 changes: 1 addition & 2 deletions Projects/Irrlicht/Source/CD3D11CallBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ void CD3D11CallBridge::setPixelShader(SShader* shader)
{
shaders[EST_PIXEL_SHADER] = shader;

// Duc Pham Hong add
// need reset sampler state
// duc.phamhong: need reset sampler state when shader is changed
samplersChanged = 0;
texturesChanged = 0;

Expand Down
19 changes: 9 additions & 10 deletions Projects/Irrlicht/Source/CD3D11Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ namespace irr
// Try creating hardware device
//! If you got an error here that 'D3D_FEATURE_LEVEL_11_1' is undeclared you have to download an appropriate DXSDK
//! Download: http://msdn.microsoft.com/en-us/windows/desktop/hh852363.aspx
D3D_FEATURE_LEVEL RequestedLevels[] = {
D3D_FEATURE_LEVEL RequestedLevels[] = {
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
Expand Down Expand Up @@ -352,13 +352,12 @@ namespace irr
#else
// Preparing for swap chain creation
::ZeroMemory(&present, sizeof(DXGI_SWAP_CHAIN_DESC));
present.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
present.BufferCount = 1;
present.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
present.BufferCount = 2;
present.BufferDesc.Format = D3DColorFormat;
present.BufferDesc.Width = Params.WindowSize.Width;
present.BufferDesc.Height = Params.WindowSize.Height;
present.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
//present.BufferUsage |= DXGI_USAGE_SHADER_INPUT;
present.SampleDesc.Count = 1;
present.SampleDesc.Quality = 0;
present.OutputWindow = (HWND)Params.WindowId;
Expand Down Expand Up @@ -418,7 +417,7 @@ namespace irr
{
present.SampleDesc.Count = Params.AntiAlias;
present.SampleDesc.Quality = qualityLevels - 1;
present.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
present.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
break;
}
--Params.AntiAlias;
Expand Down Expand Up @@ -759,7 +758,7 @@ namespace irr
default:
return false;
}
}
}

bool CD3D11Driver::setActiveTexture(u32 stage, video::ITexture* texture)
{
Expand Down Expand Up @@ -1537,7 +1536,7 @@ namespace irr


void CD3D11Driver::OnResize(const core::dimension2d<u32>& size)
{
{
if (!Device || !SwapChain)
return;

Expand Down Expand Up @@ -2704,7 +2703,7 @@ namespace irr
#if defined(WINDOWS_STORE)
hr = SwapChain->ResizeBuffers(2, lround(ScreenSize.Width), lround(ScreenSize.Height), D3DColorFormat, 0);
#else
hr = SwapChain->ResizeBuffers(1, ScreenSize.Width, ScreenSize.Height, D3DColorFormat, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH);
hr = SwapChain->ResizeBuffers(2, ScreenSize.Width, ScreenSize.Height, D3DColorFormat, 0);
#endif
if (FAILED(hr))
{
Expand Down Expand Up @@ -2907,8 +2906,8 @@ namespace irr
setViewPort(core::rect<s32>(0, 0, size.Width, size.Height));
}

} // end namespace video
} // end namespace irr
} // end namespace video
} // end namespace irr

#endif

Expand Down
Loading

0 comments on commit 73be5cf

Please sign in to comment.