Skip to content

Commit

Permalink
Gloom: Working on the deferred shading pass
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent e2bd4e0 commit 2575b35
Show file tree
Hide file tree
Showing 29 changed files with 584 additions and 88 deletions.
2 changes: 2 additions & 0 deletions doomsday/tests/test_gloom/CMakeLists.txt
Expand Up @@ -59,6 +59,8 @@ set (SOURCES
gloom/render/skybox.h
gloom/render/ssao.cpp
gloom/render/ssao.h
gloom/render/tonemap.cpp
gloom/render/tonemap.h
gloom/render/view.cpp
gloom/render/view.h
gloom/world/entity.cpp
Expand Down
44 changes: 32 additions & 12 deletions doomsday/tests/test_gloom/gloom/gloomworld.cpp
Expand Up @@ -22,8 +22,10 @@
#include "gloom/render/context.h"
#include "gloom/render/gbuffer.h"
#include "gloom/render/maprender.h"
#include "gloom/render/lightrender.h"
#include "gloom/render/skybox.h"
#include "gloom/render/ssao.h"
#include "gloom/render/tonemap.h"
#include "gloom/world/entitymap.h"
#include "gloom/world/environment.h"
#include "gloom/world/map.h"
Expand All @@ -48,12 +50,14 @@ DENG2_PIMPL(GloomWorld), public Asset
User * localUser = nullptr;
Context renderContext;
Environment environ;
GLTextureFramebuffer framebuf{Image::RGBA_16f};
GBuffer gbuffer;
SkyBox sky;
Map map;
QHash<ID, double> initialPlaneY;
MapRender mapRender;
SSAO ssao;
Tonemap tonemap;

float visibleDistance;
double currentTime = 0.0;
Expand All @@ -75,14 +79,15 @@ DENG2_PIMPL(GloomWorld), public Asset
atlas->setFilter(gl::Linear, gl::Linear, gl::MipNearest);
#endif

renderContext.images = &GloomApp::images(); // TODO: remove dependency on App
renderContext.shaders = &GloomApp::shaders();
renderContext.atlas = atlas.get();
renderContext.uAtlas = renderContext.atlas;
renderContext.ssao = &ssao;
renderContext.gbuffer = &gbuffer;
renderContext.lights = &mapRender.lights();
renderContext.map = &map;
renderContext.images = &GloomApp::images(); // TODO: remove dependency on App
renderContext.shaders = &GloomApp::shaders();
renderContext.atlas = atlas.get();
renderContext.uAtlas = renderContext.atlas;
renderContext.ssao = &ssao;
renderContext.gbuffer = &gbuffer;
renderContext.framebuf = &framebuf;
renderContext.lights = &mapRender.lights();
renderContext.map = &map;

environ.setWorld(thisPublic);
}
Expand All @@ -97,10 +102,12 @@ DENG2_PIMPL(GloomWorld), public Asset

sky.setSize(visibleDistance);

framebuf .glInit();
gbuffer .glInit(renderContext);
sky .glInit(renderContext);
mapRender.glInit(renderContext);
ssao .glInit(renderContext);
tonemap .glInit(renderContext);

