Simulant  21.12-1292
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 GPUProgramPtr 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 GPUProgramPtr();
59  }
60 
61  virtual GPUProgramPtr current_gpu_program() const { return GPUProgramPtr(); }
62  virtual GPUProgramPtr gpu_program(const GPUProgramID&) const { return GPUProgramPtr(); }
63  virtual GPUProgramPtr default_gpu_program() const { return GPUProgramPtr(); }
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  virtual void apply_viewport(const RenderTarget& target,
89  const Viewport& viewport) {
90  _S_UNUSED(target);
91  _S_UNUSED(viewport);
92  }
93 
94  virtual void clear(const RenderTarget& target, const Color& color,
95  uint32_t clear_flags) {
96  _S_UNUSED(target);
97  _S_UNUSED(color);
98  _S_UNUSED(clear_flags);
99  }
100 
101  virtual void do_swap_buffers() {}
102 
103  virtual std::size_t max_texture_size() const {
104  return 1024;
105  }
106 
107 public:
111  virtual bool texture_format_is_native(TextureFormat fmt);
112 
115  virtual bool texture_format_is_usable(TextureFormat fmt);
116 
117  // Render support flags
118  virtual bool supports_gpu_programs() const { return false; }
119 
120  /*
121  * Returns true if the texture has been allocated, false otherwise.
122  */
123  bool is_texture_registered(AssetID texture_id) const;
124  void pre_render();
125  void post_render();
126 
127  void prepare_texture(Texture *texture);
128  void prepare_material(Material* material);
129 
130 private:
131  friend class Texture;
132 
133  void register_texture(AssetID tex_id, Texture *texture);
134  void unregister_texture(AssetID texture_id, Texture* texture);
135 
136  bool convert_if_necessary(Texture* tex);
137 
138  Window* window_ = nullptr;
139 
140  /*
141  * Called when a texture is created. This should do whatever is necessary to
142  * prepare a texture for later upload
143  *
144  * It's likely that data will need to also be stored here for prepare_texture
145  * to function
146  *
147  * This will be called when a render group (which uses textures) is created, which
148  * means it can be called from any thread and should be thread-safe.
149  */
150  virtual void on_texture_register(AssetID tex_id, Texture* texture) {
151  _S_UNUSED(tex_id);
152  _S_UNUSED(texture);
153  }
154 
155  /*
156  * Called when a texture is destroyed, should perform any clean_up from
157  * register_texture.
158  *
159  * This will be called when all render groups sharing the texture are destroyed
160  * and so can be called from any thread and should be thread-safe
161  */
162  virtual void on_texture_unregister(AssetID tex_id, Texture* texture) {
163  _S_UNUSED(tex_id);
164  _S_UNUSED(texture);
165  }
166 
167  /*
168  * Given a Texture, this should take care of:
169  * - Uploading the texture if the data is dirty
170  * - Updating texture filters and wrap modes if necessary
171  * - Generating or deleting mipmaps if the mipmap generation changed
172  *
173  * Guaranteed to be called from the main (render) thread, although must obviously
174  * be aware of register/unregister
175  */
176  virtual void on_texture_prepare(Texture* texture) {
177  _S_UNUSED(texture);
178  }
179 
180  /* Called when a Material is created, or when it is changed via
181  * an assignment. FIXME: This seems like we'd miss occasions where
182  * we should call this. Might be better being called on first update? */
183  virtual void on_material_prepare(Material* material) {
184  _S_UNUSED(material);
185  }
186 
187  /* Called at the start of pre_render() */
188  virtual void on_pre_render() {}
189  virtual void on_post_render() {}
190 
191  mutable thread::Mutex texture_registry_mutex_;
192  std::unordered_map<AssetID, Texture*> texture_registry_;
193 };
194 
195 }
196 
197 #endif // RENDERER_H
smlt::Property
Definition: property.h:181
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:68
smlt::Camera
Definition: camera.h:17
smlt::RenderTarget
Definition: interfaces.h:27
smlt::Renderer::texture_format_is_usable
virtual bool texture_format_is_usable(TextureFormat fmt)
Definition: renderer.cpp:63
Material
Definition: dcm.h:152
smlt::Renderer::supports_texture_format
bool supports_texture_format(TextureFormat fmt)
Definition: renderer.h:80
smlt::batcher::RenderGroupFactory
Definition: render_queue.h:109
smlt::Renderer
Definition: renderer.h:40
smlt::Renderable
Definition: renderable.h:67
smlt::Renderer::natively_supports_texture_format
bool natively_supports_texture_format(TextureFormat fmt)
Definition: renderer.h:74
smlt::Viewport
Definition: viewport.h:44