Simulant  21.09-46
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
renderer.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef RENDERER_H
20 #define RENDERER_H
21 
22 #include <set>
23 #include <vector>
24 #include <memory>
25 
26 #include "../types.h"
27 #include "../threads/shared_mutex.h"
28 #include "../macros.h"
29 #include "../texture.h"
30 
31 #include "batching/renderable.h"
32 #include "batching/render_queue.h"
33 
34 namespace smlt {
35 
36 class SubActor;
37 class Window;
38 
39 class Renderer:
41 
42 public:
43  typedef std::shared_ptr<Renderer> ptr;
44 
45  Renderer(Window* window):
46  window_(window) {}
47 
48  virtual std::shared_ptr<batcher::RenderQueueVisitor> get_render_queue_visitor(CameraPtr camera) = 0;
49 
50  Property<Window* Renderer::*> window = { this, &Renderer::window_ };
51 
52  virtual void init_context() = 0;
53  // virtual void upload_texture(Texture* texture) = 0;
54 
55  virtual GPUProgramID new_or_existing_gpu_program(const std::string& vertex_shader, const std::string& fragment_shader) {
56  _S_UNUSED(vertex_shader);
57  _S_UNUSED(fragment_shader);
58  return GPUProgramID();
59  }
60 
61  virtual GPUProgramID current_gpu_program_id() const { return GPUProgramID(); }
62  virtual GPUProgramPtr gpu_program(const GPUProgramID&) const { return GPUProgramPtr(); }
63  virtual GPUProgramID default_gpu_program_id() const { return GPUProgramID(); }
64 
65  virtual std::string name() const = 0;
66 
67  /* This function is called just before drawing the renderable, it can be
68  * used to upload any data to VRAM if necessary */
69  virtual void prepare_to_render(const Renderable* renderable) = 0;
70 
71 
74  bool natively_supports_texture_format(TextureFormat fmt) {
75  return texture_format_is_native(fmt);
76  }
77 
80  bool supports_texture_format(TextureFormat fmt) {
82  return true;
83  }
84 
85  return texture_format_is_usable(fmt);
86  }
87 
88 public:
92  virtual bool texture_format_is_native(TextureFormat fmt);
93 
96  virtual bool texture_format_is_usable(TextureFormat fmt);
97 
98  // Render support flags
99  virtual bool supports_gpu_programs() const { return false; }
100 
101  /*
102  * Returns true if the texture has been allocated, false otherwise.
103  */
104  bool is_texture_registered(TextureID texture_id) const;
105  void pre_render();
106  void prepare_texture(Texture *texture);
107  void prepare_material(Material* material);
108 
109 private:
110  friend class Texture;
111 
112  void register_texture(TextureID tex_id, Texture *texture);
113  void unregister_texture(TextureID texture_id, Texture* texture);
114 
115  bool convert_if_necessary(Texture* tex);
116 
117  Window* window_ = nullptr;
118 
119  /*
120  * Called when a texture is created. This should do whatever is necessary to
121  * prepare a texture for later upload
122  *
123  * It's likely that data will need to also be stored here for prepare_texture
124  * to function
125  *
126  * This will be called when a render group (which uses textures) is created, which
127  * means it can be called from any thread and should be thread-safe.
128  */
129  virtual void on_texture_register(TextureID tex_id, Texture* texture) {
130  _S_UNUSED(tex_id);
131  _S_UNUSED(texture);
132  }
133 
134  /*
135  * Called when a texture is destroyed, should perform any clean_up from
136  * register_texture.
137  *
138  * This will be called when all render groups sharing the texture are destroyed
139  * and so can be called from any thread and should be thread-safe
140  */
141  virtual void on_texture_unregister(TextureID tex_id, Texture* texture) {
142  _S_UNUSED(tex_id);
143  _S_UNUSED(texture);
144  }
145 
146  /*
147  * Given a Texture, this should take care of:
148  * - Uploading the texture if the data is dirty
149  * - Updating texture filters and wrap modes if necessary
150  * - Generating or deleting mipmaps if the mipmap generation changed
151  *
152  * Guaranteed to be called from the main (render) thread, although must obviously
153  * be aware of register/unregister
154  */
155  virtual void on_texture_prepare(Texture* texture) {
156  _S_UNUSED(texture);
157  }
158 
159  /* Called when a Material is created, or when it is changed via
160  * an assignment. FIXME: This seems like we'd miss occasions where
161  * we should call this. Might be better being called on first update? */
162  virtual void on_material_prepare(Material* material) {
163  _S_UNUSED(material);
164  }
165 
166  mutable thread::Mutex texture_registry_mutex_;
167  std::unordered_map<TextureID, Texture*> texture_registry_;
168 };
169 
170 }
171 
172 #endif // RENDERER_H
smlt::Property
Definition: property.h:202
smlt::Renderer::texture_format_is_native
virtual bool texture_format_is_native(TextureFormat fmt)
Definition: renderer.cpp:35
smlt
Definition: animation.cpp:25
smlt::Window
Definition: window.h:90
smlt::UniqueID< GPUProgramPtr >
smlt::Renderer::texture_format_is_usable
virtual bool texture_format_is_usable(TextureFormat fmt)
Definition: renderer.cpp:60
smlt::Renderer::supports_texture_format
bool supports_texture_format(TextureFormat fmt)
Definition: renderer.h:80
smlt::batcher::RenderGroupFactory
Definition: render_queue.h:93
smlt::Renderer
Definition: renderer.h:40
smlt::default_init_ptr< Camera >
smlt::Renderable
Definition: renderable.h:66
smlt::Renderer::natively_supports_texture_format
bool natively_supports_texture_format(TextureFormat fmt)
Definition: renderer.h:74