// Vec3f const fogColor{.83f, .89f, 1.f};
// uFog = Vec4f(fogColor, visibleDistance);
Expand All @@ -113,10 +120,12 @@ DENG2_PIMPL(GloomWorld), public Asset
{
setState(NotReady);

tonemap .glDeinit();
ssao .glDeinit();
mapRender.glDeinit();
sky .glDeinit();
gbuffer .glDeinit();
framebuf .glDeinit();

atlas->clear();

Expand Down Expand Up @@ -224,23 +233,34 @@ void GloomWorld::render(ICamera const &camera)
{
if (!d->isReady()) return;

d->gbuffer.resize(GLState::current().target().size());
const auto frameSize = GLState::current().target().size();

d->framebuf.resize(frameSize);
d->framebuf.clear(GLFramebuffer::Color0);

d->gbuffer.resize(frameSize);
d->gbuffer.clear();

d->renderContext.view.setCamera(camera);

// Render the G-buffer contents: albedo, normals, depth.
GLState::push()
.setTarget(d->gbuffer.framebuf())
.setCull(gl::Back)
.setDepthTest(true);

d->renderContext.view.setCamera(camera);

d->mapRender.render();
d->ssao.render();
d->sky.render();

GLState::pop();

d->gbuffer.render();
// Render the frame: deferred shading using the G-buffer.
GLState::push().setTarget(d->framebuf);
d->mapRender.lights().renderLighting();
GLState::pop();

d->tonemap.render();
}

User *GloomWorld::localUser() const
Expand Down
18 changes: 10 additions & 8 deletions doomsday/tests/test_gloom/gloom/render/context.h
Expand Up @@ -26,6 +26,7 @@
#include <de/ImageBank>
#include <de/GLProgram>
#include <de/GLShaderBank>
#include <de/GLTextureFramebuffer>

namespace gloom {

Expand All @@ -34,14 +35,15 @@ class SSAO;
class LightRender;

struct Context {
const de::ImageBank *images;
de::GLShaderBank * shaders;
const Map * map;
de::AtlasTexture * atlas;
View view;
SSAO * ssao;
GBuffer * gbuffer;
LightRender * lights;
const de::ImageBank * images;
de::GLShaderBank * shaders;
const Map * map;
de::AtlasTexture * atlas;
View view;
SSAO * ssao;
GBuffer * gbuffer;
de::GLTextureFramebuffer *framebuf;
LightRender * lights;

de::GLUniform uAtlas {"uTex", de::GLUniform::Sampler2D};
de::GLUniform uCurrentTime {"uCurrentTime", de::GLUniform::Float};
Expand Down
33 changes: 23 additions & 10 deletions doomsday/tests/test_gloom/gloom/render/gbuffer.cpp
Expand Up @@ -34,12 +34,13 @@ namespace gloom {
DENG2_PIMPL(GBuffer)
{
ScreenQuad quad;
GLTextureFramebuffer frame{
QList<Image::Format>({Image::RGBA_16f /* albedo */, Image::RGBA_8888 /* normals */})};
GLTextureFramebuffer frame{GLTextureFramebuffer::Formats({Image::RGBA_8888, // albedo
Image::RGBA_8888, // normals
Image::RGBA_8888})}; // emissive
GLUniform uGBufferAlbedo {"uGBufferAlbedo", GLUniform::Sampler2D};
GLUniform uGBufferEmissive {"uGBufferEmissive", GLUniform::Sampler2D};
GLUniform uGBufferNormal {"uGBufferNormal", GLUniform::Sampler2D};
GLUniform uGBufferDepth {"uGBufferDepth", GLUniform::Sampler2D};
GLUniform uSSAOBuf {"uSSAOBuf", GLUniform::Sampler2D};
GLUniform uShadowMap {"uShadowMap", GLUniform::Sampler2D}; // <----TESTING-----
GLUniform uViewToLightMatrix{"uViewToLightMatrix", GLUniform::Mat4};
GLUniform uDebugMode {"uDebugMode", GLUniform::Int};
Expand All @@ -57,9 +58,10 @@ DENG2_PIMPL(GBuffer)

void updateUniforms()
{
uGBufferAlbedo = frame.attachedTexture(GLFramebuffer::Color0);
uGBufferNormal = frame.attachedTexture(GLFramebuffer::Color1);
uGBufferDepth = frame.attachedTexture(GLFramebuffer::DepthStencil);
uGBufferAlbedo = frame.attachedTexture(GLFramebuffer::Color0);
uGBufferNormal = frame.attachedTexture(GLFramebuffer::Color1);
uGBufferEmissive = frame.attachedTexture(GLFramebuffer::Color2);
uGBufferDepth = frame.attachedTexture(GLFramebuffer::DepthStencil);
}
};

Expand All @@ -73,9 +75,16 @@ void GBuffer::glInit(Context &context)

d->quad.glInit(context);
context.shaders->build(d->quad.program(), "gloom.finalize")
<< context.view.uInverseProjMatrix << d->uGBufferAlbedo << d->uGBufferNormal
<< d->uGBufferDepth << d->uSSAOBuf << d->uShadowMap << d->uDebugMode
<< d->uViewToLightMatrix << context.lights->uViewSpaceLightDir();
<< context.view.uInverseProjMatrix
<< d->uGBufferAlbedo
<< d->uGBufferEmissive
<< d->uGBufferNormal
<< d->uGBufferDepth
<< context.ssao->uSSAOBuf()
<< d->uShadowMap
<< d->uDebugMode
<< d->uViewToLightMatrix
<< context.lights->uViewSpaceLightDir();

d->frame.glInit();
d->updateUniforms();
Expand Down Expand Up @@ -108,7 +117,6 @@ void GBuffer::render()
d->uViewToLightMatrix = context().uLightMatrix.toMat4f() *
context().view.camera->cameraModelView().inverse();

d->uSSAOBuf = context().ssao->occlusionFactors();
d->uShadowMap = context().lights->shadowMap();

d->quad.state().setTarget(GLState::current().target());
Expand All @@ -133,6 +141,11 @@ GLUniform &GBuffer::uGBufferAlbedo()
return d->uGBufferAlbedo;
}

GLUniform &GBuffer::uGBufferEmissive()
{
return d->uGBufferEmissive;
}

GLUniform &GBuffer::uGBufferNormal()
{
return d->uGBufferNormal;
Expand Down
13 changes: 7 additions & 6 deletions doomsday/tests/test_gloom/gloom/render/gbuffer.h
Expand Up @@ -38,15 +38,16 @@ class GBuffer : public Render
void glDeinit() override;
void render() override;

void clear();
void resize(const de::Vec2ui &size);
void clear();
void resize(const de::Vec2ui &size);
de::Vec2ui size() const;
void setDebugMode(int debugMode);
void setDebugMode(int debugMode);

de::GLFramebuffer &framebuf();
de::GLUniform &uGBufferAlbedo();
de::GLUniform &uGBufferNormal();
de::GLUniform &uGBufferDepth();
de::GLUniform & uGBufferAlbedo();
de::GLUniform & uGBufferEmissive();
de::GLUniform & uGBufferNormal();
de::GLUniform & uGBufferDepth();

private:
DENG2_PRIVATE(d)
Expand Down
92 changes: 77 additions & 15 deletions doomsday/tests/test_gloom/gloom/render/light.cpp
Expand Up @@ -26,45 +26,107 @@ namespace gloom {

DENG2_PIMPL(Light)
{
Vec3d origin;
Vec3f dir { -.41f, -.51f, -.75f };
Vec3f intensity { 10, 10, 10 };
GLTexture shadowMap;
GLFramebuffer framebuf;
const Entity *entity = nullptr;
Type type = Omni;
Vec3d origin;
Vec3f dir{-.41f, -.51f, -.75f};
Vec3f intensity{10, 10, 10};

struct Shadow {
GLTexture map;
GLFramebuffer framebuf;
};
std::unique_ptr<Shadow> shadow;

Impl(Public *i) : Base(i)
{
origin = -dir * 50;
}

void setCastShadows(bool yes)
{
if (yes && !shadow)
{
shadow.reset(new Shadow);

shadowMap.setAutoGenMips(false);
shadowMap.setFilter(gl::Nearest, gl::Nearest, gl::MipNone);
shadowMap.setWrap(gl::ClampToBorder, gl::ClampToBorder);
shadowMap.setBorderColor(Vec4f(1, 1, 1, 1));
shadowMap.setUndefinedContent(
GLTexture::Size(2048, 2048),
GLPixelFormat(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_FLOAT));
shadow->map.setAutoGenMips(false);
shadow->map.setFilter(gl::Nearest, gl::Nearest, gl::MipNone);
shadow->map.setWrap(gl::ClampToBorder, gl::ClampToBorder);
shadow->map.setBorderColor(Vec4f(1, 1, 1, 1));
shadow->map.setUndefinedContent(
GLTexture::Size(2048, 2048),
GLPixelFormat(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_FLOAT));

framebuf.configure(GLFramebuffer::Depth, shadowMap);
shadow->framebuf.configure(GLFramebuffer::Depth, shadow->map);
}
else if (!yes && shadow)
{
shadow.reset();
}
}
};

Light::Light()
: d(new Impl(this))
{}

void Light::setEntity(const Entity *entity)
{
d->entity = entity;
}

void Light::setType(Type type)
{
d->type = type;
}

void Light::setCastShadows(bool castShadows)
{
d->setCastShadows(castShadows);
}

bool Light::castShadows() const
{
return bool(d->shadow);
}

Vec3f Light::origin() const
{
if (d->entity)
{
const auto p = d->entity->position();
return p + Vec3f(0, 2, 0); // <---TESTING---
}
return d->origin;
}

Vec3f Light::direction() const
{
if (d->type == Omni) return Vec3f(); // emits in all directions
return d->dir.normalize();
}

Vec3f Light::intensity() const
{
return d->intensity;
}

float Light::falloffDistance() const
{
float maxInt = d->intensity.max();
return maxInt;
}

GLTexture &gloom::Light::shadowMap()
{
return d->shadowMap;
DENG2_ASSERT(d->shadow);
return d->shadow->map;
}

GLFramebuffer &Light::framebuf()
{
return d->framebuf;
DENG2_ASSERT(d->shadow);
return d->shadow->framebuf;
}

Mat4f Light::lightMatrix() const
Expand Down
3 changes: 3 additions & 0 deletions doomsday/tests/test_gloom/gloom/render/light.h
Expand Up @@ -40,15 +40,18 @@ class Light

void setEntity(const Entity *entity);
void setType(Type type);
void setCastShadows(bool castShadows);

const Entity *entity() const;

bool castShadows() const;
Type type() const;
de::Vec3f origin() const; // from entity
de::Vec3f direction() const;
de::Vec3f intensity() const;
float fovY() const;
float aspectRatio() const;
float falloffDistance() const;

de::GLTexture & shadowMap();
de::GLFramebuffer &framebuf();
Expand Down

0 comments on commit 2575b35

Please sign in to comment.