-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
291 lines (235 loc) · 10.7 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright (c) Adubbz
* Copyright (c) Ixaruz
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, or any later version, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <optional>
#include <switch.h>
#include <nanovg.h>
#include <nanovg_dk.h>
#include <nanovg/framework/CApplication.h>
#include "ui.hpp"
extern "C" {
void userAppInit(void) {
setsysInitialize();
socketInitializeDefault();
nxlinkStdio();
nsInitialize();
//accountInitialize(AccountServiceType_Administrator);
plInitialize(PlServiceType_User);
psmInitialize();
pminfoInitialize();
pmdmntInitialize();
dmntchtInitialize();
romfsInit();
hidsysInitialize();
clkrstInitialize();
pcvInitialize();
}
void userAppExit(void) {
setsysExit();
socketExit();
nsExit();
//accountExit();
plExit();
psmExit();
pminfoExit();
//keep those services alive, bc other programs may be using them at the same time
//pmdmntExit();
//dmntchtExit();
romfsExit();
hidsysExit();
clkrstExit();
pcvExit();
}
}
void createFileStructure() {
std::string luna_dir = std::string(LUNA_DIR).substr(0, strlen(LUNA_DIR));
if (access(luna_dir.c_str(), F_OK) == -1) {
mkdir(luna_dir.c_str(), 0777);
util::PrintToNXLink("created %s\n", luna_dir.c_str());
}
std::string luna_dump_dir = std::string(LUNA_DUMP_DIR).substr(0, strlen(LUNA_DUMP_DIR));
if (access(luna_dump_dir.c_str(), F_OK) == -1) {
mkdir(luna_dump_dir.c_str(), 0777);
util::PrintToNXLink("created %s\n", luna_dump_dir.c_str());
}
std::string luna_template_dir = std::string(LUNA_TEMPLATE_DIR).substr(0, strlen(LUNA_TEMPLATE_DIR));
if (access(luna_template_dir.c_str(), F_OK) == -1) {
mkdir(luna_template_dir.c_str(), 0777);
util::PrintToNXLink("created %s\n", luna_template_dir.c_str());
}
}
namespace {
static constexpr u32 FramebufferWidth = 1280;
static constexpr u32 FramebufferHeight = 720;
}
class Luna : public CApplication {
private:
static constexpr unsigned NumFramebuffers = 2;
static constexpr unsigned StaticCmdSize = 0x1000;
dk::UniqueDevice m_device;
dk::UniqueQueue m_queue;
dk::UniqueSwapchain m_swapchain;
std::optional<CMemPool> m_pool_images;
std::optional<CMemPool> m_pool_code;
std::optional<CMemPool> m_pool_data;
dk::UniqueCmdBuf m_cmd_buf;
DkCmdList m_render_cmdlist;
dk::Image m_depth_buffer;
CMemPool::Handle m_depth_buffer_mem;
dk::Image m_framebuffers[NumFramebuffers];
CMemPool::Handle m_framebuffers_mem[NumFramebuffers];
DkCmdList m_framebuffer_cmdlists[NumFramebuffers];
std::optional<nvg::DkRenderer> m_renderer;
NVGcontext *m_vg;
int m_standard_font;
public:
Luna() {
Result rc = 0;
createFileStructure();
/* Create the deko3d device. */
m_device = dk::DeviceMaker{}.create();
/* Create the main queue. */
m_queue = dk::QueueMaker{m_device}.setFlags(DkQueueFlags_Graphics).create();
/* Create the memory pools. */
m_pool_images.emplace(m_device, DkMemBlockFlags_GpuCached | DkMemBlockFlags_Image, 16*1024*1024);
m_pool_code.emplace(m_device, DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached | DkMemBlockFlags_Code, 128*1024);
m_pool_data.emplace(m_device, DkMemBlockFlags_CpuUncached | DkMemBlockFlags_GpuCached, 1*1024*1024);
/* Create the static command buffer and feed it freshly allocated memory. */
m_cmd_buf = dk::CmdBufMaker{m_device}.create();
CMemPool::Handle cmdmem = m_pool_data->allocate(StaticCmdSize);
m_cmd_buf.addMemory(cmdmem.getMemBlock(), cmdmem.getOffset(), cmdmem.getSize());
/* Create the framebuffer resources. */
this->CreateFramebufferResources();
m_renderer.emplace(FramebufferWidth, FramebufferHeight, m_device, m_queue, *m_pool_images, *m_pool_code, *m_pool_data);
m_vg = nvgCreateDk(&*m_renderer, NVG_ANTIALIAS | NVG_STENCIL_STROKES);
PlFontData font;
if (R_FAILED(rc = plGetSharedFontByType(&font, PlSharedFontType_Standard))) {
fatalThrow(12);
}
nvgCreateFontMem(m_vg, "switch-standard", static_cast<u8 *>(font.address), font.size, 0);
if (R_FAILED(rc = plGetSharedFontByType(&font, PlSharedFontType_NintendoExt))) {
fatalThrow(13);
}
nvgCreateFontMem(m_vg, "switch-ext", static_cast<u8*>(font.address), font.size, 0);
m_standard_font = nvgAddFallbackFont(m_vg, "switch-standard", "switch-ext");
}
~Luna() {
/* Destroy the framebuffer resources. This should be done first. */
this->DestroyFramebufferResources();
/* Cleanup vg. */
nvgDeleteDk(m_vg);
/* Destroy the renderer. */
m_renderer.reset();
}
private:
void CreateFramebufferResources() {
/* Create layout for the depth buffer. */
dk::ImageLayout layout_depth_buffer;
dk::ImageLayoutMaker{m_device}
.setFlags(DkImageFlags_UsageRender | DkImageFlags_HwCompression)
.setFormat(DkImageFormat_S8)
.setDimensions(FramebufferWidth, FramebufferHeight)
.initialize(layout_depth_buffer);
/* Create the depth buffer. */
m_depth_buffer_mem = m_pool_images->allocate(layout_depth_buffer.getSize(), layout_depth_buffer.getAlignment());
m_depth_buffer.initialize(layout_depth_buffer, m_depth_buffer_mem.getMemBlock(), m_depth_buffer_mem.getOffset());
/* Create layout for the framebuffers. */
dk::ImageLayout layout_framebuffer;
dk::ImageLayoutMaker{m_device}
.setFlags(DkImageFlags_UsageRender | DkImageFlags_UsagePresent | DkImageFlags_HwCompression)
.setFormat(DkImageFormat_RGBA8_Unorm)
.setDimensions(FramebufferWidth, FramebufferHeight)
.initialize(layout_framebuffer);
/* Create the framebuffers. */
std::array<DkImage const*, NumFramebuffers> fb_array;
const u64 fb_size = layout_framebuffer.getSize();
const u32 fb_align = layout_framebuffer.getAlignment();
for (unsigned int i = 0; i < NumFramebuffers; i++) {
/* Allocate a framebuffer. */
m_framebuffers_mem[i] = m_pool_images->allocate(fb_size, fb_align);
m_framebuffers[i].initialize(layout_framebuffer, m_framebuffers_mem[i].getMemBlock(), m_framebuffers_mem[i].getOffset());
/* Generate a command list that binds it. */
dk::ImageView color_target{ m_framebuffers[i] }, depth_target{ m_depth_buffer };
m_cmd_buf.bindRenderTargets(&color_target, &depth_target);
m_framebuffer_cmdlists[i] = m_cmd_buf.finishList();
/* Fill in the array for use later by the swapchain creation code. */
fb_array[i] = &m_framebuffers[i];
}
/* Create the swapchain using the framebuffers. */
m_swapchain = dk::SwapchainMaker{m_device, nwindowGetDefault(), fb_array}.create();
/* Generate the main rendering cmdlist. */
this->RecordStaticCommands();
}
void DestroyFramebufferResources() {
/* Return early if we have nothing to destroy. */
if (!m_swapchain) return;
/* Make sure the queue is idle before destroying anything. */
m_queue.waitIdle();
/* Clear the static cmdbuf, destroying the static cmdlists in the process. */
m_cmd_buf.clear();
/* Destroy the swapchain. */
m_swapchain.destroy();
/* Destroy the framebuffers. */
for (unsigned int i = 0; i < NumFramebuffers; i ++) {
m_framebuffers_mem[i].destroy();
}
/* Destroy the depth buffer. */
m_depth_buffer_mem.destroy();
}
void RecordStaticCommands() {
/* Initialize state structs with deko3d defaults. */
dk::RasterizerState rasterizer_state;
dk::ColorState color_state;
dk::ColorWriteState color_write_state;
/* Configure the viewport and scissor. */
m_cmd_buf.setViewports(0, { { 0.0f, 0.0f, FramebufferWidth, FramebufferHeight, 0.0f, 1.0f } });
m_cmd_buf.setScissors(0, { { 0, 0, FramebufferWidth, FramebufferHeight } });
/* Clear the color and depth buffers. */
m_cmd_buf.clearColor(0, DkColorMask_RGBA, 0.f, 0.f, 0.f, 1.0f);
m_cmd_buf.clearDepthStencil(true, 1.0f, 0xFF, 0);
/* Bind required state. */
m_cmd_buf.bindRasterizerState(rasterizer_state);
m_cmd_buf.bindColorState(color_state);
m_cmd_buf.bindColorWriteState(color_write_state);
m_render_cmdlist = m_cmd_buf.finishList();
}
void Render(u64 ns) {
/* Acquire a framebuffer from the swapchain (and wait for it to be available). */
int slot = m_queue.acquireImage(m_swapchain);
/* Run the command list that attaches said framebuffer to the queue. */
m_queue.submitCommands(m_framebuffer_cmdlists[slot]);
/* Run the main rendering command list. */
m_queue.submitCommands(m_render_cmdlist);
nvgBeginFrame(m_vg, FramebufferWidth, FramebufferHeight, 1.0f);
dbk::RenderMenu(m_vg, ns);
nvgEndFrame(m_vg);
/* Now that we are done rendering, present it to the screen. */
m_queue.presentImage(m_swapchain, slot);
}
public:
bool onFrame(u64 ns) override {
dbk::UpdateMenu(ns);
this->Render(ns);
return !dbk::IsExitRequested();
}
};
int main(int argc, char **argv) {
/* Initialize the menu. */
dbk::InitializeMenu(FramebufferWidth, FramebufferHeight);
Luna luna;
luna.run();
return 0;
}