00001 // 00002 // $Id: buffer_pool.cpp 2 2008-03-01 20:58:50Z kulibali $ 00003 // 00004 // Copyright (c) 2008, The Periapsis Project. All rights reserved. 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are 00008 // met: 00009 // 00010 // * Redistributions of source code must retain the above copyright notice, 00011 // this list of conditions and the following disclaimer. 00012 // 00013 // * Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // * Neither the name of the The Periapsis Project nor the names of its 00018 // contributors may be used to endorse or promote products derived from 00019 // this software without specific prior written permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00022 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00023 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00024 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00025 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00026 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00027 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 00034 #include "platform/buffer_pool.hpp" 00035 00036 00037 namespace gsgl 00038 { 00039 00040 using namespace data; 00041 00042 namespace platform 00043 { 00044 00045 buffer_pool::buffer_pool(const vbuffer::update_mode & mode, const gsgl::index_t & num_objects_per_bucket, const gsgl::index_t & num_vertices_per_object, const gsgl::index_t & num_indices_per_object) 00046 : mode(mode), num_objects_per_bucket(num_objects_per_bucket), 00047 num_vertices_per_object(num_vertices_per_object), num_indices_per_object(num_indices_per_object) 00048 { 00049 } // buffer_pool::buffer_pool() 00050 00051 00052 buffer_pool::~buffer_pool() 00053 { 00054 for (simple_array<bucket *>::iterator i = buckets.iter(); i.is_valid(); ++i) 00055 delete *i; 00056 } // buffer_pool::~buffer_pool() 00057 00058 00059 buffer_pool::object_record buffer_pool::allocate_object() 00060 { 00061 buffer_pool::object_record result; 00062 00063 if (available_objects.size()) 00064 { 00065 result = available_objects.top(); 00066 available_objects.pop(); 00067 } 00068 else 00069 { 00070 // allocate a new bucket, return the first object, and free the rest (go backwards so earlier objects will appear sooner) 00071 bucket *new_bucket = new bucket(mode, num_objects_per_bucket * num_vertices_per_object, num_objects_per_bucket * num_indices_per_object); 00072 00073 for (int i = num_objects_per_bucket-1; i >= 0; --i) 00074 { 00075 object_record rec; 00076 00077 rec.parent = new_bucket; 00078 rec.pos_in_vertices = i * num_vertices_per_object; 00079 rec.pos_in_indices = i * num_indices_per_object; 00080 00081 if (i == 0) 00082 result = rec; 00083 else 00084 free_object(rec); 00085 } 00086 } 00087 00088 return result; 00089 } // buffer_pool::allocate_object() 00090 00091 00092 void buffer_pool::free_object(const object_record & obj) 00093 { 00094 available_objects.push(obj); 00095 } // buffer_pool::free_object() 00096 00097 00098 void buffer_pool::load() 00099 { 00100 for (simple_array<bucket *>::iterator i = buckets.iter(); i.is_valid(); ++i) 00101 { 00102 (*i)->vertices.load(); 00103 (*i)->indices.load(); 00104 } 00105 } // buffer_pool::load() 00106 00107 00108 void buffer_pool::unload() 00109 { 00110 for (simple_array<bucket *>::iterator i = buckets.iter(); i.is_valid(); ++i) 00111 { 00112 (*i)->vertices.unload(); 00113 (*i)->vertices.unload(); 00114 } 00115 } // buffer_pool::unload() 00116 00117 00118 } // namespace platform 00119 00120 } // namespace gsgl