Changeset 302
- Timestamp:
- 08/07/14 19:06:34 (11 years ago)
- Location:
- trunk
- Files:
-
- 2 deleted
- 22 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/gfx/debug_draw.hh
r237 r302 31 31 void reset(); 32 32 program* get_program() { return m_program; } 33 vertex_array *get_vertex_array() { return m_va; }33 vertex_array get_vertex_array() { return m_va; } 34 34 uint32 get_count() { return m_data.size(); } 35 35 … … 40 40 device* m_device; 41 41 program* m_program; 42 vertex_array *m_va;42 vertex_array m_va; 43 43 std::vector< debug_vtx > m_data; 44 44 }; -
trunk/nv/gfx/keyframed_mesh.hh
r299 r302 19 19 { 20 20 public: 21 keyframed_mesh( const mesh_data* a_data, const mesh_nodes_data* a_tag_map ); 21 keyframed_mesh( context* a_context, const mesh_data* a_data, const mesh_nodes_data* a_tag_map ); 22 virtual vertex_array get_vertex_array() const { return m_va; } 22 23 virtual size_t get_index_count() const { return m_index_count; } 23 24 virtual void run_animation( animation_entry* a_anim ); … … 50 51 const mesh_nodes_data* m_tag_map; 51 52 const mesh_raw_channel* m_vchannel; 53 54 context* m_context; 55 buffer m_pbuffer; 56 vertex_array m_va; 52 57 53 58 uint32 m_last_frame; … … 86 91 ~keyframed_mesh_cpu(); 87 92 private: 88 context* m_context; 89 90 uint8* m_data; 91 vertex_buffer* m_vb; 93 uint8* m_data; 92 94 }; 93 95 -
trunk/nv/gfx/skeletal_mesh.hh
r299 r302 19 19 { 20 20 public: 21 skeletal_mesh() : animated_mesh() {} 21 skeletal_mesh( context* a_context ) : animated_mesh(), m_context( a_context ) {} 22 virtual vertex_array get_vertex_array() const { return m_va; } 22 23 virtual void run_animation( animation_entry* a_anim ) 23 24 { 24 25 update_animation( a_anim, 0 ); 25 26 } 27 ~skeletal_mesh() 28 { 29 m_context->get_device()->release( m_va ); 30 } 31 protected: 32 vertex_array m_va; 33 context* m_context; 26 34 }; 27 35 … … 44 52 virtual size_t get_index_count() const { return m_indices; } 45 53 virtual void update_animation( animation_entry* a_anim, uint32 a_anim_time ); 46 virtual ~skeletal_mesh_cpu();47 54 protected: 48 context* m_context;55 buffer m_pbuffer; 49 56 uint32 m_indices; 50 57 dynamic_array< md5_vtx_pnt > m_pntdata; -
trunk/nv/gfx/sliced_buffer.hh
r299 r302 93 93 static const size_t value_type_size = sizeof(T); 94 94 95 sliced_buffer( context* ctx, buffer_ hint hint, size_t initial_size, bool is_vertex = true )95 sliced_buffer( context* ctx, buffer_type type, buffer_hint hint, size_t initial_size ) 96 96 : m_context( ctx ) 97 , m_buffer( nullptr ) 97 , m_buffer() 98 , m_type( type ) 98 99 , m_hint( hint ) 99 100 , m_full_update( true ) 100 , m_is_vertex( is_vertex )101 101 , m_data() 102 , m_capacity(0) 102 103 , m_min(0) 103 104 , m_max(0) … … 168 169 size_t offset = m_min * value_type_size; 169 170 size_t size = (m_max-m_min) * value_type_size; 170 if ( m_is_vertex ) 171 m_context->update( (vertex_buffer*)m_buffer, m_data.data() + m_min, offset, size ); 172 else 173 m_context->update( (index_buffer*)m_buffer, m_data.data() + m_min, offset, size ); 171 m_context->update( m_buffer, m_data.data() + m_min, offset, size ); 174 172 } 175 173 m_full_update = false; … … 181 179 size_t get_max_size() const 182 180 { 183 return m_ buffer->get_size() / value_type_size;181 return m_capacity; 184 182 } 185 183 … … 189 187 } 190 188 191 buffer *get_buffer()189 buffer get_buffer() 192 190 { 193 191 return m_buffer; … … 196 194 virtual ~sliced_buffer() 197 195 { 198 delete m_buffer;196 m_context->get_device()->release( m_buffer ); 199 197 } 200 198 private: 201 199 void create_buffer( size_t size ) 202 200 { 203 delete m_buffer; 204 if ( m_is_vertex ) 205 m_buffer = m_context->get_device()->create_vertex_buffer( m_hint, size * value_type_size, nullptr ); 206 else 207 m_buffer = m_context->get_device()->create_index_buffer( m_hint, size * value_type_size, nullptr ); 201 m_context->get_device()->release( m_buffer ); 202 m_capacity = size; 203 m_buffer = m_context->get_device()->create_buffer( m_type, m_hint, size * value_type_size, nullptr ); 208 204 } 209 205 private: 210 206 context* m_context; 211 buffer *m_buffer;207 buffer m_buffer; 212 208 buffer_hint m_hint; 209 buffer_type m_type; 213 210 bool m_full_update; 214 bool m_is_vertex;215 211 vector m_data; 212 uint32 m_capacity; 216 213 217 214 size_t m_min; … … 275 272 276 273 indexed_sliced_buffer( device* dev, buffer_hint hint, int initial_size, int initial_index_size ) 277 : m_vertex_buffer( dev, hint, initial_size, true )278 , m_index_buffer( dev, hint, initial_index_size, false )274 : m_vertex_buffer( dev, VERTEX_BUFFER, hint, initial_size ) 275 , m_index_buffer( dev, INDEX_BUFFER, hint, initial_index_size ) 279 276 { 280 277 … … 298 295 int get_vertex_size() const { return m_vertex_buffer.get_size(); } 299 296 int get_index_size() const { return m_index_buffer.get_size(); } 300 vertex_buffer* get_vertex_buffer() { return static_cast<vertex_buffer*>( m_vertex_buffer.get_buffer() ); } 301 index_buffer* get_index_buffer() { return static_cast<index_buffer*>( m_index_buffer.get_buffer() ); } 297 buffer* get_buffer() { return m_vertex_buffer.get_buffer(); } 302 298 sliced_vertex_buffer& get_vertex_cache() { return m_vertex_buffer; } 303 299 sliced_index_buffer& get_index_cache() { return m_index_buffer; } -
trunk/nv/gl/gl_context.hh
r301 r302 25 25 virtual void bind( texture t, texture_slot slot ); 26 26 virtual void bind( program* p ); 27 virtual void bind( vertex_buffer* b ); 28 virtual void bind( index_buffer* b ); 29 virtual void bind( vertex_array* va ); 27 virtual void bind( buffer b ); 28 virtual void bind( vertex_array va ); 30 29 virtual void unbind( program* p ); 31 virtual void unbind( index_buffer* b ); 32 virtual void unbind( vertex_buffer* b ); 33 virtual void unbind( vertex_array* va ); 30 virtual void unbind( buffer b ); 31 virtual void unbind( vertex_array va ); 34 32 35 33 virtual void update( texture t, void* data ); 36 virtual void update( index_buffer* b, const void* data, size_t offset, size_t size ); 37 virtual void update( vertex_buffer* b, const void* data, size_t offset, size_t size ); 34 virtual void update( buffer b, const void* data, size_t offset, size_t size ); 38 35 39 36 virtual void clear( const clear_state& cs ); 40 37 // temporary 41 virtual void draw( primitive prim, const render_state& rs, program* p, vertex_array *va, size_t count );38 virtual void draw( primitive prim, const render_state& rs, program* p, vertex_array va, size_t count ); 42 39 virtual const ivec4& get_viewport(); 43 40 virtual void set_viewport( const ivec4& viewport ); -
trunk/nv/gl/gl_device.hh
r301 r302 22 22 }; 23 23 24 struct gl_buffer_info : public buffer_info 25 { 26 unsigned glid; 27 }; 28 24 29 class gl_device : public device 25 30 { … … 28 33 virtual window* create_window( uint16 width, uint16 height, bool fullscreen ); 29 34 virtual window* adopt_window( void* sys_w_handle, void* sys_dc ); 35 virtual image_data* create_image_data( const std::string& filename ); // temporary 36 30 37 virtual program* create_program( const string& vs_source, const string& fs_source ); 31 virtual vertex_buffer* create_vertex_buffer( buffer_hint hint, size_t size, const void* source = nullptr ); 32 virtual index_buffer* create_index_buffer( buffer_hint hint, size_t size, const void* source = nullptr ); 33 virtual vertex_array* create_vertex_array(); 34 virtual image_data* create_image_data( const std::string& filename ); // temporary 38 virtual buffer create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source = nullptr ); 39 virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, void* data = nullptr ); 35 40 virtual uint32 get_ticks(); 36 41 virtual void delay( uint32 ms ); 37 42 38 virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, void* data = nullptr);39 virtual void release _texture( texture t );43 virtual void release( buffer b ); 44 virtual void release( texture t ); 40 45 virtual const texture_info* get_texture_info( texture t ); 46 virtual const buffer_info* get_buffer_info( buffer t ); 41 47 42 48 virtual ~gl_device(); … … 44 50 const void* m_info; 45 51 entity_store< gl_texture_info, texture > m_textures; 52 entity_store< gl_buffer_info, buffer > m_buffers; 46 53 }; 47 54 -
trunk/nv/gl/gl_enum.hh
r301 r302 33 33 unsigned int stencil_operation_to_enum( stencil_test_face::operation type ); 34 34 unsigned int buffer_hint_to_enum( buffer_hint hint ); 35 unsigned int buffer_type_to_enum( buffer_type type ); 35 36 unsigned int image_format_to_enum( pixel_format format ); 36 37 unsigned int sampler_filter_to_enum( sampler::filter filter ); -
trunk/nv/gui/gui_renderer.hh
r299 r302 33 33 struct vertex 34 34 { 35 ivec2 coord;36 vec2 t coord;35 ivec2 position; 36 vec2 texcoord; 37 37 vec4 color; 38 38 vertex() {} 39 39 vertex( const nv::ivec2& cr, const nv::vec2& tc, const nv::vec4& c ) 40 : coord( cr ), tcoord( tc ), color( c ) {}40 : position( cr ), texcoord( tc ), color( c ) {} 41 41 }; 42 42 -
trunk/nv/interface/context.hh
r301 r302 37 37 { 38 38 public: 39 mesh_interface() : m_va( nullptr ) {} 40 explicit mesh_interface( vertex_array* array ) : m_va( array ) {} 39 mesh_interface() {} 41 40 virtual void update( uint32 ) {} 42 41 virtual void update( program* ) {} 43 virtual nv::vertex_array * get_vertex_array() const { return m_va; }44 virtual size_t get_index_count() const { return 0; }42 virtual nv::vertex_array get_vertex_array() const = 0; 43 virtual size_t get_index_count() const = 0; 45 44 virtual nv::primitive get_primitive_type() const { return nv::TRIANGLES; } 46 45 virtual ~mesh_interface() {} 47 protected:48 vertex_array* m_va;49 46 }; 50 47 51 class device;52 class texture2d;53 48 class context 54 49 { … … 61 56 } 62 57 virtual void bind( texture, texture_slot ) = 0; 63 virtual void bind( vertex_buffer* ) = 0; 64 virtual void bind( index_buffer* ) = 0; 58 virtual void bind( buffer ) = 0; 65 59 virtual void bind( program* ) = 0; 66 virtual void bind( vertex_array* ) = 0; 67 virtual void unbind( vertex_buffer* ) = 0; 68 virtual void unbind( index_buffer* ) = 0; 60 virtual void bind( vertex_array ) = 0; 61 virtual void unbind( buffer ) = 0; 69 62 virtual void unbind( program* ) = 0; 70 virtual void unbind( vertex_array *) = 0;63 virtual void unbind( vertex_array ) = 0; 71 64 virtual void update( texture, void* ) = 0; 72 virtual void update( index_buffer*, const void*, size_t /*offset*/, size_t /*size*/ ) = 0; 73 virtual void update( vertex_buffer*, const void*, size_t /*offset*/, size_t /*size*/ ) = 0; 65 virtual void update( buffer, const void*, size_t /*offset*/, size_t /*size*/ ) = 0; 74 66 75 67 virtual void clear( const clear_state& cs ) = 0; 76 68 // temporary 77 virtual void draw( primitive prim, const render_state& rs, program* p, vertex_array *va, size_t count ) = 0;69 virtual void draw( primitive prim, const render_state& rs, program* p, vertex_array va, size_t count ) = 0; 78 70 79 71 virtual void draw( const render_state& rs, program* p, const mesh_interface* mesh ) … … 86 78 draw( rs, p, mesh ); 87 79 } 88 virtual void draw( primitive prim, const render_state& rs, const scene_state& s, program* p, vertex_array *va, size_t count )80 virtual void draw( primitive prim, const render_state& rs, const scene_state& s, program* p, vertex_array va, size_t count ) 89 81 { 90 82 p->apply_engine_uniforms( this, &s ); … … 102 94 } 103 95 protected: 96 vertex_array_info* get_vertex_array_info( vertex_array va ) 97 { 98 return m_device->m_vertex_arrays.get( va ); 99 } 100 104 101 device* m_device; 105 102 clear_state m_clear_state; -
trunk/nv/interface/device.hh
r301 r302 25 25 class program; 26 26 27 struct texture_tag {}; 28 struct vertex_array_tag {}; 29 struct buffer_tag {}; 30 typedef handle< uint32, 16, 16, buffer_tag > buffer; 31 typedef handle< uint32, 16, 16, texture_tag > texture; 32 typedef handle< uint32, 16, 16, vertex_array_tag > vertex_array; 33 34 27 35 struct sampler 28 36 { … … 57 65 }; 58 66 67 struct buffer_info 68 { 69 buffer_type type; 70 buffer_hint hint; 71 size_t size; 72 }; 73 74 59 75 struct texture_info 60 76 { … … 64 80 }; 65 81 66 67 struct texture_tag {}; 68 typedef handle< uint32, 16, 16, texture_tag > texture; 82 struct vertex_buffer_attribute 83 { 84 buffer vbuffer; 85 datatype dtype; 86 size_t components; 87 size_t offset; 88 size_t stride; 89 slot location; 90 bool owner; 91 }; 92 93 struct vertex_array_info 94 { 95 static const int MAX_ATTRIBUTES = 24; 96 97 uint32 count; 98 buffer index; 99 bool index_owner; 100 datatype index_type; 101 vertex_buffer_attribute attr[MAX_ATTRIBUTES]; 102 }; 69 103 70 104 class device 71 105 { 106 friend class context; 72 107 public: 73 108 virtual window* create_window( uint16 width, uint16 height, bool fullscreen ) = 0; 74 109 virtual window* adopt_window( void* sys_w_handle, void* sys_dc ) = 0; 75 110 virtual program* create_program( const string& vs_source, const string& fs_source ) = 0; 76 virtual vertex_buffer* create_vertex_buffer( buffer_hint hint, size_t size, const void* source = nullptr ) = 0; 77 virtual index_buffer* create_index_buffer( buffer_hint hint, size_t size, const void* source = nullptr ) = 0; 78 virtual vertex_array* create_vertex_array() = 0; 111 virtual buffer create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source = nullptr ) = 0; 79 112 virtual image_data* create_image_data( const std::string& filename ) = 0; // temporary 80 113 virtual texture create_texture( ivec2 size, image_format aformat, sampler asampler, void* data = nullptr ) = 0; 81 virtual void release_texture( texture ) = 0; 114 virtual void release( texture ) = 0; 115 virtual void release( buffer ) = 0; 82 116 virtual const texture_info* get_texture_info( texture ) = 0; 117 virtual const buffer_info* get_buffer_info( buffer ) = 0; 83 118 virtual uint32 get_ticks() = 0; 84 119 virtual void delay( uint32 ms ) = 0; 85 86 120 virtual texture create_texture( image_data* data, sampler asampler ) 87 121 { … … 89 123 } 90 124 125 virtual vertex_array create_vertex_array() 126 { 127 vertex_array result = m_vertex_arrays.create(); 128 vertex_array_info* info = m_vertex_arrays.get( result ); 129 info->count = 0; 130 info->index = buffer(); 131 info->index_owner = false; 132 info->index_type = USHORT; 133 return result; 134 } 135 136 virtual void release( vertex_array va ) 137 { 138 vertex_array_info* info = m_vertex_arrays.get( va ); 139 if ( info ) 140 { 141 for ( uint32 i = 0; i < info->count; ++i ) 142 { 143 if ( info->attr[i].owner ) release( info->attr[i].vbuffer ); 144 } 145 if ( info->index.is_valid() && info->index_owner) release( info->index ); 146 m_vertex_arrays.destroy( va ); 147 } 148 }; 149 91 150 template < typename VTX, slot SLOT > 92 void add_vertex_buffer_impl( vertex_array *, vertex_buffer*, const std::false_type& )151 void add_vertex_buffer_impl( vertex_array, buffer, const std::false_type& ) 93 152 { 94 153 } 95 154 96 155 template < typename VTX, slot SLOT > 97 void add_vertex_buffer_impl( vertex_array * va, vertex_buffer*vb, const std::true_type& )156 void add_vertex_buffer_impl( vertex_array va, buffer vb, const std::true_type& ) 98 157 { 99 158 typedef vertex_slot_info< VTX, SLOT > vinfo; 100 159 typedef datatype_traits< typename vinfo::value_type > dt_traits; 101 va->add_vertex_buffer(SLOT, vb, type_to_enum< dt_traits::base_type >::type, dt_traits::size, vinfo::offset, sizeof( VTX ), false );160 add_vertex_buffer( va, SLOT, vb, type_to_enum< dt_traits::base_type >::type, dt_traits::size, vinfo::offset, sizeof( VTX ), false ); 102 161 } 103 162 104 163 template < typename VTX, slot SLOT > 105 void add_vertex_buffer( vertex_array * va, vertex_buffer*vb )164 void add_vertex_buffer( vertex_array va, buffer vb ) 106 165 { 107 166 add_vertex_buffer_impl< VTX, SLOT >( va, vb, std::integral_constant< bool, vertex_has_slot< VTX, SLOT >::value >() ); 108 167 } 109 168 110 111 169 template < typename VTX > 112 vertex_array* create_vertex_array( const VTX* v, size_t count, buffer_hint hint ) 113 { 114 // TODO: vb will not be owned or freed! 115 vertex_array* va = create_vertex_array(); 116 vertex_buffer* vb = create_vertex_buffer( hint, count * sizeof( VTX ), v ); 170 void add_vertex_buffers( vertex_array va, buffer vb ) 171 { 117 172 add_vertex_buffer< VTX, slot::POSITION > ( va, vb ); 118 173 add_vertex_buffer< VTX, slot::TEXCOORD > ( va, vb ); … … 122 177 add_vertex_buffer< VTX, slot::BONEWEIGHT >( va, vb ); 123 178 add_vertex_buffer< VTX, slot::COLOR > ( va, vb ); 179 } 180 181 void add_vertex_buffers( vertex_array va, buffer buf, const mesh_raw_channel* channel ) 182 { 183 for ( uint32 s = 0; s < channel->desc.count; ++s ) 184 { 185 const vertex_descriptor_slot& slot = channel->desc.slots[s]; 186 const datatype_info& info = get_datatype_info(slot.etype); 187 add_vertex_buffer( va, slot.vslot, buf, info.base , info.elements, slot.offset, channel->desc.size, false ); 188 } 189 } 190 191 template < typename VTX > 192 vertex_array create_vertex_array( const VTX* v, size_t count, buffer_hint hint ) 193 { 194 // TODO: vb will not be owned or freed! 195 vertex_array va = create_vertex_array(); 196 buffer vb = create_buffer( VERTEX_BUFFER, hint, count * sizeof( VTX ), v ); 197 add_vertex_buffers< VTX >( va, vb ); 124 198 return va; 125 199 } 126 200 127 201 template < typename VTX > 128 vertex_array *create_vertex_array( const std::vector< VTX >& data, buffer_hint hint )202 vertex_array create_vertex_array( const std::vector< VTX >& data, buffer_hint hint ) 129 203 { 130 204 return create_vertex_array( data.data(), data.size(), hint ); … … 132 206 133 207 template < typename VTX, typename IDX > 134 vertex_array *create_vertex_array( const VTX* v, size_t vcount, const IDX* i, size_t icount, buffer_hint hint )135 { 136 vertex_array *va = create_vertex_array( v, vcount, hint );137 index_buffer* ib = create_index_buffer(hint, icount * sizeof( IDX ), i );208 vertex_array create_vertex_array( const VTX* v, size_t vcount, const IDX* i, size_t icount, buffer_hint hint ) 209 { 210 vertex_array va = create_vertex_array( v, vcount, hint ); 211 buffer ib = create_buffer( INDEX_BUFFER, hint, icount * sizeof( IDX ), i ); 138 212 va->set_index_buffer( ib, type_to_enum< IDX >::type, true ); 139 213 return va; … … 141 215 142 216 template < typename VTX, typename IDX > 143 vertex_array *create_vertex_array( const std::vector< VTX >& data, const std::vector< IDX >& idata, buffer_hint hint )217 vertex_array create_vertex_array( const std::vector< VTX >& data, const std::vector< IDX >& idata, buffer_hint hint ) 144 218 { 145 219 return create_vertex_array( data.data(), data.size(), idata.data(), idata.size(), hint ); 146 220 } 147 221 222 void replace_vertex_buffer( vertex_array va, buffer vb, bool owner ) 223 { 224 vertex_array_info* info = m_vertex_arrays.get( va ); 225 if ( info ) 226 { 227 for ( uint32 i = 0; i < info->count; ++i ) 228 { 229 vertex_buffer_attribute& vba = info->attr[i]; 230 if ( vba.owner ) release( vba.vbuffer ); 231 vba.vbuffer = vb; 232 vba.owner = owner; 233 } 234 } 235 } 236 237 void replace_vertex_buffer( vertex_array va, buffer vb, slot location, bool owner ) 238 { 239 vertex_array_info* info = m_vertex_arrays.get( va ); 240 if ( info ) 241 { 242 for ( uint32 i = 0; i < info->count; ++i ) 243 { 244 if ( info->attr[i].location == location ) 245 { 246 vertex_buffer_attribute& vba = info->attr[i]; 247 if ( vba.owner ) release( vba.vbuffer ); 248 vba.vbuffer = vb; 249 vba.owner = owner; 250 } 251 } 252 } 253 } 254 255 void update_attribute_offset( vertex_array va, slot location, size_t offset ) 256 { 257 vertex_array_info* info = m_vertex_arrays.get( va ); 258 if ( info ) 259 { 260 for ( uint32 i = 0; i < info->count; ++i ) 261 { 262 if ( info->attr[i].location == location ) 263 { 264 info->attr[i].offset = offset; 265 } 266 } 267 } 268 } 269 270 void set_index_buffer( vertex_array va, buffer b, datatype datatype, bool owner ) 271 { 272 vertex_array_info* info = m_vertex_arrays.get( va ); 273 if ( info ) 274 { 275 if ( get_buffer_info( b )->type == INDEX_BUFFER ) 276 { 277 if (info->index.is_valid() && info->index_owner) release( info->index ); 278 279 info->index = b; 280 info->index_owner = owner; 281 info->index_type = datatype; 282 } 283 } 284 } 285 286 void add_vertex_buffer( vertex_array va, slot location, buffer buf, datatype datatype, size_t components, size_t offset = 0, size_t stride = 0, bool owner = true ) 287 { 288 vertex_array_info* info = m_vertex_arrays.get( va ); 289 if ( info ) 290 { 291 NV_ASSERT( info->count < vertex_array_info::MAX_ATTRIBUTES, "MAX_ATTRIBUTES reached!" ); 292 vertex_buffer_attribute& p = info->attr[ info->count ]; 293 p.vbuffer = buf; 294 p.dtype = datatype; 295 p.components = components; 296 p.offset = offset; 297 p.stride = stride; 298 p.owner = owner; 299 p.location = location; 300 info->count++; 301 } 302 } 303 304 buffer find_buffer( vertex_array va, slot location ) 305 { 306 vertex_array_info* info = m_vertex_arrays.get( va ); 307 if ( info ) 308 { 309 for ( uint32 i = 0; i < info->count; ++i ) 310 { 311 if ( info->attr[i].location == location ) 312 { 313 return info->attr[i].vbuffer; 314 } 315 } 316 } 317 return buffer(); 318 } 319 148 320 // TODO: HINTS ARE DIFFERENT! 149 vertex_array *create_vertex_array( const mesh_data* data, buffer_hint hint )150 { 151 vertex_array *va = create_vertex_array();321 vertex_array create_vertex_array( const mesh_data* data, buffer_hint hint ) 322 { 323 vertex_array va = create_vertex_array(); 152 324 const std::vector< mesh_raw_channel* >& channels = data->get_raw_channels(); 153 325 for ( uint32 ch = 0; ch < channels.size(); ++ch ) … … 156 328 if ( channel->count > 0 ) 157 329 { 158 if ( channel->is_index() ) 159 { 160 index_buffer* ib = create_index_buffer( hint, channel->size(), channel->data ); 161 va->set_index_buffer( ib, channel->desc.slots[0].etype, true ); 330 buffer_type type = channel->get_buffer_type(); 331 buffer b = create_buffer( type, hint, channel->size(), channel->data ); 332 // TODO: no if switch 333 if ( type == INDEX_BUFFER ) 334 { 335 set_index_buffer( va, b, channel->desc.slots[0].etype, true ); 162 336 } 163 337 else 164 338 { 165 vertex_buffer* vb = create_vertex_buffer( hint, channel->size(), channel->data ); 166 va->add_vertex_buffers( vb, channel ); 339 add_vertex_buffers( va, b, channel ); 167 340 } 168 341 } … … 172 345 173 346 174 virtual ~device() {} 347 virtual ~device() 348 { 349 // TODO: releases buffers via gl_context which is destoryed! 350 while ( m_vertex_arrays.size() > 0 ) 351 release( m_vertex_arrays.get_handle(0) ); 352 } 353 protected: 354 entity_store< vertex_array_info, vertex_array > m_vertex_arrays; 175 355 }; 176 356 -
trunk/nv/interface/mesh_data.hh
r297 r302 36 36 } 37 37 38 bool is_index() const { return count > 0 && desc.slots[0].vslot == slot::INDEX; } 38 // TODO: this could have more options if stored! 39 buffer_type get_buffer_type() const 40 { 41 if ( count > 0 && desc.slots[0].vslot == slot::INDEX ) 42 { 43 return INDEX_BUFFER; 44 } 45 return VERTEX_BUFFER; 46 } 39 47 40 48 uint32 size() const { return count * desc.size; } … … 92 100 NV_ASSERT( channel, "nullptr passed to add_channel!" ); 93 101 m_channels.push_back( channel ); 94 if ( channel-> is_index())102 if ( channel->get_buffer_type() == INDEX_BUFFER ) 95 103 { 96 104 NV_ASSERT( !m_index_channel, "second index channel!" ); -
trunk/nv/interface/vertex.hh
r295 r302 31 31 SLOT_MAX_STORE = 8, 32 32 }; 33 34 enum buffer_hint 35 { 36 STATIC_DRAW, 37 STREAM_DRAW, 38 DYNAMIC_DRAW 39 }; 40 41 enum buffer_type 42 { 43 VERTEX_BUFFER, 44 INDEX_BUFFER, 45 }; 46 33 47 34 48 namespace detail -
trunk/nv/interface/vertex_buffer.hh
r299 r302 21 21 namespace nv 22 22 { 23 class vertex_array;24 25 enum buffer_hint26 {27 STATIC_DRAW,28 STREAM_DRAW,29 DYNAMIC_DRAW30 };31 32 class buffer33 {34 public:35 buffer( buffer_hint hint, size_t size ) { m_size = size; m_hint = hint; }36 size_t get_size() const { return m_size; }37 buffer_hint get_hint() const { return m_hint; }38 virtual ~buffer() {}39 protected:40 size_t m_size;41 buffer_hint m_hint;42 };43 44 class vertex_buffer : public buffer45 {46 public:47 vertex_buffer( buffer_hint hint, size_t size ) : buffer( hint, size ) {}48 };49 50 class index_buffer : public buffer51 {52 public:53 index_buffer( buffer_hint hint, size_t size ) : buffer( hint, size ) {}54 };55 56 class vertex_buffer_attribute57 {58 public:59 vertex_buffer_attribute( vertex_buffer* buffer, datatype datatype, size_t components, size_t offset = 0, size_t stride = 0, bool owner = true )60 : m_buffer( buffer ), m_datatype( datatype ), m_components( components ), m_offset( offset ), m_stride( stride ), m_owner( owner ) {}61 62 vertex_buffer* get_buffer() const { return m_buffer; }63 void set_buffer( vertex_buffer* b, bool owner ) { if (m_owner) delete m_buffer; m_buffer = b; m_owner = owner; }64 datatype get_datatype() const { return m_datatype; }65 size_t get_components() const { return m_components; }66 size_t get_offset() const { return m_offset; }67 size_t get_stride() const { return m_stride; }68 69 ~vertex_buffer_attribute()70 {71 if (m_owner)72 {73 delete m_buffer;74 }75 }76 protected:77 friend class vertex_array;78 79 vertex_buffer* m_buffer;80 datatype m_datatype;81 size_t m_components;82 size_t m_offset;83 size_t m_stride;84 bool m_owner;85 };86 87 typedef std::unordered_map< int, vertex_buffer_attribute* > vertex_buffer_attribute_map;88 89 class vertex_array90 {91 public:92 friend class context;93 friend class gl_context; // TODO: HACK, remove94 95 vertex_array() : m_map(), m_index( nullptr ), m_index_owner( false ), m_index_type(USHORT) {}96 void add_vertex_buffer( int location, vertex_buffer* buffer, datatype datatype, size_t components, size_t offset = 0, size_t stride = 0, bool owner = true )97 {98 auto p = new vertex_buffer_attribute( buffer, datatype, components, offset, stride, owner );99 m_map[ location ] = p;100 }101 void add_vertex_buffer( slot location, vertex_buffer* buffer, datatype datatype, size_t components, size_t offset = 0, size_t stride = 0, bool owner = true )102 {103 add_vertex_buffer( (int)location, buffer, datatype, components, offset, stride, owner );104 }105 106 void add_vertex_buffers( vertex_buffer* buffer, const mesh_raw_channel* channel )107 {108 for ( uint32 s = 0; s < channel->desc.count; ++s )109 {110 add_vertex_buffer( buffer, channel->desc.slots[s], channel->desc.size );111 }112 }113 114 void add_vertex_buffer( vertex_buffer* buffer, const vertex_descriptor_slot& slot, size_t vtx_size )115 {116 const datatype_info& info = get_datatype_info(slot.etype);117 add_vertex_buffer( slot.vslot, buffer, info.base , info.elements, slot.offset, vtx_size, false );118 }119 120 void update_vertex_buffer( int location, vertex_buffer* b, bool owner )121 {122 auto i = m_map.find( location );123 if ( i != m_map.end() )124 {125 i->second->set_buffer( b, owner );126 }127 }128 void update_vertex_buffer( int location, size_t offset )129 {130 auto i = m_map.find( location );131 if ( i != m_map.end() )132 {133 i->second->m_offset = offset;134 }135 }136 void update_vertex_buffer( slot location, vertex_buffer* b, bool owner )137 {update_vertex_buffer( (int)location, b, owner ); }138 void update_vertex_buffer( slot location, size_t offset ) {update_vertex_buffer( (int)location, offset ); }139 void set_index_buffer( index_buffer* buffer, datatype datatype, bool owner )140 {141 if (m_index && m_index_owner) delete m_index;142 m_index = buffer;143 m_index_owner = owner;144 m_index_type = datatype;145 }146 void set_index_buffer( index_buffer* buffer )147 {148 if (m_index && m_index_owner) delete m_index;149 m_index = buffer;150 }151 vertex_buffer* find_buffer( int location )152 {153 auto i = m_map.find( location );154 if ( i != m_map.end() )155 {156 return i->second->get_buffer();157 }158 return nullptr;159 }160 vertex_buffer* find_buffer( slot location ) { return find_buffer((int)location); }161 bool has_index_buffer() const { return m_index != nullptr; }162 datatype get_index_buffer_type() const { return m_index_type; }163 virtual ~vertex_array() {164 for ( vertex_buffer_attribute_map::iterator i = m_map.begin(); i != m_map.end(); ++i )165 {166 delete i->second;167 }168 if (m_index && m_index_owner) delete m_index;169 }170 protected:171 vertex_buffer_attribute_map m_map;172 index_buffer* m_index;173 bool m_index_owner;174 datatype m_index_type;175 };176 177 23 178 24 } // namespace nv -
trunk/src/formats/assimp_loader.cc
r294 r302 317 317 { 318 318 mesh_raw_channel* channel = meshes[m].get_raw_channels()[0]; 319 NV_ASSERT( !channel->is_index(), "index channel in release_merged!" );320 319 assimp_skinned_vtx* va = (assimp_skinned_vtx*)channel->data; 321 320 for ( unsigned v = 0; v < channel->count; ++v ) -
trunk/src/gfx/debug_draw.cc
r237 r302 29 29 30 30 nv::debug_data::debug_data( device* a_device ) 31 : m_device( a_device ), m_program( nullptr ), m_va( nullptr)31 : m_device( a_device ), m_program( nullptr ), m_va() 32 32 { 33 33 m_program = m_device->create_program( nv_debug_draw_vertex_shader, nv_debug_draw_fragment_shader ); … … 36 36 void nv::debug_data::update() 37 37 { 38 delete m_va;38 m_device->release( m_va ); 39 39 m_va = m_device->create_vertex_array( m_data, nv::STATIC_DRAW ); 40 40 } … … 75 75 nv::debug_data::~debug_data() 76 76 { 77 delete m_va;77 m_device->release( m_va ); 78 78 delete m_program; 79 79 } -
trunk/src/gfx/keyframed_mesh.cc
r299 r302 15 15 using namespace nv; 16 16 17 nv::keyframed_mesh::keyframed_mesh( con st mesh_data* a_data, const mesh_nodes_data* a_tag_map )17 nv::keyframed_mesh::keyframed_mesh( context* a_context, const mesh_data* a_data, const mesh_nodes_data* a_tag_map ) 18 18 : animated_mesh() 19 , m_context( a_context ) 19 20 , m_mesh_data( a_data ) 20 21 , m_tag_map( a_tag_map ) … … 36 37 } 37 38 m_frame_count = m_vchannel->count / m_vertex_count; 39 m_pbuffer = buffer(); 38 40 } 39 41 … … 103 105 nv::keyframed_mesh::~keyframed_mesh() 104 106 { 105 delete m_va;107 m_context->get_device()->release( m_va ); 106 108 } 107 109 … … 122 124 123 125 nv::keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, const mesh_data* a_data, const mesh_nodes_data* a_tag_map ) 124 : keyframed_mesh( a_ data, a_tag_map )126 : keyframed_mesh( a_context, a_data, a_tag_map ) 125 127 , m_loc_next_position( -1 ) 126 128 , m_loc_next_normal( -1 ) … … 129 131 , m_gpu_next_frame( 0xFFFFFFFF ) 130 132 { 131 m_va = a_context->get_device()->create_vertex_array( a_data, STATIC_DRAW ); 133 m_va = a_context->get_device()->create_vertex_array( a_data, STATIC_DRAW ); 134 m_pbuffer = a_context->get_device()->find_buffer( m_va, slot::POSITION ); 132 135 } 133 136 … … 137 140 if ( m_loc_next_position == -1 ) return; 138 141 animated_mesh::update( ms ); 139 142 device* dev = m_context->get_device(); 140 143 if ( m_gpu_last_frame != m_last_frame ) 141 144 { 142 m_va->update_vertex_buffer( slot::POSITION, m_last_frame * m_vertex_count * m_vsize ); 143 m_va->update_vertex_buffer( slot::NORMAL, m_last_frame * m_vertex_count * m_vsize + sizeof( vec3 ) ); 145 uint32 base_offset = m_last_frame * m_vertex_count * m_vsize; 146 dev->update_attribute_offset( m_va, slot::POSITION, base_offset ); 147 dev->update_attribute_offset( m_va, slot::NORMAL, base_offset + sizeof( vec3 ) ); 144 148 if ( m_has_tangent && m_loc_next_tangent != -1 ) 145 149 { 146 m_va->update_vertex_buffer( slot::TANGENT, m_last_frame * m_vertex_count * m_vsize+ 2*sizeof( vec3 ) );150 dev->update_attribute_offset( m_va, slot::TANGENT, base_offset + 2*sizeof( vec3 ) ); 147 151 } 148 152 m_gpu_last_frame = m_last_frame; … … 150 154 if ( m_loc_next_position != -1 && m_gpu_next_frame != m_next_frame ) 151 155 { 152 m_va->update_vertex_buffer( m_loc_next_position, m_next_frame * m_vertex_count * m_vsize ); 153 m_va->update_vertex_buffer( m_loc_next_normal, m_next_frame * m_vertex_count * m_vsize + sizeof( vec3 ) ); 154 m_va->update_vertex_buffer( m_loc_next_tangent, m_next_frame * m_vertex_count * m_vsize + 2*sizeof( vec3 ) ); 156 uint32 base_offset = m_next_frame * m_vertex_count * m_vsize; 157 dev->update_attribute_offset( m_va, (slot)m_loc_next_position, base_offset ); 158 dev->update_attribute_offset( m_va, (slot)m_loc_next_normal, base_offset + sizeof( vec3 ) ); 159 if ( m_has_tangent && m_loc_next_tangent != -1 ) 160 { 161 dev->update_attribute_offset( m_va, (slot)m_loc_next_tangent, base_offset + 2*sizeof( vec3 ) ); 162 } 155 163 m_gpu_next_frame = m_next_frame; 156 164 } … … 166 174 m_loc_next_tangent = a_program->get_attribute( "nv_next_tangent" )->get_location(); 167 175 168 vertex_buffer* vb = m_va->find_buffer( slot::POSITION);169 m_va->add_vertex_buffer( m_loc_next_position, vb, FLOAT, 3, 0, m_vsize, false );170 m_va->add_vertex_buffer( m_loc_next_normal, vb, FLOAT, 3, sizeof( vec3 ), m_vsize, false );176 device* dev = m_context->get_device(); 177 dev->add_vertex_buffer( m_va, (slot)m_loc_next_position, m_pbuffer, FLOAT, 3, 0, m_vsize, false ); 178 dev->add_vertex_buffer( m_va, (slot)m_loc_next_normal, m_pbuffer, FLOAT, 3, sizeof( vec3 ), m_vsize, false ); 171 179 if ( m_has_tangent ) 172 m_va->add_vertex_buffer( m_loc_next_tangent, vb, FLOAT, 4, 2*sizeof( vec3 ), m_vsize, false );180 dev->add_vertex_buffer( m_va, (slot)m_loc_next_tangent, m_pbuffer, FLOAT, 4, 2*sizeof( vec3 ), m_vsize, false ); 173 181 } 174 182 keyframed_mesh::update( a_program ); … … 176 184 177 185 nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, const mesh_data* a_data, const mesh_nodes_data* a_tag_map ) 178 : keyframed_mesh( a_ data, a_tag_map )179 , m_context( a_context ) 180 { 181 m_ va = m_context->get_device()->create_vertex_array();182 m_ vb = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * m_vsize, (void*)m_vchannel->data);183 m_va->add_vertex_buffers( m_vb, m_vchannel ); 184 185 nv::vertex_buffer* vb = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * sizeof( nv::vec2 ), (void*)m_mesh_data->get_channel<vertex_t>()->data);186 m_va->add_vertex_buffers( vb, m_mesh_data->get_channel<vertex_t>() ); 187 188 nv::index_buffer* ib = m_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_mesh_data->get_index_channel()->size(), (void*)m_mesh_data->get_index_channel()->data ); 189 m_ va->set_index_buffer(ib, m_mesh_data->get_index_channel()->desc.slots[0].etype, true );186 : keyframed_mesh( a_context, a_data, a_tag_map ) 187 { 188 m_va = m_context->get_device()->create_vertex_array(); 189 m_pbuffer = m_context->get_device()->create_buffer( VERTEX_BUFFER, STATIC_DRAW, m_vertex_count * m_vsize, (void*)m_vchannel->data ); 190 m_context->get_device()->add_vertex_buffers( m_va, m_pbuffer, m_vchannel ); 191 192 buffer vb = m_context->get_device()->create_buffer( VERTEX_BUFFER, STATIC_DRAW, m_vertex_count * sizeof( vec2 ), (void*)m_mesh_data->get_channel<vertex_t>()->data ); 193 m_context->get_device()->add_vertex_buffers( m_va, vb, m_mesh_data->get_channel<vertex_t>() ); 194 195 buffer ib = m_context->get_device()->create_buffer( INDEX_BUFFER, STATIC_DRAW, m_mesh_data->get_index_channel()->size(), (void*)m_mesh_data->get_index_channel()->data ); 196 197 m_context->get_device()->set_index_buffer( m_va, ib, m_mesh_data->get_index_channel()->desc.slots[0].etype, true ); 190 198 191 199 m_data = new uint8[ m_vertex_count * m_vsize ]; … … 224 232 } 225 233 226 m_context->update( m_ vb, m_data, 0, m_vertex_count * m_vsize );234 m_context->update( m_pbuffer, m_data, 0, m_vertex_count * m_vsize ); 227 235 } 228 236 -
trunk/src/gfx/mesh_creator.cc
r295 r302 430 430 { 431 431 mesh_raw_channel* old = m_data->m_channels[c]; 432 size_t frame_count = ( old-> is_index()? 1 : old->count / size );432 size_t frame_count = ( old->get_buffer_type() == INDEX_BUFFER ? 1 : old->count / size ); 433 433 m_data->m_channels[c] = append_channels( old, other->m_channels[c], frame_count ); 434 434 NV_ASSERT( m_data->m_channels[c], "Merge problem!" ); 435 if ( old-> is_index())435 if ( old->get_buffer_type() == INDEX_BUFFER ) 436 436 { 437 437 switch ( old->desc.slots[0].etype ) -
trunk/src/gfx/skeletal_mesh.cc
r299 r302 12 12 13 13 nv::skeletal_mesh_cpu::skeletal_mesh_cpu( context* a_context, const mesh_data* a_mesh_data, const mesh_nodes_data* bones ) 14 : skeletal_mesh() 15 , m_context( a_context ) 14 : skeletal_mesh( a_context ) 16 15 , m_data( a_mesh_data ) 17 16 { … … 27 26 m_vtx_data = a_mesh_data->get_channel_data<md5_vtx_pntiw>(); 28 27 m_indices = a_mesh_data->get_count(); 29 m_va = a_context->get_device()->create_vertex_array( a_mesh_data, nv::STREAM_DRAW ); 28 m_va = a_context->get_device()->create_vertex_array( a_mesh_data, 29 STREAM_DRAW ); 30 m_pbuffer = a_context->get_device()->find_buffer( m_va, slot::POSITION ); 30 31 } 31 32 … … 63 64 } 64 65 65 vertex_buffer* vb = m_va->find_buffer( nv::slot::POSITION ); 66 m_context->update( vb, m_pntdata.data(), 0, m_pntdata.raw_size() ); 66 m_context->update( m_pbuffer, m_pntdata.data(), 0, m_pntdata.raw_size() ); 67 67 } 68 68 } … … 80 80 skeleton[i] = m_node_data->get_node(i)->data->get_transform( frame_num ); 81 81 } 82 }83 84 85 86 nv::skeletal_mesh_cpu::~skeletal_mesh_cpu()87 {88 delete m_va;89 82 } 90 83 … … 200 193 201 194 nv::skeletal_mesh_gpu::skeletal_mesh_gpu( context* a_context, const mesh_data* a_mesh, const mesh_nodes_data* a_bone_data ) 202 : skeletal_mesh( ), m_bone_data( a_bone_data ), m_transform( nullptr )195 : skeletal_mesh( a_context ), m_bone_data( a_bone_data ), m_transform( nullptr ) 203 196 { 204 197 m_va = a_context->get_device()->create_vertex_array( a_mesh, nv::STATIC_DRAW ); -
trunk/src/gl/gl_context.cc
r301 r302 10 10 #include "nv/gl/gl_device.hh" 11 11 #include "nv/gl/gl_program.hh" 12 #include "nv/gl/gl_vertex_buffer.hh"13 12 14 13 using namespace nv; … … 31 30 } 32 31 33 void nv::gl_context::bind( vertex_buffer*b )34 { 35 GLuint id = static_cast< gl_vertex_buffer* >( b )->glid;36 glBindBuffer( GL_ARRAY_BUFFER, id );37 } 38 39 void nv::gl_context::bind( index_buffer* b ) 40 { 41 GLuint id = static_cast< gl_index_buffer* >( b )->glid; 42 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, id ); 43 } 44 45 void nv::gl_context::bind( vertex_array* va)46 {47 for ( vertex_buffer_attribute_map::iterator i = va->m_map.begin(); i != va->m_map.end(); ++i )48 {49 uint32 location = static_cast<uint32>( i->first );50 vertex_buffer_attribute* va = i->second;51 vertex_buffer* vb = va->get_buffer();52 glEnableVertexAttribArray( location);53 bind( vb );54 glVertexAttribPointer(55 location,56 static_cast<GLint>( va->get_components() ),57 nv::datatype_to_gl_enum( va->get_datatype() ),58 GL_FALSE,59 static_cast<GLsizei>( va->get_stride() ),60 (void*)va->get_offset()61 );62 unbind( vb );63 } 64 65 if ( va->m_index )66 {67 bind( va->m_index );32 void nv::gl_context::bind( buffer b ) 33 { 34 const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) ); 35 if ( info ) 36 { 37 glBindBuffer( buffer_type_to_enum( info->type ), info->glid ); 38 } 39 } 40 41 void nv::gl_context::bind( vertex_array va ) 42 { 43 vertex_array_info* info = get_vertex_array_info( va ); 44 if ( info ) 45 { 46 for ( uint32 i = 0; i < info->count; ++i ) 47 { 48 const vertex_buffer_attribute& vba = info->attr[i]; 49 uint32 location = static_cast<uint32>( vba.location ); 50 glEnableVertexAttribArray( location ); 51 bind( vba.vbuffer ); 52 glVertexAttribPointer( 53 location, 54 static_cast<GLint>( vba.components ), 55 nv::datatype_to_gl_enum( vba.dtype ), 56 GL_FALSE, 57 static_cast<GLsizei>( vba.stride ), 58 (void*)vba.offset 59 ); 60 unbind( vba.vbuffer ); 61 } 62 63 if ( info->index.is_valid() ) 64 { 65 bind( info->index ); 66 } 68 67 } 69 68 } … … 74 73 } 75 74 76 void nv::gl_context::unbind( vertex_buffer* ) 77 { 78 glBindBuffer( GL_ARRAY_BUFFER, 0 ); 79 } 80 81 void nv::gl_context::unbind( index_buffer* ) 82 { 83 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); 84 } 85 86 void nv::gl_context::unbind( vertex_array* va ) 87 { 88 if ( va->m_index ) 89 { 90 unbind( va->m_index ); 91 } 92 93 for ( vertex_buffer_attribute_map::iterator i = va->m_map.begin(); i != va->m_map.end(); ++i ) 94 { 95 glDisableVertexAttribArray( static_cast<uint32>( i->first ) ); 75 void nv::gl_context::unbind( buffer b ) 76 { 77 const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) ); 78 if ( info ) 79 { 80 glBindBuffer( buffer_type_to_enum( info->type ), 0 ); 81 } 82 } 83 84 void nv::gl_context::unbind( vertex_array va ) 85 { 86 vertex_array_info* info = get_vertex_array_info( va ); 87 if ( info ) 88 { 89 if ( info->index.is_valid() ) 90 { 91 unbind( info->index ); 92 } 93 94 for ( uint32 i = 0; i < info->count; ++i ) 95 { 96 glDisableVertexAttribArray( static_cast<uint32>( info->attr[i].location ) ); 97 } 96 98 } 97 99 } … … 110 112 } 111 113 112 void gl_context::update( vertex_buffer* b, const void* data, size_t offset, size_t size ) 113 { 114 bind( b ); 115 glBufferSubData( GL_ARRAY_BUFFER, (GLintptr)offset, (GLsizeiptr)size, data ); 116 } 117 118 void gl_context::update( index_buffer* b, const void* data, size_t offset, size_t size ) 119 { 120 bind( b ); 121 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, (GLintptr)offset, (GLsizeiptr)size, data ); 114 void gl_context::update( buffer b, const void* data, size_t offset, size_t size ) 115 { 116 const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) ); 117 if ( info ) 118 { 119 bind( b ); 120 glBufferSubData( buffer_type_to_enum( info->type ), (GLintptr)offset, (GLsizeiptr)size, data ); 121 } 122 122 } 123 123 … … 483 483 } 484 484 485 void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array *va, size_t count )485 void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array va, size_t count ) 486 486 { 487 487 apply_render_state( rs ); 488 if ( count > 0 ) 488 vertex_array_info* info = get_vertex_array_info( va ); 489 if ( count > 0 && info ) 489 490 { 490 491 bind( p ); 491 492 bind( va ); 492 if ( va->has_index_buffer() )493 { 494 glDrawElements( primitive_to_enum(prim), static_cast<GLsizei>( count ), datatype_to_gl_enum( va->get_index_buffer_type()), 0 );493 if ( info->index.is_valid() ) 494 { 495 glDrawElements( primitive_to_enum(prim), static_cast<GLsizei>( count ), datatype_to_gl_enum( info->index_type ), 0 ); 495 496 } 496 497 else -
trunk/src/gl/gl_device.cc
r301 r302 7 7 #include "nv/gl/gl_window.hh" 8 8 #include "nv/gl/gl_program.hh" 9 #include "nv/gl/gl_vertex_buffer.hh"10 9 #include "nv/logging.hh" 11 10 #include "nv/lib/sdl.hh" … … 56 55 } 57 56 58 vertex_buffer* gl_device::create_vertex_buffer( buffer_hint hint, size_t size, const void* source /*= nullptr */ )59 {60 return new gl_vertex_buffer( hint, size, source );61 }62 63 index_buffer* gl_device::create_index_buffer( buffer_hint hint, size_t size, const void* source /*= nullptr */ )64 {65 return new gl_index_buffer( hint, size, source );66 }67 68 vertex_array* gl_device::create_vertex_array()69 {70 return new vertex_array();71 }72 73 57 // this is a temporary function that will be removed once we find a way to 74 58 // pass binary file data around … … 101 85 gl_device::~gl_device() 102 86 { 103 // TODO: better use release_texture 104 for ( auto& t : m_textures ) glDeleteTextures( 1, &t.glid ); 87 while ( m_textures.size() > 0 ) 88 release( m_textures.get_handle(0) ); 89 while ( m_buffers.size() > 0 ) 90 release( m_buffers.get_handle(0) ); 105 91 106 92 SDL_Quit(); … … 142 128 } 143 129 144 void nv::gl_device::release _texture( texture t )130 void nv::gl_device::release( texture t ) 145 131 { 146 132 gl_texture_info* info = m_textures.get( t ); 147 133 if ( info ) 148 134 { 149 glDeleteTextures( 1, &(info->glid) ); 135 if ( info->glid != 0 ) 136 { 137 glDeleteTextures( 1, &(info->glid) ); 138 } 150 139 m_textures.destroy( t ); 140 } 141 } 142 143 void nv::gl_device::release( buffer b ) 144 { 145 gl_buffer_info* info = m_buffers.get( b ); 146 if ( info ) 147 { 148 if ( info->glid != 0 ) 149 { 150 glDeleteBuffers( 1, &(info->glid) ); 151 } 152 m_buffers.destroy( b ); 151 153 } 152 154 } … … 157 159 } 158 160 161 nv::buffer nv::gl_device::create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source /*= nullptr */ ) 162 { 163 unsigned glid = 0; 164 unsigned glenum = buffer_type_to_enum( type ); 165 glGenBuffers( 1, &glid ); 166 167 glBindBuffer( glenum, glid ); 168 glBufferData( glenum, (GLsizeiptr)size, source, buffer_hint_to_enum( hint ) ); 169 glBindBuffer( glenum, 0 ); 170 171 buffer result = m_buffers.create(); 172 gl_buffer_info* info = m_buffers.get( result ); 173 info->type = type; 174 info->hint = hint; 175 info->size = size; 176 info->glid = glid; 177 return result; 178 } 179 180 const buffer_info* nv::gl_device::get_buffer_info( buffer t ) 181 { 182 return m_buffers.get( t ); 183 } -
trunk/src/gl/gl_enum.cc
r292 r302 144 144 case DYNAMIC_DRAW : return GL_DYNAMIC_DRAW; 145 145 NV_RETURN_COVERED_DEFAULT( 0 ); 146 } 147 } 148 149 unsigned int nv::buffer_type_to_enum( buffer_type type ) 150 { 151 switch( type ) 152 { 153 case VERTEX_BUFFER : return GL_ARRAY_BUFFER; 154 case INDEX_BUFFER : return GL_ELEMENT_ARRAY_BUFFER; 155 NV_RETURN_COVERED_DEFAULT( 0 ); 146 156 } 147 157 } -
trunk/src/gui/gui_renderer.cc
r301 r302 21 21 { 22 22 set_color( color ); 23 vtx[0]. coord= coorda;24 vtx[1]. coord= nv::ivec2( coorda.x, coordb.y );25 vtx[2]. coord= coordb;26 vtx[3]. coord= coordb;27 vtx[4]. coord= nv::ivec2( coordb.x, coorda.y );28 vtx[5]. coord= coorda;23 vtx[0].position = coorda; 24 vtx[1].position = nv::ivec2( coorda.x, coordb.y ); 25 vtx[2].position = coordb; 26 vtx[3].position = coordb; 27 vtx[4].position = nv::ivec2( coordb.x, coorda.y ); 28 vtx[5].position = coorda; 29 29 } 30 30 gui_quad( const nv::ivec2& coorda, const nv::ivec2& coordb, const nv::vec4& color, const nv::vec2& tcoorda, const nv::vec2& tcoordb ) 31 31 { 32 32 set_color( color ); 33 vtx[0]. coord= coorda;34 vtx[1]. coord= nv::ivec2( coorda.x, coordb.y );35 vtx[2]. coord= coordb;36 vtx[3]. coord= coordb;37 vtx[4]. coord= nv::ivec2( coordb.x, coorda.y );38 vtx[5]. coord= coorda;39 vtx[0].t coord = tcoorda;40 vtx[1].t coord = nv::vec2( tcoorda.x, tcoordb.y );41 vtx[2].t coord = tcoordb;42 vtx[3].t coord = tcoordb;43 vtx[4].t coord = nv::vec2( tcoordb.x, tcoorda.y );44 vtx[5].t coord = tcoorda;33 vtx[0].position = coorda; 34 vtx[1].position = nv::ivec2( coorda.x, coordb.y ); 35 vtx[2].position = coordb; 36 vtx[3].position = coordb; 37 vtx[4].position = nv::ivec2( coordb.x, coorda.y ); 38 vtx[5].position = coorda; 39 vtx[0].texcoord = tcoorda; 40 vtx[1].texcoord = nv::vec2( tcoorda.x, tcoordb.y ); 41 vtx[2].texcoord = tcoordb; 42 vtx[3].texcoord = tcoordb; 43 vtx[4].texcoord = nv::vec2( tcoordb.x, tcoorda.y ); 44 vtx[5].texcoord = tcoorda; 45 45 } 46 46 gui_quad( const nv::ivec2& coorda, const nv::ivec2& coordb, const nv::ivec2& coordc, const nv::ivec2& coordd, const nv::vec4& color ) 47 47 { 48 48 set_color( color ); 49 vtx[0]. coord= coorda;50 vtx[1]. coord= coordb;51 vtx[2]. coord= coordc;52 vtx[3]. coord= coordc;53 vtx[4]. coord= coordd;54 vtx[5]. coord= coorda;49 vtx[0].position = coorda; 50 vtx[1].position = coordb; 51 vtx[2].position = coordc; 52 vtx[3].position = coordc; 53 vtx[4].position = coordd; 54 vtx[5].position = coorda; 55 55 } 56 56 inline void set_color( const nv::vec4& color ) … … 66 66 public: 67 67 screen_render_data( context* actx, size_t initial_size ) 68 : buffer( actx, nv::DYNAMIC_DRAW, initial_size ), varray( nullptr), shader(nullptr)68 : buffer( actx, VERTEX_BUFFER, DYNAMIC_DRAW, initial_size ), ctx( actx ), varray(), shader(nullptr) 69 69 { 70 70 … … 73 73 { 74 74 delete shader; 75 delete varray;75 ctx->get_device()->release( varray ); 76 76 } 77 77 78 78 nv::sliced_buffer<gui_quad> buffer; 79 nv::context* ctx; 79 80 nv::texture tex; 80 nv::vertex_array *varray;81 nv::vertex_array varray; 81 82 nv::program* shader; 82 83 }; … … 110 111 m_render_data = sr; 111 112 // ** EXTREMELY TEMPORARY! 112 sr->varray = m_window->get_device()->create_vertex_array();113 113 sr->shader = m_window->get_device()->create_program( nv::slurp( shader_path + ".vert" ), nv::slurp( shader_path + ".frag" ) ); 114 114 m_scene_state.get_camera().set_ortho( 0.0f, float( m_window->get_width() ), float( m_window->get_height() ), 0.0f ); 115 115 116 vertex_buffer* vb = (vertex_buffer*)sr->buffer.get_buffer(); 117 sr->varray->add_vertex_buffer( slot::POSITION, vb, nv::INT, 2, 0, sizeof( vertex ), false ); 118 sr->varray->add_vertex_buffer( slot::TEXCOORD, vb, nv::FLOAT, 2, offset_of( &vertex::tcoord ), sizeof( vertex ), false ); 119 sr->varray->add_vertex_buffer( slot::COLOR, vb, nv::FLOAT, 4, offset_of( &vertex::color ), sizeof( vertex ), false ); 116 sr->varray = m_window->get_device()->create_vertex_array(); 117 buffer vb = sr->buffer.get_buffer(); 118 m_window->get_device()->add_vertex_buffers< vertex >( sr->varray, vb ); 120 119 121 120 nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::CLAMP_TO_EDGE ); … … 267 266 if ( sr->buffer.commit() ) 268 267 { 269 nv::vertex_buffer* vb = (nv::vertex_buffer*)sr->buffer.get_buffer(); 270 sr->varray->update_vertex_buffer( nv::slot::POSITION, vb, false ); 271 sr->varray->update_vertex_buffer( nv::slot::TEXCOORD, vb, false ); 272 sr->varray->update_vertex_buffer( nv::slot::COLOR, vb, false ); 268 buffer vb = sr->buffer.get_buffer(); 269 m_context->get_device()->replace_vertex_buffer( sr->varray, vb, false ); 273 270 } 274 271 m_context->bind( sr->tex, TEX_DIFFUSE ); … … 284 281 if ( m_render_data ) 285 282 { 286 m_context->get_device()->release _texture( ((screen_render_data*)m_render_data)->tex );283 m_context->get_device()->release( ((screen_render_data*)m_render_data)->tex ); 287 284 delete m_render_data; 288 285 }
Note: See TracChangeset
for help on using the changeset viewer.