Skip to content

Commit

Permalink
feat: #47 Implement MRTT Deferred Rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Jan 16, 2020
1 parent d4b58f0 commit 1568760
Show file tree
Hide file tree
Showing 15 changed files with 264 additions and 20 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
Expand Up @@ -29,7 +29,7 @@ void main(void)
vWorldTangent = normalize(worldTangent.xyz);
vWorldBinormal = cross(vWorldNormal.xyz, vWorldTangent.xyz);

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

gl_Position = uMvpMatrix * inPosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT output;
output.pos = mul(input.pos, uMvpMatrix);
output.tex0 = input.tex0 * uUVScale.xy + uUVScale.zw;
output.tex0 = input.tex0;// * uUVScale.xy + uUVScale.zw;
output.tangentw = input.tangentw.x;

float4 worldPos = mul(input.pos, uWorldMatrix);
Expand Down
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());
}
}
15 changes: 11 additions & 4 deletions Projects/Demo/Source/View/CViewInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,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 Down Expand Up @@ -87,11 +89,16 @@ void CViewInit::initScene()
textureFolders.push_back("Demo/Sponza/Textures");
// load model
prefab = meshManager->loadModel("Demo/Sponza/Sponza.dae", NULL, false);
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();
Expand Down Expand Up @@ -190,8 +197,8 @@ void CViewInit::onUpdate()
// retry download
delete m_getFile;
m_getFile = NULL;
}
}
}
}
#else

