Showing the changes to interface/src/VoxelSystem.cpp (1 of 2 files). Show all
Unified · Split
| @@ -205,17 +205,86 @@ void VoxelSystem::cleanupRemovedVoxels() { · 204 unmodified lines | |||
| 205 | } | 205 | } |
| 206 | } | 206 | } |
| 207 | 207 | ||
| 208 | void VoxelSystem::copyWrittenDataToReadArraysFullVBOs() { | ||
| 209 | // lock on the buffer write lock so we can't modify the data when the GPU is reading it | ||
| 210 | pthread_mutex_lock(&_bufferWriteLock); | ||
| 211 | int bytesOfVertices = (_voxelsInWriteArrays * VERTEX_POINTS_PER_VOXEL) * sizeof(GLfloat); | ||
| 212 | int bytesOfColors = (_voxelsInWriteArrays * VERTEX_POINTS_PER_VOXEL) * sizeof(GLubyte); | ||
| 213 | memcpy(_readVerticesArray, _writeVerticesArray, bytesOfVertices); | ||
| 214 | memcpy(_readColorsArray, _writeColorsArray, bytesOfColors ); | ||
| 215 | _voxelsInReadArrays = _voxelsInWriteArrays; | ||
| 216 | pthread_mutex_unlock(&_bufferWriteLock); | ||
| 217 | } | ||
| 218 | |||
| 219 | void VoxelSystem::copyWrittenDataToReadArraysPartialVBOs() { | ||
| 220 | // lock on the buffer write lock so we can't modify the data when the GPU is reading it | ||
| 221 | pthread_mutex_lock(&_bufferWriteLock); | ||
| 222 | |||
| 223 | glBufferIndex segmentStart = 0; | ||
| 224 | glBufferIndex segmentEnd = 0; | ||
| 225 | bool inSegment = false; | ||
| 226 | for (glBufferIndex i = 0; i < _voxelsInWriteArrays; i++) { | ||
| 227 | bool thisVoxelDirty = _voxelDirtyArray[i]; | ||
| 228 | if (!inSegment) { | ||
| 229 | if (thisVoxelDirty) { | ||
| 230 | segmentStart = i; | ||
| 231 | inSegment = true; | ||
| 232 | } | ||
| 233 | } else { | ||
| 234 | if (!thisVoxelDirty) { | ||
| 235 | // If we got here because because this voxel is NOT dirty, so the last dirty voxel was the one before | ||
| 236 | // this one and so that's where the "segment" ends | ||
| 237 | segmentEnd = i - 1; | ||
| 238 | inSegment = false; | ||
| 239 | int segmentLength = (segmentEnd - segmentStart) + 1; | ||
| 240 | |||
| 241 | GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | ||
| 242 | GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | ||
| 243 | GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | ||
| 244 | GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | ||
| 245 | memcpy(readVerticesAt, writeVerticesAt, segmentSizeBytes); | ||
| 246 | |||
| 247 | segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | ||
| 248 | segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | ||
| 249 | GLubyte* readColorsAt = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | ||
| 250 | GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | ||
| 251 | memcpy(readColorsAt, writeColorsAt, segmentSizeBytes); | ||
| 252 | } | ||
| 253 | } | ||
| 254 | } | ||
| 255 | |||
| 256 | // if we got to the end of the array, and we're in an active dirty segment... | ||
| 257 | if (inSegment) { | ||
| 258 | segmentEnd = _voxelsInWriteArrays - 1; | ||
| 259 | int segmentLength = (segmentEnd - segmentStart) + 1; | ||
| 260 | |||
| 261 | GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | ||
| 262 | GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | ||
| 263 | GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | ||
| 264 | GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | ||
| 265 | memcpy(readVerticesAt, writeVerticesAt, segmentSizeBytes); | ||
| 266 | |||
| 267 | segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | ||
| 268 | segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | ||
| 269 | GLubyte* readColorsAt = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | ||
| 270 | GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | ||
| 271 | memcpy(readColorsAt, writeColorsAt, segmentSizeBytes); | ||
| 272 | } | ||
| 273 | |||
| 274 | // update our length | ||
| 275 | _voxelsInReadArrays = _voxelsInWriteArrays; | ||
| 276 | |||
| 277 | pthread_mutex_unlock(&_bufferWriteLock); | ||
| 278 | } | ||
| 279 | |||
| 208 | void VoxelSystem::copyWrittenDataToReadArrays() { | 280 | void VoxelSystem::copyWrittenDataToReadArrays() { |
| 209 | PerformanceWarning warn(_renderWarningsOn, "copyWrittenDataToReadArrays()"); // would like to include _voxelsInArrays, _voxelsUpdated | 281 | PerformanceWarning warn(_renderWarningsOn, "copyWrittenDataToReadArrays()"); |
| 210 | if (_voxelsDirty && _voxelsUpdated) { | 282 | if (_voxelsDirty && _voxelsUpdated) { |
| 211 | // lock on the buffer write lock so we can't modify the data when the GPU is reading it | 283 | if (_renderFullVBO) { |
| 212 | pthread_mutex_lock(&_bufferWriteLock); | 284 | copyWrittenDataToReadArraysFullVBOs(); |
| 213 | int bytesOfVertices = (_voxelsInWriteArrays * VERTEX_POINTS_PER_VOXEL) * sizeof(GLfloat); | 285 | } else { |
| 214 | int bytesOfColors = (_voxelsInWriteArrays * VERTEX_POINTS_PER_VOXEL) * sizeof(GLubyte); | 286 | copyWrittenDataToReadArraysPartialVBOs(); |
| 215 | memcpy(_readVerticesArray, _writeVerticesArray, bytesOfVertices); | 287 | } |
| 216 | memcpy(_readColorsArray, _writeColorsArray, bytesOfColors ); | ||
| 217 | _voxelsInReadArrays = _voxelsInWriteArrays; | ||
| 218 | pthread_mutex_unlock(&_bufferWriteLock); | ||
| 219 | } | 288 | } |
| 220 | } | 289 | } |
| 221 | 290 | ||
| @@ -493,11 +562,10 @@ void VoxelSystem::updateVBOs() { · 271 unmodified lines | |||
| 493 | }; | 562 | }; |
| 494 | PerformanceWarning warn(_renderWarningsOn, buffer); // would like to include _callsToTreesToArrays | 563 | PerformanceWarning warn(_renderWarningsOn, buffer); // would like to include _callsToTreesToArrays |
| 495 | if (_voxelsDirty) { | 564 | if (_voxelsDirty) { |
| 496 | // updatePartialVBOs() is not yet working. For now, ALWAYS call updateFullVBOs() | ||
| 497 | if (_renderFullVBO) { | 565 | if (_renderFullVBO) { |
| 498 | updateFullVBOs(); | 566 | updateFullVBOs(); |
| 499 | } else { | 567 | } else { |
| 500 | updatePartialVBOs(); // too many small segments? | 568 | updatePartialVBOs(); |
| 501 | } | 569 | } |
| 502 | _voxelsDirty = false; | 570 | _voxelsDirty = false; |
| 503 | } | 571 | } |