1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of Nova libraries.
|
---|
5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * @file device.hh
|
---|
9 | * @author Kornel Kisielewicz epyon@chaosforge.org
|
---|
10 | * @brief Device class
|
---|
11 | */
|
---|
12 |
|
---|
13 | #ifndef NV_INTERFACE_DEVICE_HH
|
---|
14 | #define NV_INTERFACE_DEVICE_HH
|
---|
15 |
|
---|
16 | #include <nv/common.hh>
|
---|
17 | #include <nv/stl/string.hh>
|
---|
18 | #include <nv/stl/handle.hh>
|
---|
19 | #include <nv/stl/hash_store.hh>
|
---|
20 | #include <nv/interface/uniform.hh>
|
---|
21 | #include <nv/interface/mesh_data.hh>
|
---|
22 | #include <nv/interface/image_data.hh>
|
---|
23 |
|
---|
24 | namespace nv
|
---|
25 | {
|
---|
26 | class window;
|
---|
27 |
|
---|
28 | enum shader_type
|
---|
29 | {
|
---|
30 | VERTEX_SHADER,
|
---|
31 | FRAGMENT_SHADER
|
---|
32 | };
|
---|
33 |
|
---|
34 | enum output_slot
|
---|
35 | {
|
---|
36 | OUTPUT_0 = 0,
|
---|
37 | OUTPUT_1 = 1,
|
---|
38 | OUTPUT_2 = 2,
|
---|
39 | OUTPUT_3 = 3,
|
---|
40 | OUTPUT_4 = 4,
|
---|
41 | OUTPUT_5 = 5,
|
---|
42 | OUTPUT_6 = 6,
|
---|
43 | OUTPUT_7 = 7,
|
---|
44 |
|
---|
45 | OUTPUT_NONE = 16,
|
---|
46 | OUTPUT_FRONT = 17,
|
---|
47 | OUTPUT_BACK = 18,
|
---|
48 | };
|
---|
49 |
|
---|
50 | enum texture_type
|
---|
51 | {
|
---|
52 | TEXTURE_1D,
|
---|
53 | TEXTURE_2D,
|
---|
54 | TEXTURE_RECT,
|
---|
55 | TEXTURE_3D,
|
---|
56 | TEXTURE_CUBE,
|
---|
57 | TEXTURE_1D_ARRAY,
|
---|
58 | TEXTURE_2D_ARRAY,
|
---|
59 | TEXTURE_1D_BUFFER,
|
---|
60 | TEXTURE_2D_MULTISAMPLE,
|
---|
61 | };
|
---|
62 |
|
---|
63 | enum texture_slot
|
---|
64 | {
|
---|
65 | TEXTURE_0 = 0, TEX_DIFFUSE = 0,
|
---|
66 | TEXTURE_1 = 1, TEX_NORMAL = 1,
|
---|
67 | TEXTURE_2 = 2, TEX_METALLIC = 2,
|
---|
68 | TEXTURE_3 = 3, TEX_ROUGHNESS = 3,
|
---|
69 | TEXTURE_4 = 4, TEX_EMISSIVE = 4,
|
---|
70 | TEXTURE_5 = 5, TEX_DEPTH = 5,
|
---|
71 | TEXTURE_6 = 6, TEX_OCCLUSION = 6,
|
---|
72 | TEXTURE_7 = 7, TEX_FINAL = 7,
|
---|
73 | TEXTURE_8 = 8, TEX_CUSTOM_0 = 8,
|
---|
74 | TEXTURE_9 = 9, TEX_CUSTOM_1 = 9,
|
---|
75 | TEXTURE_10 = 10, TEX_CUSTOM_2 = 10,
|
---|
76 | TEXTURE_11 = 11, TEX_CUSTOM_3 = 11,
|
---|
77 | TEXTURE_12 = 12, TEX_CUSTOM_4 = 12,
|
---|
78 | TEXTURE_13 = 13, TEX_CUSTOM_5 = 13,
|
---|
79 | TEXTURE_14 = 14, TEX_CUSTOM_6 = 14,
|
---|
80 | TEXTURE_15 = 15, TEX_CUSTOM_7 = 15,
|
---|
81 | MAX_TEXTURES = 16,
|
---|
82 | };
|
---|
83 |
|
---|
84 |
|
---|
85 | struct attribute
|
---|
86 | {
|
---|
87 | int location;
|
---|
88 | datatype type;
|
---|
89 | int length;
|
---|
90 | };
|
---|
91 |
|
---|
92 | typedef hash_store< shash64, attribute > attribute_map;
|
---|
93 |
|
---|
94 | struct texture_tag {};
|
---|
95 | struct buffer_tag {};
|
---|
96 | struct shader_tag {};
|
---|
97 | struct program_tag {};
|
---|
98 |
|
---|
99 | typedef handle< uint32, 16, 16, buffer_tag > buffer;
|
---|
100 | typedef handle< uint32, 16, 16, texture_tag > texture;
|
---|
101 | typedef handle< uint32, 16, 16, shader_tag > shader;
|
---|
102 | typedef handle< uint32, 16, 16, program_tag > program;
|
---|
103 |
|
---|
104 | NV_RTTI_DECLARE_NAME( buffer, "buffer" )
|
---|
105 | NV_RTTI_DECLARE_NAME( texture, "texture" )
|
---|
106 | NV_RTTI_DECLARE_NAME( shader, "shader" )
|
---|
107 | NV_RTTI_DECLARE_NAME( program, "program" )
|
---|
108 |
|
---|
109 | struct sampler
|
---|
110 | {
|
---|
111 | enum filter
|
---|
112 | {
|
---|
113 | LINEAR,
|
---|
114 | NEAREST,
|
---|
115 | NEAREST_MIPMAP_NEAREST,
|
---|
116 | LINEAR_MIPMAP_NEAREST,
|
---|
117 | NEAREST_MIPMAP_LINEAR,
|
---|
118 | LINEAR_MIPMAP_LINEAR
|
---|
119 | };
|
---|
120 | enum wrap
|
---|
121 | {
|
---|
122 | CLAMP_TO_EDGE,
|
---|
123 | CLAMP_TO_BORDER,
|
---|
124 | MIRRORED_REPEAT,
|
---|
125 | REPEAT
|
---|
126 | };
|
---|
127 |
|
---|
128 | filter filter_min;
|
---|
129 | filter filter_max;
|
---|
130 | wrap wrap_s;
|
---|
131 | wrap wrap_t;
|
---|
132 | wrap wrap_r;
|
---|
133 |
|
---|
134 | sampler() : filter_min( LINEAR ), filter_max( LINEAR ), wrap_s( REPEAT ), wrap_t( REPEAT ), wrap_r( REPEAT ) {}
|
---|
135 | sampler( filter min, filter max, wrap s, wrap t, wrap r )
|
---|
136 | : filter_min( min ), filter_max( max ), wrap_s( s ), wrap_t( t ), wrap_r( r ) {}
|
---|
137 | sampler( filter f, wrap w )
|
---|
138 | : filter_min( f ), filter_max( f ), wrap_s( w ), wrap_t( w ), wrap_r( w )
|
---|
139 | {}
|
---|
140 |
|
---|
141 | };
|
---|
142 |
|
---|
143 | enum buffer_hint
|
---|
144 | {
|
---|
145 | STATIC_DRAW,
|
---|
146 | STREAM_DRAW,
|
---|
147 | DYNAMIC_DRAW
|
---|
148 | };
|
---|
149 |
|
---|
150 | enum buffer_type
|
---|
151 | {
|
---|
152 | VERTEX_BUFFER,
|
---|
153 | INDEX_BUFFER,
|
---|
154 | UNIFORM_BUFFER,
|
---|
155 | TEXTURE_BUFFER,
|
---|
156 | };
|
---|
157 |
|
---|
158 | enum buffer_access
|
---|
159 | {
|
---|
160 | WRITE_UNSYNCHRONIZED,
|
---|
161 | };
|
---|
162 |
|
---|
163 | struct buffer_info
|
---|
164 | {
|
---|
165 | buffer_type type;
|
---|
166 | buffer_hint hint;
|
---|
167 | size_t size;
|
---|
168 | };
|
---|
169 |
|
---|
170 |
|
---|
171 | struct texture_info
|
---|
172 | {
|
---|
173 | texture_type type;
|
---|
174 | ivec3 size;
|
---|
175 | image_format format;
|
---|
176 | sampler tsampler;
|
---|
177 | };
|
---|
178 |
|
---|
179 | struct vertex_buffer_attribute
|
---|
180 | {
|
---|
181 | buffer vbuffer;
|
---|
182 | datatype dtype;
|
---|
183 | size_t components;
|
---|
184 | size_t offset;
|
---|
185 | size_t stride;
|
---|
186 | slot location;
|
---|
187 | bool owner;
|
---|
188 | };
|
---|
189 |
|
---|
190 | struct shader_info
|
---|
191 | {
|
---|
192 | shader_type type;
|
---|
193 | };
|
---|
194 |
|
---|
195 | struct program_info
|
---|
196 | {
|
---|
197 | attribute_map* m_attribute_map;
|
---|
198 | uniform_map* m_uniform_map;
|
---|
199 | engine_uniform_list* m_engine_uniforms;
|
---|
200 | };
|
---|
201 |
|
---|
202 | class device
|
---|
203 | {
|
---|
204 | friend class context;
|
---|
205 | public:
|
---|
206 | device()
|
---|
207 | {
|
---|
208 | initialize_engine_uniforms();
|
---|
209 | }
|
---|
210 | protected:
|
---|
211 | virtual shader create_shader( shader_type type, string_view sh_source ) = 0;
|
---|
212 | // virtual program create_program( string_view vs_source, string_view fs_source ) = 0;
|
---|
213 | virtual program create_program( shader vs, shader fs ) = 0;
|
---|
214 | virtual buffer create_buffer( buffer_type type, buffer_hint hint ) = 0;
|
---|
215 | virtual texture create_texture( texture_type type, pixel_format format ) = 0;
|
---|
216 |
|
---|
217 | // TODO: remove?
|
---|
218 | // virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, const void* data = nullptr ) { return create_texture( TEXTURE_2D, size, aformat, asampler, data ); }
|
---|
219 | virtual void attach( program p, shader s ) = 0;
|
---|
220 | virtual void detach( program p, shader s ) = 0;
|
---|
221 |
|
---|
222 | virtual void release( texture ) = 0;
|
---|
223 | virtual void release( buffer ) = 0;
|
---|
224 | virtual void release( program ) = 0;
|
---|
225 | virtual void release( shader ) = 0;
|
---|
226 | public:
|
---|
227 |
|
---|
228 |
|
---|
229 | virtual image_data* create_image_data( string_view filename ) = 0; // temporary
|
---|
230 | virtual image_data* create_image_data( const uint8* data, uint32 size ) = 0; // temporary
|
---|
231 | virtual const texture_info* get_texture_info( texture ) const = 0;
|
---|
232 | virtual const buffer_info* get_buffer_info( buffer ) const = 0;
|
---|
233 | virtual string_view get_shader_header() const = 0;
|
---|
234 |
|
---|
235 | virtual int get_attribute_location( program p, const string_view& name, bool fatal = true ) const = 0;
|
---|
236 | virtual int get_block_location( program p, const string_view& name, bool fatal = true ) const = 0;
|
---|
237 | virtual bool bind_block( program p, const string_view& name, uint32 index ) = 0;
|
---|
238 |
|
---|
239 | virtual ~device()
|
---|
240 | {
|
---|
241 | destroy_engine_uniforms();
|
---|
242 | }
|
---|
243 |
|
---|
244 | template < typename T >
|
---|
245 | bool set_uniform_array( program p, const string_view& name, const T* value, uint32 count, bool fatal = true )
|
---|
246 | {
|
---|
247 | uniform_base* base = get_uniform( p, name, fatal );
|
---|
248 | if ( base != nullptr )
|
---|
249 | {
|
---|
250 | if ( base->type_check( type_to_enum<T>::type ) )
|
---|
251 | {
|
---|
252 | // TODO: nicer check
|
---|
253 | NV_ASSERT( static_cast<int>( count ) <= base->get_length(), "LENGTH CHECK FAIL" );
|
---|
254 | static_cast<uniform<T>*>( base )->set_value( value, count );
|
---|
255 | return true;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | return false;
|
---|
259 | }
|
---|
260 |
|
---|
261 | template < typename T >
|
---|
262 | bool set_opt_uniform_array( program p, const string_view& name, const T* value, uint32 count )
|
---|
263 | {
|
---|
264 | return set_uniform_array( p, name, value, count, false );
|
---|
265 | }
|
---|
266 |
|
---|
267 | template < typename T >
|
---|
268 | bool set_opt_uniform_array( program p, const string_view& name, const array_view<T>& value )
|
---|
269 | {
|
---|
270 | return set_uniform_array( p, name, value.data(), value.size(), false );
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | template < typename T >
|
---|
275 | bool set_uniform( program p, const string_view& name, const T& value, bool fatal = true )
|
---|
276 | {
|
---|
277 | uniform_base* base = get_uniform( p, name, fatal );
|
---|
278 | if ( base != nullptr )
|
---|
279 | {
|
---|
280 | if ( base->type_check( type_to_enum<T>::type ) )
|
---|
281 | {
|
---|
282 | static_cast<uniform<T>*>( base )->set_value( value );
|
---|
283 | return true;
|
---|
284 | }
|
---|
285 | }
|
---|
286 | return false;
|
---|
287 | }
|
---|
288 |
|
---|
289 | template < typename T >
|
---|
290 | bool set_opt_uniform( program p, const string_view& name, const T& value )
|
---|
291 | {
|
---|
292 | return set_uniform( p, name, value, false );
|
---|
293 | }
|
---|
294 |
|
---|
295 | // This is done this way to avoid compilation unit creation
|
---|
296 | static engine_uniform_factory_map& get_uniform_factory()
|
---|
297 | {
|
---|
298 | static engine_uniform_factory_map s_engine_uniform_factory_map;
|
---|
299 | return s_engine_uniform_factory_map;
|
---|
300 | }
|
---|
301 |
|
---|
302 | // This is done this way to avoid compilation unit creation
|
---|
303 | static engine_link_uniform_factory_map& get_link_uniform_factory()
|
---|
304 | {
|
---|
305 | static engine_link_uniform_factory_map s_engine_link_uniform_factory_map;
|
---|
306 | return s_engine_link_uniform_factory_map;
|
---|
307 | }
|
---|
308 |
|
---|
309 | virtual void prepare_program( program p ) = 0;
|
---|
310 |
|
---|
311 | protected:
|
---|
312 |
|
---|
313 | virtual uniform_base* get_uniform( program p, const string_view& name, bool fatal = true ) const = 0;
|
---|
314 |
|
---|
315 | void initialize_engine_uniforms()
|
---|
316 | {
|
---|
317 | engine_uniform_factory_map& factory_map = get_uniform_factory();
|
---|
318 | factory_map[ "nv_f_near"] = new engine_uniform_factory< engine_uniform_f_near >();
|
---|
319 | factory_map[ "nv_f_far"] = new engine_uniform_factory< engine_uniform_f_far >();
|
---|
320 | factory_map[ "nv_m_view" ] = new engine_uniform_factory< engine_uniform_m_view >();
|
---|
321 | factory_map[ "nv_m_view_inv" ] = new engine_uniform_factory< engine_uniform_m_view_inv >();
|
---|
322 | factory_map[ "nv_m_model" ] = new engine_uniform_factory< engine_uniform_m_model >();
|
---|
323 | factory_map[ "nv_m_model_inv" ] = new engine_uniform_factory< engine_uniform_m_model_inv >();
|
---|
324 | factory_map[ "nv_m_modelview" ] = new engine_uniform_factory< engine_uniform_m_modelview >();
|
---|
325 | factory_map[ "nv_m_modelview_inv" ] = new engine_uniform_factory< engine_uniform_m_modelview_inv >();
|
---|
326 | factory_map[ "nv_m_projection" ] = new engine_uniform_factory< engine_uniform_m_projection >();
|
---|
327 | factory_map[ "nv_m_projection_inv"] = new engine_uniform_factory< engine_uniform_m_projection_inv >();
|
---|
328 | factory_map[ "nv_m_viewprojection" ] = new engine_uniform_factory< engine_uniform_m_viewprojection >();
|
---|
329 | factory_map[ "nv_m_viewprojection_inv" ] = new engine_uniform_factory< engine_uniform_m_viewprojection_inv >();
|
---|
330 | factory_map[ "nv_m_normal" ] = new engine_uniform_factory< engine_uniform_m_normal >();
|
---|
331 | factory_map[ "nv_m_mvp" ] = new engine_uniform_factory< engine_uniform_m_mvp >();
|
---|
332 | factory_map[ "nv_m_mvp_inv"] = new engine_uniform_factory< engine_uniform_m_mvp_inv >();
|
---|
333 | factory_map[ "nv_v_camera_position" ] = new engine_uniform_factory< engine_uniform_v_camera_position >();
|
---|
334 | factory_map[ "nv_v_camera_direction" ] = new engine_uniform_factory< engine_uniform_v_camera_direction >();
|
---|
335 | factory_map[ "nv_v_viewport" ] = new engine_uniform_factory< engine_uniform_v_viewport >();
|
---|
336 | factory_map[ "nv_v_screen_size" ] = new engine_uniform_factory< engine_uniform_v_screen_size >();
|
---|
337 |
|
---|
338 | engine_link_uniform_factory_map& factory_link_map = get_link_uniform_factory();
|
---|
339 | factory_link_map[ "nv_texture_0" ] = new engine_link_uniform_int<0>();
|
---|
340 | factory_link_map[ "nv_texture_1" ] = new engine_link_uniform_int<1>();
|
---|
341 | factory_link_map[ "nv_texture_2" ] = new engine_link_uniform_int<2>();
|
---|
342 | factory_link_map[ "nv_texture_3" ] = new engine_link_uniform_int<3>();
|
---|
343 | factory_link_map[ "nv_texture_4" ] = new engine_link_uniform_int<4>();
|
---|
344 | factory_link_map[ "nv_texture_5" ] = new engine_link_uniform_int<5>();
|
---|
345 | factory_link_map[ "nv_texture_6" ] = new engine_link_uniform_int<6>();
|
---|
346 | factory_link_map[ "nv_texture_7" ] = new engine_link_uniform_int<7>();
|
---|
347 | factory_link_map[ "nv_texture_8" ] = new engine_link_uniform_int<8>();
|
---|
348 | factory_link_map[ "nv_texture_9" ] = new engine_link_uniform_int<9>();
|
---|
349 | factory_link_map[ "nv_t_diffuse" ] = new engine_link_uniform_int<int( TEX_DIFFUSE )>();
|
---|
350 | factory_link_map[ "nv_t_normal" ] = new engine_link_uniform_int<int( TEX_NORMAL )>();
|
---|
351 | factory_link_map[ "nv_t_metallic" ] = new engine_link_uniform_int<int( TEX_METALLIC )>();
|
---|
352 | factory_link_map[ "nv_t_roughness"] = new engine_link_uniform_int<int( TEX_ROUGHNESS )>();
|
---|
353 | factory_link_map[ "nv_t_emissive" ] = new engine_link_uniform_int<int( TEX_EMISSIVE )>();
|
---|
354 | factory_link_map[ "nv_t_depth" ] = new engine_link_uniform_int<int( TEX_DEPTH )>();
|
---|
355 | factory_link_map[ "nv_t_occlusion"] = new engine_link_uniform_int<int( TEX_OCCLUSION )>();
|
---|
356 | factory_link_map[ "nv_t_final" ] = new engine_link_uniform_int<int( TEX_FINAL )>();
|
---|
357 | }
|
---|
358 | void destroy_engine_uniforms()
|
---|
359 | {
|
---|
360 | for ( auto& i : get_uniform_factory() ) delete i.second;
|
---|
361 | for ( auto& i : get_link_uniform_factory() ) delete i.second;
|
---|
362 | get_uniform_factory().clear();
|
---|
363 | get_link_uniform_factory().clear();
|
---|
364 | }
|
---|
365 |
|
---|
366 | };
|
---|
367 |
|
---|
368 | } // namespace nv
|
---|
369 |
|
---|
370 |
|
---|
371 | #endif // NV_INTERFACE_DEVICE_HH
|
---|