2016-07-25 01:39:50 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Joseph Cherlin. All rights reserved.
|
|
|
|
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
|
|
|
|
*/
|
|
|
|
|
2017-04-10 05:40:16 +03:00
|
|
|
#include <common.h>
|
|
|
|
#include <camera.h>
|
|
|
|
#include <bgfx_utils.h>
|
|
|
|
#include <imgui/imgui.h>
|
2016-07-25 01:39:50 +03:00
|
|
|
#include <bx/rng.h>
|
|
|
|
|
2017-06-26 07:44:04 +03:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2016-07-25 05:49:27 +03:00
|
|
|
/*
|
|
|
|
* Intro
|
|
|
|
* =====
|
|
|
|
*
|
|
|
|
* RSM (reflective shadow map) is a technique for global illumination.
|
|
|
|
* It is similar to shadow map. It piggybacks on the shadow map, in fact.
|
|
|
|
*
|
|
|
|
* RSM is compatible with any type of lighting which can handle handle
|
|
|
|
* a lot of point lights. This sample happens to use a deferred renderer,
|
|
|
|
* but other types would work.
|
|
|
|
*
|
|
|
|
* Overview:
|
|
|
|
*
|
|
|
|
* - Draw into G-Buffer
|
|
|
|
* - Draw Shadow Map (with RSM piggybacked on)
|
|
|
|
* - Populate light buffer
|
|
|
|
* - Deferred "combine" pass.
|
|
|
|
*
|
|
|
|
* Details
|
|
|
|
* =======
|
|
|
|
*
|
|
|
|
* ## G-Buffer
|
|
|
|
*
|
|
|
|
* Typical G-Buffer with normals, color, depth.
|
|
|
|
*
|
|
|
|
* ## RSM
|
|
|
|
*
|
|
|
|
* A typical shadow map, except it also outputs to a "RSM" buffer.
|
|
|
|
* The RSM contains the color of the item drawn, as well as a scalar value which represents
|
|
|
|
* how much light would bounce off of the surface if it were hit with light from the origin
|
|
|
|
* of the shadow map.
|
|
|
|
*
|
|
|
|
* ## Light Buffer
|
|
|
|
*
|
|
|
|
* We draw a lot of spheres into the light buffer. These spheres are called VPL (virtual
|
|
|
|
* point lights). VPLs represent bounced light, and let us eliminate the classic "ambient"
|
|
|
|
* term. Instead of us supplying their world space position in a transform matrix,
|
|
|
|
* VPLs gain their position from the shadow map from step 2, using an unprojection. They gain
|
|
|
|
* their color from the RSM. You could also store their position in a buffer while drawing shadows,
|
|
|
|
* I'm just using depth to keep the sample smaller.
|
|
|
|
*
|
|
|
|
* ## Deferred combine
|
|
|
|
*
|
|
|
|
* Typical combine used in almost any sort of deferred renderer.
|
|
|
|
*
|
|
|
|
* References
|
|
|
|
* ==========
|
|
|
|
*
|
|
|
|
* http: *www.bpeers.com/blog/?itemid=517
|
|
|
|
*
|
|
|
|
*/
|
2016-07-25 01:39:50 +03:00
|
|
|
|
|
|
|
// Render passes
|
|
|
|
#define RENDER_PASS_GBUFFER 0 // GBuffer for normals and albedo
|
|
|
|
#define RENDER_PASS_SHADOW_MAP 1 // Draw into the shadow map (RSM and regular shadow map at same time)
|
|
|
|
#define RENDER_PASS_LIGHT_BUFFER 2 // Light buffer for point lights
|
|
|
|
#define RENDER_PASS_COMBINE 3 // Directional light and final result
|
|
|
|
|
|
|
|
// Gbuffer has multiple render targets
|
|
|
|
#define GBUFFER_RT_NORMAL 0
|
|
|
|
#define GBUFFER_RT_COLOR 1
|
|
|
|
#define GBUFFER_RT_DEPTH 2
|
|
|
|
|
|
|
|
// Shadow map has multiple render targets
|
|
|
|
#define SHADOW_RT_RSM 0 // In this algorithm, shadows write lighting info as well.
|
|
|
|
#define SHADOW_RT_DEPTH 1 // Shadow maps always write a depth
|
|
|
|
|
|
|
|
// Random meshes we draw
|
|
|
|
#define MODEL_COUNT 222 // In this demo, a model is a mesh plus a transform and a color
|
|
|
|
|
|
|
|
#define SHADOW_MAP_DIM 512
|
|
|
|
#define LIGHT_DIST 10.0f
|
|
|
|
|
2016-07-25 02:18:26 +03:00
|
|
|
static const char * s_meshPaths[] =
|
2016-07-25 01:50:42 +03:00
|
|
|
{
|
|
|
|
"meshes/cube.bin",
|
|
|
|
"meshes/orb.bin",
|
|
|
|
"meshes/column.bin",
|
|
|
|
"meshes/bunny.bin",
|
|
|
|
"meshes/tree.bin",
|
|
|
|
"meshes/hollowcube.bin"
|
|
|
|
};
|
|
|
|
|
2016-07-25 02:18:26 +03:00
|
|
|
static const float s_meshScale[] =
|
|
|
|
{
|
|
|
|
0.25f,
|
|
|
|
0.5f,
|
|
|
|
0.05f,
|
|
|
|
0.5f,
|
|
|
|
0.05f,
|
|
|
|
0.05f
|
|
|
|
};
|
|
|
|
|
2019-08-17 20:35:21 +03:00
|
|
|
// Vertex layout for our screen space quad (used in deferred rendering)
|
2016-07-25 01:39:50 +03:00
|
|
|
struct PosTexCoord0Vertex
|
|
|
|
{
|
2016-07-25 01:50:42 +03:00
|
|
|
float m_x;
|
|
|
|
float m_y;
|
|
|
|
float m_z;
|
|
|
|
float m_u;
|
|
|
|
float m_v;
|
|
|
|
|
|
|
|
static void init()
|
|
|
|
{
|
2019-08-17 20:35:21 +03:00
|
|
|
ms_layout
|
2016-07-25 01:50:42 +03:00
|
|
|
.begin()
|
|
|
|
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
|
|
|
|
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
|
|
|
|
.end();
|
|
|
|
}
|
|
|
|
|
2019-08-17 20:35:21 +03:00
|
|
|
static bgfx::VertexLayout ms_layout;
|
2016-07-25 01:39:50 +03:00
|
|
|
};
|
2019-08-17 20:35:21 +03:00
|
|
|
bgfx::VertexLayout PosTexCoord0Vertex::ms_layout;
|
2016-07-25 01:39:50 +03:00
|
|
|
|
|
|
|
// Utility function to draw a screen space quad for deferred rendering
|
|
|
|
void screenSpaceQuad(float _textureWidth, float _textureHeight, float _texelHalf, bool _originBottomLeft, float _width = 1.0f, float _height = 1.0f)
|
|
|
|
{
|
2019-08-17 20:35:21 +03:00
|
|
|
if (3 == bgfx::getAvailTransientVertexBuffer(3, PosTexCoord0Vertex::ms_layout) )
|
2016-07-25 01:50:42 +03:00
|
|
|
{
|
|
|
|
bgfx::TransientVertexBuffer vb;
|
2019-08-17 20:35:21 +03:00
|
|
|
bgfx::allocTransientVertexBuffer(&vb, 3, PosTexCoord0Vertex::ms_layout);
|
2016-07-25 01:50:42 +03:00
|
|
|
PosTexCoord0Vertex* vertex = (PosTexCoord0Vertex*)vb.data;
|
|
|
|
|
|
|
|
const float minx = -_width;
|
|
|
|
const float maxx = _width;
|
|
|
|
const float miny = 0.0f;
|
|
|
|
const float maxy = _height*2.0f;
|
|
|
|
|
|
|
|
const float texelHalfW = _texelHalf/_textureWidth;
|
|
|
|
const float texelHalfH = _texelHalf/_textureHeight;
|
|
|
|
const float minu = -1.0f + texelHalfW;
|
|
|
|
const float maxu = 1.0f + texelHalfH;
|
|
|
|
|
|
|
|
const float zz = 0.0f;
|
|
|
|
|
|
|
|
float minv = texelHalfH;
|
|
|
|
float maxv = 2.0f + texelHalfH;
|
|
|
|
|
|
|
|
if (_originBottomLeft)
|
|
|
|
{
|
|
|
|
float temp = minv;
|
|
|
|
minv = maxv;
|
|
|
|
maxv = temp;
|
|
|
|
|
|
|
|
minv -= 1.0f;
|
|
|
|
maxv -= 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
vertex[0].m_x = minx;
|
|
|
|
vertex[0].m_y = miny;
|
|
|
|
vertex[0].m_z = zz;
|
|
|
|
vertex[0].m_u = minu;
|
|
|
|
vertex[0].m_v = minv;
|
|
|
|
|
|
|
|
vertex[1].m_x = maxx;
|
|
|
|
vertex[1].m_y = miny;
|
|
|
|
vertex[1].m_z = zz;
|
|
|
|
vertex[1].m_u = maxu;
|
|
|
|
vertex[1].m_v = minv;
|
|
|
|
|
|
|
|
vertex[2].m_x = maxx;
|
|
|
|
vertex[2].m_y = maxy;
|
|
|
|
vertex[2].m_z = zz;
|
|
|
|
vertex[2].m_u = maxu;
|
|
|
|
vertex[2].m_v = maxv;
|
|
|
|
|
2017-05-14 21:48:59 +03:00
|
|
|
bgfx::setVertexBuffer(0, &vb);
|
2016-07-25 01:50:42 +03:00
|
|
|
}
|
2016-07-25 01:39:50 +03:00
|
|
|
}
|
|
|
|
|
2016-07-25 02:18:26 +03:00
|
|
|
class ExampleRSM : public entry::AppI
|
2016-07-25 01:39:50 +03:00
|
|
|
{
|
2016-07-25 02:18:26 +03:00
|
|
|
public:
|
2019-08-18 00:40:38 +03:00
|
|
|
ExampleRSM(const char* _name, const char* _description, const char* _url)
|
|
|
|
: entry::AppI(_name, _description, _url)
|
2017-06-26 07:44:04 +03:00
|
|
|
, m_reading(0)
|
2016-07-25 02:18:26 +03:00
|
|
|
, m_currFrame(UINT32_MAX)
|
|
|
|
, m_cameraSpin(false)
|
|
|
|
, m_lightElevation(35.0f)
|
|
|
|
, m_lightAzimuth(215.0f)
|
|
|
|
, m_rsmAmount(0.25f)
|
|
|
|
, m_vplRadius(3.0f)
|
|
|
|
, m_texelHalf(0.0f)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-15 10:17:29 +03:00
|
|
|
void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
|
2016-07-25 01:39:50 +03:00
|
|
|
{
|
|
|
|
Args args(_argc, _argv);
|
|
|
|
|
2017-06-30 08:23:18 +03:00
|
|
|
m_width = _width;
|
|
|
|
m_height = _height;
|
2017-06-26 07:44:04 +03:00
|
|
|
m_debug = BGFX_DEBUG_NONE;
|
2016-07-25 01:39:50 +03:00
|
|
|
m_reset = BGFX_RESET_VSYNC;
|
|
|
|
|
2018-04-18 01:42:18 +03:00
|
|
|
bgfx::Init init;
|
|
|
|
init.type = args.m_type;
|
|
|
|
init.vendorId = args.m_pciId;
|
|
|
|
init.resolution.width = m_width;
|
|
|
|
init.resolution.height = m_height;
|
|
|
|
init.resolution.reset = m_reset;
|
|
|
|
bgfx::init(init);
|
2016-07-25 01:39:50 +03:00
|
|
|
|
|
|
|
// Enable debug text.
|
|
|
|
bgfx::setDebug(m_debug);
|
|
|
|
|
2016-07-25 01:50:42 +03:00
|
|
|
// Labeling for renderdoc captures, etc
|
|
|
|
bgfx::setViewName(RENDER_PASS_GBUFFER, "gbuffer" );
|
|
|
|
bgfx::setViewName(RENDER_PASS_SHADOW_MAP, "shadow map" );
|
|
|
|
bgfx::setViewName(RENDER_PASS_LIGHT_BUFFER, "light buffer");
|
|
|
|
bgfx::setViewName(RENDER_PASS_COMBINE, "post combine");
|
2016-07-25 01:39:50 +03:00
|
|
|
|
|
|
|
// Set up screen clears
|
|
|
|
bgfx::setViewClear(RENDER_PASS_GBUFFER
|
2017-06-26 07:44:04 +03:00
|
|
|
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
|
|
|
|
, 0
|
|
|
|
, 1.0f
|
|
|
|
, 0
|
|
|
|
);
|
2016-07-25 01:50:42 +03:00
|
|
|
|
|
|
|
bgfx::setViewClear(RENDER_PASS_LIGHT_BUFFER
|
2017-06-26 07:44:04 +03:00
|
|
|
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
|
|
|
|
, 0
|
|
|
|
, 1.0f
|
|
|
|
, 0
|
|
|
|
);
|
2016-07-25 01:50:42 +03:00
|
|
|
|
|
|
|
bgfx::setViewClear(RENDER_PASS_SHADOW_MAP
|
2017-06-26 07:44:04 +03:00
|
|
|
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
|
|
|
|
, 0
|
|
|
|
, 1.0f
|
|
|
|
, 0
|
|
|
|
);
|
2016-07-25 01:50:42 +03:00
|
|
|
|
|
|
|
// Create uniforms
|
|
|
|
u_tint = bgfx::createUniform("u_tint", bgfx::UniformType::Vec4); // Tint for when you click on items
|
|
|
|
u_lightDir = bgfx::createUniform("u_lightDir", bgfx::UniformType::Vec4); // Single directional light for entire scene
|
|
|
|
u_sphereInfo = bgfx::createUniform("u_sphereInfo", bgfx::UniformType::Vec4); // Info for RSM
|
|
|
|
u_invMvp = bgfx::createUniform("u_invMvp", bgfx::UniformType::Mat4); // Matrix needed in light buffer
|
|
|
|
u_invMvpShadow = bgfx::createUniform("u_invMvpShadow", bgfx::UniformType::Mat4); // Matrix needed in light buffer
|
|
|
|
u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Mat4); // Matrix needed to use shadow map (world to shadow space)
|
|
|
|
u_shadowDimsInv = bgfx::createUniform("u_shadowDimsInv", bgfx::UniformType::Vec4); // Used in PCF
|
|
|
|
u_rsmAmount = bgfx::createUniform("u_rsmAmount", bgfx::UniformType::Vec4); // How much RSM to use vs directional light
|
|
|
|
|
|
|
|
// Create texture sampler uniforms (used when we bind textures)
|
2019-01-12 01:14:17 +03:00
|
|
|
s_normal = bgfx::createUniform("s_normal", bgfx::UniformType::Sampler); // Normal gbuffer
|
|
|
|
s_depth = bgfx::createUniform("s_depth", bgfx::UniformType::Sampler); // Normal gbuffer
|
|
|
|
s_color = bgfx::createUniform("s_color", bgfx::UniformType::Sampler); // Color (albedo) gbuffer
|
|
|
|
s_light = bgfx::createUniform("s_light", bgfx::UniformType::Sampler); // Light buffer
|
|
|
|
s_shadowMap = bgfx::createUniform("s_shadowMap", bgfx::UniformType::Sampler); // Shadow map
|
|
|
|
s_rsm = bgfx::createUniform("s_rsm", bgfx::UniformType::Sampler); // Reflective shadow map
|
2016-07-25 01:50:42 +03:00
|
|
|
|
2016-07-25 01:39:50 +03:00
|
|
|
// Create program from shaders.
|
|
|
|
m_gbufferProgram = loadProgram("vs_rsm_gbuffer", "fs_rsm_gbuffer"); // Gbuffer
|
2016-07-25 01:50:42 +03:00
|
|
|
m_shadowProgram = loadProgram("vs_rsm_shadow", "fs_rsm_shadow" ); // Drawing shadow map
|
|
|
|
m_lightProgram = loadProgram("vs_rsm_lbuffer", "fs_rsm_lbuffer"); // Light buffer
|
|
|
|
m_combineProgram = loadProgram("vs_rsm_combine", "fs_rsm_combine"); // Combiner
|
|
|
|
|
|
|
|
// Load some meshes
|
2016-07-25 03:17:12 +03:00
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
|
2016-07-25 02:18:26 +03:00
|
|
|
{
|
2016-07-25 03:17:12 +03:00
|
|
|
m_meshes[ii] = meshLoad(s_meshPaths[ii]);
|
2016-07-25 01:50:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Randomly create some models
|
|
|
|
bx::RngMwc mwc; // Random number generator
|
2016-07-25 02:45:34 +03:00
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(m_models); ++ii)
|
2016-07-25 02:18:26 +03:00
|
|
|
{
|
2016-07-25 02:45:34 +03:00
|
|
|
Model& model = m_models[ii];
|
|
|
|
|
|
|
|
uint32_t rr = mwc.gen() % 256;
|
|
|
|
uint32_t gg = mwc.gen() % 256;
|
|
|
|
uint32_t bb = mwc.gen() % 256;
|
2016-07-25 03:17:12 +03:00
|
|
|
model.mesh = 1+mwc.gen()%(BX_COUNTOF(s_meshPaths)-1);
|
2016-07-25 02:45:34 +03:00
|
|
|
model.color[0] = rr/255.0f;
|
|
|
|
model.color[1] = gg/255.0f;
|
|
|
|
model.color[2] = bb/255.0f;
|
|
|
|
model.color[3] = 1.0f;
|
|
|
|
model.position[0] = (((mwc.gen() % 256)) - 128.0f)/20.0f;
|
|
|
|
model.position[1] = 0;
|
|
|
|
model.position[2] = (((mwc.gen() % 256)) - 128.0f)/20.0f;
|
2016-07-25 01:50:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load ground. We'll just use the cube since I don't have a ground model right now
|
|
|
|
m_ground = meshLoad("meshes/cube.bin");
|
|
|
|
|
|
|
|
// Light sphere
|
|
|
|
m_lightSphere = meshLoad("meshes/unit_sphere.bin");
|
|
|
|
|
2018-08-24 03:59:47 +03:00
|
|
|
const uint64_t tsFlags = 0
|
2016-07-25 01:50:42 +03:00
|
|
|
| BGFX_TEXTURE_RT
|
2018-08-21 03:12:26 +03:00
|
|
|
| BGFX_SAMPLER_MIN_POINT
|
|
|
|
| BGFX_SAMPLER_MAG_POINT
|
|
|
|
| BGFX_SAMPLER_MIP_POINT
|
|
|
|
| BGFX_SAMPLER_U_CLAMP
|
|
|
|
| BGFX_SAMPLER_V_CLAMP
|
2016-07-25 01:50:42 +03:00
|
|
|
;
|
|
|
|
|
2018-08-24 03:59:47 +03:00
|
|
|
m_gbufferTex[GBUFFER_RT_NORMAL] = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::BGRA8, tsFlags);
|
|
|
|
m_gbufferTex[GBUFFER_RT_COLOR] = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::BGRA8, tsFlags);
|
2021-02-26 06:44:13 +03:00
|
|
|
m_gbufferTex[GBUFFER_RT_DEPTH] = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::D32F, tsFlags);
|
2016-07-25 01:50:42 +03:00
|
|
|
m_gbuffer = bgfx::createFrameBuffer(BX_COUNTOF(m_gbufferTex), m_gbufferTex, true);
|
|
|
|
|
|
|
|
// Make light buffer
|
2018-08-24 03:59:47 +03:00
|
|
|
m_lightBufferTex = bgfx::createTexture2D(bgfx::BackbufferRatio::Equal, false, 1, bgfx::TextureFormat::BGRA8, tsFlags);
|
2016-07-25 01:50:42 +03:00
|
|
|
bgfx::TextureHandle lightBufferRTs[] = {
|
|
|
|
m_lightBufferTex
|
|
|
|
};
|
|
|
|
m_lightBuffer = bgfx::createFrameBuffer(BX_COUNTOF(lightBufferRTs), lightBufferRTs, true);
|
|
|
|
|
|
|
|
// Make shadow buffer
|
2018-08-24 03:59:47 +03:00
|
|
|
const uint64_t rsmFlags = 0
|
2016-07-25 01:50:42 +03:00
|
|
|
| BGFX_TEXTURE_RT
|
2018-08-21 03:12:26 +03:00
|
|
|
| BGFX_SAMPLER_MIN_POINT
|
|
|
|
| BGFX_SAMPLER_MAG_POINT
|
|
|
|
| BGFX_SAMPLER_MIP_POINT
|
|
|
|
| BGFX_SAMPLER_U_CLAMP
|
|
|
|
| BGFX_SAMPLER_V_CLAMP
|
2016-07-25 01:50:42 +03:00
|
|
|
;
|
|
|
|
|
|
|
|
// Reflective shadow map
|
|
|
|
m_shadowBufferTex[SHADOW_RT_RSM] = bgfx::createTexture2D(
|
2016-08-20 07:05:37 +03:00
|
|
|
SHADOW_MAP_DIM
|
2016-07-25 01:50:42 +03:00
|
|
|
, SHADOW_MAP_DIM
|
2016-08-20 07:05:37 +03:00
|
|
|
, false
|
2016-07-25 01:50:42 +03:00
|
|
|
, 1
|
2018-08-24 03:59:47 +03:00
|
|
|
, bgfx::TextureFormat::BGRA8
|
|
|
|
, rsmFlags
|
2016-07-25 01:50:42 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
// Typical shadow map
|
|
|
|
m_shadowBufferTex[SHADOW_RT_DEPTH] = bgfx::createTexture2D(
|
2016-08-20 07:05:37 +03:00
|
|
|
SHADOW_MAP_DIM
|
2016-07-25 01:50:42 +03:00
|
|
|
, SHADOW_MAP_DIM
|
2016-08-20 07:05:37 +03:00
|
|
|
, false
|
2016-07-25 01:50:42 +03:00
|
|
|
, 1
|
2018-08-24 03:59:47 +03:00
|
|
|
, bgfx::TextureFormat::D16
|
2018-12-11 22:49:31 +03:00
|
|
|
, BGFX_TEXTURE_RT /* | BGFX_SAMPLER_COMPARE_LEQUAL*/
|
|
|
|
); // Note I'm not setting BGFX_SAMPLER_COMPARE_LEQUAL. Why?
|
2018-08-24 03:59:47 +03:00
|
|
|
// Normally a PCF shadow map such as this requires a compare. However, this sample also
|
|
|
|
// reads from this texture in the lighting pass, and only uses the PCF capabilites in the
|
|
|
|
// combine pass, so the flag is disabled by default.
|
2016-07-25 01:50:42 +03:00
|
|
|
|
|
|
|
m_shadowBuffer = bgfx::createFrameBuffer(BX_COUNTOF(m_shadowBufferTex), m_shadowBufferTex, true);
|
|
|
|
|
2019-08-17 20:35:21 +03:00
|
|
|
// Vertex layout
|
2016-07-25 01:50:42 +03:00
|
|
|
PosTexCoord0Vertex::init();
|
|
|
|
|
|
|
|
// Init camera
|
|
|
|
cameraCreate();
|
2018-12-22 08:05:26 +03:00
|
|
|
cameraSetPosition({0.0f, 1.5f, 0.0f});
|
2016-07-25 01:50:42 +03:00
|
|
|
cameraSetVerticalAngle(-0.3f);
|
|
|
|
|
|
|
|
// Init directional light
|
|
|
|
updateLightDir();
|
|
|
|
|
|
|
|
// Get renderer capabilities info.
|
|
|
|
m_caps = bgfx::getCaps();
|
|
|
|
const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
|
|
|
|
m_texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
|
|
|
|
|
|
|
|
imguiCreate();
|
|
|
|
}
|
2016-07-25 01:39:50 +03:00
|
|
|
|
2017-07-15 10:17:29 +03:00
|
|
|
int shutdown() override
|
2016-07-25 01:39:50 +03:00
|
|
|
{
|
2016-07-25 03:17:12 +03:00
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
|
2016-07-25 01:50:42 +03:00
|
|
|
{
|
2016-07-25 03:17:12 +03:00
|
|
|
meshUnload(m_meshes[ii]);
|
2016-07-25 01:50:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
meshUnload(m_ground);
|
|
|
|
meshUnload(m_lightSphere);
|
2016-07-25 01:39:50 +03:00
|
|
|
|
|
|
|
// Cleanup.
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(m_gbufferProgram);
|
|
|
|
bgfx::destroy(m_lightProgram);
|
|
|
|
bgfx::destroy(m_combineProgram);
|
|
|
|
bgfx::destroy(m_shadowProgram);
|
|
|
|
|
|
|
|
bgfx::destroy(u_tint);
|
|
|
|
bgfx::destroy(u_lightDir);
|
|
|
|
bgfx::destroy(u_sphereInfo);
|
|
|
|
bgfx::destroy(u_invMvp);
|
|
|
|
bgfx::destroy(u_invMvpShadow);
|
|
|
|
bgfx::destroy(u_lightMtx);
|
|
|
|
bgfx::destroy(u_shadowDimsInv);
|
|
|
|
bgfx::destroy(u_rsmAmount);
|
|
|
|
bgfx::destroy(s_normal);
|
|
|
|
bgfx::destroy(s_depth);
|
|
|
|
bgfx::destroy(s_light);
|
|
|
|
bgfx::destroy(s_color);
|
|
|
|
bgfx::destroy(s_shadowMap);
|
|
|
|
bgfx::destroy(s_rsm);
|
|
|
|
|
|
|
|
bgfx::destroy(m_gbuffer);
|
|
|
|
bgfx::destroy(m_lightBuffer);
|
|
|
|
bgfx::destroy(m_shadowBuffer);
|
2016-07-25 01:50:42 +03:00
|
|
|
|
2016-07-25 02:45:34 +03:00
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(m_gbufferTex); ++ii)
|
|
|
|
{
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(m_gbufferTex[ii]);
|
2016-07-25 02:45:34 +03:00
|
|
|
}
|
2016-07-25 01:50:42 +03:00
|
|
|
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(m_lightBufferTex);
|
2016-07-25 02:45:34 +03:00
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(m_shadowBufferTex); ++ii)
|
|
|
|
{
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(m_shadowBufferTex[ii]);
|
2016-07-25 02:45:34 +03:00
|
|
|
}
|
2016-07-25 01:50:42 +03:00
|
|
|
|
|
|
|
cameraDestroy();
|
|
|
|
|
|
|
|
imguiDestroy();
|
2016-07-25 01:39:50 +03:00
|
|
|
|
|
|
|
// Shutdown bgfx.
|
|
|
|
bgfx::shutdown();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-15 10:17:29 +03:00
|
|
|
bool update() override
|
2016-07-25 01:39:50 +03:00
|
|
|
{
|
|
|
|
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
|
|
|
|
{
|
2016-07-25 01:50:42 +03:00
|
|
|
// Update frame timer
|
|
|
|
int64_t now = bx::getHPCounter();
|
|
|
|
static int64_t last = now;
|
|
|
|
const int64_t frameTime = now - last;
|
|
|
|
last = now;
|
|
|
|
const double freq = double(bx::getHPFrequency());
|
|
|
|
const float deltaTime = float(frameTime/freq);
|
2016-07-25 01:39:50 +03:00
|
|
|
|
2016-07-25 01:50:42 +03:00
|
|
|
// Update camera
|
2020-12-27 09:46:26 +03:00
|
|
|
cameraUpdate(deltaTime*0.15f, m_mouseState, ImGui::MouseOverArea() );
|
2016-07-25 01:39:50 +03:00
|
|
|
|
2016-07-25 01:50:42 +03:00
|
|
|
// Set up matrices for gbuffer
|
2016-07-25 01:39:50 +03:00
|
|
|
float view[16];
|
|
|
|
cameraGetViewMtx(view);
|
|
|
|
|
|
|
|
float proj[16];
|
2017-02-23 09:26:39 +03:00
|
|
|
bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
|
2016-07-25 01:39:50 +03:00
|
|
|
|
2016-07-25 01:50:42 +03:00
|
|
|
bgfx::setViewRect(RENDER_PASS_GBUFFER, 0, 0, uint16_t(m_width), uint16_t(m_height));
|
|
|
|
bgfx::setViewTransform(RENDER_PASS_GBUFFER, view, proj);
|
|
|
|
// Make sure when we draw it goes into gbuffer and not backbuffer
|
|
|
|
bgfx::setViewFrameBuffer(RENDER_PASS_GBUFFER, m_gbuffer);
|
|
|
|
// Draw everything into g-buffer
|
|
|
|
drawAllModels(RENDER_PASS_GBUFFER, m_gbufferProgram);
|
|
|
|
|
|
|
|
// Draw shadow map
|
|
|
|
|
|
|
|
// Set up transforms for shadow map
|
|
|
|
float smView[16], smProj[16], lightEye[3], lightAt[3];
|
|
|
|
lightEye[0] = m_lightDir[0]*LIGHT_DIST;
|
|
|
|
lightEye[1] = m_lightDir[1]*LIGHT_DIST;
|
|
|
|
lightEye[2] = m_lightDir[2]*LIGHT_DIST;
|
|
|
|
|
|
|
|
lightAt[0] = 0.0f;
|
|
|
|
lightAt[1] = 0.0f;
|
|
|
|
lightAt[2] = 0.0f;
|
|
|
|
|
2018-12-22 05:02:39 +03:00
|
|
|
bx::mtxLookAt(smView, bx::load<bx::Vec3>(lightEye), bx::load<bx::Vec3>(lightAt) );
|
2016-07-25 01:50:42 +03:00
|
|
|
const float area = 10.0f;
|
2017-06-13 08:43:07 +03:00
|
|
|
const bgfx::Caps* caps = bgfx::getCaps();
|
|
|
|
bx::mtxOrtho(smProj, -area, area, -area, area, -100.0f, 100.0f, 0.0f, caps->homogeneousDepth);
|
2016-07-25 01:50:42 +03:00
|
|
|
bgfx::setViewTransform(RENDER_PASS_SHADOW_MAP, smView, smProj);
|
|
|
|
bgfx::setViewFrameBuffer(RENDER_PASS_SHADOW_MAP, m_shadowBuffer);
|
|
|
|
bgfx::setViewRect(RENDER_PASS_SHADOW_MAP, 0, 0, SHADOW_MAP_DIM, SHADOW_MAP_DIM);
|
|
|
|
|
|
|
|
drawAllModels(RENDER_PASS_SHADOW_MAP, m_shadowProgram);
|
|
|
|
|
|
|
|
// Next draw light buffer
|
|
|
|
|
|
|
|
// Set up matrices for light buffer
|
|
|
|
bgfx::setViewRect(RENDER_PASS_LIGHT_BUFFER, 0, 0, uint16_t(m_width), uint16_t(m_height));
|
|
|
|
bgfx::setViewTransform(RENDER_PASS_LIGHT_BUFFER, view, proj); // Notice, same view and proj as gbuffer
|
|
|
|
// Set drawing into light buffer
|
|
|
|
bgfx::setViewFrameBuffer(RENDER_PASS_LIGHT_BUFFER, m_lightBuffer);
|
|
|
|
|
|
|
|
// Inverse view projection is needed in shader so set that up
|
|
|
|
float vp[16], invMvp[16];
|
|
|
|
bx::mtxMul(vp, view, proj);
|
|
|
|
bx::mtxInverse(invMvp, vp);
|
|
|
|
|
|
|
|
// Light matrix used in combine pass and inverse used in light pass
|
|
|
|
float lightMtx[16]; // World space to light space (shadow map space)
|
|
|
|
bx::mtxMul(lightMtx, smView, smProj);
|
|
|
|
float invMvpShadow[16];
|
|
|
|
bx::mtxInverse(invMvpShadow, lightMtx);
|
|
|
|
|
|
|
|
// Draw some lights (these should really be instanced but for this example they aren't...)
|
2017-06-26 07:44:04 +03:00
|
|
|
const uint32_t kMaxSpheres = 32;
|
|
|
|
for (uint32_t i = 0; i < kMaxSpheres; i++)
|
2016-07-25 02:18:26 +03:00
|
|
|
{
|
2017-06-26 07:44:04 +03:00
|
|
|
for (uint32_t j = 0; j < kMaxSpheres; j++)
|
2016-07-25 02:18:26 +03:00
|
|
|
{
|
2016-07-25 01:50:42 +03:00
|
|
|
// These are used in the fragment shader
|
2016-11-16 07:23:52 +03:00
|
|
|
bgfx::setTexture(0, s_normal, bgfx::getTexture(m_gbuffer, GBUFFER_RT_NORMAL) ); // Normal for lighting calculations
|
|
|
|
bgfx::setTexture(1, s_depth, bgfx::getTexture(m_gbuffer, GBUFFER_RT_DEPTH) ); // Depth to reconstruct world position
|
2016-07-25 01:50:42 +03:00
|
|
|
|
|
|
|
// Thse are used in the vert shader
|
2016-11-16 07:23:52 +03:00
|
|
|
bgfx::setTexture(2, s_shadowMap, bgfx::getTexture(m_shadowBuffer, SHADOW_RT_DEPTH) ); // Used to place sphere
|
|
|
|
bgfx::setTexture(3, s_rsm, bgfx::getTexture(m_shadowBuffer, SHADOW_RT_RSM) ); // Used to scale/color sphere
|
2016-07-25 01:50:42 +03:00
|
|
|
|
|
|
|
bgfx::setUniform(u_invMvp, invMvp);
|
|
|
|
bgfx::setUniform(u_invMvpShadow, invMvpShadow);
|
|
|
|
float sphereInfo[4];
|
2017-06-26 07:44:04 +03:00
|
|
|
sphereInfo[0] = ((float)i/(kMaxSpheres-1));
|
|
|
|
sphereInfo[1] = ((float)j/(kMaxSpheres-1));
|
2016-07-25 01:50:42 +03:00
|
|
|
sphereInfo[2] = m_vplRadius;
|
|
|
|
sphereInfo[3] = 0.0; // Unused
|
|
|
|
bgfx::setUniform(u_sphereInfo, sphereInfo);
|
|
|
|
|
|
|
|
const uint64_t lightDrawState = 0
|
2018-02-13 23:35:23 +03:00
|
|
|
| BGFX_STATE_WRITE_RGB
|
2016-07-25 01:50:42 +03:00
|
|
|
| BGFX_STATE_BLEND_ADD // <=== Overlapping lights contribute more
|
2018-02-13 23:35:23 +03:00
|
|
|
| BGFX_STATE_WRITE_A
|
2016-07-25 01:50:42 +03:00
|
|
|
| BGFX_STATE_CULL_CW // <=== If we go into the lights, there will be problems, so we draw the far back face.
|
|
|
|
;
|
|
|
|
|
|
|
|
meshSubmit(
|
2017-06-26 07:44:04 +03:00
|
|
|
m_lightSphere
|
|
|
|
, RENDER_PASS_LIGHT_BUFFER
|
|
|
|
, m_lightProgram
|
|
|
|
, NULL
|
|
|
|
, lightDrawState
|
|
|
|
);
|
2016-07-25 01:50:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw combine pass
|
|
|
|
|
|
|
|
// Texture inputs for combine pass
|
2016-11-16 07:23:52 +03:00
|
|
|
bgfx::setTexture(0, s_normal, bgfx::getTexture(m_gbuffer, GBUFFER_RT_NORMAL) );
|
|
|
|
bgfx::setTexture(1, s_color, bgfx::getTexture(m_gbuffer, GBUFFER_RT_COLOR) );
|
|
|
|
bgfx::setTexture(2, s_light, bgfx::getTexture(m_lightBuffer, 0) );
|
|
|
|
bgfx::setTexture(3, s_depth, bgfx::getTexture(m_gbuffer, GBUFFER_RT_DEPTH) );
|
|
|
|
bgfx::setTexture(4, s_shadowMap, bgfx::getTexture(m_shadowBuffer, SHADOW_RT_DEPTH)
|
2018-08-21 03:12:26 +03:00
|
|
|
, BGFX_SAMPLER_COMPARE_LEQUAL
|
2016-11-16 07:23:52 +03:00
|
|
|
);
|
2016-07-25 01:50:42 +03:00
|
|
|
|
|
|
|
// Uniforms for combine pass
|
|
|
|
|
|
|
|
bgfx::setUniform(u_lightDir, m_lightDir);
|
|
|
|
bgfx::setUniform(u_invMvp, invMvp);
|
|
|
|
bgfx::setUniform(u_lightMtx, lightMtx);
|
|
|
|
const float invDim[4] = {1.0f/SHADOW_MAP_DIM, 0.0f, 0.0f, 0.0f};
|
|
|
|
bgfx::setUniform(u_shadowDimsInv, invDim);
|
|
|
|
float rsmAmount[4] = {m_rsmAmount,m_rsmAmount,m_rsmAmount,m_rsmAmount};
|
|
|
|
bgfx::setUniform(u_rsmAmount, rsmAmount);
|
|
|
|
|
|
|
|
// Set up state for combine pass
|
|
|
|
// point of this is to avoid doing depth test, which is in the default state
|
|
|
|
bgfx::setState(0
|
2018-02-13 23:35:23 +03:00
|
|
|
| BGFX_STATE_WRITE_RGB
|
|
|
|
| BGFX_STATE_WRITE_A
|
2017-06-26 07:44:04 +03:00
|
|
|
);
|
2016-07-25 01:50:42 +03:00
|
|
|
|
|
|
|
// Set up transform matrix for fullscreen quad
|
|
|
|
float orthoProj[16];
|
2017-06-13 08:43:07 +03:00
|
|
|
bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0f, caps->homogeneousDepth);
|
2017-03-13 04:25:23 +03:00
|
|
|
bgfx::setViewTransform(RENDER_PASS_COMBINE, NULL, orthoProj);
|
|
|
|
bgfx::setViewRect(RENDER_PASS_COMBINE, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
2016-07-25 01:50:42 +03:00
|
|
|
// Bind vertex buffer and draw quad
|
|
|
|
screenSpaceQuad( (float)m_width, (float)m_height, m_texelHalf, m_caps->originBottomLeft);
|
|
|
|
bgfx::submit(RENDER_PASS_COMBINE, m_combineProgram);
|
|
|
|
|
|
|
|
// Draw UI
|
|
|
|
imguiBeginFrame(m_mouseState.m_mx
|
2017-06-26 07:44:04 +03:00
|
|
|
, m_mouseState.m_my
|
|
|
|
, (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
|
|
|
|
| (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
|
|
|
|
| (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
|
|
|
|
, m_mouseState.m_mz
|
|
|
|
, uint16_t(m_width)
|
|
|
|
, uint16_t(m_height)
|
|
|
|
);
|
|
|
|
|
2017-06-30 09:19:20 +03:00
|
|
|
showExampleDialog(this);
|
2016-07-25 01:50:42 +03:00
|
|
|
|
2017-06-27 08:51:56 +03:00
|
|
|
ImGui::SetNextWindowPos(
|
|
|
|
ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
|
2017-12-02 08:04:27 +03:00
|
|
|
, ImGuiCond_FirstUseEver
|
|
|
|
);
|
|
|
|
ImGui::SetNextWindowSize(
|
|
|
|
ImVec2(m_width / 5.0f, m_height / 3.0f)
|
|
|
|
, ImGuiCond_FirstUseEver
|
2017-06-27 08:51:56 +03:00
|
|
|
);
|
|
|
|
ImGui::Begin("Settings"
|
2017-04-10 05:40:16 +03:00
|
|
|
, NULL
|
2017-12-02 08:04:27 +03:00
|
|
|
, 0
|
2017-04-10 05:40:16 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
ImGui::SliderFloat("RSM Amount", &m_rsmAmount, 0.0f, 0.7f);
|
|
|
|
ImGui::SliderFloat("VPL Radius", &m_vplRadius, 0.25f, 20.0f);
|
|
|
|
ImGui::SliderFloat("Light Azimuth", &m_lightAzimuth, 0.0f, 360.0f);
|
|
|
|
ImGui::SliderFloat("Light Elevation", &m_lightElevation, 35.0f, 90.0f);
|
2016-07-25 01:50:42 +03:00
|
|
|
|
2017-04-10 05:40:16 +03:00
|
|
|
ImGui::End();
|
2016-07-25 01:50:42 +03:00
|
|
|
|
|
|
|
imguiEndFrame();
|
|
|
|
|
|
|
|
updateLightDir();
|
2016-07-25 01:39:50 +03:00
|
|
|
|
|
|
|
// Advance to next frame. Rendering thread will be kicked to
|
|
|
|
// process submitted rendering primitives.
|
|
|
|
m_currFrame = bgfx::frame();
|
|
|
|
|
2017-06-30 09:19:20 +03:00
|
|
|
return true;
|
2016-07-25 01:39:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-25 01:50:42 +03:00
|
|
|
void drawAllModels(uint8_t _pass, bgfx::ProgramHandle _program)
|
|
|
|
{
|
2016-07-25 02:18:26 +03:00
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(m_models); ++ii)
|
2016-07-25 01:50:42 +03:00
|
|
|
{
|
2016-07-25 02:18:26 +03:00
|
|
|
const Model& model = m_models[ii];
|
|
|
|
|
2016-07-25 01:50:42 +03:00
|
|
|
// Set up transform matrix for each model
|
2016-07-25 02:18:26 +03:00
|
|
|
float scale = s_meshScale[model.mesh];
|
2016-07-25 01:50:42 +03:00
|
|
|
float mtx[16];
|
|
|
|
bx::mtxSRT(mtx
|
2017-06-26 07:44:04 +03:00
|
|
|
, scale
|
|
|
|
, scale
|
|
|
|
, scale
|
|
|
|
, 0.0f
|
|
|
|
, 0.0f
|
|
|
|
, 0.0f
|
|
|
|
, model.position[0]
|
|
|
|
, model.position[1]
|
|
|
|
, model.position[2]
|
|
|
|
);
|
2016-07-25 01:50:42 +03:00
|
|
|
|
|
|
|
// Submit mesh to gbuffer
|
2016-07-25 02:18:26 +03:00
|
|
|
bgfx::setUniform(u_tint, model.color);
|
|
|
|
meshSubmit(m_meshes[model.mesh], _pass, _program, mtx);
|
2016-07-25 01:50:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw ground
|
|
|
|
const float white[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
|
|
|
|
bgfx::setUniform(u_tint, white);
|
|
|
|
float mtxScale[16];
|
|
|
|
float scale = 10.0;
|
|
|
|
bx::mtxScale(mtxScale
|
2017-06-26 07:44:04 +03:00
|
|
|
, scale
|
|
|
|
, scale
|
|
|
|
, scale
|
|
|
|
);
|
2016-07-25 01:50:42 +03:00
|
|
|
float mtxTrans[16];
|
|
|
|
bx::mtxTranslate(mtxTrans
|
2017-06-26 07:44:04 +03:00
|
|
|
, 0.0f
|
|
|
|
, -10.0f
|
|
|
|
, 0.0f
|
|
|
|
);
|
2016-07-25 01:50:42 +03:00
|
|
|
float mtx[16];
|
|
|
|
bx::mtxMul(mtx, mtxScale, mtxTrans);
|
|
|
|
meshSubmit(m_ground, _pass, _program, mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateLightDir()
|
|
|
|
{
|
2017-06-10 06:08:52 +03:00
|
|
|
float el = m_lightElevation * (bx::kPi/180.0f);
|
|
|
|
float az = m_lightAzimuth * (bx::kPi/180.0f);
|
2018-01-14 02:33:50 +03:00
|
|
|
m_lightDir[0] = bx::cos(el)*bx::cos(az);
|
|
|
|
m_lightDir[2] = bx::cos(el)*bx::sin(az);
|
|
|
|
m_lightDir[1] = bx::sin(el);
|
2016-07-25 01:50:42 +03:00
|
|
|
m_lightDir[3] = 0.0f;
|
|
|
|
}
|
2016-07-25 01:39:50 +03:00
|
|
|
|
|
|
|
uint32_t m_width;
|
|
|
|
uint32_t m_height;
|
|
|
|
uint32_t m_debug;
|
|
|
|
uint32_t m_reset;
|
|
|
|
|
2016-07-25 01:50:42 +03:00
|
|
|
entry::MouseState m_mouseState;
|
|
|
|
|
|
|
|
Mesh* m_ground;
|
|
|
|
Mesh* m_lightSphere; // Unit sphere
|
|
|
|
|
|
|
|
// Resource handles
|
2016-07-25 01:39:50 +03:00
|
|
|
bgfx::ProgramHandle m_gbufferProgram;
|
2016-07-25 01:50:42 +03:00
|
|
|
bgfx::ProgramHandle m_shadowProgram;
|
|
|
|
bgfx::ProgramHandle m_lightProgram;
|
|
|
|
bgfx::ProgramHandle m_combineProgram;
|
|
|
|
bgfx::FrameBufferHandle m_gbuffer;
|
|
|
|
bgfx::FrameBufferHandle m_lightBuffer;
|
|
|
|
bgfx::FrameBufferHandle m_shadowBuffer;
|
|
|
|
|
|
|
|
// Shader uniforms
|
|
|
|
bgfx::UniformHandle u_tint;
|
|
|
|
bgfx::UniformHandle u_invMvp;
|
|
|
|
bgfx::UniformHandle u_invMvpShadow;
|
|
|
|
bgfx::UniformHandle u_lightMtx;
|
|
|
|
bgfx::UniformHandle u_lightDir;
|
|
|
|
bgfx::UniformHandle u_sphereInfo;
|
|
|
|
bgfx::UniformHandle u_shadowDimsInv;
|
|
|
|
bgfx::UniformHandle u_rsmAmount;
|
|
|
|
|
|
|
|
// Uniforms to identify texture samples
|
|
|
|
bgfx::UniformHandle s_normal;
|
|
|
|
bgfx::UniformHandle s_depth;
|
|
|
|
bgfx::UniformHandle s_color;
|
|
|
|
bgfx::UniformHandle s_light;
|
|
|
|
bgfx::UniformHandle s_shadowMap;
|
|
|
|
bgfx::UniformHandle s_rsm;
|
|
|
|
|
|
|
|
// Various render targets
|
|
|
|
bgfx::TextureHandle m_gbufferTex[3];
|
|
|
|
bgfx::TextureHandle m_lightBufferTex;
|
|
|
|
bgfx::TextureHandle m_shadowBufferTex[2];
|
|
|
|
|
2016-07-25 02:18:26 +03:00
|
|
|
const bgfx::Caps* m_caps;
|
2016-07-25 01:50:42 +03:00
|
|
|
|
|
|
|
struct Model
|
|
|
|
{
|
|
|
|
uint32_t mesh; // Index of mesh in m_meshes
|
|
|
|
float color[4];
|
|
|
|
float position[3];
|
|
|
|
};
|
|
|
|
|
|
|
|
Model m_models[MODEL_COUNT];
|
2016-07-25 03:17:12 +03:00
|
|
|
Mesh * m_meshes[BX_COUNTOF(s_meshPaths)];
|
2016-07-25 01:50:42 +03:00
|
|
|
|
2016-07-25 02:18:26 +03:00
|
|
|
uint32_t m_reading;
|
|
|
|
uint32_t m_currFrame;
|
2016-07-25 01:50:42 +03:00
|
|
|
|
2016-07-25 02:18:26 +03:00
|
|
|
// UI
|
|
|
|
bool m_cameraSpin;
|
2016-07-25 01:50:42 +03:00
|
|
|
|
2016-07-25 02:18:26 +03:00
|
|
|
// Light position;
|
|
|
|
float m_lightDir[4];
|
|
|
|
float m_lightElevation;
|
|
|
|
float m_lightAzimuth;
|
2016-07-25 01:50:42 +03:00
|
|
|
|
2016-07-25 02:18:26 +03:00
|
|
|
float m_rsmAmount; // Amount of rsm
|
|
|
|
float m_vplRadius; // Radius of virtual point light
|
2016-07-25 01:50:42 +03:00
|
|
|
|
2016-07-25 02:18:26 +03:00
|
|
|
float m_texelHalf;
|
2016-07-25 01:39:50 +03:00
|
|
|
};
|
|
|
|
|
2017-06-26 07:44:04 +03:00
|
|
|
} // namespace
|
|
|
|
|
2019-08-17 23:25:39 +03:00
|
|
|
ENTRY_IMPLEMENT_MAIN(
|
2019-08-18 00:40:38 +03:00
|
|
|
ExampleRSM
|
|
|
|
, "31-rsm"
|
|
|
|
, "Global Illumination with Reflective Shadow Map."
|
|
|
|
, "https://bkaradzic.github.io/bgfx/examples.html#rsm"
|
|
|
|
);
|