Skip to content

Commit 49bdfa2

Browse files
committed
Prepared renderer for full bgfx management
1 parent b2fff03 commit 49bdfa2

File tree

9 files changed

+118
-36
lines changed

9 files changed

+118
-36
lines changed

igneous/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ set(SOURCES
88
src/core/igneous.cpp
99
src/core/input.cpp
1010
src/core/log.cpp
11-
src/ecs/systems/captureSystem.cpp
12-
src/ecs/systems/rendererSystem.cpp
1311
src/gui/gui.cpp
1412
src/gui/imgui_assets.h
1513
src/physics/physics.cpp
@@ -33,8 +31,6 @@ set(INCLUDES
3331
include/igneous/core/log.hpp
3432
include/igneous/core/version.hpp
3533
include/igneous/ecs/components/transformationComponent.hpp
36-
include/igneous/ecs/systems/captureSystem.hpp
37-
include/igneous/ecs/systems/rendererSystem.hpp
3834
include/igneous/gui/gui.hpp
3935
include/igneous/physics/physics.hpp
4036
include/igneous/renderer/aviwriter.hpp

igneous/include/igneous/ecs.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22

33
#include "igneous/ecs/components/transformationComponent.hpp"
44
#include "igneous/ecs/systems/captureSystem.hpp"
5-
#include "igneous/ecs/systems/rendererSystem.hpp"

igneous/include/igneous/ecs/systems/captureSystem.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
namespace igneous {
77
namespace CaptureSystem
88
{
9-
void screenshot();
10-
11-
void record();
12-
139
void onKey(int key, int scancode, int action, int mods);
1410

1511
extern bool capture;

igneous/include/igneous/renderer/renderer.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string>
55

66
#include <bgfx/bgfx.h>
7+
#include <entt/entt.hpp>
78

89
#include "igneous/renderer/model.hpp"
910

@@ -22,10 +23,11 @@ namespace renderer
2223
template<typename VertexType>
2324
Model* loadModel(std::string path, bgfx::ProgramHandle program = BGFX_INVALID_HANDLE);
2425

25-
void render();
26+
void render(entt::registry& registry);
2627
void screenshot();
27-
void setRecording(bool recording);
28+
void setRecording(bool record);
2829
bool isRecording();
30+
void setDebugOverlay(bool debugOverlay);
2931

3032
void shutdown();
3133

igneous/src/core/igneous.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,9 @@ int Application::run(int argc, char** argv, bgfx::Init init)
216216

217217
IG_CORE_INFO("Initializing Services");
218218
reset();
219+
console::init();
219220
renderer::init();
220-
RendererSystem::init();
221221
gui::init(mWindow);
222-
console::init();
223222
input::init(mWindow);
224223
console::runFile("startup.cmd");
225224
IG_CORE_INFO("Services Initialized");
@@ -290,7 +289,6 @@ int Application::run(int argc, char** argv, bgfx::Init init)
290289
IG_CORE_INFO("Shuting Down Services");
291290
audio::shutdown();
292291
gui::shutdown();
293-
RendererSystem::shutdown();
294292
renderer::shutdown();
295293
IG_CORE_INFO("Services Shut Down");
296294
bgfx::shutdown();
@@ -305,7 +303,7 @@ int Application::run(int argc, char** argv, bgfx::RendererType::Enum type, uint1
305303
init.type = type;
306304
init.vendorId = vendorId;
307305
init.deviceId = deviceId;
308-
init.callback = callback;
306+
init.callback = new CaptureCallback;
309307
init.allocator = allocator;
310308
return run(argc, argv, init);
311309
}

igneous/src/renderer/capture.cpp

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <bx/debug.h>
77
#include <inttypes.h>
88
#include <string>
9+
#include <filesystem>
10+
#include "igneous/core/log.hpp"
911

1012
namespace igneous {
1113
void savePng(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, bimg::TextureFormat::Enum _format, bool _yflip)
@@ -134,22 +136,42 @@ void CaptureCallback::screenShot(const char* _filePath, uint32_t _width, uint32_
134136
{
135137
char temp[1024];
136138

137-
// Save screen shot as PNG.
138-
bx::snprintf(temp, BX_COUNTOF(temp), "%s.png", _filePath);
139-
savePng(temp, _width, _height, _pitch, _data, bimg::TextureFormat::BGRA8, _yflip);
140-
141-
// Save screen shot as TGA.
142-
bx::snprintf(temp, BX_COUNTOF(temp), "%s.tga", _filePath);
143-
saveTga(temp, _width, _height, _pitch, _data, false, _yflip);
139+
std::string filePath(_filePath);
140+
std::string directory = filePath.substr(0, filePath.find_last_of('/'));
141+
if (!std::filesystem::exists(directory) && !std::filesystem::create_directories(directory))
142+
{
143+
IG_CORE_ERROR("Could not save screenshot");
144+
}
145+
else
146+
{
147+
// Save screen shot as PNG.
148+
bx::snprintf(temp, BX_COUNTOF(temp), "%s.png", _filePath);
149+
savePng(temp, _width, _height, _pitch, _data, bimg::TextureFormat::BGRA8, _yflip);
150+
IG_CORE_INFO("Screenshot saved to: {}", temp);
151+
152+
// Save screen shot as TGA.
153+
bx::snprintf(temp, BX_COUNTOF(temp), "%s.tga", _filePath);
154+
saveTga(temp, _width, _height, _pitch, _data, false, _yflip);
155+
IG_CORE_INFO("Screenshot saved to: {}", temp);
156+
}
144157
}
145158

146159
void CaptureCallback::captureBegin(uint32_t _width, uint32_t _height, uint32_t /*_pitch*/, bgfx::TextureFormat::Enum /*_format*/, bool _yflip)
147160
{
148-
m_writer = BX_NEW(allocator, AviWriter)(s_fileWriter);
149-
if (!m_writer->open("capture/capture.avi", _width, _height, 30, _yflip))
161+
std::string filePath("capture/capture.avi");
162+
std::string directory = filePath.substr(0, filePath.find_last_of('/'));
163+
if (!std::filesystem::exists(directory) && !std::filesystem::create_directories(directory))
150164
{
151-
BX_DELETE(allocator, m_writer);
152-
m_writer = NULL;
165+
IG_CORE_ERROR("Could not begin capture");
166+
}
167+
else
168+
{
169+
m_writer = BX_NEW(allocator, AviWriter)(s_fileWriter);
170+
if (!m_writer->open(filePath.c_str(), _width, _height, 30, _yflip))
171+
{
172+
BX_DELETE(allocator, m_writer);
173+
m_writer = NULL;
174+
}
153175
}
154176
}
155177

igneous/src/renderer/renderer.cpp

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include "igneous/core/log.hpp"
1010
#include "igneous/renderer/vertex.hpp"
1111
#include "splash_assets.h"
12+
#include "igneous/ecs/components/transformationComponent.hpp"
13+
#include "igneous/console/console.hpp"
14+
#include "igneous/core/input.hpp"
1215

1316
namespace igneous {
1417
namespace renderer
@@ -17,6 +20,26 @@ namespace renderer
1720
static std::map<std::string, bgfx::ProgramHandle> programs;
1821
std::map<std::string, Model*> models;
1922

23+
static bgfx::TextureHandle checkerBoard;
24+
25+
void screenshotCallback(const std::string& name, const arg_list& args)
26+
{
27+
screenshot();
28+
}
29+
30+
void recordingCallback(float oldValue, float newValue)
31+
{
32+
setRecording(newValue);
33+
}
34+
35+
void debugCallback(float oldValue, float newValue)
36+
{
37+
setDebugOverlay(newValue);
38+
}
39+
40+
static ConVar* recording;
41+
static ConVar* debug;
42+
2043
void init()
2144
{
2245
IG_CORE_INFO("Initializing Renderer");
@@ -55,9 +78,26 @@ namespace renderer
5578
Vertex::init();
5679
GenericVertex::init();
5780

58-
//console::command("screenshot", screenshotCallback);
59-
//console::command("start_record", startRecordCallback);
60-
//console::command("end_record", endRecordCallback);
81+
console::command("screenshot", screenshotCallback);
82+
console::bind(IG_KEY_F2, "screenshot");
83+
recording = &console::variable("recording", false, recordingCallback);
84+
console::bind(IG_KEY_F9, "^recording");
85+
debug = &console::variable("debug", false, debugCallback);
86+
console::bind(IG_KEY_F3, "^debug");
87+
88+
const uint32_t checkerBoardSize = 64;
89+
{
90+
const bgfx::Memory* mem = bgfx::alloc(checkerBoardSize * checkerBoardSize * 4);
91+
bimg::imageCheckerboard(mem->data, checkerBoardSize, checkerBoardSize, 8, 0xffe75480, 0xff000000);
92+
checkerBoard = bgfx::createTexture2D(checkerBoardSize, checkerBoardSize, false, 1
93+
, bgfx::TextureFormat::BGRA8
94+
, 0
95+
| BGFX_SAMPLER_MIN_POINT
96+
| BGFX_SAMPLER_MIP_POINT
97+
| BGFX_SAMPLER_MAG_POINT
98+
, mem
99+
);
100+
}
61101

62102
IG_CORE_INFO("Renderer Initialized");
63103
}
@@ -193,24 +233,48 @@ namespace renderer
193233
return bgfx::createProgram(vs, fs, true);
194234
}
195235

196-
void render()
236+
void render(entt::registry& registry)
197237
{
198-
238+
bgfx::UniformHandle s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Sampler);
239+
registry.view<ModelHandle, Transformation>().each([s_tex](const auto, auto& model, auto& transformation)
240+
{
241+
for (Mesh mesh : model->meshes)
242+
{
243+
bgfx::setTransform(&transformation.mtx);
244+
bgfx::setVertexBuffer(0, mesh.vbh);
245+
bgfx::setIndexBuffer(mesh.ibh);
246+
if (mesh.textures.size() > 0)
247+
bgfx::setTexture(0, s_tex, mesh.textures[0]);
248+
else
249+
bgfx::setTexture(0, s_tex, checkerBoard);
250+
bgfx::setState(BGFX_STATE_DEFAULT | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA));
251+
bgfx::submit(0, model->program);
252+
}
253+
});
254+
bgfx::destroy(s_tex);
199255
}
200256

201257
void screenshot()
202258
{
203-
259+
bgfx::requestScreenShot(BGFX_INVALID_HANDLE, "capture/screenshot");
260+
IG_CORE_INFO("Screenshot requested");
204261
}
205262

206-
void setRecording(bool recording)
263+
void setRecording(bool record)
207264
{
208-
265+
//set reset
266+
IG_CORE_INFO("Capture: {}", *recording ? "ON" : "OFF");
209267
}
210268

211269
bool isRecording()
212270
{
271+
return *recording;
272+
}
213273

274+
void setDebugOverlay(bool debugOverlay)
275+
{
276+
//set reset
277+
IG_CORE_INFO("Debug Overlay: {}", *debug ? "ON" : "OFF");
214278
}
215279

216280
void shutdown()
@@ -232,6 +296,8 @@ namespace renderer
232296
}
233297
delete it->second;
234298
}
299+
300+
bgfx::destroy(checkerBoard);
235301
IG_CORE_INFO("Renderer Shutdown");
236302
}
237303
}

samples/sandbox/src/sandbox.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Sandbox : public Application
4444
void render()
4545
{
4646
camera->use(input::width, input::height);
47-
RendererSystem::render(registry);
47+
renderer::render(registry);
4848

4949
ImGui::ShowDemoWindow();
5050
}

test/igneous/console.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
namespace {
66
TEST(Parse, Empty)
77
{
8-
EXPECT_NO_FATAL_FAILURE(igneous::Console::getInstance().execute(""));
8+
EXPECT_NO_FATAL_FAILURE({
9+
igneous::console::init();
10+
igneous::console::execute("");
11+
});
912
}
1013
} // end namespace

0 commit comments

Comments
 (0)