#if defined(WINDOWS_STORE)
Expand Down
4 changes: 4 additions & 0 deletions Projects/Skylicht/Engine/Source/Material/CMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,10 @@ namespace Skylicht
{
t->Path = uniform->Path;
t->Texture = uniform->Texture;

if (t->Texture == NULL)
t->Texture = CTextureManager::getInstance()->getTexture(t->Path.c_str());

break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,7 @@ namespace Skylicht
break;
case CShader::NODE_PARAM:
{

}
break;
case CShader::SHADER_VEC2:
Expand Down
126 changes: 123 additions & 3 deletions Projects/Skylicht/Engine/Source/RenderPipeline/CBaseRP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,141 @@ This file is part of the "Skylicht Engine".

namespace Skylicht
{
CBaseRP::CBaseRP()
CBaseRP::CBaseRP() :
m_next(NULL)
{
const core::dimension2du &size = getVideoDriver()->getCurrentRenderTargetSize();
m_viewport2DW = (float)size.Width;
m_viewport2DH = (float)size.Height;

m_drawBuffer = new scene::CMeshBuffer<video::S3DVertex2TCoords>(getVideoDriver()->getVertexDescriptor(video::EVT_2TCOORDS), video::EIT_16BIT);
m_drawBuffer->setHardwareMappingHint(EHM_STREAM);

m_verticesImage = m_drawBuffer->getVertexBuffer();
m_indicesImage = m_drawBuffer->getIndexBuffer();

// init index
m_indicesImage->set_used(6);
u16 *index = (u16*)m_indicesImage->getIndices();
index[0] = 0;
index[1] = 1;
index[2] = 2;
index[3] = 0;
index[4] = 2;
index[5] = 3;
}

CBaseRP::~CBaseRP()
{

m_drawBuffer->drop();
m_verticesImage = NULL;
m_indicesImage = NULL;
}

void CBaseRP::setCamera(CCamera *camera)
{
const SViewFrustum& viewArea = camera->getViewFrustum();
const SViewFrustum& viewArea = camera->getViewFrustum();
video::IVideoDriver* driver = getVideoDriver();
driver->setTransform(video::ETS_PROJECTION, viewArea.getTransform(video::ETS_PROJECTION));
driver->setTransform(video::ETS_VIEW, viewArea.getTransform(video::ETS_VIEW));
}

void CBaseRP::setNextPipeLine(IRenderPipeline *next)
{
m_next = next;
}

void CBaseRP::onNext(ITexture *target, CCamera *camera, CEntityManager* entity)
{
if (m_next != NULL)
m_next->render(target, camera, entity);
}

void CBaseRP::beginRender2D(float w, float h)
{
core::matrix4 orthoMatrix;
orthoMatrix.makeIdentity();
orthoMatrix.buildProjectionMatrixOrthoLH(w, -h, -1.0f, 1.0f);
orthoMatrix.setTranslation(core::vector3df(-1, 1, 0));

IVideoDriver *driver = getVideoDriver();

driver->setTransform(video::ETS_PROJECTION, orthoMatrix);
driver->setTransform(video::ETS_VIEW, core::IdentityMatrix);
driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);

m_viewport2DW = w;
m_viewport2DH = h;
}

void CBaseRP::renderBufferToTarget(float sx, float sy, float sw, float sh, SMaterial& material, bool flipY, bool flipX)
{
ITexture *tex = material.getTexture(0);
if (tex == NULL)
return;

IVideoDriver *driver = getVideoDriver();

int numVerticesUse = 4;

m_verticesImage->set_used(numVerticesUse);
S3DVertex2TCoords *vertices = (S3DVertex2TCoords*)m_verticesImage->getVertices();
SColor color(255, 255, 255, 255);

float x = 0;
float y = 0;
float w = m_viewport2DW;
float h = m_viewport2DH;

float texW = (float)tex->getSize().Width;
float texH = (float)tex->getSize().Height;

const f32 invW = 1.f / static_cast<f32>(texW);
const f32 invH = 1.f / static_cast<f32>(texH);

float tx1 = sx * invW;
float ty1 = sy * invH;
float tw1 = tx1 + sw * invW;
float th1 = ty1 + sh * invH;

float tx2 = 0.0f;
float ty2 = 0.0f;
float tw2 = 1.0f;
float th2 = 1.0f;

if (driver->getDriverType() != EDT_DIRECT3D11)
{
if (flipY)
{
ty1 = 1.0f - ty1;
th1 = 1.0f - th1;

ty2 = 1.0f - ty2;
th2 = 1.0f - th2;
}

if (flipX)
{
tx1 = 1.0f - tx1;
tw1 = 1.0f - tw1;

tx2 = 1.0f - tx2;
tw2 = 1.0f - tw2;
}
}

// add vertices
vertices[0] = S3DVertex2TCoords(x, y, 0.0f, 0.0f, 0.0f, 1.0f, color, tx1, ty1, tx2, ty2);
vertices[1] = S3DVertex2TCoords(x + w, y, 0.0f, 0.0f, 0.0f, 1.0f, color, tw1, ty1, tw2, ty2);
vertices[2] = S3DVertex2TCoords(x + w, y + h, 0.0f, 0.0f, 0.0f, 1.0f, color, tw1, th1, tw2, th2);
vertices[3] = S3DVertex2TCoords(x, y + h, 0.0f, 0.0f, 0.0f, 1.0f, color, tx1, th1, tx2, th2);

// no depth test
material.ZBuffer = video::ECFN_DISABLED;
material.ZWriteEnable = false;

// draw buffer
driver->setMaterial(material);
driver->drawMeshBuffer(m_drawBuffer);
}
}
22 changes: 21 additions & 1 deletion Projects/Skylicht/Engine/Source/RenderPipeline/CBaseRP.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,33 @@ namespace Skylicht
{
class CBaseRP : public IRenderPipeline
{
protected:
IRenderPipeline *m_next;

CMeshBuffer<video::S3DVertex2TCoords> *m_drawBuffer;
IVertexBuffer* m_verticesImage;
IIndexBuffer* m_indicesImage;

float m_viewport2DW;
float m_viewport2DH;

public:
CBaseRP();

virtual ~CBaseRP();

virtual void render(CCamera *camera, CEntityManager *entityManager) = 0;
virtual void render(ITexture *target, CCamera *camera, CEntityManager *entityManager) = 0;

virtual void setCamera(CCamera *camera);

virtual void setNextPipeLine(IRenderPipeline *next);

virtual void onNext(ITexture *target, CCamera *camera, CEntityManager* entity);

public:

void beginRender2D(float w, float h);

void renderBufferToTarget(float sx, float sy, float sw, float sh, SMaterial& material, bool flipY = true, bool flipX = false);
};
}
Loading

0 comments on commit 1568760

Please sign in to comment.