Golias Engine
A C++ 20 'game engine' built with SDL3 with wide platform support.
rendering_device.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "core/gstl/str.h"
4 #include "core/math/math.h"
5 #include "shader_preprocessor.h"
6 #include <SDL3_ttf/SDL_ttf.h>
7 
8 #include <glm/gtx/string_cast.hpp>
9 
10 
11 
12 using RID = uint32_t;
13 constexpr RID INVALID_RID = -1;
14 
15 struct Shader {
17 
18 };
19 
20 struct Font {
21 
22  Font() = default;
23  explicit Font(TTF_Font* ptr, int size = 24) : handle(ptr), size(size) {
24  }
25 
26  TTF_Font* get_native_handle() const {
27  return handle;
28  }
29 
30 
31  void get_text_size(const String& text, int* out_w, int* out_h) const {
32  if (!TTF_GetStringSize(handle, text.c_str(), text.length(), out_w, out_h)) {
33  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get text size: %s", SDL_GetError());
34  }
35  }
36 
37  int get_font_size_internal() const {
38  return TTF_GetFontHeight(handle);
39  }
40 
41  void destroy() {
42  if (handle) {
43  TTF_CloseFont(handle);
44  handle = nullptr;
45  }
46  }
47 
48 private:
49  TTF_Font* handle = nullptr;
50  int size = 16;
51 
52 };
53 
54 
55 struct Color {
56  float r, g, b, a;
57 
58  Color(float r = 1.0f, float g = 1.0f, float b = 1.0f, float a = 1.0f) : r(r), g(g), b(b), a(a) {
59  }
60 
61  glm::vec4 to_vec4() const {
62  return glm::vec4(r, g, b, a);
63  }
64 
65  static const Color WHITE;
66  static const Color BLACK;
67  static const Color RED;
68  static const Color GREEN;
69  static const Color BLUE;
70  static const Color YELLOW;
71  static const Color CYAN;
72  static const Color MAGENTA;
73 };
74 
75 
76 struct Rect {
77  float x, y, width, height;
78  Rect(float x = 0, float y = 0, float w = 0, float h = 0) : x(x), y(y), width(w), height(h) {
79  }
80 };
81 
82 
83 enum class DataFormat {
84  R8_UNORM,
85  R8G8_UNORM,
88  R16_SFLOAT,
91  R32_SFLOAT,
95  R32_UINT,
99  R32_SINT,
100  R32G32_SINT,
104  D32_SFLOAT
105 };
106 
108 
110 
112 
114 
116 
117 enum class BlendFactor {
118  ZERO,
119  ONE,
120  SRC_COLOR,
122  DST_COLOR,
124  SRC_ALPHA,
126  DST_ALPHA,
128 };
129 
131 
133 
134 enum class PolygonMode { FILL, LINE, POINT };
135 
137 
138 enum class ShaderStage { VERTEX = 1 << 0, FRAGMENT = 1 << 1, COMPUTE = 1 << 2 };
139 
140 enum class BufferUsage {
141  BUFFER_USAGE_VERTEX = 1 << 0,
142  BUFFER_USAGE_INDEX = 1 << 1,
143  BUFFER_USAGE_UNIFORM = 1 << 2,
144  BUFFER_USAGE_STORAGE = 1 << 3,
145  BUFFER_USAGE_TRANSFER_SRC = 1 << 4,
147 };
148 
149 bool operator&(uint32_t lhs, BufferUsage rhs);
150 
151 enum class IndexType { UINT16, UINT32 };
152 
154 public:
155  RIDAllocator() = default;
156  virtual ~RIDAllocator() = default;
157 
158  RID allocate_rid();
159 
160 protected:
162 };
163 
165  uint32_t location;
167  uint32_t offset;
168 };
169 
170 struct VertexFormat {
171  uint32_t binding = 0;
172  uint32_t stride = 0;
174 };
175 
179  uint32_t width = 1;
180  uint32_t height = 1;
181  uint32_t depth = 1;
182  uint32_t array_layers = 1;
183  uint32_t mipmaps = 1;
184  uint32_t samples = 1;
185  bool depth_stencil = false;
186 };
187 
188 struct SamplerState {
194  float max_anisotropy = 1.0f;
195  bool compare_enabled = false;
197  float lod_bias = 0.0f;
198  float min_lod = 0.0f;
199  float max_lod = 1000.0f;
200 };
201 
204  bool front_face_ccw = true;
206  float line_width = 1.0f;
207  bool depth_clamp_enable = false;
208  bool depth_bias_enable = false;
209  float depth_bias_constant = 0.0f;
210  float depth_bias_slope = 0.0f;
211 };
212 
214  bool depth_test_enable = true;
215  bool depth_write_enable = true;
217  bool stencil_test_enable = false;
222  uint32_t stencil_compare_mask = 0xFF;
223  uint32_t stencil_write_mask = 0xFF;
224  uint32_t stencil_reference = 0;
225 };
226 
227 struct BlendState {
228  bool enable = false;
235  bool write_r = true, write_g = true, write_b = true, write_a = true;
236 };
237 
246 };
247 
248 struct Viewport {
249  float x = 0, y = 0, width = 800, height = 600;
250  float min_depth = 0.0f, max_depth = 1.0f;
251 };
252 
253 struct Scissor {
254  int32_t x = 0, y = 0;
255  uint32_t width = 800, height = 600;
256 };
257 
258 struct ClearValue {
259  glm::vec4 color = glm::vec4(0, 0, 0, 1);
260  float depth = 1.0f;
261  uint32_t stencil = 0;
262 };
263 
266  uint32_t mip_level = 0;
267  uint32_t layer = 0;
268  bool clear = true;
270 };
271 
272 
273 enum class BlendMode { NONE, ALPHA, ADD, MULTIPLY };
274 
275 enum class ScaleMode {
276  NONE,
277  KEEP,
278  EXPAND
279 };
280 
286  bool generate_mipmaps = false;
287 };
288 
289 struct ShaderModule {
290  uint32_t program = 0;
292 };
293 
294 struct Buffer {
295  uint32_t handle = 0;
296  size_t size = 0;
297  uint32_t usage_flags = 0;
298  uint32_t target = 0;
299 };
300 
301 struct Texture {
303  uint32_t handle = 0;
305 };
306 
307 struct Sampler {
308  uint32_t handle = 0;
310 };
311 
312 struct Framebuffer {
313  uint32_t handle = 0;
315  uint32_t width = 0;
316  uint32_t height = 0;
317 };
318 
319 struct Pipeline {
321  uint32_t handle = 0;
322 };
327 public:
328  virtual ~RenderingDevice() = default;
329 
330  virtual bool initialize(SDL_Window* sdl_window) = 0;
331  virtual void shutdown() = 0;
332 
333  virtual RID shader_create_from_source(const String& vertex_src, const String& fragment_src) = 0;
334  virtual void shader_destroy(RID shader) = 0;
335 
336  virtual RID buffer_create(size_t size, uint32_t usage_flags, const void* data = nullptr) = 0;
337  virtual void buffer_update(RID buffer, size_t offset, size_t size, const void* data) = 0;
338  virtual void buffer_destroy(RID buffer) = 0;
339 
340  virtual RID texture_create(const TextureFormat& format, void* data = nullptr) = 0;
341  virtual void texture_update(RID texture, uint32_t mip_level, uint32_t layer, const void* data, size_t size) = 0;
342  virtual void texture_generate_mipmaps(RID texture) = 0;
343  virtual void texture_destroy(RID texture) = 0;
344  virtual void get_texture_size(RID texture, uint32_t& width, uint32_t& height) = 0;
345  virtual uint32_t texture_get_native_handle(RID texture) = 0;
346  virtual Texture get_texture(RID texture) = 0;
347 
348  virtual RID sampler_create(const SamplerState& state) = 0;
349  virtual void sampler_destroy(RID sampler) = 0;
350 
351  virtual RID framebuffer_create(const Vector<RenderPassAttachment>& attachments) = 0;
352  virtual void framebuffer_destroy(RID framebuffer) = 0;
353 
354  virtual RID pipeline_create(const PipelineState& state) = 0;
355  virtual void pipeline_destroy(RID pipeline) = 0;
356 
357  virtual void begin_frame() = 0;
358  virtual void end_frame() = 0;
359 
360  virtual void render_pass_begin(RID framebuffer, const Viewport& viewport, const Scissor& scissor) = 0;
361  virtual void render_pass_end() = 0;
362 
363  virtual void bind_pipeline(RID pipeline) = 0;
364  virtual void bind_vertex_buffers(const Vector<RID>& buffers, const Vector<size_t>& offsets = {}) = 0;
365  virtual void bind_index_buffer(RID buffer, IndexType type, size_t offset = 0) = 0;
366  virtual void bind_uniform_buffer(uint32_t binding, RID buffer, size_t offset = 0, size_t size = 0) = 0;
367  virtual void bind_texture(uint32_t binding, RID texture, RID sampler) = 0;
368 
369  virtual void push_constant(const String& name, const void* data, size_t size) = 0;
370 
371  virtual void draw(uint32_t vertex_count, uint32_t instance_count = 1, uint32_t first_vertex = 0, uint32_t first_instance = 0) = 0;
372  virtual void draw_indexed(uint32_t index_count, uint32_t instance_count = 1, uint32_t first_index = 0, int32_t vertex_offset = 0,
373  uint32_t first_instance = 0) = 0;
374 
375  virtual void set_viewport(const Viewport& viewport) = 0;
376  virtual void set_scissor(const Scissor& scissor) = 0;
377  virtual void clear_color(const glm::vec4& color) = 0;
378  virtual void clear_depth_stencil(float depth = 1.0f, uint32_t stencil = 0) = 0;
379 
380  virtual void swap_buffers() = 0;
381 
382  virtual void get_drawable_size(int& width, int& height);
383 
384 protected:
385  SDL_Window* _window = nullptr;
386 };
Definition: rendering_device.h:153
RID next_rid
Definition: rendering_device.h:161
RID allocate_rid()
Definition: rendering_device.cpp:14
RIDAllocator()=default
virtual ~RIDAllocator()=default
Abstract base class for rendering devices.
Definition: rendering_device.h:326
virtual RID shader_create_from_source(const String &vertex_src, const String &fragment_src)=0
SDL_Window * _window
Definition: rendering_device.h:385
virtual void begin_frame()=0
virtual void clear_color(const glm::vec4 &color)=0
virtual void bind_index_buffer(RID buffer, IndexType type, size_t offset=0)=0
virtual void texture_destroy(RID texture)=0
virtual RID texture_create(const TextureFormat &format, void *data=nullptr)=0
virtual void end_frame()=0
virtual ~RenderingDevice()=default
virtual void render_pass_begin(RID framebuffer, const Viewport &viewport, const Scissor &scissor)=0
virtual Texture get_texture(RID texture)=0
virtual void pipeline_destroy(RID pipeline)=0
virtual void bind_uniform_buffer(uint32_t binding, RID buffer, size_t offset=0, size_t size=0)=0
virtual void bind_pipeline(RID pipeline)=0
virtual void clear_depth_stencil(float depth=1.0f, uint32_t stencil=0)=0
virtual void draw_indexed(uint32_t index_count, uint32_t instance_count=1, uint32_t first_index=0, int32_t vertex_offset=0, uint32_t first_instance=0)=0
virtual void push_constant(const String &name, const void *data, size_t size)=0
virtual void set_scissor(const Scissor &scissor)=0
virtual void buffer_destroy(RID buffer)=0
virtual void bind_vertex_buffers(const Vector< RID > &buffers, const Vector< size_t > &offsets={})=0
virtual void get_drawable_size(int &width, int &height)
Definition: rendering_device.cpp:18
virtual void draw(uint32_t vertex_count, uint32_t instance_count=1, uint32_t first_vertex=0, uint32_t first_instance=0)=0
virtual RID buffer_create(size_t size, uint32_t usage_flags, const void *data=nullptr)=0
virtual RID sampler_create(const SamplerState &state)=0
virtual void bind_texture(uint32_t binding, RID texture, RID sampler)=0
virtual void render_pass_end()=0
virtual void shutdown()=0
virtual void texture_generate_mipmaps(RID texture)=0
virtual void framebuffer_destroy(RID framebuffer)=0
virtual void get_texture_size(RID texture, uint32_t &width, uint32_t &height)=0
virtual RID pipeline_create(const PipelineState &state)=0
virtual void shader_destroy(RID shader)=0
virtual void set_viewport(const Viewport &viewport)=0
virtual bool initialize(SDL_Window *sdl_window)=0
virtual uint32_t texture_get_native_handle(RID texture)=0
virtual void sampler_destroy(RID sampler)=0
virtual void swap_buffers()=0
virtual void buffer_update(RID buffer, size_t offset, size_t size, const void *data)=0
virtual void texture_update(RID texture, uint32_t mip_level, uint32_t layer, const void *data, size_t size)=0
virtual RID framebuffer_create(const Vector< RenderPassAttachment > &attachments)=0
Definition: str.h:8
size_t length() const
Definition: str.h:16
Definition: vector.h:10
BufferUsage
Definition: rendering_device.h:140
@ BUFFER_USAGE_TRANSFER_DST
@ BUFFER_USAGE_STORAGE
@ BUFFER_USAGE_UNIFORM
@ BUFFER_USAGE_TRANSFER_SRC
ShaderStage
Definition: rendering_device.h:138
IndexType
Definition: rendering_device.h:151
BlendMode
Definition: rendering_device.h:273
BlendFactor
Definition: rendering_device.h:117
ScaleMode
Definition: rendering_device.h:275
@ EXPAND
Keep aspect ratio - letterbox/pillarbox.
CompareOp
Definition: rendering_device.h:113
@ GREATER_OR_EQUAL
TextureType
Definition: rendering_device.h:107
@ TEXTURE_TYPE_2D_ARRAY
@ TEXTURE_TYPE_CUBEMAP_ARRAY
@ TEXTURE_TYPE_CUBEMAP
bool operator&(uint32_t lhs, BufferUsage rhs)
DataFormat
Definition: rendering_device.h:83
@ R32G32B32A32_SFLOAT
@ R16G16B16A16_SFLOAT
PrimitiveTopology
Definition: rendering_device.h:136
CullMode
Definition: rendering_device.h:132
@ FRONT_AND_BACK
PolygonMode
Definition: rendering_device.h:134
BlendOp
Definition: rendering_device.h:130
@ REVERSE_SUBTRACT
constexpr RID INVALID_RID
Definition: rendering_device.h:13
TextureFilter
Definition: rendering_device.h:109
TextureWrap
Definition: rendering_device.h:111
StencilOp
Definition: rendering_device.h:115
@ INCREMENT_AND_WRAP
@ DECREMENT_AND_WRAP
@ INCREMENT_AND_CLAMP
@ DECREMENT_AND_CLAMP
uint32_t RID
Definition: rendering_device.h:12
Definition: rendering_device.h:227
bool write_b
Definition: rendering_device.h:235
BlendFactor dst_alpha
Definition: rendering_device.h:233
BlendOp alpha_op
Definition: rendering_device.h:234
bool write_r
Definition: rendering_device.h:235
BlendOp color_op
Definition: rendering_device.h:231
bool write_a
Definition: rendering_device.h:235
BlendFactor src_alpha
Definition: rendering_device.h:232
bool enable
Definition: rendering_device.h:228
bool write_g
Definition: rendering_device.h:235
BlendFactor src_color
Definition: rendering_device.h:229
BlendFactor dst_color
Definition: rendering_device.h:230
Definition: rendering_device.h:294
size_t size
Definition: rendering_device.h:296
uint32_t handle
Definition: rendering_device.h:295
uint32_t usage_flags
Definition: rendering_device.h:297
uint32_t target
Definition: rendering_device.h:298
Definition: rendering_device.h:258
uint32_t stencil
Definition: rendering_device.h:261
float depth
Definition: rendering_device.h:260
glm::vec4 color
Definition: rendering_device.h:259
Definition: rendering_device.h:55
static const Color GREEN
Definition: rendering_device.h:68
static const Color WHITE
Definition: rendering_device.h:65
float b
Definition: rendering_device.h:56
float r
Definition: rendering_device.h:56
static const Color BLACK
Definition: rendering_device.h:66
float g
Definition: rendering_device.h:56
static const Color BLUE
Definition: rendering_device.h:69
static const Color YELLOW
Definition: rendering_device.h:70
glm::vec4 to_vec4() const
Definition: rendering_device.h:61
float a
Definition: rendering_device.h:56
Color(float r=1.0f, float g=1.0f, float b=1.0f, float a=1.0f)
Definition: rendering_device.h:58
static const Color RED
Definition: rendering_device.h:67
static const Color MAGENTA
Definition: rendering_device.h:72
static const Color CYAN
Definition: rendering_device.h:71
Definition: rendering_device.h:213
StencilOp stencil_fail_op
Definition: rendering_device.h:218
CompareOp depth_compare_op
Definition: rendering_device.h:216
CompareOp stencil_compare_op
Definition: rendering_device.h:221
bool stencil_test_enable
Definition: rendering_device.h:217
uint32_t stencil_reference
Definition: rendering_device.h:224
uint32_t stencil_compare_mask
Definition: rendering_device.h:222
StencilOp stencil_depth_fail_op
Definition: rendering_device.h:219
bool depth_write_enable
Definition: rendering_device.h:215
bool depth_test_enable
Definition: rendering_device.h:214
uint32_t stencil_write_mask
Definition: rendering_device.h:223
StencilOp stencil_pass_op
Definition: rendering_device.h:220
Definition: rendering_device.h:20
void destroy()
Definition: rendering_device.h:41
TTF_Font * get_native_handle() const
Definition: rendering_device.h:26
Font(TTF_Font *ptr, int size=24)
Definition: rendering_device.h:23
void get_text_size(const String &text, int *out_w, int *out_h) const
Definition: rendering_device.h:31
Font()=default
int get_font_size_internal() const
Definition: rendering_device.h:37
Definition: rendering_device.h:312
uint32_t handle
Definition: rendering_device.h:313
uint32_t height
Definition: rendering_device.h:316
Vector< RenderPassAttachment > attachments
Definition: rendering_device.h:314
uint32_t width
Definition: rendering_device.h:315
Definition: rendering_device.h:238
RasterizationState rasterization
Definition: rendering_device.h:242
Vector< BlendState > blend_states
Definition: rendering_device.h:244
VertexFormat vertex_format
Definition: rendering_device.h:240
RID shader
Definition: rendering_device.h:239
PrimitiveTopology topology
Definition: rendering_device.h:241
DepthStencilState depth_stencil
Definition: rendering_device.h:243
uint32_t color_attachment_count
Definition: rendering_device.h:245
Definition: rendering_device.h:319
PipelineState state
Definition: rendering_device.h:320
uint32_t handle
Definition: rendering_device.h:321
Definition: rendering_device.h:202
bool depth_bias_enable
Definition: rendering_device.h:208
bool depth_clamp_enable
Definition: rendering_device.h:207
float line_width
Definition: rendering_device.h:206
CullMode cull_mode
Definition: rendering_device.h:203
float depth_bias_constant
Definition: rendering_device.h:209
bool front_face_ccw
Definition: rendering_device.h:204
PolygonMode polygon_mode
Definition: rendering_device.h:205
float depth_bias_slope
Definition: rendering_device.h:210
Definition: rendering_device.h:76
Rect(float x=0, float y=0, float w=0, float h=0)
Definition: rendering_device.h:78
float x
Definition: rendering_device.h:77
float height
Definition: rendering_device.h:77
float y
Definition: rendering_device.h:77
float width
Definition: rendering_device.h:77
Definition: rendering_device.h:264
uint32_t layer
Definition: rendering_device.h:267
ClearValue clear_value
Definition: rendering_device.h:269
RID texture
Definition: rendering_device.h:265
uint32_t mip_level
Definition: rendering_device.h:266
bool clear
Definition: rendering_device.h:268
Definition: rendering_device.h:188
float lod_bias
Definition: rendering_device.h:197
bool compare_enabled
Definition: rendering_device.h:195
TextureWrap wrap_v
Definition: rendering_device.h:192
TextureWrap wrap_w
Definition: rendering_device.h:193
CompareOp compare_op
Definition: rendering_device.h:196
float min_lod
Definition: rendering_device.h:198
TextureWrap wrap_u
Definition: rendering_device.h:191
TextureFilter min_filter
Definition: rendering_device.h:189
TextureFilter mag_filter
Definition: rendering_device.h:190
float max_anisotropy
Definition: rendering_device.h:194
float max_lod
Definition: rendering_device.h:199
Definition: rendering_device.h:307
SamplerState state
Definition: rendering_device.h:309
uint32_t handle
Definition: rendering_device.h:308
Definition: rendering_device.h:253
uint32_t width
Definition: rendering_device.h:255
int32_t y
Definition: rendering_device.h:254
uint32_t height
Definition: rendering_device.h:255
int32_t x
Definition: rendering_device.h:254
Definition: rendering_device.h:289
HashMap< String, int32_t > uniform_locations
Definition: rendering_device.h:291
uint32_t program
Definition: rendering_device.h:290
Definition: rendering_device.h:15
RID handle
Definition: rendering_device.h:16
Definition: rendering_device.h:281
bool generate_mipmaps
Definition: rendering_device.h:286
TextureFilter min_filter
Definition: rendering_device.h:282
TextureFilter mag_filter
Definition: rendering_device.h:283
TextureWrap wrap_v
Definition: rendering_device.h:285
TextureWrap wrap_u
Definition: rendering_device.h:284
Definition: rendering_device.h:176
uint32_t depth
Definition: rendering_device.h:181
uint32_t width
Definition: rendering_device.h:179
TextureType type
Definition: rendering_device.h:177
DataFormat format
Definition: rendering_device.h:178
uint32_t array_layers
Definition: rendering_device.h:182
uint32_t height
Definition: rendering_device.h:180
bool depth_stencil
Definition: rendering_device.h:185
uint32_t samples
Definition: rendering_device.h:184
uint32_t mipmaps
Definition: rendering_device.h:183
Definition: rendering_device.h:301
RID rid
Definition: rendering_device.h:302
TextureFormat format
Definition: rendering_device.h:304
uint32_t handle
Definition: rendering_device.h:303
Definition: rendering_device.h:164
DataFormat format
Definition: rendering_device.h:166
uint32_t location
Definition: rendering_device.h:165
uint32_t offset
Definition: rendering_device.h:167
Definition: rendering_device.h:170
Vector< VertexAttribute > attributes
Definition: rendering_device.h:173
uint32_t stride
Definition: rendering_device.h:172
uint32_t binding
Definition: rendering_device.h:171
Definition: rendering_device.h:248
float x
Definition: rendering_device.h:249
float max_depth
Definition: rendering_device.h:250
float min_depth
Definition: rendering_device.h:250
float y
Definition: rendering_device.h:249
float height
Definition: rendering_device.h:249
float width
Definition: rendering_device.h:249
std::unordered_map< K, V > HashMap
Definition: type_alias.h:20