2014-05-22 07:33:12 +04:00
|
|
|
/*
|
2018-01-01 22:16:06 +03:00
|
|
|
* Copyright 2011-2018 Branimir Karadzic. All rights reserved.
|
2016-01-01 11:11:04 +03:00
|
|
|
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
|
2014-05-22 07:33:12 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "bgfx_utils.h"
|
|
|
|
#include "imgui/imgui.h"
|
|
|
|
#include "camera.h"
|
|
|
|
#include "bounds.h"
|
|
|
|
|
2017-06-26 07:44:04 +03:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2014-05-22 07:33:12 +04:00
|
|
|
#define RENDER_PASS_GEOMETRY_ID 0
|
|
|
|
#define RENDER_PASS_LIGHT_ID 1
|
|
|
|
#define RENDER_PASS_COMBINE_ID 2
|
|
|
|
#define RENDER_PASS_DEBUG_LIGHTS_ID 3
|
|
|
|
#define RENDER_PASS_DEBUG_GBUFFER_ID 4
|
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
static float s_texelHalf = 0.0f;
|
2014-10-06 09:03:41 +04:00
|
|
|
|
2014-05-22 07:33:12 +04:00
|
|
|
struct PosNormalTangentTexcoordVertex
|
|
|
|
{
|
|
|
|
float m_x;
|
|
|
|
float m_y;
|
|
|
|
float m_z;
|
|
|
|
uint32_t m_normal;
|
|
|
|
uint32_t m_tangent;
|
|
|
|
int16_t m_u;
|
|
|
|
int16_t m_v;
|
|
|
|
|
|
|
|
static void init()
|
|
|
|
{
|
|
|
|
ms_decl
|
|
|
|
.begin()
|
|
|
|
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
|
|
|
|
.add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
|
|
|
|
.add(bgfx::Attrib::Tangent, 4, bgfx::AttribType::Uint8, true, true)
|
|
|
|
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Int16, true, true)
|
|
|
|
.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bgfx::VertexDecl ms_decl;
|
|
|
|
};
|
|
|
|
|
|
|
|
bgfx::VertexDecl PosNormalTangentTexcoordVertex::ms_decl;
|
|
|
|
|
|
|
|
struct PosTexCoord0Vertex
|
|
|
|
{
|
|
|
|
float m_x;
|
|
|
|
float m_y;
|
|
|
|
float m_z;
|
|
|
|
float m_u;
|
|
|
|
float m_v;
|
|
|
|
|
|
|
|
static void init()
|
|
|
|
{
|
|
|
|
ms_decl
|
|
|
|
.begin()
|
|
|
|
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
|
|
|
|
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
|
|
|
|
.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bgfx::VertexDecl ms_decl;
|
|
|
|
};
|
|
|
|
|
|
|
|
bgfx::VertexDecl PosTexCoord0Vertex::ms_decl;
|
|
|
|
|
|
|
|
struct DebugVertex
|
|
|
|
{
|
|
|
|
float m_x;
|
|
|
|
float m_y;
|
|
|
|
float m_z;
|
|
|
|
uint32_t m_abgr;
|
|
|
|
|
|
|
|
static void init()
|
|
|
|
{
|
|
|
|
ms_decl
|
|
|
|
.begin()
|
|
|
|
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
|
|
|
|
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
|
|
|
|
.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bgfx::VertexDecl ms_decl;
|
|
|
|
};
|
|
|
|
|
|
|
|
bgfx::VertexDecl DebugVertex::ms_decl;
|
|
|
|
|
|
|
|
static PosNormalTangentTexcoordVertex s_cubeVertices[24] =
|
|
|
|
{
|
2017-04-01 07:01:08 +03:00
|
|
|
{-1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0, 0 },
|
|
|
|
{ 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0 },
|
|
|
|
{-1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0, 0x7fff },
|
|
|
|
{ 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0x7fff },
|
|
|
|
{-1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0, 0 },
|
|
|
|
{ 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0 },
|
|
|
|
{-1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0, 0x7fff },
|
|
|
|
{ 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0x7fff },
|
|
|
|
{-1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0, 0 },
|
|
|
|
{ 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0 },
|
|
|
|
{-1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0, 0x7fff },
|
|
|
|
{ 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0x7fff },
|
|
|
|
{-1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0, 0 },
|
|
|
|
{ 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0 },
|
|
|
|
{-1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0, 0x7fff },
|
|
|
|
{ 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0x7fff },
|
|
|
|
{ 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0, 0 },
|
|
|
|
{ 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
|
|
|
|
{ 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
|
|
|
|
{ 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
|
|
|
|
{-1.0f, -1.0f, 1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0, 0 },
|
|
|
|
{-1.0f, 1.0f, 1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
|
|
|
|
{-1.0f, -1.0f, -1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
|
|
|
|
{-1.0f, 1.0f, -1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
|
2014-05-22 07:33:12 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static const uint16_t s_cubeIndices[36] =
|
|
|
|
{
|
|
|
|
0, 2, 1,
|
|
|
|
1, 2, 3,
|
|
|
|
4, 5, 6,
|
|
|
|
5, 7, 6,
|
|
|
|
|
|
|
|
8, 10, 9,
|
|
|
|
9, 10, 11,
|
2014-09-03 11:29:54 +04:00
|
|
|
12, 13, 14,
|
2014-05-22 07:33:12 +04:00
|
|
|
13, 15, 14,
|
|
|
|
|
|
|
|
16, 18, 17,
|
|
|
|
17, 18, 19,
|
|
|
|
20, 21, 22,
|
|
|
|
21, 23, 22,
|
|
|
|
};
|
|
|
|
|
|
|
|
void screenSpaceQuad(float _textureWidth, float _textureHeight, float _texelHalf, bool _originBottomLeft, float _width = 1.0f, float _height = 1.0f)
|
|
|
|
{
|
2016-12-23 03:18:44 +03:00
|
|
|
if (3 == bgfx::getAvailTransientVertexBuffer(3, PosTexCoord0Vertex::ms_decl) )
|
2014-05-22 07:33:12 +04:00
|
|
|
{
|
|
|
|
bgfx::TransientVertexBuffer vb;
|
|
|
|
bgfx::allocTransientVertexBuffer(&vb, 3, PosTexCoord0Vertex::ms_decl);
|
|
|
|
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);
|
2014-05-22 07:33:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 02:29:22 +03:00
|
|
|
class ExampleDeferred : public entry::AppI
|
2014-05-22 07:33:12 +04:00
|
|
|
{
|
2017-06-26 07:44:04 +03:00
|
|
|
public:
|
|
|
|
ExampleDeferred(const char* _name, const char* _description)
|
|
|
|
: entry::AppI(_name, _description)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-15 10:17:29 +03:00
|
|
|
void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
|
2014-05-22 07:33:12 +04:00
|
|
|
{
|
2015-10-24 06:52:22 +03:00
|
|
|
Args args(_argc, _argv);
|
2016-04-02 08:48:57 +03:00
|
|
|
|
2017-06-30 08:23:18 +03:00
|
|
|
m_width = _width;
|
|
|
|
m_height = _height;
|
2017-12-27 22:41:40 +03:00
|
|
|
m_debug = BGFX_DEBUG_TEXT;
|
2015-10-20 01:09:56 +03:00
|
|
|
m_reset = BGFX_RESET_VSYNC;
|
2014-05-22 07:33:12 +04:00
|
|
|
|
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);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Enable m_debug text.
|
|
|
|
bgfx::setDebug(m_debug);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-09-17 03:21:28 +03:00
|
|
|
// Set palette color for index 0
|
|
|
|
bgfx::setPaletteColor(0, UINT32_C(0x00000000) );
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-09-17 03:21:28 +03:00
|
|
|
// Set palette color for index 1
|
|
|
|
bgfx::setPaletteColor(1, UINT32_C(0x303030ff) );
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Set geometry pass view clear state.
|
|
|
|
bgfx::setViewClear(RENDER_PASS_GEOMETRY_ID
|
|
|
|
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
|
|
|
|
, 1.0f
|
|
|
|
, 0
|
|
|
|
, 1
|
|
|
|
);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Set light pass view clear state.
|
|
|
|
bgfx::setViewClear(RENDER_PASS_LIGHT_ID
|
|
|
|
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
|
|
|
|
, 1.0f
|
|
|
|
, 0
|
|
|
|
, 0
|
2014-05-22 07:33:12 +04:00
|
|
|
);
|
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Create vertex stream declaration.
|
|
|
|
PosNormalTangentTexcoordVertex::init();
|
|
|
|
PosTexCoord0Vertex::init();
|
|
|
|
DebugVertex::init();
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
calcTangents(s_cubeVertices
|
|
|
|
, BX_COUNTOF(s_cubeVertices)
|
|
|
|
, PosNormalTangentTexcoordVertex::ms_decl
|
|
|
|
, s_cubeIndices
|
|
|
|
, BX_COUNTOF(s_cubeIndices)
|
|
|
|
);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Create static vertex buffer.
|
|
|
|
m_vbh = bgfx::createVertexBuffer(
|
|
|
|
bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
|
|
|
|
, PosNormalTangentTexcoordVertex::ms_decl
|
|
|
|
);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Create static index buffer.
|
|
|
|
m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Create texture sampler uniforms.
|
|
|
|
s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
|
|
|
|
s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Int1);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
s_albedo = bgfx::createUniform("s_albedo", bgfx::UniformType::Int1);
|
|
|
|
s_normal = bgfx::createUniform("s_normal", bgfx::UniformType::Int1);
|
|
|
|
s_depth = bgfx::createUniform("s_depth", bgfx::UniformType::Int1);
|
|
|
|
s_light = bgfx::createUniform("s_light", bgfx::UniformType::Int1);
|
2015-01-23 08:01:09 +03:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
|
|
|
|
u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4);
|
|
|
|
u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Vec4);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Create program from shaders.
|
|
|
|
m_geomProgram = loadProgram("vs_deferred_geom", "fs_deferred_geom");
|
|
|
|
m_lightProgram = loadProgram("vs_deferred_light", "fs_deferred_light");
|
|
|
|
m_combineProgram = loadProgram("vs_deferred_combine", "fs_deferred_combine");
|
|
|
|
m_debugProgram = loadProgram("vs_deferred_debug", "fs_deferred_debug");
|
|
|
|
m_lineProgram = loadProgram("vs_deferred_debug_line", "fs_deferred_debug_line");
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Load diffuse texture.
|
2016-04-22 08:12:35 +03:00
|
|
|
m_textureColor = loadTexture("textures/fieldstone-rgba.dds");
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Load normal texture.
|
2016-04-22 08:12:35 +03:00
|
|
|
m_textureNormal = loadTexture("textures/fieldstone-n.dds");
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2017-06-10 07:57:08 +03:00
|
|
|
m_gbufferTex[0].idx = bgfx::kInvalidHandle;
|
|
|
|
m_gbufferTex[1].idx = bgfx::kInvalidHandle;
|
|
|
|
m_gbufferTex[2].idx = bgfx::kInvalidHandle;
|
|
|
|
m_gbuffer.idx = bgfx::kInvalidHandle;
|
|
|
|
m_lightBuffer.idx = bgfx::kInvalidHandle;
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Imgui.
|
|
|
|
imguiCreate();
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
m_timeOffset = bx::getHPCounter();
|
|
|
|
const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
|
|
|
|
s_texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Get renderer capabilities info.
|
|
|
|
m_caps = bgfx::getCaps();
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
m_oldWidth = 0;
|
|
|
|
m_oldHeight = 0;
|
|
|
|
m_oldReset = m_reset;
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
m_scrollArea = 0;
|
|
|
|
m_numLights = 512;
|
|
|
|
m_lightAnimationSpeed = 0.3f;
|
|
|
|
m_animateMesh = true;
|
|
|
|
m_showScissorRects = false;
|
|
|
|
m_showGBuffer = true;
|
|
|
|
|
|
|
|
cameraCreate();
|
|
|
|
|
2018-12-22 08:05:26 +03:00
|
|
|
cameraSetPosition({ 0.0f, 0.0f, -15.0f });
|
2015-08-22 08:52:37 +03:00
|
|
|
cameraSetVerticalAngle(0.0f);
|
|
|
|
}
|
|
|
|
|
2017-07-15 10:17:29 +03:00
|
|
|
virtual int shutdown() override
|
2015-08-22 08:52:37 +03:00
|
|
|
{
|
|
|
|
// Cleanup.
|
|
|
|
cameraDestroy();
|
|
|
|
imguiDestroy();
|
|
|
|
|
|
|
|
if (bgfx::isValid(m_gbuffer) )
|
|
|
|
{
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(m_gbuffer);
|
|
|
|
bgfx::destroy(m_lightBuffer);
|
2015-08-22 08:52:37 +03:00
|
|
|
}
|
|
|
|
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(m_ibh);
|
|
|
|
bgfx::destroy(m_vbh);
|
2015-08-22 08:52:37 +03:00
|
|
|
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(m_geomProgram);
|
|
|
|
bgfx::destroy(m_lightProgram);
|
|
|
|
bgfx::destroy(m_combineProgram);
|
|
|
|
bgfx::destroy(m_debugProgram);
|
|
|
|
bgfx::destroy(m_lineProgram);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(m_textureColor);
|
|
|
|
bgfx::destroy(m_textureNormal);
|
|
|
|
bgfx::destroy(s_texColor);
|
|
|
|
bgfx::destroy(s_texNormal);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(s_albedo);
|
|
|
|
bgfx::destroy(s_normal);
|
|
|
|
bgfx::destroy(s_depth);
|
|
|
|
bgfx::destroy(s_light);
|
2015-08-22 08:52:37 +03:00
|
|
|
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(u_lightPosRadius);
|
|
|
|
bgfx::destroy(u_lightRgbInnerR);
|
|
|
|
bgfx::destroy(u_mtx);
|
2015-08-22 08:52:37 +03:00
|
|
|
|
|
|
|
// Shutdown bgfx.
|
|
|
|
bgfx::shutdown();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-15 10:17:29 +03:00
|
|
|
bool update() override
|
2015-08-22 08:52:37 +03:00
|
|
|
{
|
|
|
|
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
|
|
|
|
{
|
2017-06-26 07:44:04 +03:00
|
|
|
imguiBeginFrame(m_mouseState.m_mx
|
|
|
|
, 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);
|
2017-06-26 07:44:04 +03:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
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);
|
|
|
|
|
|
|
|
float time = (float)( (now-m_timeOffset)/freq);
|
|
|
|
|
2016-09-24 10:29:59 +03:00
|
|
|
if (2 > m_caps->limits.maxFBAttachments)
|
2014-05-22 07:33:12 +04:00
|
|
|
{
|
2015-08-22 08:52:37 +03:00
|
|
|
// When multiple render targets (MRT) is not supported by GPU,
|
|
|
|
// implement alternative code path that doesn't use MRT.
|
|
|
|
bool blink = uint32_t(time*3.0f)&1;
|
2018-01-26 07:34:17 +03:00
|
|
|
bgfx::dbgTextPrintf(0, 0, blink ? 0x4f : 0x04, " MRT not supported by GPU. ");
|
2015-08-22 08:52:37 +03:00
|
|
|
|
|
|
|
// Set view 0 default viewport.
|
2017-03-13 04:25:23 +03:00
|
|
|
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
2015-08-22 08:52:37 +03:00
|
|
|
|
|
|
|
// This dummy draw call is here to make sure that view 0 is cleared
|
|
|
|
// if no other draw calls are submitted to view 0.
|
|
|
|
bgfx::touch(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_oldWidth != m_width
|
2015-10-20 01:09:56 +03:00
|
|
|
|| m_oldHeight != m_height
|
|
|
|
|| m_oldReset != m_reset
|
|
|
|
|| !bgfx::isValid(m_gbuffer) )
|
2014-05-22 07:33:12 +04:00
|
|
|
{
|
2015-08-22 08:52:37 +03:00
|
|
|
// Recreate variable size render targets when resolution changes.
|
|
|
|
m_oldWidth = m_width;
|
|
|
|
m_oldHeight = m_height;
|
|
|
|
m_oldReset = m_reset;
|
|
|
|
|
|
|
|
if (bgfx::isValid(m_gbuffer) )
|
2014-05-22 07:33:12 +04:00
|
|
|
{
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(m_gbuffer);
|
2014-05-22 07:33:12 +04:00
|
|
|
}
|
2015-08-22 08:52:37 +03:00
|
|
|
|
2018-08-24 03:59:47 +03:00
|
|
|
const uint64_t tsFlags = 0
|
2015-08-22 08:52:37 +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
|
2015-08-22 08:52:37 +03:00
|
|
|
;
|
2018-08-24 03:59:47 +03:00
|
|
|
m_gbufferTex[0] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::BGRA8, tsFlags);
|
|
|
|
m_gbufferTex[1] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::BGRA8, tsFlags);
|
|
|
|
m_gbufferTex[2] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::D24S8, tsFlags);
|
2015-08-22 08:52:37 +03:00
|
|
|
m_gbuffer = bgfx::createFrameBuffer(BX_COUNTOF(m_gbufferTex), m_gbufferTex, true);
|
|
|
|
|
|
|
|
if (bgfx::isValid(m_lightBuffer) )
|
2014-05-22 07:33:12 +04:00
|
|
|
{
|
2017-07-18 08:29:43 +03:00
|
|
|
bgfx::destroy(m_lightBuffer);
|
2014-05-22 07:33:12 +04:00
|
|
|
}
|
|
|
|
|
2018-08-24 03:59:47 +03:00
|
|
|
m_lightBuffer = bgfx::createFrameBuffer(uint16_t(m_width), uint16_t(m_height), bgfx::TextureFormat::BGRA8, tsFlags);
|
2015-08-22 08:52:37 +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-06-02 06:54:29 +03:00
|
|
|
, NULL
|
2017-12-02 08:04:27 +03:00
|
|
|
, 0
|
2017-06-02 06:54:29 +03:00
|
|
|
);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2017-06-02 06:54:29 +03:00
|
|
|
ImGui::SliderInt("Num lights", &m_numLights, 1, 2048);
|
|
|
|
ImGui::Checkbox("Show G-Buffer.", &m_showGBuffer);
|
|
|
|
ImGui::Checkbox("Show light scissor.", &m_showScissorRects);
|
|
|
|
ImGui::Checkbox("Animate mesh.", &m_animateMesh);
|
|
|
|
ImGui::SliderFloat("Anim.speed", &m_lightAnimationSpeed, 0.0f, 0.4f);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2017-06-02 06:54:29 +03:00
|
|
|
ImGui::End();
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Update camera.
|
|
|
|
cameraUpdate(deltaTime, m_mouseState);
|
|
|
|
|
|
|
|
float view[16];
|
|
|
|
cameraGetViewMtx(view);
|
|
|
|
|
|
|
|
// Setup views
|
|
|
|
float vp[16];
|
|
|
|
float invMvp[16];
|
2014-05-22 07:33:12 +04:00
|
|
|
{
|
2017-03-13 04:25:23 +03:00
|
|
|
bgfx::setViewRect(RENDER_PASS_GEOMETRY_ID, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
|
|
|
bgfx::setViewRect(RENDER_PASS_LIGHT_ID, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
|
|
|
bgfx::setViewRect(RENDER_PASS_COMBINE_ID, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
|
|
|
bgfx::setViewRect(RENDER_PASS_DEBUG_LIGHTS_ID, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
|
|
|
bgfx::setViewRect(RENDER_PASS_DEBUG_GBUFFER_ID, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
2015-08-22 08:52:37 +03:00
|
|
|
|
|
|
|
bgfx::setViewFrameBuffer(RENDER_PASS_LIGHT_ID, m_lightBuffer);
|
|
|
|
|
|
|
|
float proj[16];
|
2016-04-02 08:48:57 +03:00
|
|
|
bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, m_caps->homogeneousDepth);
|
2015-08-22 08:52:37 +03:00
|
|
|
|
|
|
|
bgfx::setViewFrameBuffer(RENDER_PASS_GEOMETRY_ID, m_gbuffer);
|
|
|
|
bgfx::setViewTransform(RENDER_PASS_GEOMETRY_ID, view, proj);
|
|
|
|
|
|
|
|
bx::mtxMul(vp, view, proj);
|
|
|
|
bx::mtxInverse(invMvp, vp);
|
|
|
|
|
2017-06-13 08:43:07 +03:00
|
|
|
const bgfx::Caps* caps = bgfx::getCaps();
|
|
|
|
|
|
|
|
bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0f, caps->homogeneousDepth);
|
2015-08-22 08:52:37 +03:00
|
|
|
bgfx::setViewTransform(RENDER_PASS_LIGHT_ID, NULL, proj);
|
|
|
|
bgfx::setViewTransform(RENDER_PASS_COMBINE_ID, NULL, proj);
|
|
|
|
|
|
|
|
const float aspectRatio = float(m_height)/float(m_width);
|
|
|
|
const float size = 10.0f;
|
2017-06-13 08:43:07 +03:00
|
|
|
bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
|
2015-08-22 08:52:37 +03:00
|
|
|
bgfx::setViewTransform(RENDER_PASS_DEBUG_GBUFFER_ID, NULL, proj);
|
|
|
|
|
2017-06-13 08:43:07 +03:00
|
|
|
bx::mtxOrtho(proj, 0.0f, (float)m_width, 0.0f, (float)m_height, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
|
2015-08-22 08:52:37 +03:00
|
|
|
bgfx::setViewTransform(RENDER_PASS_DEBUG_LIGHTS_ID, NULL, proj);
|
2014-05-22 07:33:12 +04:00
|
|
|
}
|
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
const uint32_t dim = 11;
|
|
|
|
const float offset = (float(dim-1) * 3.0f) * 0.5f;
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Draw into geometry pass.
|
|
|
|
for (uint32_t yy = 0; yy < dim; ++yy)
|
|
|
|
{
|
|
|
|
for (uint32_t xx = 0; xx < dim; ++xx)
|
2014-05-22 07:33:12 +04:00
|
|
|
{
|
2015-08-22 08:52:37 +03:00
|
|
|
float mtx[16];
|
|
|
|
if (m_animateMesh)
|
2014-05-22 07:33:12 +04:00
|
|
|
{
|
2015-08-22 08:52:37 +03:00
|
|
|
bx::mtxRotateXY(mtx, time*1.023f + xx*0.21f, time*0.03f + yy*0.37f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bx::mtxIdentity(mtx);
|
|
|
|
}
|
|
|
|
mtx[12] = -offset + float(xx)*3.0f;
|
|
|
|
mtx[13] = -offset + float(yy)*3.0f;
|
|
|
|
mtx[14] = 0.0f;
|
|
|
|
|
|
|
|
// Set transform for draw call.
|
|
|
|
bgfx::setTransform(mtx);
|
|
|
|
|
|
|
|
// Set vertex and index buffer.
|
2017-05-14 21:48:59 +03:00
|
|
|
bgfx::setVertexBuffer(0, m_vbh);
|
2015-08-22 08:52:37 +03:00
|
|
|
bgfx::setIndexBuffer(m_ibh);
|
|
|
|
|
|
|
|
// Bind textures.
|
|
|
|
bgfx::setTexture(0, s_texColor, m_textureColor);
|
|
|
|
bgfx::setTexture(1, s_texNormal, m_textureNormal);
|
|
|
|
|
|
|
|
// Set render states.
|
|
|
|
bgfx::setState(0
|
2018-02-13 23:35:23 +03:00
|
|
|
| BGFX_STATE_WRITE_RGB
|
|
|
|
| BGFX_STATE_WRITE_A
|
|
|
|
| BGFX_STATE_WRITE_Z
|
2015-08-22 08:52:37 +03:00
|
|
|
| BGFX_STATE_DEPTH_TEST_LESS
|
|
|
|
| BGFX_STATE_MSAA
|
2014-05-22 07:33:12 +04:00
|
|
|
);
|
2015-08-22 08:52:37 +03:00
|
|
|
|
|
|
|
// Submit primitive for rendering to view 0.
|
|
|
|
bgfx::submit(RENDER_PASS_GEOMETRY_ID, m_geomProgram);
|
2014-05-22 07:33:12 +04:00
|
|
|
}
|
2015-08-22 08:52:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw lights into light buffer.
|
|
|
|
for (int32_t light = 0; light < m_numLights; ++light)
|
|
|
|
{
|
|
|
|
Sphere lightPosRadius;
|
|
|
|
|
2018-01-14 02:33:50 +03:00
|
|
|
float lightTime = time * m_lightAnimationSpeed * (bx::sin(light/float(m_numLights) * bx::kPiHalf ) * 0.5f + 0.5f);
|
2018-11-14 09:38:41 +03:00
|
|
|
lightPosRadius.m_center.x = bx::sin( ( (lightTime + light*0.47f) + bx::kPiHalf*1.37f ) )*offset;
|
|
|
|
lightPosRadius.m_center.y = bx::cos( ( (lightTime + light*0.69f) + bx::kPiHalf*1.49f ) )*offset;
|
|
|
|
lightPosRadius.m_center.z = bx::sin( ( (lightTime + light*0.37f) + bx::kPiHalf*1.57f ) )*2.0f;
|
|
|
|
lightPosRadius.m_radius = 2.0f;
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
Aabb aabb;
|
2016-12-06 04:06:13 +03:00
|
|
|
toAabb(aabb, lightPosRadius);
|
2015-08-22 08:52:37 +03:00
|
|
|
|
2019-01-04 02:16:29 +03:00
|
|
|
const bx::Vec3 box[8] =
|
2015-01-23 08:01:09 +03:00
|
|
|
{
|
2018-11-14 09:38:41 +03:00
|
|
|
{ aabb.m_min.x, aabb.m_min.y, aabb.m_min.z },
|
|
|
|
{ aabb.m_min.x, aabb.m_min.y, aabb.m_max.z },
|
|
|
|
{ aabb.m_min.x, aabb.m_max.y, aabb.m_min.z },
|
|
|
|
{ aabb.m_min.x, aabb.m_max.y, aabb.m_max.z },
|
|
|
|
{ aabb.m_max.x, aabb.m_min.y, aabb.m_min.z },
|
|
|
|
{ aabb.m_max.x, aabb.m_min.y, aabb.m_max.z },
|
|
|
|
{ aabb.m_max.x, aabb.m_max.y, aabb.m_min.z },
|
|
|
|
{ aabb.m_max.x, aabb.m_max.y, aabb.m_max.z },
|
2014-05-22 07:33:12 +04:00
|
|
|
};
|
|
|
|
|
2019-01-04 02:16:29 +03:00
|
|
|
bx::Vec3 xyz = bx::mulH(box[0], vp);
|
|
|
|
bx::Vec3 min = xyz;
|
|
|
|
bx::Vec3 max = xyz;
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
for (uint32_t ii = 1; ii < 8; ++ii)
|
|
|
|
{
|
2019-01-04 02:16:29 +03:00
|
|
|
xyz = bx::mulH(box[ii], vp);
|
|
|
|
min = bx::min(min, xyz);
|
|
|
|
max = bx::max(max, xyz);
|
2015-08-22 08:52:37 +03:00
|
|
|
}
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Cull light if it's fully behind camera.
|
2019-01-04 02:16:29 +03:00
|
|
|
if (max.z >= 0.0f)
|
2015-08-22 08:52:37 +03:00
|
|
|
{
|
2019-01-04 02:16:29 +03:00
|
|
|
const float x0 = bx::clamp( (min.x * 0.5f + 0.5f) * m_width, 0.0f, (float)m_width);
|
|
|
|
const float y0 = bx::clamp( (min.y * 0.5f + 0.5f) * m_height, 0.0f, (float)m_height);
|
|
|
|
const float x1 = bx::clamp( (max.x * 0.5f + 0.5f) * m_width, 0.0f, (float)m_width);
|
|
|
|
const float y1 = bx::clamp( (max.y * 0.5f + 0.5f) * m_height, 0.0f, (float)m_height);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
if (m_showScissorRects)
|
|
|
|
{
|
|
|
|
bgfx::TransientVertexBuffer tvb;
|
|
|
|
bgfx::TransientIndexBuffer tib;
|
|
|
|
if (bgfx::allocTransientBuffers(&tvb, DebugVertex::ms_decl, 4, &tib, 8) )
|
|
|
|
{
|
|
|
|
uint32_t abgr = 0x8000ff00;
|
|
|
|
|
|
|
|
DebugVertex* vertex = (DebugVertex*)tvb.data;
|
|
|
|
vertex->m_x = x0;
|
|
|
|
vertex->m_y = y0;
|
|
|
|
vertex->m_z = 0.0f;
|
|
|
|
vertex->m_abgr = abgr;
|
|
|
|
++vertex;
|
|
|
|
|
|
|
|
vertex->m_x = x1;
|
|
|
|
vertex->m_y = y0;
|
|
|
|
vertex->m_z = 0.0f;
|
|
|
|
vertex->m_abgr = abgr;
|
|
|
|
++vertex;
|
|
|
|
|
|
|
|
vertex->m_x = x1;
|
|
|
|
vertex->m_y = y1;
|
|
|
|
vertex->m_z = 0.0f;
|
|
|
|
vertex->m_abgr = abgr;
|
|
|
|
++vertex;
|
|
|
|
|
|
|
|
vertex->m_x = x0;
|
|
|
|
vertex->m_y = y1;
|
|
|
|
vertex->m_z = 0.0f;
|
|
|
|
vertex->m_abgr = abgr;
|
|
|
|
|
|
|
|
uint16_t* indices = (uint16_t*)tib.data;
|
|
|
|
*indices++ = 0;
|
|
|
|
*indices++ = 1;
|
|
|
|
*indices++ = 1;
|
|
|
|
*indices++ = 2;
|
|
|
|
*indices++ = 2;
|
|
|
|
*indices++ = 3;
|
|
|
|
*indices++ = 3;
|
|
|
|
*indices++ = 0;
|
|
|
|
|
2017-05-14 21:48:59 +03:00
|
|
|
bgfx::setVertexBuffer(0, &tvb);
|
2015-08-22 08:52:37 +03:00
|
|
|
bgfx::setIndexBuffer(&tib);
|
|
|
|
bgfx::setState(0
|
2018-02-13 23:35:23 +03:00
|
|
|
| BGFX_STATE_WRITE_RGB
|
2015-08-22 08:52:37 +03:00
|
|
|
| BGFX_STATE_PT_LINES
|
|
|
|
| BGFX_STATE_BLEND_ALPHA
|
|
|
|
);
|
|
|
|
bgfx::submit(RENDER_PASS_DEBUG_LIGHTS_ID, m_lineProgram);
|
|
|
|
}
|
|
|
|
}
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
uint8_t val = light&7;
|
|
|
|
float lightRgbInnerR[4] =
|
|
|
|
{
|
|
|
|
val & 0x1 ? 1.0f : 0.25f,
|
|
|
|
val & 0x2 ? 1.0f : 0.25f,
|
|
|
|
val & 0x4 ? 1.0f : 0.25f,
|
|
|
|
0.8f,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Draw light.
|
|
|
|
bgfx::setUniform(u_lightPosRadius, &lightPosRadius);
|
|
|
|
bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR);
|
|
|
|
bgfx::setUniform(u_mtx, invMvp);
|
|
|
|
const uint16_t scissorHeight = uint16_t(y1-y0);
|
2017-03-13 04:25:23 +03:00
|
|
|
bgfx::setScissor(uint16_t(x0), uint16_t(m_height-scissorHeight-y0), uint16_t(x1-x0), uint16_t(scissorHeight) );
|
2016-11-16 07:23:52 +03:00
|
|
|
bgfx::setTexture(0, s_normal, bgfx::getTexture(m_gbuffer, 1) );
|
|
|
|
bgfx::setTexture(1, s_depth, bgfx::getTexture(m_gbuffer, 2) );
|
2015-08-22 08:52:37 +03:00
|
|
|
bgfx::setState(0
|
2018-02-13 23:35:23 +03:00
|
|
|
| BGFX_STATE_WRITE_RGB
|
|
|
|
| BGFX_STATE_WRITE_A
|
2015-08-22 08:52:37 +03:00
|
|
|
| BGFX_STATE_BLEND_ADD
|
|
|
|
);
|
2016-04-02 08:48:57 +03:00
|
|
|
screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, m_caps->originBottomLeft);
|
2015-08-22 08:52:37 +03:00
|
|
|
bgfx::submit(RENDER_PASS_LIGHT_ID, m_lightProgram);
|
|
|
|
}
|
2014-05-22 07:33:12 +04:00
|
|
|
}
|
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Combine color and light buffers.
|
2016-11-16 07:23:52 +03:00
|
|
|
bgfx::setTexture(0, s_albedo, bgfx::getTexture(m_gbuffer, 0) );
|
|
|
|
bgfx::setTexture(1, s_light, bgfx::getTexture(m_lightBuffer, 0) );
|
2015-08-22 08:52:37 +03:00
|
|
|
bgfx::setState(0
|
2018-02-13 23:35:23 +03:00
|
|
|
| BGFX_STATE_WRITE_RGB
|
|
|
|
| BGFX_STATE_WRITE_A
|
2015-08-22 08:52:37 +03:00
|
|
|
);
|
2016-04-02 08:48:57 +03:00
|
|
|
screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, m_caps->originBottomLeft);
|
2015-08-22 08:52:37 +03:00
|
|
|
bgfx::submit(RENDER_PASS_COMBINE_ID, m_combineProgram);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
if (m_showGBuffer)
|
|
|
|
{
|
|
|
|
const float aspectRatio = float(m_width)/float(m_height);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Draw m_debug m_gbuffer.
|
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(m_gbufferTex); ++ii)
|
|
|
|
{
|
|
|
|
float mtx[16];
|
|
|
|
bx::mtxSRT(mtx
|
|
|
|
, aspectRatio, 1.0f, 1.0f
|
|
|
|
, 0.0f, 0.0f, 0.0f
|
|
|
|
, -7.9f - BX_COUNTOF(m_gbufferTex)*0.1f*0.5f + ii*2.1f*aspectRatio, 4.0f, 0.0f
|
|
|
|
);
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
bgfx::setTransform(mtx);
|
2017-05-14 21:48:59 +03:00
|
|
|
bgfx::setVertexBuffer(0, m_vbh);
|
2015-08-22 08:52:37 +03:00
|
|
|
bgfx::setIndexBuffer(m_ibh, 0, 6);
|
|
|
|
bgfx::setTexture(0, s_texColor, m_gbufferTex[ii]);
|
2018-02-13 23:35:23 +03:00
|
|
|
bgfx::setState(BGFX_STATE_WRITE_RGB);
|
2015-08-22 08:52:37 +03:00
|
|
|
bgfx::submit(RENDER_PASS_DEBUG_GBUFFER_ID, m_debugProgram);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2017-06-26 07:44:04 +03:00
|
|
|
imguiEndFrame();
|
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
// Advance to next frame. Rendering thread will be kicked to
|
|
|
|
// process submitted rendering primitives.
|
|
|
|
bgfx::frame();
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2017-06-30 09:19:20 +03:00
|
|
|
return true;
|
2015-08-22 08:52:37 +03:00
|
|
|
}
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2015-08-22 08:52:37 +03:00
|
|
|
bgfx::VertexBufferHandle m_vbh;
|
|
|
|
bgfx::IndexBufferHandle m_ibh;
|
|
|
|
bgfx::UniformHandle s_texColor;
|
|
|
|
bgfx::UniformHandle s_texNormal;
|
|
|
|
|
|
|
|
bgfx::UniformHandle s_albedo;
|
|
|
|
bgfx::UniformHandle s_normal;
|
|
|
|
bgfx::UniformHandle s_depth;
|
|
|
|
bgfx::UniformHandle s_light;
|
|
|
|
|
|
|
|
bgfx::UniformHandle u_mtx;
|
|
|
|
bgfx::UniformHandle u_lightPosRadius;
|
|
|
|
bgfx::UniformHandle u_lightRgbInnerR;
|
|
|
|
|
|
|
|
bgfx::ProgramHandle m_geomProgram;
|
|
|
|
bgfx::ProgramHandle m_lightProgram;
|
|
|
|
bgfx::ProgramHandle m_combineProgram;
|
|
|
|
bgfx::ProgramHandle m_debugProgram;
|
|
|
|
bgfx::ProgramHandle m_lineProgram;
|
|
|
|
bgfx::TextureHandle m_textureColor;
|
|
|
|
bgfx::TextureHandle m_textureNormal;
|
|
|
|
|
|
|
|
bgfx::TextureHandle m_gbufferTex[3];
|
|
|
|
bgfx::FrameBufferHandle m_gbuffer;
|
|
|
|
bgfx::FrameBufferHandle m_lightBuffer;
|
|
|
|
|
|
|
|
uint32_t m_width;
|
|
|
|
uint32_t m_height;
|
|
|
|
uint32_t m_debug;
|
|
|
|
uint32_t m_reset;
|
|
|
|
|
|
|
|
uint32_t m_oldWidth;
|
|
|
|
uint32_t m_oldHeight;
|
|
|
|
uint32_t m_oldReset;
|
|
|
|
|
|
|
|
int32_t m_scrollArea;
|
|
|
|
int32_t m_numLights;
|
|
|
|
float m_lightAnimationSpeed;
|
|
|
|
bool m_animateMesh;
|
|
|
|
bool m_showScissorRects;
|
|
|
|
bool m_showGBuffer;
|
|
|
|
|
|
|
|
entry::MouseState m_mouseState;
|
|
|
|
|
|
|
|
const bgfx::Caps* m_caps;
|
|
|
|
int64_t m_timeOffset;
|
|
|
|
};
|
2014-05-22 07:33:12 +04:00
|
|
|
|
2017-06-26 07:44:04 +03:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ENTRY_IMPLEMENT_MAIN(ExampleDeferred, "21-deferred", "MRT rendering and deferred shading.");
|