csplugincommon/rendermanager/svarrayholder.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2007-2008 by Marten Svanfeldt 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 00019 #ifndef __CS_CSPLUGINCOMMON_RENDERMANAGER_SVARRAYHOLDER_H__ 00020 #define __CS_CSPLUGINCOMMON_RENDERMANAGER_SVARRAYHOLDER_H__ 00021 00025 class csShaderVariable; 00026 00027 namespace CS 00028 { 00029 namespace RenderManager 00030 { 00031 00043 class SVArrayHolder 00044 { 00045 public: 00050 SVArrayHolder (size_t numLayers = 1, size_t numSVNames = 0, size_t numSets = 0) 00051 : numLayers (numLayers), numSVNames (numSVNames), numSets (numSets), svArray (0), 00052 memAllocSetUp (false) 00053 { 00054 if (numSVNames && numSets && numLayers) 00055 Setup (numLayers, numSVNames, numSets); 00056 } 00057 00058 SVArrayHolder (const SVArrayHolder& other) 00059 : svArray (0), memAllocSetUp (false) 00060 { 00061 *this = other; 00062 } 00063 00064 ~SVArrayHolder () 00065 { 00066 if (memAllocSetUp) GetMemAlloc().~csMemoryPool(); 00067 } 00068 00069 SVArrayHolder& operator= (const SVArrayHolder& other) 00070 { 00071 if (memAllocSetUp) GetMemAlloc().~csMemoryPool(); 00072 00073 numLayers = other.numLayers; 00074 numSVNames = other.numSVNames; 00075 numSets = other.numSets; 00076 00077 const size_t sliceSVs = numSVNames*numSets; 00078 const size_t sliceSize = sizeof(csShaderVariable*)*sliceSVs; 00079 #include "csutil/custom_new_disable.h" 00080 new (&memAlloc) csMemoryPool (sliceSize * 4); 00081 #include "csutil/custom_new_enable.h" 00082 memAllocSetUp = true; 00083 00084 csShaderVariable** superSlice = reinterpret_cast<csShaderVariable**> ( 00085 GetMemAlloc().Alloc (numLayers * sliceSize)); 00086 00087 for (size_t l = 0; l < numLayers; l++) 00088 { 00089 csShaderVariable** slice = superSlice + l * sliceSVs; 00090 svArray.Push (slice); 00091 memcpy (slice, other.svArray[l], sliceSize); 00092 } 00093 svArray.ShrinkBestFit(); 00094 00095 return *this; 00096 } 00097 00102 void Setup (size_t numLayers, size_t numSVNames, size_t numSets) 00103 { 00104 this->numLayers = numLayers; 00105 this->numSVNames = numSVNames; 00106 this->numSets = numSets; 00107 00108 const size_t sliceSVs = numSVNames*numSets; 00109 const size_t sliceSize = sizeof(csShaderVariable*)*sliceSVs; 00110 #ifndef DOXYGEN_RUN 00111 #include "csutil/custom_new_disable.h" 00112 #endif 00113 new (&memAlloc) csMemoryPool (sliceSize * 4); 00114 #ifndef DOXYGEN_RUN 00115 #include "csutil/custom_new_enable.h" 00116 #endif 00117 memAllocSetUp = true; 00118 00119 csShaderVariable** superSlice = reinterpret_cast<csShaderVariable**> ( 00120 GetMemAlloc().Alloc (numLayers * sliceSize)); 00121 memset (superSlice, 0, numLayers * sliceSize); 00122 00123 for (size_t l = 0; l < numLayers; l++) 00124 { 00125 csShaderVariable** slice = superSlice + l * sliceSVs; 00126 svArray.Push (slice); 00127 } 00128 svArray.ShrinkBestFit(); 00129 00130 } 00131 00136 void SetupSVStack (csShaderVariableStack& stack, size_t layer, size_t set) 00137 { 00138 CS_ASSERT (layer < numLayers); 00139 CS_ASSERT (set < numSets); 00140 00141 stack.Setup (svArray[layer] + set*numSVNames, numSVNames); 00142 } 00143 00148 void ReplicateSet (size_t layer, size_t from, size_t start, size_t end = (size_t)-1) 00149 { 00150 if (numSets == 1) 00151 return; 00152 00153 if (end == (size_t)-1) 00154 end = numSets-1; 00155 00156 CS_ASSERT (layer < numLayers); 00157 CS_ASSERT (from < numSets && start < numSets && end < numSets); 00158 CS_ASSERT (from < start || from > end); 00159 00160 for (size_t i = start; i <= end; ++i) 00161 { 00162 memcpy (svArray[layer] + i*numSVNames, 00163 svArray[layer] + from*numSVNames, 00164 sizeof(csShaderVariable*)*numSVNames); 00165 } 00166 } 00167 00171 void ReplicateLayerZero () 00172 { 00173 if (numLayers == 1) 00174 return; 00175 00176 size_t layerSize = numSets*numSVNames; 00177 00178 for (size_t layer = 1; layer < numLayers; ++layer) 00179 { 00180 memcpy (svArray[layer], svArray[0], sizeof(csShaderVariable*)*layerSize); 00181 } 00182 } 00183 00187 void ReplicateLayer (size_t from, size_t to) 00188 { 00189 size_t layerSize = numSets*numSVNames; 00190 00191 memcpy (svArray[to], svArray[from], sizeof(csShaderVariable*)*layerSize); 00192 } 00193 00197 void InsertLayer (size_t after, size_t replicateFrom = 0) 00198 { 00199 const size_t sliceSize = sizeof(csShaderVariable*)*numSVNames*numSets; 00200 00201 csShaderVariable** slice = reinterpret_cast<csShaderVariable**> ( 00202 GetMemAlloc().Alloc (sliceSize)); 00203 svArray.Insert (after+1, slice); 00204 00205 memcpy (slice, svArray[replicateFrom], sliceSize); 00206 00207 numLayers++; 00208 } 00209 00211 size_t GetNumSVNames () const 00212 { 00213 return numSVNames; 00214 } 00215 00217 size_t GetNumLayers () const 00218 { 00219 return numLayers; 00220 } 00221 00222 private: 00223 size_t numLayers; 00224 size_t numSVNames; 00225 size_t numSets; 00226 csArray<csShaderVariable**> svArray; 00227 00228 csMemoryPool& GetMemAlloc() 00229 { 00230 union 00231 { 00232 uint* a; 00233 csMemoryPool* b; 00234 } pun; 00235 pun.a = memAlloc; 00236 return *(pun.b); 00237 } 00238 00239 uint memAlloc[(sizeof(csMemoryPool) + sizeof (uint) - 1) / sizeof (uint)]; 00240 bool memAllocSetUp; 00241 }; 00242 00243 00244 } 00245 } 00246 00247 #endif
Generated for Crystal Space 2.0 by doxygen 1.6.1