From 017181f0203b24b5bdc6ef4307f240daaf6bcfb9 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Fri, 9 Sep 2016 09:46:54 -0700 Subject: [PATCH] Remove magic numbers, ensure proper buffer size for page transfers --- libraries/gpu-gl/src/gpu/gl45/GL45Backend.h | 2 +- .../gpu-gl/src/gpu/gl45/GL45BackendTexture.cpp | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/libraries/gpu-gl/src/gpu/gl45/GL45Backend.h b/libraries/gpu-gl/src/gpu/gl45/GL45Backend.h index eab1aa07d8..db297e77fd 100644 --- a/libraries/gpu-gl/src/gpu/gl45/GL45Backend.h +++ b/libraries/gpu-gl/src/gpu/gl45/GL45Backend.h @@ -36,7 +36,7 @@ struct TransferState { uvec3 currentPageSize() const; void updateSparse(); void updateMip(); - void populatePage(uint8_t* dest); + void populatePage(std::vector& dest); bool increment(); TransferState(GLTexture& texture); }; diff --git a/libraries/gpu-gl/src/gpu/gl45/GL45BackendTexture.cpp b/libraries/gpu-gl/src/gpu/gl45/GL45BackendTexture.cpp index 871a2c8a03..b511ed7811 100644 --- a/libraries/gpu-gl/src/gpu/gl45/GL45BackendTexture.cpp +++ b/libraries/gpu-gl/src/gpu/gl45/GL45BackendTexture.cpp @@ -26,6 +26,9 @@ using namespace gpu::gl45; #define SPARSE_TEXTURES 1 +// Allocate 1 MB of buffer space for paged transfers +#define DEFAULT_PAGE_BUFFER_SIZE (1024*1024) + using GL45Texture = GL45Backend::GL45Texture; GLTexture* GL45Backend::syncGPUObject(const TexturePointer& texture, bool transfer) { @@ -102,13 +105,18 @@ bool TransferState::increment() { } #define DEFAULT_GL_PIXEL_ALIGNMENT 4 -void TransferState::populatePage(uint8_t* dst) { +void TransferState::populatePage(std::vector& buffer) { uvec3 pageSize = currentPageSize(); auto bytesPerPageLine = _bytesPerPixel * pageSize.x; if (0 != (bytesPerPageLine % DEFAULT_GL_PIXEL_ALIGNMENT)) { bytesPerPageLine += DEFAULT_GL_PIXEL_ALIGNMENT - (bytesPerPageLine % DEFAULT_GL_PIXEL_ALIGNMENT); assert(0 == (bytesPerPageLine % DEFAULT_GL_PIXEL_ALIGNMENT)); } + auto totalPageSize = bytesPerPageLine * pageSize.y; + if (totalPageSize > buffer.size()) { + buffer.resize(totalPageSize); + } + uint8_t* dst = &buffer[0]; for (uint32_t y = 0; y < pageSize.y; ++y) { uint32_t srcOffset = (_bytesPerLine * (_mipOffset.y + y)) + (_bytesPerPixel * _mipOffset.x); uint32_t dstOffset = bytesPerPageLine * y; @@ -194,7 +202,7 @@ void GL45Texture::startTransfer() { bool GL45Texture::continueTransfer() { static std::vector buffer; if (buffer.empty()) { - buffer.resize(1024 * 1024); + buffer.resize(DEFAULT_PAGE_BUFFER_SIZE); } uvec3 pageSize = _transferState.currentPageSize(); uvec3 offset = _transferState._mipOffset; @@ -210,7 +218,7 @@ bool GL45Texture::continueTransfer() { if (_transferState._srcPointer) { // Transfer the mip data - _transferState.populatePage(&buffer[0]); + _transferState.populatePage(buffer); if (GL_TEXTURE_2D == _target) { glTextureSubImage2D(_id, _transferState._mipLevel, offset.x, offset.y,