nngn
Loading...
Searching...
No Matches
graphics.h
Go to the documentation of this file.
1
86#ifndef NNGN_GRAPHICS_GRAPHICS_H
87#define NNGN_GRAPHICS_GRAPHICS_H
88
89#include <array>
90#include <bit>
91#include <cmath>
92#include <cstdint>
93#include <memory>
94#include <span>
95#include <string>
96#include <tuple>
97
98#include "const.h"
99#include "math/mat4.h"
100#include "math/math.h"
101#include "math/vec2.h"
102#include "math/vec3.h"
103#include "utils/def.h"
104#include "utils/flags.h"
105#include "utils/utils.h"
106
107#include "stats.h"
108
109struct GLFWwindow;
110
111namespace nngn {
112
113struct CameraUBO { mat4 proj, view; };
114struct LightsUBO {
120 alignas(16) vec3 view_pos = {};
122 vec3 ambient = {1, 1, 1};
124 struct {
125 std::array<vec4, NNGN_MAX_LIGHTS> dir = {};
126 std::array<vec4, NNGN_MAX_LIGHTS> color_spec = {};
127 std::array<mat4, NNGN_MAX_LIGHTS> mat = {};
129 struct {
130 std::array<vec4, NNGN_MAX_LIGHTS> dir = {};
131 std::array<vec4, NNGN_MAX_LIGHTS> color_spec = {};
132 std::array<vec4, NNGN_MAX_LIGHTS> pos = {};
133 std::array<vec4, NNGN_MAX_LIGHTS> att_cutoff = {};
135};
136struct Vertex { vec3 pos, norm, color; };
137
138struct Graphics {
139 using size_callback_f = void (*)(void*, uvec2);
140 using key_callback_f = void (*)(void*, int, int, int, int);
141 using mouse_button_callback_f = void (*)(void*, int, int, int);
142 using mouse_move_callback_f = void (*)(void*, dvec2);
147 enum class LogLevel { DEBUG, WARNING, ERROR };
149 struct Parameters {
150 enum Flag {
151 HIDDEN = 1u << 0, DEBUG = 1u << 1,
152 };
154 };
155 struct Version { u32 major, minor, patch; const char *name; };
156 enum class TerminalFlag : u8 {
157 CLEAR = 1u << 0,
158 REPOSITION = 1u << 1,
159 DEDUPLICATE = 1u << 2,
160 HIDE_CURSOR = 1u << 3,
161 RESET_COLOR = 1u << 4,
162 };
163 enum class TerminalMode { ASCII, COLORED };
165 int fd = -1;
167 TerminalMode mode = TerminalMode::ASCII;
168 };
169 struct OpenGLParameters : Parameters { int maj = {}, min = {}; };
174 struct Extension {
175 static constexpr std::size_t name_max_len = 256;
176 std::array<char, name_max_len> name = {};
178 };
179 struct Layer {
180 std::array<char, 256> name = {};
181 std::array<char, 256> description = {};
182 std::array<char, 15> spec_version = {};
184 };
185 struct DeviceInfo {
186 enum class Type {
188 };
189 std::array<char, 256> name = {};
190 std::array<char, 15> version = {};
192 Type type = {};
193 };
194 struct QueueFamily {
195 enum Flag : u8 {
196 GRAPHICS = 1u << 0, COMPUTE = 1u << 1, TRANSFER = 1u << 2,
197 PRESENT = 1u << 3,
198 };
199 u32 count = {};
201 };
202 struct MemoryHeap {
203 enum Flag : u8 { DEVICE_LOCAL = 1u << 0 };
204 u64 size = {};
206 };
207 struct MemoryType {
208 enum Flag : std::uint8_t {
209 DEVICE_LOCAL = 1u << 0, HOST_VISIBLE = 1u << 1,
210 HOST_COHERENT = 1u << 2, HOST_CACHED = 1u << 3,
212 };
214 };
215 struct SurfaceInfo {
218 };
220 enum Flag : u8 {
221 DEPTH_TEST = 1u << 0,
222 DEPTH_WRITE = 1u << 1,
224 LINE = 1u << 3,
225 };
226 enum class Type : u8 {
228 };
229 const char *name = {};
230 Type type = {};
232 };
234 enum class Type { VERTEX, INDEX };
235 const char *name = {};
236 Type type = {};
237 u64 size = {};
238 };
239 struct RenderList {
240 struct Stage {
242 std::span<const std::pair<u32, u32>> buffers = {};
243 };
244 std::span<const Stage>
245 depth = {}, map_ortho = {}, map_persp = {},
246 normal = {}, no_light = {}, overlay = {}, screen = {},
248 };
250 struct Camera {
251 uint8_t *flags = nullptr;
252 const uvec2 *screen = nullptr;
253 const mat4 *proj = nullptr, *screen_proj = nullptr, *view = nullptr;
254 };
255 struct Lighting {
256 const LightsUBO *ubo = nullptr;
257 const mat4
258 *dir_proj = nullptr, *point_proj = nullptr,
259 *dir_views = nullptr, *point_views = nullptr;
260 };
261 static constexpr u32
268 static constexpr float
272 static constexpr std::size_t
276 static_assert(std::popcount(TEXTURE_EXTENT) == 1);
277 static std::unique_ptr<Graphics> create(Backend b, const void *params);
278 static const char *enum_str(DeviceInfo::Type t);
279 static const char *enum_str(QueueFamily::Flag f);
280 static const char *enum_str(PresentMode m);
281 static const char *enum_str(MemoryHeap::Flag m);
282 static const char *enum_str(MemoryType::Flag m);
283 static std::string flags_str(QueueFamily::Flag f);
284 static std::string flags_str(MemoryHeap::Flag f);
285 static std::string flags_str(MemoryType::Flag f);
287 // Initialization
292 virtual bool init();
293 virtual bool init_backend() = 0;
294 virtual bool init_instance() = 0;
295 virtual bool init_device() = 0;
296 virtual bool init_device(std::size_t i) = 0;
297 // Information
298 virtual std::size_t n_extensions() const = 0;
299 virtual std::size_t n_layers() const = 0;
300 virtual std::size_t n_devices() const = 0;
301 virtual std::size_t n_device_extensions(std::size_t i) const = 0;
302 virtual std::size_t n_queue_families(std::size_t i) const = 0;
303 virtual std::size_t n_present_modes() const = 0;
304 virtual std::size_t n_heaps(std::size_t i) const = 0;
305 virtual std::size_t n_memory_types(std::size_t i, std::size_t ih) const = 0;
306 virtual std::size_t selected_device() const = 0;
307 virtual void extensions(Extension *p) const = 0;
308 virtual void layers(Layer *p) const = 0;
309 virtual void device_infos(DeviceInfo *p) const = 0;
310 virtual void device_extensions(std::size_t i, Extension *p) const = 0;
311 virtual void queue_families(std::size_t i, QueueFamily *p) const = 0;
312 virtual SurfaceInfo surface_info() const = 0;
313 virtual void present_modes(PresentMode *p) const = 0;
314 virtual void heaps(std::size_t i, MemoryHeap *p) const = 0;
315 virtual void memory_types(
316 std::size_t i, std::size_t ih, MemoryType *p) const = 0;
317 // Operations
318 virtual bool error() = 0;
319 virtual Version version() const = 0;
320 virtual bool window_closed() const = 0;
321 virtual int swap_interval() const = 0;
322 virtual uvec2 window_size() const = 0;
323 virtual GraphicsStats stats() = 0;
324 virtual void get_keys(size_t n, int32_t *keys) const = 0;
325 virtual ivec2 mouse_pos(void) const = 0;
326 virtual bool set_n_frames(std::size_t n) = 0;
327 virtual bool set_n_swap_chain_images(std::size_t n) = 0;
328 virtual void set_swap_interval(int i) = 0;
329 virtual void set_window_title(const char *t) = 0;
330 virtual void set_cursor_mode(CursorMode m) = 0;
331 virtual void set_size_callback(void *data, size_callback_f f) = 0;
332 virtual void set_key_callback(void *data, key_callback_f f) = 0;
334 void *data, mouse_button_callback_f f) = 0;
336 void *data, mouse_move_callback_f f) = 0;
337 virtual void resize(int w, int h) = 0;
338 virtual void set_camera(const Camera &c) = 0;
339 virtual void set_lighting(const Lighting &l) = 0;
340 virtual void set_camera_updated() = 0;
341 virtual void set_lighting_updated() = 0;
342 virtual bool set_shadow_map_size(uint32_t s) = 0;
343 virtual bool set_shadow_cube_size(uint32_t s) = 0;
344 // Post-processing
345 virtual void set_automatic_exposure(bool b) = 0;
346 virtual void set_exposure(float e) = 0;
347 virtual void set_bloom_downscale(std::size_t d) = 0;
348 virtual void set_bloom_threshold(float t) = 0;
349 virtual void set_bloom_blur_size(float n) = 0;
350 virtual void set_bloom_blur_passes(std::size_t n) = 0;
351 virtual void set_bloom_amount(float a) = 0;
352 virtual void set_blur_downscale(std::size_t d) = 0;
353 virtual void set_blur_size(float n) = 0;
354 virtual void set_blur_passes(std::size_t n) = 0;
355 virtual void set_HDR_mix(float m) = 0;
356 // Pipelines
357 virtual u32 create_pipeline(const PipelineConfiguration &conf) = 0;
358 // Buffers
359 virtual u32 create_buffer(const BufferConfiguration &conf) = 0;
360 virtual bool set_buffer_capacity(u32 b, u64 size) = 0;
361 virtual bool set_buffer_size(u32 b, std::uint64_t size) = 0;
362 virtual bool write_to_buffer(
363 u32 b, u64 offset, u64 n, u64 size,
364 void *data, void f(void*, void*, u64, u64)) = 0;
365 template<typename F>
366 bool write_to_buffer(u32 b, u64 offset, u64 n, u64 size, F &&f);
367 bool update_buffers(
368 u32 vbo, u32 ebo, u64 voff, u64 eoff,
369 u64 vn, u64 vsize, u64 en, u64 esize,
370 void *data, auto &&vgen, auto &&egen);
371 // Textures
372 virtual bool resize_textures(std::uint32_t s) = 0;
373 virtual bool load_textures(
374 std::uint32_t i, std::uint32_t n, const std::byte *v) = 0;
375 // Fonts
376 virtual bool resize_font(std::uint32_t s) = 0;
377 virtual bool load_font(
378 unsigned char c, std::uint32_t n,
379 const nngn::uvec2 *size, const std::byte *v) = 0;
380 // Rendering
381 virtual bool set_render_list(const RenderList &l) = 0;
382 virtual void poll_events() const = 0;
383 virtual bool render() = 0;
384 virtual bool vsync() = 0;
385};
386
387inline bool Graphics::init() {
388 return this->init_backend()
389 && this->init_instance()
390 && this->init_device({});
391}
392
393template<typename F>
394bool Graphics::write_to_buffer(u32 b, u64 offset, u64 n, u64 size, F &&f) {
395 return this->write_to_buffer(
396 b, offset, n, size, &f,
397 [](void *p, auto ...args) { (*static_cast<F*>(p))(args...); });
398}
399
401 u32 vbo, u32 ebo, u64 voff, u64 eoff,
402 u64 vn, u64 vsize, u64 en, u64 esize,
403 void *data, auto &&vgen, auto &&egen
404) {
405 const bool ok = this->write_to_buffer(vbo, voff, vn, vsize, data, vgen)
406 && this->write_to_buffer(ebo, eoff, en, esize, data, egen);
407 if(!ok)
408 return false;
409 this->set_buffer_size(vbo, voff + vn * vsize);
410 this->set_buffer_size(ebo, eoff + en * esize);
411 return true;
412}
413
414template<Graphics::Backend>
415std::unique_ptr<Graphics> graphics_create_backend(const void *params);
416
417}
418
419#endif
static constexpr T mip_levels(T extent)
Definition math.h:236
precision highp int
Definition common.h:14
#define NNGN_TEXTURE_EXTENT
Definition const.h:4
#define NNGN_SHADOWS_ENABLED_BIT
Definition const.h:7
#define NNGN_TEXTURE_SIZE
Definition const.h:5
for i
Definition font.lua:5
local n
Definition dump_lights.lua:5
local c
Definition gamma.lua:11
local data
Definition house0.lua:10
pos
Definition lua_audio.cpp:17
local l
Definition light.lua:23
Definition audio.cpp:7
vec2_base< double > dvec2
Definition vec2.h:30
vec2_base< std::uint32_t > uvec2
Definition vec2.h:27
std::unique_ptr< Graphics > graphics_create_backend(const void *params)
std::uint32_t u32
Definition def.h:14
std::uint8_t u8
Definition def.h:12
std::uint64_t u64
Definition def.h:15
local a
Definition input.lua:35
local m
Definition input.lua:28
v[1]
Definition math.lua:22
local function f()) end
#define NNGN_VIRTUAL(x)
Definition utils.h:27
local w
Definition strict.lua:12
Definition graphics.h:113
mat4 proj
Definition graphics.h:113
mat4 view
Definition graphics.h:113
Wrapper for a small unsigned integral representing flags.
Definition flags.h:18
Definition stats.h:10
Definition graphics.h:233
const char * name
Definition graphics.h:235
Type type
Definition graphics.h:236
Type
Definition graphics.h:234
u64 size
Definition graphics.h:237
Definition graphics.h:250
const mat4 * view
Definition graphics.h:253
const mat4 * screen_proj
Definition graphics.h:253
const mat4 * proj
Definition graphics.h:253
uint8_t * flags
Definition graphics.h:251
const uvec2 * screen
Definition graphics.h:252
Definition graphics.h:185
Type
Definition graphics.h:186
u32 driver_version
Definition graphics.h:191
std::array< char, 256 > name
Definition graphics.h:189
Type type
Definition graphics.h:192
u32 vendor_id
Definition graphics.h:191
std::array< char, 15 > version
Definition graphics.h:190
u32 device_id
Definition graphics.h:191
Definition graphics.h:174
u32 version
Definition graphics.h:177
std::array< char, name_max_len > name
Definition graphics.h:176
static constexpr std::size_t name_max_len
Definition graphics.h:175
Definition graphics.h:179
u32 version
Definition graphics.h:183
std::array< char, 256 > name
Definition graphics.h:180
std::array< char, 256 > description
Definition graphics.h:181
std::array< char, 15 > spec_version
Definition graphics.h:182
Definition graphics.h:255
const mat4 * dir_views
Definition graphics.h:259
const LightsUBO * ubo
Definition graphics.h:256
const mat4 * dir_proj
Definition graphics.h:258
const mat4 * point_views
Definition graphics.h:259
const mat4 * point_proj
Definition graphics.h:258
Definition graphics.h:202
u64 size
Definition graphics.h:204
Flag
Definition graphics.h:203
@ DEVICE_LOCAL
Definition graphics.h:203
Flag flags
Definition graphics.h:205
Definition graphics.h:207
Flag
Definition graphics.h:208
@ HOST_VISIBLE
Definition graphics.h:209
@ LAZILY_ALLOCATED
Definition graphics.h:211
@ HOST_CACHED
Definition graphics.h:210
@ DEVICE_LOCAL
Definition graphics.h:209
@ HOST_COHERENT
Definition graphics.h:210
Flag flags
Definition graphics.h:213
Definition graphics.h:169
int maj
Definition graphics.h:169
int min
Definition graphics.h:169
Definition graphics.h:149
Flags< Flag > flags
Definition graphics.h:153
Flag
Definition graphics.h:150
@ HIDDEN
Definition graphics.h:151
@ DEBUG
Definition graphics.h:151
Definition graphics.h:219
Type
Definition graphics.h:226
Flag
Definition graphics.h:220
@ CULL_BACK_FACES
Definition graphics.h:223
@ LINE
Definition graphics.h:224
@ DEPTH_WRITE
Definition graphics.h:222
@ DEPTH_TEST
Definition graphics.h:221
Type type
Definition graphics.h:230
const char * name
Definition graphics.h:229
Flag flags
Definition graphics.h:231
Definition graphics.h:194
Flag flags
Definition graphics.h:200
Flag
Definition graphics.h:195
@ GRAPHICS
Definition graphics.h:196
@ COMPUTE
Definition graphics.h:196
@ TRANSFER
Definition graphics.h:196
@ PRESENT
Definition graphics.h:197
u32 count
Definition graphics.h:199
Definition graphics.h:240
std::span< const std::pair< u32, u32 > > buffers
Definition graphics.h:242
u32 pipeline
Definition graphics.h:241
Definition graphics.h:239
std::span< const Stage > map_persp
Definition graphics.h:245
std::span< const Stage > normal
Definition graphics.h:246
std::span< const Stage > overlay
Definition graphics.h:246
std::span< const Stage > shadow_maps
Definition graphics.h:247
std::span< const Stage > map_ortho
Definition graphics.h:245
std::span< const Stage > shadow_cubes
Definition graphics.h:247
std::span< const Stage > no_light
Definition graphics.h:246
std::span< const Stage > depth
Definition graphics.h:245
std::span< const Stage > screen
Definition graphics.h:246
Definition graphics.h:215
u32 max_images
Definition graphics.h:216
uvec2 min_extent
Definition graphics.h:217
uvec2 cur_extent
Definition graphics.h:217
u32 min_images
Definition graphics.h:216
uvec2 max_extent
Definition graphics.h:217
Definition graphics.h:164
TerminalMode mode
Definition graphics.h:167
TerminalFlag flags
Definition graphics.h:166
Definition graphics.h:155
u32 minor
Definition graphics.h:155
const char * name
Definition graphics.h:155
u32 major
Definition graphics.h:155
u32 patch
Definition graphics.h:155
Definition graphics.h:170
Version version
Definition graphics.h:171
LogLevel log_level
Definition graphics.h:172
Definition graphics.h:138
virtual void set_bloom_downscale(std::size_t d)=0
virtual bool load_font(unsigned char c, std::uint32_t n, const nngn::uvec2 *size, const std::byte *v)=0
virtual void set_exposure(float e)=0
Backend
Definition graphics.h:143
virtual std::size_t n_memory_types(std::size_t i, std::size_t ih) const =0
static constexpr u32 TEXTURE_SIZE
Definition graphics.h:264
virtual int swap_interval() const =0
virtual std::size_t n_present_modes() const =0
void(*)(void *, int, int, int, int) key_callback_f
Definition graphics.h:140
virtual void set_blur_passes(std::size_t n)=0
virtual bool init_instance()=0
virtual void set_blur_size(float n)=0
virtual void set_bloom_blur_size(float n)=0
static constexpr u32 TEXTURE_EXTENT_LOG2
Definition graphics.h:263
virtual bool init_device()=0
virtual void set_cursor_mode(CursorMode m)=0
virtual GraphicsStats stats()=0
virtual void set_bloom_threshold(float t)=0
virtual bool resize_textures(std::uint32_t s)=0
static constexpr u32 SHADOW_MAP_INITIAL_SIZE
Definition graphics.h:266
void(*)(void *, dvec2) mouse_move_callback_f
Definition graphics.h:142
virtual u32 create_buffer(const BufferConfiguration &conf)=0
static constexpr std::size_t DEFAULT_BLOOM_DOWNSCALE
Definition graphics.h:273
TerminalMode
Definition graphics.h:163
virtual bool init_device(std::size_t i)=0
CursorMode
Definition graphics.h:249
static std::string flags_str(QueueFamily::Flag f)
Definition graphics.cpp:50
virtual void set_mouse_move_callback(void *data, mouse_move_callback_f f)=0
virtual void set_mouse_button_callback(void *data, mouse_button_callback_f f)=0
virtual bool init()
Fully initialize the back end.
Definition graphics.h:387
static std::unique_ptr< Graphics > create(Backend b, const void *params)
Definition graphics.cpp:7
virtual void set_bloom_blur_passes(std::size_t n)=0
virtual u32 create_pipeline(const PipelineConfiguration &conf)=0
virtual std::size_t n_device_extensions(std::size_t i) const =0
static constexpr float DEFAULT_BLOOM_THRESHOLD
Definition graphics.h:270
virtual ivec2 mouse_pos(void) const =0
LogLevel
Definition graphics.h:147
void(*)(void *, uvec2) size_callback_f
Definition graphics.h:139
static constexpr float DEFAULT_BLOOM_BLUR_SIZE
Definition graphics.h:271
virtual uvec2 window_size() const =0
virtual bool resize_font(std::uint32_t s)=0
virtual void present_modes(PresentMode *p) const =0
virtual bool set_render_list(const RenderList &l)=0
virtual void set_blur_downscale(std::size_t d)=0
virtual void set_automatic_exposure(bool b)=0
virtual void set_key_callback(void *data, key_callback_f f)=0
void(*)(void *, int, int, int) mouse_button_callback_f
Definition graphics.h:141
virtual void set_camera(const Camera &c)=0
virtual void device_infos(DeviceInfo *p) const =0
virtual void set_lighting(const Lighting &l)=0
virtual void set_window_title(const char *t)=0
virtual void heaps(std::size_t i, MemoryHeap *p) const =0
virtual bool set_buffer_size(u32 b, std::uint64_t size)=0
static constexpr float DEFAULT_EXPOSURE
Definition graphics.h:269
virtual void memory_types(std::size_t i, std::size_t ih, MemoryType *p) const =0
virtual void set_HDR_mix(float m)=0
virtual bool load_textures(std::uint32_t i, std::uint32_t n, const std::byte *v)=0
virtual void queue_families(std::size_t i, QueueFamily *p) const =0
virtual void poll_events() const =0
virtual void layers(Layer *p) const =0
virtual bool window_closed() const =0
virtual void set_swap_interval(int i)=0
virtual Version version() const =0
virtual void device_extensions(std::size_t i, Extension *p) const =0
static constexpr std::size_t DEFAULT_BLOOM_BLUR_PASSES
Definition graphics.h:275
virtual bool set_shadow_cube_size(uint32_t s)=0
static constexpr u32 TEXTURE_EXTENT
Definition graphics.h:262
virtual bool render()=0
virtual bool vsync()=0
TerminalFlag
Definition graphics.h:156
static const char * enum_str(DeviceInfo::Type t)
Definition graphics.cpp:25
static constexpr u32 SHADOW_CUBE_INITIAL_SIZE
Definition graphics.h:267
bool update_buffers(u32 vbo, u32 ebo, u64 voff, u64 eoff, u64 vn, u64 vsize, u64 en, u64 esize, void *data, auto &&vgen, auto &&egen)
Definition graphics.h:400
virtual bool write_to_buffer(u32 b, u64 offset, u64 n, u64 size, void *data, void f(void *, void *, u64, u64))=0
virtual std::size_t n_extensions() const =0
virtual void extensions(Extension *p) const =0
virtual void resize(int w, int h)=0
virtual void set_camera_updated()=0
virtual std::size_t n_heaps(std::size_t i) const =0
virtual void get_keys(size_t n, int32_t *keys) const =0
virtual bool init_backend()=0
virtual SurfaceInfo surface_info() const =0
virtual void set_bloom_amount(float a)=0
virtual void set_lighting_updated()=0
virtual std::size_t selected_device() const =0
virtual std::size_t n_queue_families(std::size_t i) const =0
virtual bool error()=0
virtual std::size_t n_layers() const =0
PresentMode
Definition graphics.h:148
virtual std::size_t n_devices() const =0
virtual bool set_n_swap_chain_images(std::size_t n)=0
virtual bool set_shadow_map_size(uint32_t s)=0
virtual bool set_n_frames(std::size_t n)=0
static constexpr std::size_t DEFAULT_BLUR_DOWNSCALE
Definition graphics.h:274
virtual bool set_buffer_capacity(u32 b, u64 size)=0
virtual void set_size_callback(void *data, size_callback_f f)=0
static constexpr u32 TEXTURE_MIP_LEVELS
Definition graphics.h:265
Definition graphics.h:114
std::array< vec4, NNGN_MAX_LIGHTS > dir
Definition graphics.h:125
struct nngn::LightsUBO::@8 point
vec3 view_pos
Definition graphics.h:120
u32 n_dir
Definition graphics.h:121
u32 flags
Definition graphics.h:118
Flag
Definition graphics.h:115
@ SHADOWS_ENABLED
Definition graphics.h:116
vec3 ambient
Definition graphics.h:122
std::array< vec4, NNGN_MAX_LIGHTS > att_cutoff
Definition graphics.h:133
u32 n_point
Definition graphics.h:123
std::array< vec4, NNGN_MAX_LIGHTS > color_spec
Definition graphics.h:126
float depth_transform1
Definition graphics.h:119
float depth_transform0
Definition graphics.h:119
Definition graphics.h:136
vec3 color
Definition graphics.h:136
vec3 norm
Definition graphics.h:136
vec3 pos
Definition graphics.h:136
Definition mat.h:11
local e
Definition math.lua:4
std::chrono::seconds s
Definition timing.cpp:6
std::chrono::duration< float, typename T::period > fd
Definition timing.cpp:8