gdk-graphics 0a0aef92e917cfbac53dffbc32c295e4de914093
Loading...
Searching...
No Matches
null_context.h
1// © Joseph Cameron - MIT License
2
3#ifndef GDK_GRAPHICS_NULL_CONTEXT_H
4#define GDK_GRAPHICS_NULL_CONTEXT_H
5
6#include <gdk/graphics/context.h>
7#include <gdk/graphics/profile.h>
9
10#include <array>
11#include <functional>
12#include <map>
13#include <memory>
14#include <optional>
15#include <set>
16#include <string>
17#include <string_view>
18#include <utility>
19#include <vector>
20
21namespace gdk::graphics {
22 class null_context final : public graphics::context {
23 public:
24 using shader_check_type = std::function<void(const graphics::profile &aProfile,
25 std::string_view aVertexSource, std::string_view aFragmentSource)>;
26
27 [[nodiscard]] static std::shared_ptr<null_context> make(std::optional<graphics::profile> aProfile = {},
28 shader_check_type aShaderCheck = {});
29
32
33 [[nodiscard]] virtual scene_ptr_type make_scene() override;
34 [[nodiscard]] virtual screen_camera_ptr_type make_camera() override;
35 [[nodiscard]] virtual texture_camera_ptr_type make_texture_camera() override;
36 [[nodiscard]] virtual texture_camera_ptr_type make_texture_camera(const intvector2_type &aSize) override;
37
38 [[nodiscard]] virtual entity_ptr_type make_entity(const const_model_ptr_type pModel,
39 const const_material_ptr_type pMaterial) override;
40
41 [[nodiscard]] virtual model_ptr_type make_model() override;
42
43 [[nodiscard]] virtual model_ptr_type make_model(const gdk::graphics::model::usage_hint,
44 const model_data &aModelData) override;
45
46 [[nodiscard]] virtual material_ptr_type make_material(const const_shader_ptr_type pShader,
47 const material::render_mode aRenderMode,
48 const material::face_culling_mode aFaceCullingMode) override;
49
50 [[nodiscard]] virtual material_ptr_type make_material(
51 const const_material_ptr_type &aPrototype) override;
52
53 [[nodiscard]] virtual texture_ptr_type make_texture(
54 const texture_data::view &aTextureDataView,
55 const texture::wrap_mode aWrapModeU,
56 const texture::wrap_mode aWrapModeV,
57 const texture::filter_mode aFilterMode) override;
58
59 [[nodiscard]] virtual texture_ptr_type make_texture() override;
60
61 [[nodiscard]] std::size_t texture_count() const;
62
63 [[nodiscard]] virtual model_ptr_type make_cube_model() const override;
64 [[nodiscard]] virtual model_ptr_type make_sphere_model() const override;
65
66 [[nodiscard]] virtual shader_ptr_type make_alpha_cutoff_shader() const override;
67
68 [[nodiscard]] virtual shader_ptr_type make_alpha_blend_shader() const override;
69
70 [[nodiscard]] virtual graphics::limits limits() const override;
71
72 [[nodiscard]] shader_ptr_type make_shader(const std::string_view aVertexSource,
73 const std::string_view aFragmentSource);
74
75 [[nodiscard]] std::size_t resources_made() const;
76
77 private:
78 null_context() = default;
79
80 std::shared_ptr<const graphics::profile> mProfile;
81
82 shader_check_type mShaderCheck;
83
84 std::size_t mTextureCount{0};
85
86 mutable std::size_t mResourcesMade{0};
87
88 mutable shader_ptr_type mpAlphaCutoff;
89 mutable shader_ptr_type mpAlphaBlend;
90 };
91
92 class null_shader final : public shader_program {
93 public:
94 null_shader(std::string aVertexSource = {}, std::string aFragmentSource = {});
95
96 [[nodiscard]] const std::string &vertex_source() const;
97 [[nodiscard]] const std::string &fragment_source() const;
98
99 private:
100 std::string mVertexSource;
101 std::string mFragmentSource;
102 };
103
104 class null_texture final : public texture {
105 public:
106 null_texture() = default;
107 null_texture(const texture_data::view &aView, const wrap_mode aU, const wrap_mode aV);
108 null_texture(std::size_t aWidth, std::size_t aHeight);
109
110 void hold_to(std::shared_ptr<const profile> pProfile);
111
112 virtual void update_data(const texture_data::view &aView) override;
113
114 virtual void update_data(const texture_data::view &aView, const std::size_t aOffsetX,
115 const std::size_t aOffsetY) override;
116
117 [[nodiscard]] std::size_t width() const;
118 [[nodiscard]] std::size_t height() const;
119
120 [[nodiscard]] std::size_t upload_count() const;
121
122 [[nodiscard]] const texture_data::channel_data &data() const;
123
124 private:
125 std::shared_ptr<const profile> mProfile;
126 std::size_t mWidth{0};
127 std::size_t mHeight{0};
128 std::size_t mUploadCount{0};
129 std::size_t mChannels{0};
130 std::optional<format> mFormat;
131 texture_data::channel_data mData;
134 };
135
136 class null_model final : public model {
137 public:
138 null_model() = default;
139 null_model(const usage_hint aUsage, const model_data &aModelData);
140
141 void hold_to(std::shared_ptr<const profile> pProfile);
142
143 virtual void upload(const usage_hint &aUsage, const model_data &aModelData) override;
144
145 [[nodiscard]] std::size_t vertex_count() const;
146
147 [[nodiscard]] std::size_t index_count() const;
148
149 [[nodiscard]] std::size_t upload_count() const;
150
151 [[nodiscard]] const model_data &last_upload() const;
152
153 private:
154 std::shared_ptr<const profile> mProfile;
155 std::size_t mVertexCount{0};
156 std::size_t mIndexCount{0};
157 std::size_t mUploadCount{0};
158 model_data mLastUpload{};
159 };
160
161 class null_material final : public material {
162 public:
163 explicit null_material(const_shader_ptr_type pShader = nullptr,
164 const render_mode aRenderMode = render_mode::opaque,
165 const face_culling_mode aFaceCullingMode = face_culling_mode::none);
166
167 virtual void set_texture(const std::string_view aName, texture_ptr_type aValue) override;
168 virtual void set_float(const std::string_view aName, float aValue) override;
169 virtual void set_vector2(const std::string_view aName, vector2_type aValue) override;
170 virtual void set_vector3(const std::string_view aName, vector3_type aValue) override;
171 virtual void set_vector4(const std::string_view aName, vector4_type aValue) override;
172 virtual void set_vector4(const std::string_view aName, const color &aValue) override;
173 virtual void set_integer(const std::string_view aName, int aValue) override;
174 virtual void set_integer2(const std::string_view aName, int a1, int a2) override;
175 virtual void set_integer3(const std::string_view aName, int a1, int a2, int a3) override;
176 virtual void set_integer4(const std::string_view aName, int a1, int a2, int a3, int a4) override;
177
178 virtual void set_int_vector2_array(const std::string_view aName,
179 const std::vector<intvector2_type> &aValue) override;
180
181 [[nodiscard]] std::optional<float> float_at(std::string_view aName) const;
182 [[nodiscard]] std::optional<vector4_type> vector4_at(std::string_view aName) const;
183 [[nodiscard]] texture_ptr_type texture_at(std::string_view aName) const;
184
185 [[nodiscard]] std::vector<std::string> uniforms() const;
186
187 [[nodiscard]] render_mode mode() const;
188
189 private:
190 const_shader_ptr_type mpShader;
191 render_mode mRenderMode;
192 [[maybe_unused]] face_culling_mode mFaceCullingMode;
193
194 std::map<std::string, float, std::less<>> mFloats;
195 std::map<std::string, vector4_type, std::less<>> mVector4s;
196 std::map<std::string, texture_ptr_type, std::less<>> mTextures;
197 std::set<std::string> mNames;
198 };
199
200 class null_entity final : public entity {
201 public:
203
204 null_entity(const_model_ptr_type pModel, const_material_ptr_type pMaterial);
205
206 virtual void hide() override;
207 virtual void show() override;
208 [[nodiscard]] virtual bool is_hidden() const override;
209
210 virtual void set_transform(const vector3_type &aWorldPos,
211 const quaternion_type &aRotation, const vector3_type &aScale) override;
212
213 virtual void set_transform(const matrix4x4_type &aTransform) override;
214
215 virtual void set_bounds(const vector3_type &aCentre, floating_point_type aRadius) override;
216
217 [[nodiscard]] const matrix4x4_type &transform() const;
218
219 [[nodiscard]] const std::optional<std::pair<vector3_type, floating_point_type>> &bounds() const;
220 [[nodiscard]] const_model_ptr_type model() const;
221 [[nodiscard]] const_material_ptr_type material() const;
222
223 private:
224 const_model_ptr_type mpModel;
225 const_material_ptr_type mpMaterial;
226
227 matrix4x4_type mTransform;
228 bool mHidden{false};
229 std::optional<std::pair<vector3_type, floating_point_type>> mBounds;
230 };
231
232 class null_camera final : public screen_camera {
233 public:
234 virtual void set_perspective_projection(const floating_point_type aFieldOfView,
235 const floating_point_type aNear, const floating_point_type aFar,
236 const floating_point_type aAspectRatio) override;
237
238 virtual void set_perspective_projection(const floating_point_type aFieldOfView,
239 const floating_point_type aNear, const floating_point_type aFar,
240 const floating_point_type aAspectRatio, const vector2_type &aShift) override;
241
242 virtual void set_orthographic_projection(const vector2_type &aOrthographicSize,
243 const floating_point_type aNear, const floating_point_type aFar,
244 const floating_point_type aAspectRatio) override;
245
246 virtual void set_transform(const matrix4x4_type &aTransform) override;
247
248 virtual void set_transform(const vector3_type &aWorldPos,
249 const quaternion_type &aRotation) override;
250
251 virtual void set_clear_color(const color &aColor) override;
252 virtual void set_clear_mode(const clear_mode aClearMode) override;
253
254 virtual void set_viewport(const float aX, const float aY,
255 const float aWidth, const float aHeight) override;
256
257 virtual void set_scissor(const float aX, const float aY,
258 const float aWidth, const float aHeight) override;
259
260 virtual void clear_scissor() override;
261
262 [[nodiscard]] std::array<float, 4> scissor() const;
263
264 [[nodiscard]] const vector3_type &position() const;
265 [[nodiscard]] const quaternion_type &rotation() const;
266 [[nodiscard]] const matrix4x4_type &transform() const;
267 [[nodiscard]] const color &clear_color() const;
268 [[nodiscard]] clear_mode mode() const;
269
270 [[nodiscard]] floating_point_type field_of_view() const;
271 [[nodiscard]] floating_point_type aspect_ratio() const;
272 [[nodiscard]] floating_point_type near_clip() const;
273 [[nodiscard]] floating_point_type far_clip() const;
274
275 private:
276 vector3_type mPosition;
277 quaternion_type mRotation;
278 matrix4x4_type mTransform;
279 color mClearColor{color::black};
281
282 floating_point_type mFieldOfView{0};
283 floating_point_type mAspectRatio{0};
284 floating_point_type mNear{0};
285 floating_point_type mFar{0};
286
287 std::array<float, 4> mViewport{0, 0, 1, 1};
288
289 std::optional<std::array<float, 4>> mScissor;
290 };
291
292 class null_texture_camera final : public texture_camera {
293 public:
294 explicit null_texture_camera(const intvector2_type &aSize = {1024, 1024});
295
296 virtual void set_perspective_projection(const floating_point_type aFieldOfView,
297 const floating_point_type aNear, const floating_point_type aFar,
298 const floating_point_type aAspectRatio) override;
299
300 virtual void set_perspective_projection(const floating_point_type aFieldOfView,
301 const floating_point_type aNear, const floating_point_type aFar,
302 const floating_point_type aAspectRatio, const vector2_type &aShift) override;
303
304 virtual void set_orthographic_projection(const vector2_type &aOrthographicSize,
305 const floating_point_type aNear, const floating_point_type aFar,
306 const floating_point_type aAspectRatio) override;
307
308 virtual void set_transform(const matrix4x4_type &aTransform) override;
309
310 virtual void set_transform(const vector3_type &aWorldPos,
311 const quaternion_type &aRotation) override;
312
313 virtual void set_clear_color(const color &aColor) override;
314 virtual void set_clear_mode(const clear_mode aClearMode) override;
315
316 [[nodiscard]] virtual texture_ptr_type color_texture() const override;
317
318 [[nodiscard]] virtual texture_ptr_type depth_texture() const override;
319
320 private:
321 texture_ptr_type mpColor;
322 texture_ptr_type mpDepth;
323 };
324
325 class null_scene final : public scene {
326 public:
327 virtual void add(const std::shared_ptr<const screen_camera> &pCamera) override;
328 virtual void remove(const std::shared_ptr<const screen_camera> &pCamera) override;
329
330 virtual void add(const std::shared_ptr<const texture_camera> &pCamera) override;
331 virtual void remove(const std::shared_ptr<const texture_camera> &pCamera) override;
332
333 virtual void add(const std::shared_ptr<const entity> &pEntity) override;
334 virtual void remove(const std::shared_ptr<const entity> &pEntity) override;
335
336 virtual void draw(const gdk::graphics::intvector2_type &aFrameBufferSize) const override;
337
338 virtual void set_float(const std::string_view aName, float aValue) override;
339 virtual void set_vector2(const std::string_view aName, vector2_type aValue) override;
340 virtual void set_vector3(const std::string_view aName, vector3_type aValue) override;
341 virtual void set_vector4(const std::string_view aName, vector4_type aValue) override;
342 virtual void set_vector4(const std::string_view aName, const color &aValue) override;
343 virtual void set_integer(const std::string_view aName, int aValue) override;
344
345 [[nodiscard]] std::optional<float> float_at(std::string_view aName) const;
346 [[nodiscard]] std::optional<vector3_type> vector3_at(std::string_view aName) const;
347 [[nodiscard]] std::optional<vector4_type> vector4_at(std::string_view aName) const;
348
349 [[nodiscard]] std::size_t entity_count() const;
350
351 [[nodiscard]] std::vector<std::shared_ptr<const entity>> entities() const;
352
353 [[nodiscard]] std::size_t camera_count() const;
354 [[nodiscard]] bool contains(const std::shared_ptr<const entity> &pEntity) const;
355
356 [[nodiscard]] std::size_t draw_count() const;
357
358 [[nodiscard]] intvector2_type last_frame_buffer_size() const;
359
360 private:
361 mutable std::vector<std::weak_ptr<const entity>> mEntities;
362 mutable std::vector<std::weak_ptr<const screen_camera>> mScreenCameras;
363 mutable std::vector<std::weak_ptr<const texture_camera>> mTextureCameras;
364
365 std::map<std::string, float, std::less<>> mFloats;
366 std::map<std::string, vector2_type, std::less<>> mVector2s;
367 std::map<std::string, vector3_type, std::less<>> mVector3s;
368 std::map<std::string, vector4_type, std::less<>> mVector4s;
369 std::map<std::string, int, std::less<>> mIntegers;
370
371 mutable std::size_t mDrawCount{0};
372 mutable intvector2_type mLastFrameBufferSize{0, 0};
373 };
374}
375
376#endif
clear_mode
controls which buffers in the active frame buffer object to clear
Definition camera.h:15
@ color_and_depth
Clear the color and depth buffers.
Definition camera.h:17
entry point for gdk-graphics
Definition context.h:23
virtual material_ptr_type make_material(const const_shader_ptr_type pShader, const material::render_mode aRenderMode, const material::face_culling_mode aFaceCullingMode)=0
make a material.
virtual texture_ptr_type make_texture(const texture_data::view &aTextureDataView, const texture::wrap_mode aWrapModeU, const texture::wrap_mode aWrapModeV, const texture::filter_mode aFilterMode)=0
make a texture
represents an observable 3d object.
Definition entity.h:13
virtual void set_transform(const vector3_type &aWorldPos, const quaternion_type &aRotation, const vector3_type &aScale)=0
sets the model matrix using a vec3 position, quat rotation, vec3 scale
decides how models using the material should be drawn.
Definition material.h:21
std::shared_ptr< gdk::graphics::texture > texture_ptr_type
textures can be shared among many materials.
Definition material.h:39
render_mode
render mode decides transparency etc
Definition material.h:32
@ opaque
no transparency
Definition material.h:33
face_culling_mode
specify whether front- or back-facing polygons can be culled
Definition material.h:24
@ none
do not cull any polygons
Definition material.h:28
vertex data stored in system memory.
Definition model_data.h:25
usage_hint
Definition model.h:12
Definition null_context.h:232
virtual void set_clear_mode(const clear_mode aClearMode) override
clear mode decides which buffers to clear. see enum
virtual void set_clear_color(const color &aColor) override
sets the clear color, used to fill color buffer after it is cleared.
virtual void set_transform(const matrix4x4_type &aTransform) override
sets the camera's world and view matrix
virtual void set_viewport(const float aX, const float aY, const float aWidth, const float aHeight) override
sets the normalized size and position of the viewport within the window
virtual void set_orthographic_projection(const vector2_type &aOrthographicSize, const floating_point_type aNear, const floating_point_type aFar, const floating_point_type aAspectRatio) override
use an orthographic projection with the given view volume aOrthographicSize is the full extent of the...
virtual void set_perspective_projection(const floating_point_type aFieldOfView, const floating_point_type aNear, const floating_point_type aFar, const floating_point_type aAspectRatio) override
use a perspective projection with the given view volume
virtual void set_scissor(const float aX, const float aY, const float aWidth, const float aHeight) override
clip this camera to a rectangle of the window without moving what it draws
virtual void set_perspective_projection(const floating_point_type aFieldOfView, const floating_point_type aNear, const floating_point_type aFar, const floating_point_type aAspectRatio, const vector2_type &aShift) override
use a perspective projection whose window is off the view's axis: an asymmetric frustum,...
virtual graphics::limits limits() const override
the limits of the device, narrowed to the context's profile when it was given one
virtual entity_ptr_type make_entity(const const_model_ptr_type pModel, const const_material_ptr_type pMaterial) override
make an entity
virtual model_ptr_type make_model(const gdk::graphics::model::usage_hint, const model_data &aModelData) override
make a model
virtual material_ptr_type make_material(const const_shader_ptr_type pShader, const material::render_mode aRenderMode, const material::face_culling_mode aFaceCullingMode) override
make a material.
virtual material_ptr_type make_material(const const_material_ptr_type &aPrototype) override
make a material that starts as a copy of another
virtual shader_ptr_type make_alpha_blend_shader() const override
a shader that keeps the texel's alpha, for the blender to use.
virtual texture_ptr_type make_texture() override
make an empty texture
virtual scene_ptr_type make_scene() override
makes a scene
virtual shader_ptr_type make_alpha_cutoff_shader() const override
Basic unlit forward renderer program: Attributes:
virtual model_ptr_type make_cube_model() const override
a 1x1x1 cube model
virtual texture_camera_ptr_type make_texture_camera() override
makes a texture_camera drawing into textures 1024 texels square
virtual screen_camera_ptr_type make_camera() override
makes a camera that draws to the screen
virtual texture_ptr_type make_texture(const texture_data::view &aTextureDataView, const texture::wrap_mode aWrapModeU, const texture::wrap_mode aWrapModeV, const texture::filter_mode aFilterMode) override
make a texture
virtual model_ptr_type make_model() override
make an empty model
virtual texture_camera_ptr_type make_texture_camera(const intvector2_type &aSize) override
makes a texture_camera drawing into textures aSize texels across
virtual bool is_hidden() const override
check if the entity is hidden
virtual void set_transform(const vector3_type &aWorldPos, const quaternion_type &aRotation, const vector3_type &aScale) override
sets the model matrix using a vec3 position, quat rotation, vec3 scale
virtual void set_transform(const matrix4x4_type &aTransform) override
set the model matrix using a matrix
virtual void set_bounds(const vector3_type &aCentre, floating_point_type aRadius) override
the bounding sphere of the model, used for culling
virtual void show() override
allow this entity to be drawn
virtual void hide() override
do not allow this entity to be drawn
virtual void set_integer(const std::string_view aName, int aValue) override
assigns an integer to the material.
virtual void set_int_vector2_array(const std::string_view aName, const std::vector< intvector2_type > &aValue) override
assigns an array of integer vector2s to the material
virtual void set_vector4(const std::string_view aName, const color &aValue) override
assign an RGBA colour to a vec4 uniform
virtual void set_vector2(const std::string_view aName, vector2_type aValue) override
assigns a 2 component vector
virtual void set_integer3(const std::string_view aName, int a1, int a2, int a3) override
assigns a 3 integer array to the material.
virtual void set_float(const std::string_view aName, float aValue) override
assigns a float to the material.
virtual void set_vector4(const std::string_view aName, vector4_type aValue) override
assigns a 4 component vector
virtual void set_vector3(const std::string_view aName, vector3_type aValue) override
assigns a 3 component vector
virtual void set_integer2(const std::string_view aName, int a1, int a2) override
assigns a 2 integer array to the material.
virtual void set_integer4(const std::string_view aName, int a1, int a2, int a3, int a4) override
assigns a 4 integer array to the material.
virtual void upload(const usage_hint &aUsage, const model_data &aModelData) override
replace vertex data held by this model
Definition null_context.h:325
virtual void draw(const gdk::graphics::intvector2_type &aFrameBufferSize) const override
draws the scene
virtual void set_clear_color(const color &aColor) override
sets the clear color, used to fill color buffer after it is cleared.
virtual texture_ptr_type depth_texture() const override
the texture the camera draws depth into
virtual texture_ptr_type color_texture() const override
the texture the camera draws colour into
virtual void set_perspective_projection(const floating_point_type aFieldOfView, const floating_point_type aNear, const floating_point_type aFar, const floating_point_type aAspectRatio) override
use a perspective projection with the given view volume
virtual void set_perspective_projection(const floating_point_type aFieldOfView, const floating_point_type aNear, const floating_point_type aFar, const floating_point_type aAspectRatio, const vector2_type &aShift) override
use a perspective projection whose window is off the view's axis: an asymmetric frustum,...
virtual void set_orthographic_projection(const vector2_type &aOrthographicSize, const floating_point_type aNear, const floating_point_type aFar, const floating_point_type aAspectRatio) override
use an orthographic projection with the given view volume aOrthographicSize is the full extent of the...
virtual void set_clear_mode(const clear_mode aClearMode) override
clear mode decides which buffers to clear. see enum
virtual void set_transform(const matrix4x4_type &aTransform) override
sets the camera's world and view matrix
virtual void update_data(const texture_data::view &aView) override
replace the texture data with new data
virtual void update_data(const texture_data::view &aView, const std::size_t aOffsetX, const std::size_t aOffsetY) override
update a section of texture data
a 3d environment within which cameras draw entities
Definition scene.h:18
Position, orientation and perspective from which entity(s) are drawn.
Definition screen_camera.h:12
texture camera renders to textures instead of the screen
Definition texture_camera.h:12
texture()=default
interface type cannot be instantiated
filter_mode
how a texel is chosen when a pixel does not land exactly on one
Definition texture.h:27
wrap_mode
behavior when sampling outside of the normalized texture range (u0-1, v0-1)
Definition texture.h:20
@ repeat
sampled values repeat. e.g: {2,2}, {3,3}, 100,100} would all sample {1,1}
Definition texture.h:22
Represents a 4 channel color: {Red, Green, Blue, Alpha}.
Definition color.h:10
static const color black
opaque black color
Definition color.h:51
used to (optionally) enforce limits on graphical types used across graphics apis
Definition limits.h:12
a target's limits
Definition profile.h:18
provides a pointer a contiguous list of channel data representing 2D texture metadata that contains i...
Definition texture_data.h:27
what the core takes: decoded channel data and the metadata describing it