From 8c5f64dbada07ebc1caa6165fe054a2344c1d8f3 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sun, 3 May 2026 18:08:48 -0700 Subject: [PATCH 01/31] add capacity semantics to ManagedBuffer --- include/polyscope/render/engine.h | 4 + include/polyscope/render/managed_buffer.h | 55 +++++++ .../render/mock_opengl/mock_gl_engine.h | 1 + include/polyscope/render/opengl/gl_engine.h | 1 + src/render/managed_buffer.cpp | 138 +++++++++++++++++- src/render/mock_opengl/mock_gl_engine.cpp | 19 ++- src/render/opengl/gl_engine.cpp | 31 ++-- 7 files changed, 226 insertions(+), 23 deletions(-) diff --git a/include/polyscope/render/engine.h b/include/polyscope/render/engine.h index 38349f97..f0ba8959 100644 --- a/include/polyscope/render/engine.h +++ b/include/polyscope/render/engine.h @@ -96,6 +96,10 @@ class AttributeBuffer { virtual void setData(const std::vector>& data) = 0; virtual void setData(const std::vector>& data) = 0; + // Pre-allocate GPU memory for n elements without uploading any data. Subsequent setData() + // calls with size <= n will not need to reallocate the underlying buffer. + virtual void reserveCapacity(size_t n) = 0; + virtual uint32_t getNativeBufferID() = 0; // used to interop with external things, e.g. ImGui // == Getters diff --git a/include/polyscope/render/managed_buffer.h b/include/polyscope/render/managed_buffer.h index 26b97e63..d4b4e524 100644 --- a/include/polyscope/render/managed_buffer.h +++ b/include/polyscope/render/managed_buffer.h @@ -98,6 +98,56 @@ class ManagedBuffer : public virtual WeakReferrable { void setTextureSize(uint32_t sizeX, uint32_t sizeY, uint32_t sizeZ); std::array getTextureSize() const; + // == Resize / capacity API + + // The .size() of the buffer is the number of data elements it holds. + // + // The .capacity() of the buffer is the number of data elements it has capacity for without needing to reallocate + // (similar to std::vector). + // + // In a simple flow where we put data in a buffer once, the capacity is the same as the size and neither ever changes. + // But in settings where we e.g. incrementally add elements or change the number of data elements on each frame, we + // may want to allocate a larger capacity to avoid expensive re-allocation each time. + + // Resize the buffer to newSize elements. + // + // If newSize <= capacity(), this is a cheap constant-time operation which just updates metadata. + // + // If newSize > capacity(), this triggers a full reallocation. The new capacity is set to at least + // 2*oldCapacity (amortized doubling). Any data that was GPU-resident is first copied back to the + // host, then the GPU buffer is reallocated in-place (same buffer object, new backing memory). + // ShaderPrograms holding a reference to the GPU buffer remain valid. + // + // Returns true if a reallocation occurred, false if the resize stayed within capacity. + // + // Valid for attributes and 1D textures only; call the 2D/3D variants below for multidimensional + // textures. + bool resize(size_t newSize); + + // Resize a 2D texture. No-op (returns false) if dimensions are unchanged. Otherwise always + // triggers a reallocation (2D/3D textures have no capacity slack — capacity always equals size), + // reallocates the GPU buffer in-place, and returns true. + bool resizeTexture2D(uint32_t newSizeX, uint32_t newSizeY); + + // Resize a 3D texture. No-op (returns false) if dimensions are unchanged. Otherwise always + // triggers a reallocation (2D/3D textures have no capacity slack — capacity always equals size), + // reallocates the GPU buffer in-place, and returns true. + bool resizeTexture3D(uint32_t newSizeX, uint32_t newSizeY, uint32_t newSizeZ); + + // The maximum size the buffer can be resized to without triggering a reallocation. Always >= size(). + size_t capacity(); + + // Set the managed capacity to newCapacity. Unlike resize(), which grows capacity via amortized + // doubling, this sets the logical capacity to a precise value. Error if newCapacity < size(). + // Reallocates the GPU buffer in-place (same buffer object, new backing memory) if one exists. + // + // Note: data.capacity() is guaranteed to be >= managedCapacity, but may remain larger than + // newCapacity if the underlying vector already had more space allocated. + // + // Valid for attributes and 1D textures only; multidimensional textures always have capacity + // equal to their size, so use the 2D/3D resize() variants instead. + void setCapacity(size_t newCapacity); + // == Members for indexed data @@ -200,6 +250,11 @@ class ManagedBuffer : public virtual WeakReferrable { bool hostBufferIsPopulated; // true if the host buffer contains currently-valid data + // The buffer has capacity for at least this many elements. It is distinct from .size(), which is the actual number of + // elements currently stored in the buffer and may be smaller than the capacity. + // Any resize() operations that stay within the capacity are cheap, and do not trigger a full reallocation and copy. + size_t managedCapacity = 0; + std::shared_ptr renderAttributeBuffer; std::shared_ptr renderTextureBuffer; diff --git a/include/polyscope/render/mock_opengl/mock_gl_engine.h b/include/polyscope/render/mock_opengl/mock_gl_engine.h index cc4d2d49..b135564a 100644 --- a/include/polyscope/render/mock_opengl/mock_gl_engine.h +++ b/include/polyscope/render/mock_opengl/mock_gl_engine.h @@ -73,6 +73,7 @@ class GLAttributeBuffer : public AttributeBuffer { std::vector getDataRange_uvec4(size_t ind, size_t count) override; uint32_t getNativeBufferID() override; + void reserveCapacity(size_t n) override; protected: private: diff --git a/include/polyscope/render/opengl/gl_engine.h b/include/polyscope/render/opengl/gl_engine.h index 7e06d358..ef5ce764 100644 --- a/include/polyscope/render/opengl/gl_engine.h +++ b/include/polyscope/render/opengl/gl_engine.h @@ -101,6 +101,7 @@ class GLAttributeBuffer : public AttributeBuffer { std::vector getDataRange_uvec4(size_t ind, size_t count) override; uint32_t getNativeBufferID() override; + void reserveCapacity(size_t n) override; protected: VertexBufferHandle VBOLoc; diff --git a/src/render/managed_buffer.cpp b/src/render/managed_buffer.cpp index 67dbc7f2..14032ac6 100644 --- a/src/render/managed_buffer.cpp +++ b/src/render/managed_buffer.cpp @@ -18,7 +18,7 @@ namespace render { template ManagedBuffer::ManagedBuffer(ManagedBufferRegistry* registry_, const std::string& name_, std::vector& data_) : name(name_), uniqueID(internal::getNextUniqueID()), registry(registry_), data(data_), dataGetsComputed(false), - hostBufferIsPopulated(true) { + hostBufferIsPopulated(true), managedCapacity(data_.size()) { if (registry) { registry->addManagedBuffer(this); @@ -30,7 +30,7 @@ template ManagedBuffer::ManagedBuffer(ManagedBufferRegistry* registry_, const std::string& name_, std::vector& data_, std::function computeFunc_) : name(name_), uniqueID(internal::getNextUniqueID()), registry(registry_), data(data_), dataGetsComputed(true), - computeFunc(computeFunc_), hostBufferIsPopulated(false) { + computeFunc(computeFunc_), hostBufferIsPopulated(false), managedCapacity(0) { if (registry) { registry->addManagedBuffer(this); @@ -56,6 +56,8 @@ void ManagedBuffer::setTextureSize(uint32_t sizeX_) { deviceBufferType = DeviceBufferType::Texture1d; sizeX = sizeX_; + // Sync managedCapacity: data is expected to be populated before setTextureSize() is called. + if (data.size() > managedCapacity) managedCapacity = data.size(); } template @@ -66,6 +68,7 @@ void ManagedBuffer::setTextureSize(uint32_t sizeX_, uint32_t sizeY_) { deviceBufferType = DeviceBufferType::Texture2d; sizeX = sizeX_; sizeY = sizeY_; + if (data.size() > managedCapacity) managedCapacity = data.size(); } template @@ -77,6 +80,7 @@ void ManagedBuffer::setTextureSize(uint32_t sizeX_, uint32_t sizeY_, uint32_t sizeX = sizeX_; sizeY = sizeY_; sizeZ = sizeZ_; + if (data.size() > managedCapacity) managedCapacity = data.size(); } template @@ -85,6 +89,126 @@ std::array ManagedBuffer::getTextureSize() const { return std::array{sizeX, sizeY, sizeZ}; } +template +size_t ManagedBuffer::capacity() { + return managedCapacity; +} + +template +bool ManagedBuffer::resize(size_t newSize) { + if (deviceBufferType == DeviceBufferType::Texture2d || deviceBufferType == DeviceBufferType::Texture3d) + exception("resize() is not valid for 2D/3D texture buffers; use resizeTexture2D() or resizeTexture3D()"); + + if (newSize > managedCapacity) { + // Copy device-side data back to host BEFORE modifying data or invalidating the GPU buffer. + // ensureHostBufferPopulated() reads from renderAttributeBuffer into data; if we resize data + // first it would overwrite the resize with the old GPU contents. + ensureHostBufferPopulated(); + + // Reallocation needed: use amortized doubling + size_t newCapacity = std::max(newSize, 2 * managedCapacity); + data.reserve(newCapacity); + data.resize(newSize); + managedCapacity = newCapacity; + + // In-place reallocation: keep the same GPU buffer objects alive so ShaderPrograms + // holding shared_ptr references to them remain valid without needing to be reset. + if (renderAttributeBuffer) { + renderAttributeBuffer->reserveCapacity(managedCapacity); + } + if (renderTextureBuffer && deviceBufferType == DeviceBufferType::Texture1d) { + renderTextureBuffer->resize(static_cast(managedCapacity)); + } + hostBufferIsPopulated = true; + + if (deviceBufferType == DeviceBufferType::Texture1d) { + sizeX = static_cast(newSize); + } + + return true; + } else { + // No reallocation: just update size metadata + data.resize(newSize); + + if (deviceBufferType == DeviceBufferType::Texture1d) { + sizeX = static_cast(newSize); + } + + return false; + } +} + +template +void ManagedBuffer::setCapacity(size_t newCapacity) { + if (deviceBufferType == DeviceBufferType::Texture2d || deviceBufferType == DeviceBufferType::Texture3d) + exception("setCapacity() is not valid for 2D/3D texture buffers"); + + if (newCapacity < data.size()) + exception("setCapacity() cannot set capacity below current size (" + std::to_string(data.size()) + ")"); + + if (newCapacity == managedCapacity) return; // no-op + + // Before invalidating the GPU buffer, copy any device-side changes back to the host. + ensureHostBufferPopulated(); + + data.reserve(newCapacity); + managedCapacity = newCapacity; + + // In-place reallocation: keep the same GPU buffer objects alive. + if (renderAttributeBuffer) { + renderAttributeBuffer->reserveCapacity(managedCapacity); + } + if (renderTextureBuffer && deviceBufferType == DeviceBufferType::Texture1d) { + renderTextureBuffer->resize(static_cast(managedCapacity)); + } + hostBufferIsPopulated = true; +} + +template +bool ManagedBuffer::resizeTexture2D(uint32_t newSizeX, uint32_t newSizeY) { + checkDeviceBufferTypeIs(DeviceBufferType::Texture2d); + + if (newSizeX == sizeX && newSizeY == sizeY) return false; // no-op + + // Before invalidating the GPU buffer, copy any device-side changes back to the host. + ensureHostBufferPopulated(); + + sizeX = newSizeX; + sizeY = newSizeY; + managedCapacity = static_cast(sizeX) * sizeY; + data.resize(managedCapacity); + + if (renderTextureBuffer) { + renderTextureBuffer->resize(sizeX, sizeY); + } + hostBufferIsPopulated = true; + + return true; +} + +template +bool ManagedBuffer::resizeTexture3D(uint32_t newSizeX, uint32_t newSizeY, uint32_t newSizeZ) { + checkDeviceBufferTypeIs(DeviceBufferType::Texture3d); + + if (newSizeX == sizeX && newSizeY == sizeY && newSizeZ == sizeZ) return false; // no-op + + // Before invalidating the GPU buffer, copy any device-side changes back to the host. + ensureHostBufferPopulated(); + + sizeX = newSizeX; + sizeY = newSizeY; + sizeZ = newSizeZ; + managedCapacity = static_cast(sizeX) * sizeY * sizeZ; + data.resize(managedCapacity); + + if (renderTextureBuffer) { + renderTextureBuffer->resize(sizeX, sizeY, sizeZ); + } + hostBufferIsPopulated = true; + + return true; +} + template void ManagedBuffer::ensureHostBufferPopulated() { @@ -312,7 +436,11 @@ std::shared_ptr ManagedBuffer::getRenderAttributeBuf if (!renderAttributeBuffer) { ensureHostBufferPopulated(); // warning: the order of these matters because of how hostBufferPopulated works + // Sync managedCapacity on first GPU allocation: handles the case where data was populated + // externally before this call (e.g. class member ordering caused empty-vector-at-construction). + if (data.size() > managedCapacity) managedCapacity = data.size(); renderAttributeBuffer = generateAttributeBuffer(render::engine); + renderAttributeBuffer->reserveCapacity(managedCapacity); renderAttributeBuffer->setData(data); } return renderAttributeBuffer; @@ -324,6 +452,8 @@ std::shared_ptr ManagedBuffer::getRenderTextureBuffer( if (!renderTextureBuffer) { ensureHostBufferPopulated(); // warning: the order of these matters because of how hostBufferPopulated works + // Sync managedCapacity on first GPU allocation (same rationale as getRenderAttributeBuffer) + if (data.size() > managedCapacity) managedCapacity = data.size(); renderTextureBuffer = generateTextureBuffer(deviceBufferType, render::engine); @@ -333,7 +463,9 @@ std::shared_ptr ManagedBuffer::getRenderTextureBuffer( exception("bad call"); break; case DeviceBufferType::Texture1d: - renderTextureBuffer->resize(sizeX); + // Allocate GPU texture at the full managed capacity so future within-capacity resizes + // don't require a new GPU buffer. setData() below uploads only data.size() elements. + renderTextureBuffer->resize(static_cast(managedCapacity)); break; case DeviceBufferType::Texture2d: renderTextureBuffer->resize(sizeX, sizeY); diff --git a/src/render/mock_opengl/mock_gl_engine.cpp b/src/render/mock_opengl/mock_gl_engine.cpp index ef4877e2..a27f0ff8 100644 --- a/src/render/mock_opengl/mock_gl_engine.cpp +++ b/src/render/mock_opengl/mock_gl_engine.cpp @@ -73,19 +73,22 @@ void GLAttributeBuffer::checkArray(int testArrayCount) { } +void GLAttributeBuffer::reserveCapacity(size_t n) { + if (n <= bufferSize) return; + bufferSize = static_cast(n); + setFlag = true; +} + template void GLAttributeBuffer::setData_helper(const std::vector& data) { bind(); - // allocate if needed if (!isSet() || data.size() > bufferSize) { setFlag = true; - uint64_t newSize = data.size(); - newSize = std::max(newSize, 2 * bufferSize); // if we're expanding, at-least double + uint64_t newSize = static_cast(data.size()); bufferSize = newSize; } - // do the actual copy dataSize = data.size(); checkGLError(); @@ -411,7 +414,7 @@ void GLTextureBuffer::setData(const std::vector& data) { bind(); - if (data.size() != getTotalSize()) { + if (data.size() > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } @@ -431,7 +434,7 @@ void GLTextureBuffer::setData(const std::vector& data) { bind(); - if (data.size() != getTotalSize()) { + if (data.size() > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } @@ -450,7 +453,7 @@ void GLTextureBuffer::setData(const std::vector& data) { void GLTextureBuffer::setData(const std::vector& data) { bind(); - if (data.size() != getTotalSize()) { + if (data.size() > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } @@ -469,7 +472,7 @@ void GLTextureBuffer::setData(const std::vector& data) { void GLTextureBuffer::setData(const std::vector& data) { bind(); - if (data.size() != getTotalSize()) { + if (data.size() > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } diff --git a/src/render/opengl/gl_engine.cpp b/src/render/opengl/gl_engine.cpp index 608f0133..fe4d80dd 100644 --- a/src/render/opengl/gl_engine.cpp +++ b/src/render/opengl/gl_engine.cpp @@ -246,21 +246,26 @@ void GLAttributeBuffer::checkArray(int testArrayCount) { GLenum GLAttributeBuffer::getTarget() { return GL_ARRAY_BUFFER; } +void GLAttributeBuffer::reserveCapacity(size_t n) { + if (n <= bufferSize) return; + bind(); + glBufferData(getTarget(), n * sizeInBytes(dataType) * getArrayCount(), NULL, GL_STATIC_DRAW); + bufferSize = static_cast(n); + setFlag = true; + checkGLError(); +} template void GLAttributeBuffer::setData_helper(const std::vector& data) { bind(); - // allocate if needed if (!isSet() || data.size() > bufferSize) { setFlag = true; - uint64_t newSize = data.size(); - newSize = std::max(newSize, 2 * bufferSize); // if we're expanding, at-least double + uint64_t newSize = static_cast(data.size()); glBufferData(getTarget(), newSize * sizeof(T), NULL, GL_STATIC_DRAW); bufferSize = newSize; } - // do the actual copy dataSize = data.size(); glBufferSubData(getTarget(), 0, dataSize * sizeof(T), data.data()); @@ -614,13 +619,15 @@ void GLTextureBuffer::setData(const std::vector& data) { bind(); - if (data.size() != getTotalSize()) { + // For 1D textures, data.size() may be <= sizeX (partial upload into a capacity-allocated texture). + // For 2D/3D textures, capacity always equals size so the check is effectively ==. + if (data.size() > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } switch (dim) { case 1: - glTexSubImage1D(GL_TEXTURE_1D, 0, 0, sizeX, formatF(format), type(format), &data.front().x); + glTexSubImage1D(GL_TEXTURE_1D, 0, 0, static_cast(data.size()), formatF(format), type(format), &data.front().x); break; case 2: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, sizeX, sizeY, formatF(format), type(format), &data.front().x); @@ -637,13 +644,13 @@ void GLTextureBuffer::setData(const std::vector& data) { bind(); - if (data.size() != getTotalSize()) { + if (data.size() > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } switch (dim) { case 1: - glTexSubImage1D(GL_TEXTURE_1D, 0, 0, sizeX, formatF(format), type(format), &data.front().x); + glTexSubImage1D(GL_TEXTURE_1D, 0, 0, static_cast(data.size()), formatF(format), type(format), &data.front().x); break; case 2: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, sizeX, sizeY, formatF(format), type(format), &data.front().x); @@ -659,13 +666,13 @@ void GLTextureBuffer::setData(const std::vector& data) { void GLTextureBuffer::setData(const std::vector& data) { bind(); - if (data.size() != getTotalSize()) { + if (data.size() > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } switch (dim) { case 1: - glTexSubImage1D(GL_TEXTURE_1D, 0, 0, sizeX, formatF(format), type(format), &data.front()); + glTexSubImage1D(GL_TEXTURE_1D, 0, 0, static_cast(data.size()), formatF(format), type(format), &data.front()); break; case 2: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, sizeX, sizeY, formatF(format), type(format), &data.front()); @@ -689,13 +696,13 @@ void GLTextureBuffer::setData(const std::vector& data) { bind(); - if (data.size() != getTotalSize()) { + if (data.size() > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } switch (dim) { case 1: - glTexSubImage1D(GL_TEXTURE_1D, 0, 0, sizeX, formatF(format), type(format), &dataFloat.front()); + glTexSubImage1D(GL_TEXTURE_1D, 0, 0, static_cast(data.size()), formatF(format), type(format), &dataFloat.front()); break; case 2: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, sizeX, sizeY, formatF(format), type(format), &dataFloat.front()); From 735a44c8b804eb02f3f5346f6245ab639f79f428 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sun, 3 May 2026 20:02:50 -0700 Subject: [PATCH 02/31] make ManagedBuffer own the .data buffer, rather than wrapping it --- include/polyscope/color_image_quantity.h | 2 - include/polyscope/color_quantity.h | 2 - include/polyscope/color_quantity.ipp | 2 +- .../polyscope/color_render_image_quantity.h | 3 - include/polyscope/curve_network.h | 7 -- .../polyscope/curve_network_color_quantity.h | 3 - .../polyscope/curve_network_scalar_quantity.h | 3 - include/polyscope/parameterization_quantity.h | 3 - .../polyscope/parameterization_quantity.ipp | 5 +- include/polyscope/point_cloud.h | 2 - .../raw_color_alpha_render_image_quantity.h | 3 - .../raw_color_render_image_quantity.h | 3 - include/polyscope/render/managed_buffer.h | 9 ++- .../polyscope/render_image_quantity_base.h | 4 -- include/polyscope/scalar_quantity.h | 1 - include/polyscope/scalar_quantity.ipp | 2 +- include/polyscope/simple_triangle_mesh.h | 4 -- include/polyscope/sparse_volume_grid.h | 5 -- include/polyscope/surface_mesh.h | 35 ---------- include/polyscope/vector_quantity.h | 5 -- include/polyscope/vector_quantity.ipp | 12 ++-- include/polyscope/volume_grid.h | 5 -- include/polyscope/volume_mesh.h | 21 ------ src/color_image_quantity.cpp | 5 +- src/color_render_image_quantity.cpp | 2 +- src/curve_network.cpp | 24 ++++--- src/curve_network_color_quantity.cpp | 2 +- src/curve_network_scalar_quantity.cpp | 2 +- src/point_cloud.cpp | 5 +- src/raw_color_alpha_render_image_quantity.cpp | 2 +- src/raw_color_render_image_quantity.cpp | 2 +- src/render/managed_buffer.cpp | 12 ++-- src/render_image_quantity_base.cpp | 7 +- src/simple_triangle_mesh.cpp | 6 +- src/sparse_volume_grid.cpp | 42 +++++------ src/surface_mesh.cpp | 70 +++++++++---------- src/volume_grid.cpp | 6 +- src/volume_mesh.cpp | 19 +++-- 38 files changed, 118 insertions(+), 229 deletions(-) diff --git a/include/polyscope/color_image_quantity.h b/include/polyscope/color_image_quantity.h index 4761f6f1..c249f582 100644 --- a/include/polyscope/color_image_quantity.h +++ b/include/polyscope/color_image_quantity.h @@ -34,8 +34,6 @@ class ColorImageQuantity : public ImageQuantity { protected: - std::vector colorsData; - PersistentValue isPremultiplied; // rendering internals diff --git a/include/polyscope/color_quantity.h b/include/polyscope/color_quantity.h index e880bb87..4a63e604 100644 --- a/include/polyscope/color_quantity.h +++ b/include/polyscope/color_quantity.h @@ -39,8 +39,6 @@ class ColorQuantity { // === ~DANGER~ experimental/unsupported functions protected: - std::vector colorsData; - // === Visualization parameters // Parameters diff --git a/include/polyscope/color_quantity.ipp b/include/polyscope/color_quantity.ipp index 3a14a9b3..8d7b248f 100644 --- a/include/polyscope/color_quantity.ipp +++ b/include/polyscope/color_quantity.ipp @@ -4,7 +4,7 @@ namespace polyscope { template ColorQuantity::ColorQuantity(QuantityT& quantity_, const std::vector& colors_) - : quantity(quantity_), colors(&quantity, quantity.uniquePrefix() + "colors", colorsData), colorsData(colors_) { + : quantity(quantity_), colors(&quantity, quantity.uniquePrefix() + "colors", std::vector(colors_)) { colors.checkInvalidValues(); } diff --git a/include/polyscope/color_render_image_quantity.h b/include/polyscope/color_render_image_quantity.h index 9edfe428..c2012460 100644 --- a/include/polyscope/color_render_image_quantity.h +++ b/include/polyscope/color_render_image_quantity.h @@ -37,9 +37,6 @@ class ColorRenderImageQuantity : public RenderImageQuantityBase { protected: // === Visualization parameters - // Store the raw data - std::vector colorsData; - // === Render data std::shared_ptr program; diff --git a/include/polyscope/curve_network.h b/include/polyscope/curve_network.h index 45fd818b..48dbf6db 100644 --- a/include/polyscope/curve_network.h +++ b/include/polyscope/curve_network.h @@ -162,13 +162,6 @@ class CurveNetwork : public Structure { private: - // Storage for the managed buffers above. You should generally interact with these through the managed buffers, not - // these members. - std::vector nodePositionsData; - std::vector edgeTailIndsData; - std::vector edgeTipIndsData; - std::vector edgeCentersData; - void computeEdgeCenters(); // === Visualization parameters diff --git a/include/polyscope/curve_network_color_quantity.h b/include/polyscope/curve_network_color_quantity.h index 717c0afd..348513be 100644 --- a/include/polyscope/curve_network_color_quantity.h +++ b/include/polyscope/curve_network_color_quantity.h @@ -58,9 +58,6 @@ class CurveNetworkEdgeColorQuantity : public CurveNetworkColorQuantity { render::ManagedBuffer nodeAverageColors; void updateNodeAverageColors(); - -private: - std::vector nodeAverageColorsData; }; } // namespace polyscope diff --git a/include/polyscope/curve_network_scalar_quantity.h b/include/polyscope/curve_network_scalar_quantity.h index 8885a9cc..7307f730 100644 --- a/include/polyscope/curve_network_scalar_quantity.h +++ b/include/polyscope/curve_network_scalar_quantity.h @@ -59,9 +59,6 @@ class CurveNetworkEdgeScalarQuantity : public CurveNetworkScalarQuantity { render::ManagedBuffer nodeAverageValues; void updateNodeAverageValues(); - -private: - std::vector nodeAverageValuesData; }; diff --git a/include/polyscope/parameterization_quantity.h b/include/polyscope/parameterization_quantity.h index 6504848a..7b2e4c3f 100644 --- a/include/polyscope/parameterization_quantity.h +++ b/include/polyscope/parameterization_quantity.h @@ -70,9 +70,6 @@ class ParameterizationQuantity { void setParameterizationUniforms(render::ShaderProgram& p); protected: - // Raw storage for the data. You should only interact with this via the managed buffer above - std::vector coordsData; - std::vector islandLabelsData; bool islandLabelsPopulated = false; // === Visualization parameters diff --git a/include/polyscope/parameterization_quantity.ipp b/include/polyscope/parameterization_quantity.ipp index 80f5028b..4db3408d 100644 --- a/include/polyscope/parameterization_quantity.ipp +++ b/include/polyscope/parameterization_quantity.ipp @@ -40,9 +40,8 @@ ParameterizationQuantity::ParameterizationQuantity(QuantityT& quantit : quantity(quantity_), // buffers - coords(&quantity, quantity.uniquePrefix() + "#coords", coordsData), - islandLabels(&quantity, quantity.uniquePrefix() + "#islandLabels", islandLabelsData), coordsType(type_), - coordsData(coords_), + coords(&quantity, quantity.uniquePrefix() + "#coords", std::vector(coords_)), + islandLabels(&quantity, quantity.uniquePrefix() + "#islandLabels", std::vector{}), coordsType(type_), // options checkerSize(quantity.uniquePrefix() + "#checkerSize", 0.02), diff --git a/include/polyscope/point_cloud.h b/include/polyscope/point_cloud.h index 40e8b6e7..346b8ddd 100644 --- a/include/polyscope/point_cloud.h +++ b/include/polyscope/point_cloud.h @@ -154,8 +154,6 @@ class PointCloud : public Structure { private: - // Storage for the managed buffers above. You should generally interact with this directly through them. - std::vector pointsData; // === Visualization parameters PersistentValue pointRenderMode; diff --git a/include/polyscope/raw_color_alpha_render_image_quantity.h b/include/polyscope/raw_color_alpha_render_image_quantity.h index 03fc0cf0..ce48e84f 100644 --- a/include/polyscope/raw_color_alpha_render_image_quantity.h +++ b/include/polyscope/raw_color_alpha_render_image_quantity.h @@ -35,9 +35,6 @@ class RawColorAlphaRenderImageQuantity : public RenderImageQuantityBase { bool getIsPremultiplied(); protected: - // Store the raw data - std::vector colorsData; - // === Visualization parameters PersistentValue isPremultiplied; diff --git a/include/polyscope/raw_color_render_image_quantity.h b/include/polyscope/raw_color_render_image_quantity.h index 73a46707..e9bdffa6 100644 --- a/include/polyscope/raw_color_render_image_quantity.h +++ b/include/polyscope/raw_color_render_image_quantity.h @@ -35,9 +35,6 @@ class RawColorRenderImageQuantity : public RenderImageQuantityBase { protected: // === Visualization parameters - // Store the raw data - std::vector colorsData; - // === Render data std::shared_ptr program; diff --git a/include/polyscope/render/managed_buffer.h b/include/polyscope/render/managed_buffer.h index d4b4e524..d22ba370 100644 --- a/include/polyscope/render/managed_buffer.h +++ b/include/polyscope/render/managed_buffer.h @@ -40,11 +40,11 @@ class ManagedBuffer : public virtual WeakReferrable { // (second variants are advanced versions which allow creation of multi-dimensional texture values) // Manage a buffer of data which is explicitly set externally. - ManagedBuffer(ManagedBufferRegistry* registry, const std::string& name, std::vector& data); + ManagedBuffer(ManagedBufferRegistry* registry, const std::string& name, std::vector data); // Manage a buffer of data which gets computed lazily - ManagedBuffer(ManagedBufferRegistry* registry, const std::string& name, std::vector& data, + ManagedBuffer(ManagedBufferRegistry* registry, const std::string& name, std::function computeFunc); @@ -63,8 +63,7 @@ class ManagedBuffer : public virtual WeakReferrable { ManagedBufferRegistry* registry; - // The raw underlying buffer which this class wraps that holds the data. - // It is assumed that it never changes length (although this class may clear it to empty). + // The raw underlying buffer which this class owns and holds the data. // // It is possible that data.size() == 0 if the data is lazily computed and has not been computed yet, or if this // host-side buffer is invalidated because it is being updated externally directly on the render device. @@ -75,7 +74,7 @@ class ManagedBuffer : public virtual WeakReferrable { // buff.markHostBufferUpdated(); // // - std::vector& data; + std::vector data; // == Members for computed data diff --git a/include/polyscope/render_image_quantity_base.h b/include/polyscope/render_image_quantity_base.h index f98dd2c0..b658be94 100644 --- a/include/polyscope/render_image_quantity_base.h +++ b/include/polyscope/render_image_quantity_base.h @@ -63,10 +63,6 @@ class RenderImageQuantityBase : public FloatingQuantity, public FullscreenArtist const bool hasNormals; ImageOrigin imageOrigin; - // Store the raw data - std::vector depthsData; - std::vector normalsData; - // === Visualization parameters PersistentValue material; PersistentValue transparency; diff --git a/include/polyscope/scalar_quantity.h b/include/polyscope/scalar_quantity.h index 037b20bd..d651602c 100644 --- a/include/polyscope/scalar_quantity.h +++ b/include/polyscope/scalar_quantity.h @@ -85,7 +85,6 @@ class ScalarQuantity { double getIsolineWidth(); protected: - std::vector valuesData; const DataType dataType; // === Visualization parameters diff --git a/include/polyscope/scalar_quantity.ipp b/include/polyscope/scalar_quantity.ipp index c286f740..04c3ca69 100644 --- a/include/polyscope/scalar_quantity.ipp +++ b/include/polyscope/scalar_quantity.ipp @@ -8,7 +8,7 @@ namespace polyscope { template ScalarQuantity::ScalarQuantity(QuantityT& quantity_, const std::vector& values_, DataType dataType_) - : quantity(quantity_), values(&quantity, quantity.uniquePrefix() + "values", valuesData), valuesData(values_), + : quantity(quantity_), values(&quantity, quantity.uniquePrefix() + "values", std::vector(values_)), dataType(dataType_), dataRange(robustMinMax(values.data, 1e-5)), vizRangeMin(quantity.uniquePrefix() + "vizRangeMin", -777.), // set later, vizRangeMax(quantity.uniquePrefix() + "vizRangeMax", -777.), // including clearing cache diff --git a/include/polyscope/simple_triangle_mesh.h b/include/polyscope/simple_triangle_mesh.h index 4b0c6041..181d1c1f 100644 --- a/include/polyscope/simple_triangle_mesh.h +++ b/include/polyscope/simple_triangle_mesh.h @@ -93,10 +93,6 @@ class SimpleTriangleMesh : public Structure { private: - // Storage for the managed buffers above. You should generally interact with this directly through them. - std::vector verticesData; - std::vector facesData; - // === Visualization parameters PersistentValue surfaceColor; PersistentValue material; diff --git a/include/polyscope/sparse_volume_grid.h b/include/polyscope/sparse_volume_grid.h index 7fbe2f9d..b64dd03e 100644 --- a/include/polyscope/sparse_volume_grid.h +++ b/include/polyscope/sparse_volume_grid.h @@ -165,17 +165,12 @@ class SparseVolumeGrid : public Structure { glm::vec3 origin; glm::vec3 gridCellWidth; - // === Storage for managed quantities - std::vector cellPositionsData; - std::vector cellIndicesData; - // User-facing occupied cell indices (signed) std::vector occupiedCellsData; // Canonical sorted node indices and corner index buffers (lazily computed) bool haveCornerNodeIndices = false; std::vector canonicalNodeIndsData; - std::vector cornerNodeIndsData[8]; // === Visualization parameters PersistentValue color; diff --git a/include/polyscope/surface_mesh.h b/include/polyscope/surface_mesh.h index bff5ef31..7df67a26 100644 --- a/include/polyscope/surface_mesh.h +++ b/include/polyscope/surface_mesh.h @@ -308,41 +308,6 @@ class SurfaceMesh : public Structure { private: - // == Mesh geometry buffers - // Storage for the managed buffers above. You should generally interact with these through the managed buffers, not - // these members. - - // = positions - std::vector vertexPositionsData; - - // = connectivity / indices - - // other derived indices, all defined per corner of the triangulated mesh - std::vector triangleVertexIndsData; // index of the corresponding vertex - std::vector triangleFaceIndsData; // index of the corresponding original face - std::vector triangleCornerIndsData; // index of the corresponding original corner - std::vector triangleAllVertexIndsData; // index of the corresponding original vertex - std::vector triangleAllEdgeIndsData; // index of the corresponding original edge - std::vector triangleAllHalfedgeIndsData; // index of the corresponding original halfedge - std::vector triangleAllCornerIndsData; // index of the corresponding original corner - - // internal triangle data for rendering, defined per corner of the triangulated mesh - std::vector baryCoordData; // always triangulated - std::vector edgeIsRealData; // always triangulated - - // other internally-computed geometry - std::vector faceNormalsData; - std::vector faceCentersData; - std::vector faceAreasData; - std::vector vertexNormalsData; - std::vector vertexAreasData; - // std::vector edgeLengthsData; - - // tangent spaces - std::vector defaultFaceTangentBasisXData; - std::vector defaultFaceTangentBasisYData; - - // Derived connectivity quantities bool halfedgesHaveBeenUsed = false; bool cornersHaveBeenUsed = false; diff --git a/include/polyscope/vector_quantity.h b/include/polyscope/vector_quantity.h index f5f82ac5..15738463 100644 --- a/include/polyscope/vector_quantity.h +++ b/include/polyscope/vector_quantity.h @@ -114,8 +114,6 @@ class VectorQuantity : public VectorQuantityBase { // helpers void createProgram(); void updateMaxLength(); - - std::vector vectorsData; }; @@ -158,9 +156,6 @@ class TangentVectorQuantity : public VectorQuantityBase { void createProgram(); void updateMaxLength(); - std::vector tangentVectorsData; - std::vector tangentBasisXData; - std::vector tangentBasisYData; int nSym; }; diff --git a/include/polyscope/vector_quantity.ipp b/include/polyscope/vector_quantity.ipp index 8819451c..9721dc13 100644 --- a/include/polyscope/vector_quantity.ipp +++ b/include/polyscope/vector_quantity.ipp @@ -122,8 +122,8 @@ template VectorQuantity::VectorQuantity(QuantityT& quantity_, const std::vector& vectors_, render::ManagedBuffer& vectorRoots_, VectorType vectorType_) : VectorQuantityBase(quantity_, vectorType_), - vectors(&quantity_, quantity_.uniquePrefix() + "#values", vectorsData), vectorRoots(vectorRoots_), - vectorsData(vectors_) { + vectors(&quantity_, quantity_.uniquePrefix() + "#values", std::vector(vectors_)), + vectorRoots(vectorRoots_) { vectors.checkInvalidValues(); this->updateMaxLength(); } @@ -232,10 +232,10 @@ TangentVectorQuantity::TangentVectorQuantity(QuantityT& quantity_, VectorType vectorType_) : VectorQuantityBase(quantity_, vectorType_), - tangentVectors(&quantity_, quantity_.uniquePrefix() + "#values", tangentVectorsData), - tangentBasisX(&quantity_, quantity_.uniquePrefix() + "#basisX", tangentBasisXData), - tangentBasisY(&quantity_, quantity_.uniquePrefix() + "#basisY", tangentBasisYData), vectorRoots(vectorRoots_), - tangentVectorsData(tangentVectors_), tangentBasisXData(tangentBasisX_), tangentBasisYData(tangentBasisY_), + tangentVectors(&quantity_, quantity_.uniquePrefix() + "#values", std::vector(tangentVectors_)), + tangentBasisX(&quantity_, quantity_.uniquePrefix() + "#basisX", std::vector(tangentBasisX_)), + tangentBasisY(&quantity_, quantity_.uniquePrefix() + "#basisY", std::vector(tangentBasisY_)), + vectorRoots(vectorRoots_), nSym(nSym_) { tangentVectors.checkInvalidValues(); tangentBasisX.checkInvalidValues(); diff --git a/include/polyscope/volume_grid.h b/include/polyscope/volume_grid.h index 8161af09..a8a9fc93 100644 --- a/include/polyscope/volume_grid.h +++ b/include/polyscope/volume_grid.h @@ -151,11 +151,6 @@ class VolumeGrid : public Structure { glm::uvec3 gridCellDim; glm::vec3 boundMin, boundMax; - // === Storage for managed quantities - std::vector gridPlaneReferencePositionsData; - std::vector gridPlaneReferenceNormalsData; - std::vector gridPlaneAxisIndsData; - // === Visualization parameters PersistentValue color; PersistentValue edgeColor; diff --git a/include/polyscope/volume_mesh.h b/include/polyscope/volume_mesh.h index ffb8eddc..ddd11484 100644 --- a/include/polyscope/volume_mesh.h +++ b/include/polyscope/volume_mesh.h @@ -186,27 +186,6 @@ class VolumeMesh : public Structure { private: - // == Mesh geometry buffers - // Storage for the managed buffers above. You should generally interact with these through the managed buffers, not - // these members. - - // positions - std::vector vertexPositionsData; - - // connectivity / indices - std::vector triangleVertexIndsData; // to the split, triangulated mesh - std::vector triangleFaceIndsData; // to the split, triangulated mesh - std::vector triangleCellIndsData; // to the split, triangulated mesh - - // internal triangle data for rendering - std::vector baryCoordData; - std::vector edgeIsRealData; - std::vector faceTypeData; - - // other internally-computed geometry - std::vector faceNormalsData; - std::vector cellCentersData; - // Visualization settings PersistentValue color; PersistentValue interiorColor; diff --git a/src/color_image_quantity.cpp b/src/color_image_quantity.cpp index 1046ce25..82482a90 100644 --- a/src/color_image_quantity.cpp +++ b/src/color_image_quantity.cpp @@ -13,8 +13,9 @@ namespace polyscope { ColorImageQuantity::ColorImageQuantity(Structure& parent_, std::string name, size_t dimX, size_t dimY, const std::vector& data_, ImageOrigin imageOrigin_) - : ImageQuantity(parent_, name, dimX, dimY, imageOrigin_), colors(this, uniquePrefix() + "colors", colorsData), - colorsData(data_), isPremultiplied(uniquePrefix() + "isPremultiplied", false) { + : ImageQuantity(parent_, name, dimX, dimY, imageOrigin_), + colors(this, uniquePrefix() + "colors", std::vector(data_)), + isPremultiplied(uniquePrefix() + "isPremultiplied", false) { colors.setTextureSize(dimX, dimY); } diff --git a/src/color_render_image_quantity.cpp b/src/color_render_image_quantity.cpp index 9a6b39fc..266d6254 100644 --- a/src/color_render_image_quantity.cpp +++ b/src/color_render_image_quantity.cpp @@ -16,7 +16,7 @@ ColorRenderImageQuantity::ColorRenderImageQuantity(Structure& parent_, std::stri const std::vector& normalData, const std::vector& colorsData_, ImageOrigin imageOrigin) : RenderImageQuantityBase(parent_, name, dimX, dimY, depthData, normalData, imageOrigin), - colors(this, uniquePrefix() + "colors", colorsData), colorsData(colorsData_) { + colors(this, uniquePrefix() + "colors", std::vector(colorsData_)) { colors.setTextureSize(dimX, dimY); } diff --git a/src/curve_network.cpp b/src/curve_network.cpp index fbae2a74..6a3ef2fd 100644 --- a/src/curve_network.cpp +++ b/src/curve_network.cpp @@ -20,13 +20,12 @@ const std::string CurveNetwork::structureTypeName = "Curve Network"; // Constructor CurveNetwork::CurveNetwork(std::string name, std::vector nodes_, std::vector> edges_) : // clang-format off - Structure(name, typeName()), - nodePositions(this, uniquePrefix() + "nodePositions", nodePositionsData), - edgeTailInds(this, uniquePrefix() + "edgeTailInds", edgeTailIndsData), - edgeTipInds(this, uniquePrefix() + "edgeTipInds", edgeTipIndsData), - edgeCenters(this, uniquePrefix() + "edgeCenters", edgeCentersData, std::bind(&CurveNetwork::computeEdgeCenters, this)), - nodePositionsData(std::move(nodes_)), - color(uniquePrefix() + "#color", getNextUniqueColor()), + Structure(name, typeName()), + nodePositions(this, uniquePrefix() + "nodePositions", std::move(nodes_)), + edgeTailInds(this, uniquePrefix() + "edgeTailInds", std::vector{}), + edgeTipInds(this, uniquePrefix() + "edgeTipInds", std::vector{}), + edgeCenters(this, uniquePrefix() + "edgeCenters", std::bind(&CurveNetwork::computeEdgeCenters, this)), + color(uniquePrefix() + "#color", getNextUniqueColor()), radius(uniquePrefix() + "#radius", relativeValue(0.005)), material(uniquePrefix() + "#material", "clay") // clang-format on @@ -34,8 +33,8 @@ CurveNetwork::CurveNetwork(std::string name, std::vector nodes_, std: nodePositions.checkInvalidValues(); // Copy interleaved data in to tip and tails buffers below - edgeTailIndsData.resize(edges_.size()); - edgeTipIndsData.resize(edges_.size()); + edgeTailInds.data.resize(edges_.size()); + edgeTipInds.data.resize(edges_.size()); // Compute node degrees; some quantities want them for visualizations nodeDegrees = std::vector(nNodes(), 0); @@ -46,8 +45,8 @@ CurveNetwork::CurveNetwork(std::string name, std::vector nodes_, std: size_t nA = std::get<0>(edge); size_t nB = std::get<1>(edge); - edgeTailIndsData[iE] = nA; - edgeTipIndsData[iE] = nB; + edgeTailInds.data[iE] = nA; + edgeTipInds.data[iE] = nB; // Make sure there are no out of bounds indices if (nA >= maxInd || nB >= maxInd) { @@ -61,6 +60,9 @@ CurveNetwork::CurveNetwork(std::string name, std::vector nodes_, std: nodeDegrees[nB]++; } + edgeTailInds.markHostBufferUpdated(); + edgeTipInds.markHostBufferUpdated(); + updateObjectSpaceBounds(); } diff --git a/src/curve_network_color_quantity.cpp b/src/curve_network_color_quantity.cpp index 9b65fcd2..22486ee6 100644 --- a/src/curve_network_color_quantity.cpp +++ b/src/curve_network_color_quantity.cpp @@ -110,7 +110,7 @@ void CurveNetworkColorQuantity::refresh() { CurveNetworkEdgeColorQuantity::CurveNetworkEdgeColorQuantity(std::string name, std::vector values_, CurveNetwork& network_) : CurveNetworkColorQuantity(name, network_, "edge", values_), - nodeAverageColors(this, uniquePrefix() + "#nodeAverageColors", nodeAverageColorsData) {} + nodeAverageColors(this, uniquePrefix() + "#nodeAverageColors", std::vector{}) {} void CurveNetworkEdgeColorQuantity::createProgram() { diff --git a/src/curve_network_scalar_quantity.cpp b/src/curve_network_scalar_quantity.cpp index 65fcf6c0..fd70deba 100644 --- a/src/curve_network_scalar_quantity.cpp +++ b/src/curve_network_scalar_quantity.cpp @@ -130,7 +130,7 @@ void CurveNetworkNodeScalarQuantity::buildNodeInfoGUI(size_t nInd) { CurveNetworkEdgeScalarQuantity::CurveNetworkEdgeScalarQuantity(std::string name, const std::vector& values_, CurveNetwork& network_, DataType dataType_) : CurveNetworkScalarQuantity(name, network_, "edge", values_, dataType_), - nodeAverageValues(this, uniquePrefix() + "#nodeAverageValues", nodeAverageValuesData) {} + nodeAverageValues(this, uniquePrefix() + "#nodeAverageValues", std::vector{}) {} void CurveNetworkEdgeScalarQuantity::createProgram() { // Create the program to draw this quantity diff --git a/src/point_cloud.cpp b/src/point_cloud.cpp index edd7a60b..caa92dfc 100644 --- a/src/point_cloud.cpp +++ b/src/point_cloud.cpp @@ -24,9 +24,8 @@ const std::string PointCloud::structureTypeName = "Point Cloud"; // Constructor PointCloud::PointCloud(std::string name, std::vector points_) : // clang-format off - Structure(name, structureTypeName), - points(this, uniquePrefix() + "points", pointsData), - pointsData(std::move(points_)), + Structure(name, structureTypeName), + points(this, uniquePrefix() + "points", std::move(points_)), pointRenderMode(uniquePrefix() + "pointRenderMode", "sphere"), pointColor(uniquePrefix() + "pointColor", getNextUniqueColor()), pointRadius(uniquePrefix() + "pointRadius", relativeValue(0.005)), diff --git a/src/raw_color_alpha_render_image_quantity.cpp b/src/raw_color_alpha_render_image_quantity.cpp index 89190ab6..b4ab180e 100644 --- a/src/raw_color_alpha_render_image_quantity.cpp +++ b/src/raw_color_alpha_render_image_quantity.cpp @@ -16,7 +16,7 @@ RawColorAlphaRenderImageQuantity::RawColorAlphaRenderImageQuantity(Structure& pa const std::vector& colorsData_, ImageOrigin imageOrigin) : RenderImageQuantityBase(parent_, name, dimX, dimY, depthData, std::vector(), imageOrigin), - colors(this, uniquePrefix() + "colors", colorsData), colorsData(colorsData_), + colors(this, uniquePrefix() + "colors", std::vector(colorsData_)), isPremultiplied(uniquePrefix() + "isPremultiplied", false) { colors.setTextureSize(dimX, dimY); } diff --git a/src/raw_color_render_image_quantity.cpp b/src/raw_color_render_image_quantity.cpp index a3b32b0b..5ba8cd52 100644 --- a/src/raw_color_render_image_quantity.cpp +++ b/src/raw_color_render_image_quantity.cpp @@ -16,7 +16,7 @@ RawColorRenderImageQuantity::RawColorRenderImageQuantity(Structure& parent_, std const std::vector& colorsData_, ImageOrigin imageOrigin) : RenderImageQuantityBase(parent_, name, dimX, dimY, depthData, std::vector(), imageOrigin), - colors(this, uniquePrefix() + "colors", colorsData), colorsData(colorsData_) { + colors(this, uniquePrefix() + "colors", std::vector(colorsData_)) { colors.setTextureSize(dimX, dimY); } diff --git a/src/render/managed_buffer.cpp b/src/render/managed_buffer.cpp index 14032ac6..65a54df6 100644 --- a/src/render/managed_buffer.cpp +++ b/src/render/managed_buffer.cpp @@ -16,9 +16,11 @@ namespace polyscope { namespace render { template -ManagedBuffer::ManagedBuffer(ManagedBufferRegistry* registry_, const std::string& name_, std::vector& data_) - : name(name_), uniqueID(internal::getNextUniqueID()), registry(registry_), data(data_), dataGetsComputed(false), - hostBufferIsPopulated(true), managedCapacity(data_.size()) { +ManagedBuffer::ManagedBuffer(ManagedBufferRegistry* registry_, const std::string& name_, std::vector data_) + : name(name_), uniqueID(internal::getNextUniqueID()), registry(registry_), data(std::move(data_)), + dataGetsComputed(false), hostBufferIsPopulated(true) { + + managedCapacity = data.size(); if (registry) { registry->addManagedBuffer(this); @@ -27,9 +29,9 @@ ManagedBuffer::ManagedBuffer(ManagedBufferRegistry* registry_, const std::str template -ManagedBuffer::ManagedBuffer(ManagedBufferRegistry* registry_, const std::string& name_, std::vector& data_, +ManagedBuffer::ManagedBuffer(ManagedBufferRegistry* registry_, const std::string& name_, std::function computeFunc_) - : name(name_), uniqueID(internal::getNextUniqueID()), registry(registry_), data(data_), dataGetsComputed(true), + : name(name_), uniqueID(internal::getNextUniqueID()), registry(registry_), dataGetsComputed(true), computeFunc(computeFunc_), hostBufferIsPopulated(false), managedCapacity(0) { if (registry) { diff --git a/src/render_image_quantity_base.cpp b/src/render_image_quantity_base.cpp index 356356dd..9c8295f8 100644 --- a/src/render_image_quantity_base.cpp +++ b/src/render_image_quantity_base.cpp @@ -12,9 +12,10 @@ namespace polyscope { RenderImageQuantityBase::RenderImageQuantityBase(Structure& parent_, std::string name, size_t dimX_, size_t dimY_, const std::vector& depthData_, const std::vector& normalData_, ImageOrigin imageOrigin_) - : FloatingQuantity(name, parent_), depths(this, uniquePrefix() + "depths", depthsData), - normals(this, uniquePrefix() + "normals", normalsData), dimX(dimX_), dimY(dimY_), - hasNormals(normalData_.size() > 0), imageOrigin(imageOrigin_), depthsData(depthData_), normalsData(normalData_), + : FloatingQuantity(name, parent_), + depths(this, uniquePrefix() + "depths", std::vector(depthData_)), + normals(this, uniquePrefix() + "normals", std::vector(normalData_)), dimX(dimX_), dimY(dimY_), + hasNormals(normalData_.size() > 0), imageOrigin(imageOrigin_), material(uniquePrefix() + "material", "clay"), transparency(uniquePrefix() + "transparency", 1.0), allowFullscreenCompositing(uniquePrefix() + "allowFullscreenCompositing", false) { depths.setTextureSize(dimX, dimY); diff --git a/src/simple_triangle_mesh.cpp b/src/simple_triangle_mesh.cpp index 549a2ce4..6dfb0103 100644 --- a/src/simple_triangle_mesh.cpp +++ b/src/simple_triangle_mesh.cpp @@ -21,10 +21,8 @@ SimpleTriangleMesh::SimpleTriangleMesh(std::string name, std::vector std::vector faces_) : // clang-format off Structure(name, structureTypeName), - vertices(this, uniquePrefix() + "vertices", verticesData), - faces(this, uniquePrefix() + "faces", facesData), - verticesData(std::move(vertices_)), - facesData(std::move(faces_)), + vertices(this, uniquePrefix() + "vertices", std::move(vertices_)), + faces(this, uniquePrefix() + "faces", std::move(faces_)), surfaceColor(uniquePrefix() + "surfaceColor", getNextUniqueColor()), material(uniquePrefix() + "material", "clay"), backFacePolicy(uniquePrefix() + "backFacePolicy", BackFacePolicy::Different), diff --git a/src/sparse_volume_grid.cpp b/src/sparse_volume_grid.cpp index a5436a5e..0c6ec1f3 100644 --- a/src/sparse_volume_grid.cpp +++ b/src/sparse_volume_grid.cpp @@ -33,17 +33,17 @@ SparseVolumeGrid::SparseVolumeGrid(std::string name, glm::vec3 origin_, glm::vec : Structure(name, typeName()), // clang-format off // == managed quantities - cellPositions(this, uniquePrefix() + "#cellPositions", cellPositionsData, std::bind(&SparseVolumeGrid::computeCellPositions, this)), - cellIndices(this, uniquePrefix() + "#cellIndices", cellIndicesData, [](){/* do nothing, gets handled by computeCellPositions */}), + cellPositions(this, uniquePrefix() + "#cellPositions", std::bind(&SparseVolumeGrid::computeCellPositions, this)), + cellIndices(this, uniquePrefix() + "#cellIndices", [](){/* do nothing, gets handled by computeCellPositions */}), cornerNodeInds{ - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds0", cornerNodeIndsData[0]), - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds1", cornerNodeIndsData[1]), - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds2", cornerNodeIndsData[2]), - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds3", cornerNodeIndsData[3]), - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds4", cornerNodeIndsData[4]), - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds5", cornerNodeIndsData[5]), - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds6", cornerNodeIndsData[6]), - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds7", cornerNodeIndsData[7]), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds0", std::vector{}), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds1", std::vector{}), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds2", std::vector{}), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds3", std::vector{}), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds4", std::vector{}), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds5", std::vector{}), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds6", std::vector{}), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds7", std::vector{}), }, origin(origin_), gridCellWidth(gridCellWidth_), @@ -88,13 +88,13 @@ void SparseVolumeGrid::checkForDuplicateCells() { void SparseVolumeGrid::computeCellPositions() { size_t n = occupiedCellsData.size(); - cellPositionsData.resize(n); - cellIndicesData.resize(n); + cellPositions.data.resize(n); + cellIndices.data.resize(n); for (size_t i = 0; i < n; i++) { glm::ivec3 ijk = occupiedCellsData[i]; - cellPositionsData[i] = origin + (glm::vec3(ijk) + 0.5f) * gridCellWidth; - cellIndicesData[i] = ijk; + cellPositions.data[i] = origin + (glm::vec3(ijk) + 0.5f) * gridCellWidth; + cellIndices.data[i] = ijk; } cellPositions.markHostBufferUpdated(); @@ -142,7 +142,7 @@ void SparseVolumeGrid::computeCornerNodeIndices() { // Build corner index buffers using hashmap lookup for (int c = 0; c < 8; c++) { - cornerNodeIndsData[c].resize(n); + cornerNodeInds[c].data.resize(n); } for (size_t i = 0; i < n; i++) { @@ -152,7 +152,7 @@ void SparseVolumeGrid::computeCornerNodeIndices() { for (int dz = 0; dz < 2; dz++) { int c = dx * 4 + dy * 2 + dz; glm::ivec3 nodeIjk(ci.x + dx, ci.y + dy, ci.z + dz); - cornerNodeIndsData[c][i] = nodeToIndex[nodeIjk]; + cornerNodeInds[c].data[i] = nodeToIndex[nodeIjk]; } } } @@ -505,16 +505,16 @@ void SparseVolumeGrid::ensurePickProgramPrepared() { void SparseVolumeGrid::updateObjectSpaceBounds() { - if (cellPositionsData.empty()) { + if (cellPositions.data.empty()) { // no cells, degenerate bounds at origin objectSpaceBoundingBox = std::make_tuple(origin, origin); objectSpaceLengthScale = glm::length(gridCellWidth); return; } - glm::vec3 bboxMin = cellPositionsData[0]; - glm::vec3 bboxMax = cellPositionsData[0]; - for (const glm::vec3& p : cellPositionsData) { + glm::vec3 bboxMin = cellPositions.data[0]; + glm::vec3 bboxMax = cellPositions.data[0]; + for (const glm::vec3& p : cellPositions.data) { bboxMin = glm::min(bboxMin, p); bboxMax = glm::max(bboxMax, p); } @@ -557,7 +557,7 @@ SparseVolumeGridPickResult SparseVolumeGrid::interpretPickResult(const PickResul glm::vec3 localPos = (rawResult.position - origin) / gridCellWidth; // Find the cell index - glm::ivec3 cellInd3 = cellIndicesData[rawResult.localIndex]; + glm::ivec3 cellInd3 = cellIndices.data[rawResult.localIndex]; // Fractional position within cell [0,1] glm::vec3 fractional = localPos - glm::vec3(cellInd3); diff --git a/src/surface_mesh.cpp b/src/surface_mesh.cpp index a8fa78a1..986cec15 100644 --- a/src/surface_mesh.cpp +++ b/src/surface_mesh.cpp @@ -28,32 +28,32 @@ SurfaceMesh::SurfaceMesh(std::string name_) // == managed quantities // positions -vertexPositions( this, uniquePrefix() + "vertexPositions", vertexPositionsData), +vertexPositions( this, uniquePrefix() + "vertexPositions", std::vector{}), // connectivity / indices // (triangle and face inds are always computed initially when we triangulate the mesh) -triangleVertexInds( this, uniquePrefix() + "triangleVertexInds", triangleVertexIndsData), -triangleFaceInds( this, uniquePrefix() + "triangleFaceInds", triangleFaceIndsData), -triangleCornerInds( this, uniquePrefix() + "triangleCornerInds", triangleCornerIndsData, std::bind(&SurfaceMesh::computeTriangleCornerInds, this)), -triangleAllVertexInds( this, uniquePrefix() + "triangleAllVertexInds", triangleAllVertexIndsData, std::bind(&SurfaceMesh::computeTriangleAllVertexInds, this)), -triangleAllEdgeInds( this, uniquePrefix() + "triangleAllEdgeInds", triangleAllEdgeIndsData, std::bind(&SurfaceMesh::computeTriangleAllEdgeInds, this)), -triangleAllHalfedgeInds( this, uniquePrefix() + "triangleHalfedgeInds", triangleAllHalfedgeIndsData, std::bind(&SurfaceMesh::computeTriangleAllHalfedgeInds, this)), -triangleAllCornerInds( this, uniquePrefix() + "triangleAllCornerInds", triangleAllCornerIndsData, std::bind(&SurfaceMesh::computeTriangleAllCornerInds, this)), +triangleVertexInds( this, uniquePrefix() + "triangleVertexInds", std::vector{}), +triangleFaceInds( this, uniquePrefix() + "triangleFaceInds", std::vector{}), +triangleCornerInds( this, uniquePrefix() + "triangleCornerInds", std::bind(&SurfaceMesh::computeTriangleCornerInds, this)), +triangleAllVertexInds( this, uniquePrefix() + "triangleAllVertexInds", std::bind(&SurfaceMesh::computeTriangleAllVertexInds, this)), +triangleAllEdgeInds( this, uniquePrefix() + "triangleAllEdgeInds", std::bind(&SurfaceMesh::computeTriangleAllEdgeInds, this)), +triangleAllHalfedgeInds( this, uniquePrefix() + "triangleHalfedgeInds", std::bind(&SurfaceMesh::computeTriangleAllHalfedgeInds, this)), +triangleAllCornerInds( this, uniquePrefix() + "triangleAllCornerInds", std::bind(&SurfaceMesh::computeTriangleAllCornerInds, this)), // internal triangle data for rendering -baryCoord( this, uniquePrefix() + "baryCoord", baryCoordData), -edgeIsReal( this, uniquePrefix() + "edgeIsReal", edgeIsRealData), +baryCoord( this, uniquePrefix() + "baryCoord", std::vector{}), +edgeIsReal( this, uniquePrefix() + "edgeIsReal", std::vector{}), // other internally-computed geometry -faceNormals( this, uniquePrefix() + "faceNormals", faceNormalsData, std::bind(&SurfaceMesh::computeFaceNormals, this)), -faceCenters( this, uniquePrefix() + "faceCenters", faceCentersData, std::bind(&SurfaceMesh::computeFaceCenters, this)), -faceAreas( this, uniquePrefix() + "faceAreas", faceAreasData, std::bind(&SurfaceMesh::computeFaceAreas, this)), -vertexNormals( this, uniquePrefix() + "vertexNormals", vertexNormalsData, std::bind(&SurfaceMesh::computeVertexNormals, this)), -vertexAreas( this, uniquePrefix() + "vertexAreas", vertexAreasData, std::bind(&SurfaceMesh::computeVertexAreas, this)), +faceNormals( this, uniquePrefix() + "faceNormals", std::bind(&SurfaceMesh::computeFaceNormals, this)), +faceCenters( this, uniquePrefix() + "faceCenters", std::bind(&SurfaceMesh::computeFaceCenters, this)), +faceAreas( this, uniquePrefix() + "faceAreas", std::bind(&SurfaceMesh::computeFaceAreas, this)), +vertexNormals( this, uniquePrefix() + "vertexNormals", std::bind(&SurfaceMesh::computeVertexNormals, this)), +vertexAreas( this, uniquePrefix() + "vertexAreas", std::bind(&SurfaceMesh::computeVertexAreas, this)), // tangent spaces -defaultFaceTangentBasisX( this, uniquePrefix() + "defaultFaceTangentBasisX", defaultFaceTangentBasisXData, std::bind(&SurfaceMesh::computeDefaultFaceTangentBasisX, this)), -defaultFaceTangentBasisY( this, uniquePrefix() + "defaultFaceTangentBasisY", defaultFaceTangentBasisYData, std::bind(&SurfaceMesh::computeDefaultFaceTangentBasisY, this)), +defaultFaceTangentBasisX( this, uniquePrefix() + "defaultFaceTangentBasisX", std::bind(&SurfaceMesh::computeDefaultFaceTangentBasisX, this)), +defaultFaceTangentBasisY( this, uniquePrefix() + "defaultFaceTangentBasisY", std::bind(&SurfaceMesh::computeDefaultFaceTangentBasisY, this)), // == persistent options surfaceColor( uniquePrefix() + "surfaceColor", getNextUniqueColor()), @@ -71,7 +71,7 @@ SurfaceMesh::SurfaceMesh(std::string name_, const std::vector& vertex const std::vector& faceIndsEntries_, const std::vector& faceIndsStart_) : SurfaceMesh(name_) { - vertexPositionsData = vertexPositions_; + vertexPositions.data = vertexPositions_; faceIndsEntries = faceIndsEntries_; faceIndsStart = faceIndsStart_; @@ -84,7 +84,7 @@ SurfaceMesh::SurfaceMesh(std::string name_, const std::vector& vertex const std::vector>& facesIn) : SurfaceMesh(name_) { - vertexPositionsData = vertexPositions_; + vertexPositions.data = vertexPositions_; nestedFacesToFlat(facesIn); vertexPositions.checkInvalidValues(); @@ -114,14 +114,14 @@ void SurfaceMesh::computeConnectivityData() { nFacesTriangulationCount = nCornersCount - 2 * numFaces; // fill out these buffers as we construct the triangulation - triangleVertexIndsData.clear(); - triangleVertexIndsData.resize(3 * nFacesTriangulationCount); - triangleFaceIndsData.clear(); - triangleFaceIndsData.resize(3 * nFacesTriangulationCount); - baryCoordData.clear(); - baryCoordData.resize(3 * nFacesTriangulationCount); - edgeIsRealData.clear(); - edgeIsRealData.resize(3 * nFacesTriangulationCount); + triangleVertexInds.data.clear(); + triangleVertexInds.data.resize(3 * nFacesTriangulationCount); + triangleFaceInds.data.clear(); + triangleFaceInds.data.resize(3 * nFacesTriangulationCount); + baryCoord.data.clear(); + baryCoord.data.resize(3 * nFacesTriangulationCount); + edgeIsReal.data.clear(); + edgeIsReal.data.resize(3 * nFacesTriangulationCount); // validate the face-vertex indices for (size_t iV : faceIndsEntries) { @@ -144,17 +144,17 @@ void SurfaceMesh::computeConnectivityData() { uint32_t vC = faceIndsEntries[iStart + ((j + 1) % D)]; // triangle vertex indices - triangleVertexIndsData[3 * iTriFace + 0] = vRoot; - triangleVertexIndsData[3 * iTriFace + 1] = vB; - triangleVertexIndsData[3 * iTriFace + 2] = vC; + triangleVertexInds.data[3 * iTriFace + 0] = vRoot; + triangleVertexInds.data[3 * iTriFace + 1] = vB; + triangleVertexInds.data[3 * iTriFace + 2] = vC; // triangle face indices - for (size_t k = 0; k < 3; k++) triangleFaceIndsData[3 * iTriFace + k] = iF; + for (size_t k = 0; k < 3; k++) triangleFaceInds.data[3 * iTriFace + k] = iF; // barycentric coordinates - baryCoordData[3 * iTriFace + 0] = glm::vec3{1., 0., 0.}; - baryCoordData[3 * iTriFace + 1] = glm::vec3{0., 1., 0.}; - baryCoordData[3 * iTriFace + 2] = glm::vec3{0., 0., 1.}; + baryCoord.data[3 * iTriFace + 0] = glm::vec3{1., 0., 0.}; + baryCoord.data[3 * iTriFace + 1] = glm::vec3{0., 1., 0.}; + baryCoord.data[3 * iTriFace + 2] = glm::vec3{0., 0., 1.}; // internal edges for triangulated polygons glm::vec3 edgeRealV{0., 1., 0.}; @@ -164,7 +164,7 @@ void SurfaceMesh::computeConnectivityData() { if (j + 2 == D) { edgeRealV.z = 1.; } - for (size_t k = 0; k < 3; k++) edgeIsRealData[3 * iTriFace + k] = edgeRealV; + for (size_t k = 0; k < 3; k++) edgeIsReal.data[3 * iTriFace + k] = edgeRealV; iTriFace++; } diff --git a/src/volume_grid.cpp b/src/volume_grid.cpp index 15529f70..ae31a314 100644 --- a/src/volume_grid.cpp +++ b/src/volume_grid.cpp @@ -16,9 +16,9 @@ VolumeGrid::VolumeGrid(std::string name, glm::uvec3 gridNodeDim_, glm::vec3 boun // clang-format off // == managed quantities - gridPlaneReferencePositions(this, uniquePrefix() + "#gridPlaneReferencePositions", gridPlaneReferencePositionsData, std::bind(&VolumeGrid::computeGridPlaneReferenceGeometry, this)), - gridPlaneReferenceNormals(this, uniquePrefix() + "#gridPlaneReferenceNormals", gridPlaneReferenceNormalsData, [](){/* do nothing, gets handled by position func */} ), - gridPlaneAxisInds(this, uniquePrefix() + "#gridPlaneAxisInds", gridPlaneAxisIndsData, [](){/* do nothing, gets handled by position func */} ), + gridPlaneReferencePositions(this, uniquePrefix() + "#gridPlaneReferencePositions", std::bind(&VolumeGrid::computeGridPlaneReferenceGeometry, this)), + gridPlaneReferenceNormals(this, uniquePrefix() + "#gridPlaneReferenceNormals", [](){/* do nothing, gets handled by position func */} ), + gridPlaneAxisInds(this, uniquePrefix() + "#gridPlaneAxisInds", [](){/* do nothing, gets handled by position func */} ), gridNodeDim(gridNodeDim_), gridCellDim(gridNodeDim_ - 1u), boundMin(boundMin_), boundMax(boundMax_), diff --git a/src/volume_mesh.cpp b/src/volume_mesh.cpp index dcd6bc1d..3b8907fa 100644 --- a/src/volume_mesh.cpp +++ b/src/volume_mesh.cpp @@ -347,26 +347,25 @@ VolumeMesh::VolumeMesh(std::string name, const std::vector& vertexPos // == managed quantities // positions -vertexPositions( this, uniquePrefix() + "vertexPositions", vertexPositionsData), +vertexPositions( this, uniquePrefix() + "vertexPositions", std::vector(vertexPositions_)), // connectivity / indices -triangleVertexInds( this, uniquePrefix() + "triangleVertexInds", triangleVertexIndsData), -triangleFaceInds( this, uniquePrefix() + "triangleFaceInds", triangleFaceIndsData), -triangleCellInds( this, uniquePrefix() + "triangleCellInds", triangleCellIndsData), +triangleVertexInds( this, uniquePrefix() + "triangleVertexInds", std::vector{}), +triangleFaceInds( this, uniquePrefix() + "triangleFaceInds", std::vector{}), +triangleCellInds( this, uniquePrefix() + "triangleCellInds", std::vector{}), // internal triangle data for rendering -baryCoord( this, uniquePrefix() + "baryCoord", baryCoordData), -edgeIsReal( this, uniquePrefix() + "edgeIsReal", edgeIsRealData), -faceType( this, uniquePrefix() + "faceType", faceTypeData), +baryCoord( this, uniquePrefix() + "baryCoord", std::vector{}), +edgeIsReal( this, uniquePrefix() + "edgeIsReal", std::vector{}), +faceType( this, uniquePrefix() + "faceType", std::vector{}), // other internally-computed geometry -faceNormals( this, uniquePrefix() + "faceNormals", faceNormalsData, std::bind(&VolumeMesh::computeFaceNormals, this)), -cellCenters( this, uniquePrefix() + "cellCenters", cellCentersData, std::bind(&VolumeMesh::computeCellCenters, this)), +faceNormals( this, uniquePrefix() + "faceNormals", std::bind(&VolumeMesh::computeFaceNormals, this)), +cellCenters( this, uniquePrefix() + "cellCenters", std::bind(&VolumeMesh::computeCellCenters, this)), // == core input data cells(cellIndices_), -vertexPositionsData(vertexPositions_), // == persistent options color(uniquePrefix() + "color", getNextUniqueColor()), From e2a8ebc666385c17038d4e244959b65c32dc58b7 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Fri, 8 May 2026 23:29:32 -0700 Subject: [PATCH 03/31] ManagedBuffer now owns data arrays, rather than wrapping --- include/polyscope/color_quantity.ipp | 4 +- .../polyscope/color_render_image_quantity.h | 2 +- include/polyscope/curve_network.ipp | 4 +- .../polyscope/parameterization_quantity.ipp | 4 +- include/polyscope/point_cloud.ipp | 4 +- .../raw_color_alpha_render_image_quantity.h | 2 +- .../raw_color_render_image_quantity.h | 2 +- include/polyscope/render/managed_buffer.h | 121 +++++----- include/polyscope/scalar_quantity.ipp | 8 +- .../polyscope/scalar_render_image_quantity.h | 2 +- include/polyscope/simple_triangle_mesh.ipp | 17 +- include/polyscope/surface_mesh.ipp | 4 +- .../surface_parameterization_quantity.h | 3 +- include/polyscope/vector_quantity.ipp | 18 +- include/polyscope/volume_mesh.ipp | 4 +- src/curve_network.cpp | 27 ++- src/curve_network_color_quantity.cpp | 18 +- src/curve_network_scalar_quantity.cpp | 28 ++- src/point_cloud.cpp | 4 +- src/render/managed_buffer.cpp | 143 +++++++++--- src/render_image_quantity_base.cpp | 6 +- src/scalar_render_image_quantity.cpp | 6 +- src/simple_triangle_mesh.cpp | 4 +- src/slice_plane.cpp | 2 + src/sparse_volume_grid.cpp | 27 ++- src/surface_mesh.cpp | 216 +++++++++--------- src/surface_parameterization_quantity.cpp | 17 +- src/surface_vector_quantity.cpp | 31 ++- src/volume_grid.cpp | 37 +-- src/volume_grid_scalar_quantity.cpp | 5 +- src/volume_mesh.cpp | 66 +++--- src/volume_mesh_color_quantity.cpp | 9 +- src/volume_mesh_scalar_quantity.cpp | 27 ++- test/src/volume_grid_test.cpp | 2 +- 34 files changed, 504 insertions(+), 370 deletions(-) diff --git a/include/polyscope/color_quantity.ipp b/include/polyscope/color_quantity.ipp index 8d7b248f..e487c70f 100644 --- a/include/polyscope/color_quantity.ipp +++ b/include/polyscope/color_quantity.ipp @@ -27,7 +27,9 @@ template template void ColorQuantity::updateData(const V& newColors) { validateSize(newColors, colors.size(), "color quantity"); - colors.data = standardizeVectorArray(newColors); + auto d = standardizeVectorArray(newColors); + colors.resize(d.size()); + colors.setDataHost(d); colors.markHostBufferUpdated(); } diff --git a/include/polyscope/color_render_image_quantity.h b/include/polyscope/color_render_image_quantity.h index c2012460..74e578f2 100644 --- a/include/polyscope/color_render_image_quantity.h +++ b/include/polyscope/color_render_image_quantity.h @@ -57,7 +57,7 @@ void ColorRenderImageQuantity::updateBuffers(const T1& depthData, const T2& norm std::vector standardNormal(standardizeVectorArray(normalData)); std::vector standardColor(standardizeVectorArray(colorsData)); - colors.data = standardColor; + colors.setDataHost(standardColor); colors.markHostBufferUpdated(); updateBaseBuffers(standardDepth, standardNormal); diff --git a/include/polyscope/curve_network.ipp b/include/polyscope/curve_network.ipp index dc0dc906..1ff5573a 100644 --- a/include/polyscope/curve_network.ipp +++ b/include/polyscope/curve_network.ipp @@ -170,7 +170,9 @@ CurveNetwork* registerCurveNetworkLoop2D(std::string name, const P& nodes) { template void CurveNetwork::updateNodePositions(const V& newPositions) { validateSize(newPositions, nNodes(), "newPositions"); - nodePositions.data = standardizeVectorArray(newPositions); + auto d = standardizeVectorArray(newPositions); + nodePositions.resize(d.size()); + nodePositions.setDataHost(d); nodePositions.markHostBufferUpdated(); recomputeGeometryIfPopulated(); } diff --git a/include/polyscope/parameterization_quantity.ipp b/include/polyscope/parameterization_quantity.ipp index 4db3408d..86df18b2 100644 --- a/include/polyscope/parameterization_quantity.ipp +++ b/include/polyscope/parameterization_quantity.ipp @@ -226,7 +226,9 @@ template template void ParameterizationQuantity::updateCoords(const V& newCoords) { validateSize(newCoords, coords.size(), "parameterization quantity " + quantity.name); - coords.data = standardizeVectorArray(newCoords); + auto d = standardizeVectorArray(newCoords); + coords.resize(d.size()); + coords.setDataHost(d); coords.markHostBufferUpdated(); } diff --git a/include/polyscope/point_cloud.ipp b/include/polyscope/point_cloud.ipp index dd705aa6..f102aa96 100644 --- a/include/polyscope/point_cloud.ipp +++ b/include/polyscope/point_cloud.ipp @@ -36,7 +36,9 @@ PointCloud* registerPointCloud2D(std::string name, const T& points) { template void PointCloud::updatePointPositions(const V& newPositions) { validateSize(newPositions, nPoints(), "point cloud updated positions " + name); - points.data = standardizeVectorArray(newPositions); + auto d = standardizeVectorArray(newPositions); + points.resize(d.size()); + points.setDataHost(d); points.markHostBufferUpdated(); } diff --git a/include/polyscope/raw_color_alpha_render_image_quantity.h b/include/polyscope/raw_color_alpha_render_image_quantity.h index ce48e84f..4c801d3f 100644 --- a/include/polyscope/raw_color_alpha_render_image_quantity.h +++ b/include/polyscope/raw_color_alpha_render_image_quantity.h @@ -56,7 +56,7 @@ void RawColorAlphaRenderImageQuantity::updateBuffers(const T1& depthData, const std::vector standardNormal; std::vector standardColor(standardizeVectorArray(colorsData)); - colors.data = standardColor; + colors.setDataHost(standardColor); colors.markHostBufferUpdated(); updateBaseBuffers(standardDepth, standardNormal); diff --git a/include/polyscope/raw_color_render_image_quantity.h b/include/polyscope/raw_color_render_image_quantity.h index e9bdffa6..74f96563 100644 --- a/include/polyscope/raw_color_render_image_quantity.h +++ b/include/polyscope/raw_color_render_image_quantity.h @@ -53,7 +53,7 @@ void RawColorRenderImageQuantity::updateBuffers(const T1& depthData, const T2& c std::vector standardNormal; std::vector standardColor(standardizeVectorArray(colorsData)); - colors.data = standardColor; + colors.setDataHost(standardColor); colors.markHostBufferUpdated(); updateBaseBuffers(standardDepth, standardNormal); diff --git a/include/polyscope/render/managed_buffer.h b/include/polyscope/render/managed_buffer.h index d22ba370..7049890a 100644 --- a/include/polyscope/render/managed_buffer.h +++ b/include/polyscope/render/managed_buffer.h @@ -18,20 +18,18 @@ namespace render { class ManagedBufferRegistry; /* - * This class is a wrapper which sits on top of data buffers in Polyscope, and handles common data-management concerns - * of: + * This class owns and manages a typed data buffer in Polyscope, handling common data-management concerns of: * * (a) mirroring the buffer to the GPU/rendering framework * (b) allowing external users to update data either on the CPU- or GPU-side, and updating mirrored copies * appropriately - * (c) managed _indexed_ data, which gts expanded according to some index set for access at rendering time. + * (c) managing _indexed_ data, which gets expanded according to some index set for access at rendering time. * - * Most often this class is used to wrap structure/quantity data passed in by the user, such as a scalar quantity, but - * it is also sometimes wraps automatically-computed values within Polyscope, such as a vertex normal buffer for + * Most often this class is used to hold structure/quantity data passed in by the user, such as a scalar quantity, but + * it also sometimes holds automatically-computed values within Polyscope, such as a vertex normal buffer for * rendering. * - * This class offers functions and accessors which can (and generally, MUST) be used to interact with the underlying - * data buffer. + * Use the public accessor API (getHostValue, setHostValue, setDataHost, begin/end, etc.) to interact with the buffer. */ template class ManagedBuffer : public virtual WeakReferrable { @@ -63,31 +61,6 @@ class ManagedBuffer : public virtual WeakReferrable { ManagedBufferRegistry* registry; - // The raw underlying buffer which this class owns and holds the data. - // - // It is possible that data.size() == 0 if the data is lazily computed and has not been computed yet, or if this - // host-side buffer is invalidated because it is being updated externally directly on the render device. - // - // External users can write directly for this buffer. The required order of operations for writing to this buffer is: - // buff.ensureHostBufferAllocated(); - // buff.data = // fill .data with your values - // buff.markHostBufferUpdated(); - // - // - std::vector data; - - // == Members for computed data - - // This class somewhat weirdly tracks data from two separate sources: - // A) Values directly stored in `data` by an external source - // B) Values that get computed lazily by some function when needed - // - // When `dataGetsComputed = true`, we are in Case B, and computeFunc() must be set to a callback that does the - // lazy computing. - - bool dataGetsComputed; // if true, the value gets computed on-demand by calling computeFunc() - std::function computeFunc; // (optional) callback which populates the `data` buffer - // sanity check helper void checkInvalidValues(); @@ -108,7 +81,7 @@ class ManagedBuffer : public virtual WeakReferrable { // But in settings where we e.g. incrementally add elements or change the number of data elements on each frame, we // may want to allocate a larger capacity to avoid expensive re-allocation each time. - // Resize the buffer to newSize elements. + // Resize the buffer to newSize elements. Sets hostBufferIsPopulated = true. // // If newSize <= capacity(), this is a cheap constant-time operation which just updates metadata. // @@ -140,33 +113,46 @@ class ManagedBuffer : public virtual WeakReferrable { // doubling, this sets the logical capacity to a precise value. Error if newCapacity < size(). // Reallocates the GPU buffer in-place (same buffer object, new backing memory) if one exists. // - // Note: data.capacity() is guaranteed to be >= managedCapacity, but may remain larger than - // newCapacity if the underlying vector already had more space allocated. - // // Valid for attributes and 1D textures only; multidimensional textures always have capacity // equal to their size, so use the 2D/3D resize() variants instead. void setCapacity(size_t newCapacity); - // == Members for indexed data - // == Basic interactions - // Ensure that the `data` member vector reference is populated with the current values. In the common-case where - // the user sets data and it never changes, then this function will do nothing. However, if e.g. the value is - // being updated directly from GPU memory, this will mirror the updates to the cpu-side vector. Also, if the value - // is lazily computed by computeFunc(), it ensures that that function has been called. + // Ensure that the host buffer is populated with the current values. In the common case where the user sets data + // and it never changes, this does nothing. However, if the value is being updated directly from GPU memory, this + // mirrors the updates to the host-side buffer. Also, if the value is lazily computed by computeFunc(), it ensures + // that function has been called. void ensureHostBufferPopulated(); - // Ensure that the `data` member has the proper size. This does _not_ populate the buffer with any particular data, - // just ensures it is allocated. It is useful for when an external wants to fill the buffer with data. - void ensureHostBufferAllocated(); - - // Combines calling ensureHostBufferPopulated() and returning a reference to the `data` member - std::vector& getPopulatedHostBufferRef(); - - // If the contents of `data` are updated, this function MUST be called. It internally handles concerns like - // reflecting updates to the render buffer. + // Copy newData into the host buffer. Errors if newData.size() > capacity() — call resize() or + // setCapacity() first if needed. Does not change capacity. Automatically syncs to any allocated + // device buffers and triggers a redraw. Prefer this over setHostValue() for bulk writes. + void setDataHost(const std::vector& newData); + + // Iterator support. Caller must call ensureHostBufferPopulated() before using these. + // A debug-mode check will fire if this precondition is violated. + const T* begin() const; + const T* end() const; + + // Single-element read. Caller MUST call ensureHostBufferPopulated() before calling this + // (especially in loops — call it once before the loop, not per element). + // A debug-mode check will fire if this precondition is violated. + T getHostValue(size_t ind) const; + T getHostValue(size_t indX, size_t indY) const; // only valid for 2d texture data + T getHostValue(size_t indX, size_t indY, size_t indZ) const; // only valid for 3d texture data + + // Single-element write. Caller MUST call ensureHostBufferPopulated() (or resize()) before + // calling this. Automatically syncs to any allocated device buffers and triggers a redraw. + // For bulk writes, prefer setDataHost() to avoid redundant GPU uploads. + // A debug-mode check will fire if the precondition is violated. + void setHostValue(size_t ind, T val); + void setHostValue(size_t indX, size_t indY, T val); // only valid for 2d texture data + void setHostValue(size_t indX, size_t indY, size_t indZ, T val); // only valid for 3d texture data + + // If the contents of the host buffer have been updated externally, this function MUST be called. + // It internally handles concerns like reflecting updates to the render buffer. void markHostBufferUpdated(); // Get the value at index `i`. It may be dynamically fetched from either the cpu-side `data` member or the render @@ -247,6 +233,17 @@ class ManagedBuffer : public virtual WeakReferrable { protected: // == Internal members + // This class tracks data from two separate sources: + // A) Values directly stored in the buffer by an external source (dataGetsComputed == false) + // B) Values that get computed lazily by some function when needed (dataGetsComputed == true) + // + // When dataGetsComputed is true, computeFunc() must be set to a callback that populates the buffer. + // The callback should call resize() then setHostValue() to populate the buffer. + // The callback does NOT need to call markHostBufferUpdated() — that is handled by the ManagedBuffer + // infrastructure after the callback returns. + bool dataGetsComputed; // if true, the value gets computed on-demand by calling computeFunc() + std::function computeFunc; // (optional) callback which populates the buffer + bool hostBufferIsPopulated; // true if the host buffer contains currently-valid data // The buffer has capacity for at least this many elements. It is distinct from .size(), which is the actual number of @@ -278,17 +275,27 @@ class ManagedBuffer : public virtual WeakReferrable { // == Internal helper functions void invalidateHostBuffer(); - bool deviceBufferTypeIsTexture(); - void checkDeviceBufferTypeIs(DeviceBufferType targetType); - void checkDeviceBufferTypeIsTexture(); + bool deviceBufferTypeIsTexture() const; + void checkDeviceBufferTypeIs(DeviceBufferType targetType) const; + void checkDeviceBufferTypeIsTexture() const; enum class CanonicalDataSource { HostData = 0, NeedsCompute, RenderBuffer }; CanonicalDataSource currentCanonicalDataSource(); - // Manage the program which copies indexed data from the renderBuffer to the indexed views - void ensureHaveBufferIndexCopyProgram(); - void invokeBufferIndexCopyProgram(); + // TODO: add direct GPU-side indexed copy support using a transform-feedback/compute shader program. std::shared_ptr bufferIndexCopyProgram; + +private: + template friend class ManagedBuffer; + + // The raw underlying buffer which this class owns and holds the data. + // + // It is possible that data.size() == 0 if the data is lazily computed and has not been computed yet, or if this + // host-side buffer is invalidated because it is being updated externally directly on the render device. + // + // Use the accessor API (getHostValue, setHostValue, setDataHost, begin/end) to interact + // with this buffer rather than accessing it directly. + std::vector data; }; diff --git a/include/polyscope/scalar_quantity.ipp b/include/polyscope/scalar_quantity.ipp index 04c3ca69..83af60f8 100644 --- a/include/polyscope/scalar_quantity.ipp +++ b/include/polyscope/scalar_quantity.ipp @@ -9,7 +9,7 @@ namespace polyscope { template ScalarQuantity::ScalarQuantity(QuantityT& quantity_, const std::vector& values_, DataType dataType_) : quantity(quantity_), values(&quantity, quantity.uniquePrefix() + "values", std::vector(values_)), - dataType(dataType_), dataRange(robustMinMax(values.data, 1e-5)), + dataType(dataType_), dataRange(robustMinMax(values_, 1e-5)), vizRangeMin(quantity.uniquePrefix() + "vizRangeMin", -777.), // set later, vizRangeMax(quantity.uniquePrefix() + "vizRangeMax", -777.), // including clearing cache colorBar(quantity), cMap(quantity.uniquePrefix() + "cmap", defaultColorMap(dataType)), @@ -23,7 +23,7 @@ ScalarQuantity::ScalarQuantity(QuantityT& quantity_, const std::vecto { values.checkInvalidValues(); colorBar.updateColormap(cMap.get()); - colorBar.buildHistogram(values.data, dataType); + colorBar.buildHistogram(values_, dataType); // TODO: I think we might be building the histogram ^^^ twice for many quantities if (vizRangeMin.holdsDefaultValue()) { // min and max should always have same cache state @@ -312,7 +312,9 @@ template template void ScalarQuantity::updateData(const V& newValues) { validateSize(newValues, values.size(), "scalar quantity " + quantity.name); - values.data = standardizeArray(newValues); + auto d = standardizeArray(newValues); + values.resize(d.size()); + values.setDataHost(d); values.markHostBufferUpdated(); } diff --git a/include/polyscope/scalar_render_image_quantity.h b/include/polyscope/scalar_render_image_quantity.h index 50851e7e..fed2fba0 100644 --- a/include/polyscope/scalar_render_image_quantity.h +++ b/include/polyscope/scalar_render_image_quantity.h @@ -53,7 +53,7 @@ void ScalarRenderImageQuantity::updateBuffers(const T1& depthData, const T2& nor std::vector standardNormal(standardizeVectorArray(normalData)); std::vector standardScalar(standardizeArray(scalarData)); - values.data = standardScalar; + values.setDataHost(standardScalar); values.markHostBufferUpdated(); updateBaseBuffers(standardDepth, standardNormal); diff --git a/include/polyscope/simple_triangle_mesh.ipp b/include/polyscope/simple_triangle_mesh.ipp index dc284bef..ca10bc2c 100644 --- a/include/polyscope/simple_triangle_mesh.ipp +++ b/include/polyscope/simple_triangle_mesh.ipp @@ -26,17 +26,26 @@ SimpleTriangleMesh* registerSimpleTriangleMesh(std::string name, const V& vertex template void SimpleTriangleMesh::updateVertices(const V& newPositions) { validateSize(newPositions, vertices.size(), "newPositions"); - vertices.data = standardizeVectorArray(newPositions); + auto d = standardizeVectorArray(newPositions); + vertices.resize(d.size()); + vertices.setDataHost(d); vertices.markHostBufferUpdated(); } template void SimpleTriangleMesh::update(const V& newPositions, const F& newFaces) { - - vertices.data = standardizeVectorArray(newPositions); + { + auto d = standardizeVectorArray(newPositions); + vertices.resize(d.size()); + vertices.setDataHost(d); + } vertices.markHostBufferUpdated(); - faces.data = standardizeVectorArray(newFaces); + { + auto d = standardizeVectorArray(newFaces); + faces.resize(d.size()); + faces.setDataHost(d); + } faces.markHostBufferUpdated(); } diff --git a/include/polyscope/surface_mesh.ipp b/include/polyscope/surface_mesh.ipp index 7d35e279..cc42bfa1 100644 --- a/include/polyscope/surface_mesh.ipp +++ b/include/polyscope/surface_mesh.ipp @@ -63,7 +63,9 @@ SurfaceMesh* registerSurfaceMesh(std::string name, const V& vertexPositions, con template void SurfaceMesh::updateVertexPositions(const V& newPositions) { validateSize(newPositions, vertexDataSize, "newPositions"); - vertexPositions.data = standardizeVectorArray(newPositions); + auto d = standardizeVectorArray(newPositions); + vertexPositions.resize(d.size()); + vertexPositions.setDataHost(d); vertexPositions.markHostBufferUpdated(); recomputeGeometryIfPopulated(); } diff --git a/include/polyscope/surface_parameterization_quantity.h b/include/polyscope/surface_parameterization_quantity.h index 656c5a9d..40c3d15c 100644 --- a/include/polyscope/surface_parameterization_quantity.h +++ b/include/polyscope/surface_parameterization_quantity.h @@ -94,7 +94,8 @@ class SurfaceVertexParameterizationQuantity : public SurfaceParameterizationQuan template void SurfaceParameterizationQuantity::setIslandLabels(const V& newIslandLabels) { validateSize(newIslandLabels, this->nFaces(), "scalar quantity " + quantity.name); - islandLabels.data = standardizeArray(newIslandLabels); + islandLabels.resize(this->nFaces()); + islandLabels.setDataHost(standardizeArray(newIslandLabels)); islandLabels.markHostBufferUpdated(); islandLabelsPopulated = true; } diff --git a/include/polyscope/vector_quantity.ipp b/include/polyscope/vector_quantity.ipp index 9721dc13..d3db2c3c 100644 --- a/include/polyscope/vector_quantity.ipp +++ b/include/polyscope/vector_quantity.ipp @@ -187,7 +187,7 @@ void VectorQuantity::updateMaxLength() { vectors.ensureHostBufferPopulated(); float maxLength = 0.; - for (const glm::vec3& vec : vectors.data) { + for (const glm::vec3& vec : vectors) { maxLength = std::max(maxLength, glm::length(vec)); } this->vectorLengthRange = maxLength; @@ -202,7 +202,9 @@ template template void VectorQuantity::updateData(const T& newVectors) { validateSize(newVectors, this->vectors.size(), "vector quantity " + this->quantity.name); - this->vectors.data = standardizeVectorArray(newVectors); + auto d = standardizeVectorArray(newVectors); + this->vectors.resize(d.size()); + this->vectors.setDataHost(d); this->vectors.markHostBufferUpdated(); this->updateMaxLength(); } @@ -211,10 +213,12 @@ template template void VectorQuantity::updateData2D(const T& newVectors) { validateSize(newVectors, this->vectors.size(), "vector quantity " + this->quantity.name); - this->vectors.data = standardizeVectorArray(newVectors); - for (auto& v : this->vectors.data) { + auto vectors3D = standardizeVectorArray(newVectors); + for (auto& v : vectors3D) { v.z = 0.; } + this->vectors.resize(vectors3D.size()); + this->vectors.setDataHost(vectors3D); this->vectors.markHostBufferUpdated(); this->updateMaxLength(); } @@ -309,7 +313,7 @@ void TangentVectorQuantity::updateMaxLength() { tangentVectors.ensureHostBufferPopulated(); float maxLength = 0.; - for (const glm::vec2& vec : tangentVectors.data) { + for (const glm::vec2& vec : tangentVectors) { maxLength = std::max(maxLength, glm::length(vec)); } this->vectorLengthRange = maxLength; @@ -324,7 +328,9 @@ template template void TangentVectorQuantity::updateData(const T& newVectors) { validateSize(newVectors, this->tangentVectors.size(), "tangent vector quantity " + this->quantity.name); - this->tangentVectors.data = standardizeVectorArray(newVectors); + auto d = standardizeVectorArray(newVectors); + this->tangentVectors.resize(d.size()); + this->tangentVectors.setDataHost(d); this->tangentVectors.markHostBufferUpdated(); this->updateMaxLength(); } diff --git a/include/polyscope/volume_mesh.ipp b/include/polyscope/volume_mesh.ipp index 3bff6e18..60bf5264 100644 --- a/include/polyscope/volume_mesh.ipp +++ b/include/polyscope/volume_mesh.ipp @@ -87,7 +87,9 @@ VolumeMesh* registerTetHexMesh(std::string name, const V& vertexPositions, const template void VolumeMesh::updateVertexPositions(const V& newPositions) { validateSize(newPositions, nVertices(), "newPositions"); - vertexPositions.data = standardizeVectorArray(newPositions); + auto d = standardizeVectorArray(newPositions); + vertexPositions.resize(d.size()); + vertexPositions.setDataHost(d); vertexPositions.markHostBufferUpdated(); geometryChanged(); } diff --git a/src/curve_network.cpp b/src/curve_network.cpp index 6a3ef2fd..a6a1b580 100644 --- a/src/curve_network.cpp +++ b/src/curve_network.cpp @@ -33,8 +33,8 @@ CurveNetwork::CurveNetwork(std::string name, std::vector nodes_, std: nodePositions.checkInvalidValues(); // Copy interleaved data in to tip and tails buffers below - edgeTailInds.data.resize(edges_.size()); - edgeTipInds.data.resize(edges_.size()); + edgeTailInds.resize(edges_.size()); + edgeTipInds.resize(edges_.size()); // Compute node degrees; some quantities want them for visualizations nodeDegrees = std::vector(nNodes(), 0); @@ -45,8 +45,8 @@ CurveNetwork::CurveNetwork(std::string name, std::vector nodes_, std: size_t nA = std::get<0>(edge); size_t nB = std::get<1>(edge); - edgeTailInds.data[iE] = nA; - edgeTipInds.data[iE] = nB; + edgeTailInds.setHostValue(iE, nA); + edgeTipInds.setHostValue(iE, nB); // Make sure there are no out of bounds indices if (nA >= maxInd || nB >= maxInd) { @@ -328,8 +328,8 @@ void CurveNetwork::preparePick() { // Fill posiiton and pick index buffers for (size_t iE = 0; iE < nEdges(); iE++) { - size_t eTail = edgeTailInds.data[iE]; - size_t eTip = edgeTipInds.data[iE]; + size_t eTail = edgeTailInds.getHostValue(iE); + size_t eTip = edgeTipInds.getHostValue(iE); glm::vec3 colorValTail = pick::indToVec(pickStart + eTail); glm::vec3 colorValTip = pick::indToVec(pickStart + eTip); @@ -389,16 +389,15 @@ void CurveNetwork::computeEdgeCenters() { edgeTailInds.ensureHostBufferPopulated(); edgeTipInds.ensureHostBufferPopulated(); - edgeCenters.data.resize(nEdges()); + edgeCenters.resize(nEdges()); for (size_t iE = 0; iE < nEdges(); iE++) { - size_t eTail = edgeTailInds.data[iE]; - size_t eTip = edgeTipInds.data[iE]; - glm::vec3 p = 0.5f * (nodePositions.data[eTail] + nodePositions.data[eTip]); - edgeCenters.data[iE] = p; + size_t eTail = edgeTailInds.getHostValue(iE); + size_t eTip = edgeTipInds.getHostValue(iE); + glm::vec3 p = 0.5f * (nodePositions.getHostValue(eTail) + nodePositions.getHostValue(eTip)); + edgeCenters.setHostValue(iE, p); } - edgeCenters.markHostBufferUpdated(); } void CurveNetwork::refresh() { @@ -539,7 +538,7 @@ void CurveNetwork::updateObjectSpaceBounds() { // bounding box glm::vec3 min = glm::vec3{1, 1, 1} * std::numeric_limits::infinity(); glm::vec3 max = -glm::vec3{1, 1, 1} * std::numeric_limits::infinity(); - for (const glm::vec3& p : nodePositions.data) { + for (const glm::vec3& p : nodePositions) { min = componentwiseMin(min, p); max = componentwiseMax(max, p); } @@ -548,7 +547,7 @@ void CurveNetwork::updateObjectSpaceBounds() { // length scale, as twice the radius from the center of the bounding box glm::vec3 center = 0.5f * (min + max); float lengthScale = 0.0; - for (const glm::vec3& p : nodePositions.data) { + for (const glm::vec3& p : nodePositions) { lengthScale = std::max(lengthScale, glm::length2(p - center)); } objectSpaceLengthScale = 2 * std::sqrt(lengthScale); diff --git a/src/curve_network_color_quantity.cpp b/src/curve_network_color_quantity.cpp index 22486ee6..3ec89cb2 100644 --- a/src/curve_network_color_quantity.cpp +++ b/src/curve_network_color_quantity.cpp @@ -157,20 +157,24 @@ void CurveNetworkEdgeColorQuantity::updateNodeAverageColors() { parent.edgeTailInds.ensureHostBufferPopulated(); parent.edgeTipInds.ensureHostBufferPopulated(); colors.ensureHostBufferPopulated(); - nodeAverageColors.data.resize(parent.nNodes()); + nodeAverageColors.resize(parent.nNodes()); + nodeAverageColors.ensureHostBufferPopulated(); + + // initialize to zero before accumulation + for (size_t iN = 0; iN < parent.nNodes(); iN++) nodeAverageColors.setHostValue(iN, glm::vec3{0., 0., 0.}); for (size_t iE = 0; iE < parent.nEdges(); iE++) { - size_t eTail = parent.edgeTailInds.data[iE]; - size_t eTip = parent.edgeTipInds.data[iE]; + size_t eTail = parent.edgeTailInds.getHostValue(iE); + size_t eTip = parent.edgeTipInds.getHostValue(iE); - nodeAverageColors.data[eTail] += colors.data[iE]; - nodeAverageColors.data[eTip] += colors.data[iE]; + nodeAverageColors.setHostValue(eTail, nodeAverageColors.getHostValue(eTail) + colors.getHostValue(iE)); + nodeAverageColors.setHostValue(eTip, nodeAverageColors.getHostValue(eTip) + colors.getHostValue(iE)); } for (size_t iN = 0; iN < parent.nNodes(); iN++) { - nodeAverageColors.data[iN] /= parent.nodeDegrees[iN]; + nodeAverageColors.setHostValue(iN, nodeAverageColors.getHostValue(iN) / (float)parent.nodeDegrees[iN]); if (parent.nodeDegrees[iN] == 0) { - nodeAverageColors.data[iN] = glm::vec3{0., 0., 0.}; + nodeAverageColors.setHostValue(iN, glm::vec3{0., 0., 0.}); } } diff --git a/src/curve_network_scalar_quantity.cpp b/src/curve_network_scalar_quantity.cpp index fd70deba..7d8bc141 100644 --- a/src/curve_network_scalar_quantity.cpp +++ b/src/curve_network_scalar_quantity.cpp @@ -182,7 +182,8 @@ void CurveNetworkEdgeScalarQuantity::updateNodeAverageValues() { parent.edgeTailInds.ensureHostBufferPopulated(); parent.edgeTipInds.ensureHostBufferPopulated(); values.ensureHostBufferPopulated(); - nodeAverageValues.data.resize(parent.nNodes()); + nodeAverageValues.resize(parent.nNodes()); + nodeAverageValues.ensureHostBufferPopulated(); if (dataType == DataType::CATEGORICAL) { // uncommon case: take the mode of adjacent values @@ -199,11 +200,11 @@ void CurveNetworkEdgeScalarQuantity::updateNodeAverageValues() { }; for (size_t iE = 0; iE < parent.nEdges(); iE++) { - size_t eTail = parent.edgeTailInds.data[iE]; - size_t eTip = parent.edgeTipInds.data[iE]; + size_t eTail = parent.edgeTailInds.getHostValue(iE); + size_t eTip = parent.edgeTipInds.getHostValue(iE); - incrementValueCount(eTail, values.data[iE]); - incrementValueCount(eTip, values.data[iE]); + incrementValueCount(eTail, values.getHostValue(iE)); + incrementValueCount(eTip, values.getHostValue(iE)); } for (size_t iN = 0; iN < parent.nNodes(); iN++) { @@ -216,24 +217,27 @@ void CurveNetworkEdgeScalarQuantity::updateNodeAverageValues() { maxVal = entry.first; } } - nodeAverageValues.data[iN] = maxVal; + nodeAverageValues.setHostValue(iN, maxVal); } } else { // common case: take the mean of adjacent values + // initialize to zero before accumulation + for (size_t iN = 0; iN < parent.nNodes(); iN++) nodeAverageValues.setHostValue(iN, 0.f); + // mean reduction for (size_t iE = 0; iE < parent.nEdges(); iE++) { - size_t eTail = parent.edgeTailInds.data[iE]; - size_t eTip = parent.edgeTipInds.data[iE]; + size_t eTail = parent.edgeTailInds.getHostValue(iE); + size_t eTip = parent.edgeTipInds.getHostValue(iE); - nodeAverageValues.data[eTail] += values.data[iE]; - nodeAverageValues.data[eTip] += values.data[iE]; + nodeAverageValues.setHostValue(eTail, nodeAverageValues.getHostValue(eTail) + values.getHostValue(iE)); + nodeAverageValues.setHostValue(eTip, nodeAverageValues.getHostValue(eTip) + values.getHostValue(iE)); } for (size_t iN = 0; iN < parent.nNodes(); iN++) { - nodeAverageValues.data[iN] /= parent.nodeDegrees[iN]; + nodeAverageValues.setHostValue(iN, nodeAverageValues.getHostValue(iN) / parent.nodeDegrees[iN]); if (parent.nodeDegrees[iN] == 0) { - nodeAverageValues.data[iN] = 0.; + nodeAverageValues.setHostValue(iN, 0.f); } } } diff --git a/src/point_cloud.cpp b/src/point_cloud.cpp index caa92dfc..85251730 100644 --- a/src/point_cloud.cpp +++ b/src/point_cloud.cpp @@ -385,7 +385,7 @@ void PointCloud::updateObjectSpaceBounds() { // bounding box glm::vec3 min = glm::vec3{1, 1, 1} * std::numeric_limits::infinity(); glm::vec3 max = -glm::vec3{1, 1, 1} * std::numeric_limits::infinity(); - for (const glm::vec3& p : points.data) { + for (const glm::vec3& p : points) { min = componentwiseMin(min, p); max = componentwiseMax(max, p); } @@ -394,7 +394,7 @@ void PointCloud::updateObjectSpaceBounds() { // length scale, as twice the radius from the center of the bounding box glm::vec3 center = 0.5f * (min + max); float lengthScale = 0.0; - for (const glm::vec3& p : points.data) { + for (const glm::vec3& p : points) { lengthScale = std::max(lengthScale, glm::length2(p - center)); } objectSpaceLengthScale = 2 * std::sqrt(lengthScale); diff --git a/src/render/managed_buffer.cpp b/src/render/managed_buffer.cpp index 65a54df6..793354bf 100644 --- a/src/render/managed_buffer.cpp +++ b/src/render/managed_buffer.cpp @@ -105,7 +105,12 @@ bool ManagedBuffer::resize(size_t newSize) { // Copy device-side data back to host BEFORE modifying data or invalidating the GPU buffer. // ensureHostBufferPopulated() reads from renderAttributeBuffer into data; if we resize data // first it would overwrite the resize with the old GPU contents. - ensureHostBufferPopulated(); + // Only copy existing data to host if there is data to preserve. + // If state is NeedsCompute, there is no existing data — calling ensureHostBufferPopulated() + // would recursively invoke the compute function that is currently running. + if (currentCanonicalDataSource() != CanonicalDataSource::NeedsCompute) { + ensureHostBufferPopulated(); + } // Reallocation needed: use amortized doubling size_t newCapacity = std::max(newSize, 2 * managedCapacity); @@ -131,6 +136,7 @@ bool ManagedBuffer::resize(size_t newSize) { } else { // No reallocation: just update size metadata data.resize(newSize); + hostBufferIsPopulated = true; if (deviceBufferType == DeviceBufferType::Texture1d) { sizeX = static_cast(newSize); @@ -151,7 +157,10 @@ void ManagedBuffer::setCapacity(size_t newCapacity) { if (newCapacity == managedCapacity) return; // no-op // Before invalidating the GPU buffer, copy any device-side changes back to the host. - ensureHostBufferPopulated(); + // Skip if state is NeedsCompute — there's no existing data to preserve. + if (currentCanonicalDataSource() != CanonicalDataSource::NeedsCompute) { + ensureHostBufferPopulated(); + } data.reserve(newCapacity); managedCapacity = newCapacity; @@ -247,31 +256,115 @@ void ManagedBuffer::ensureHostBufferPopulated() { } template -void ManagedBuffer::ensureHostBufferAllocated() { - data.resize(size()); +const T* ManagedBuffer::begin() const { +#ifndef NDEBUG + if (!hostBufferIsPopulated) + exception("ManagedBuffer " + name + " begin() called without ensureHostBufferPopulated()"); +#endif + return data.data(); } template -std::vector& ManagedBuffer::getPopulatedHostBufferRef() { - ensureHostBufferPopulated(); - return data; +const T* ManagedBuffer::end() const { +#ifndef NDEBUG + if (!hostBufferIsPopulated) + exception("ManagedBuffer " + name + " end() called without ensureHostBufferPopulated()"); +#endif + return data.data() + data.size(); } template -void ManagedBuffer::markHostBufferUpdated() { +void ManagedBuffer::setDataHost(const std::vector& newData) { + if (newData.size() > managedCapacity) + exception("ManagedBuffer " + name + " setDataHost() called with data that exceeds capacity (" + + std::to_string(newData.size()) + " > " + std::to_string(managedCapacity) + "). Call resize() or setCapacity() first."); + data.assign(newData.begin(), newData.end()); hostBufferIsPopulated = true; - // If the data is stored in the device-side buffers, update it as needed + // Sync to any already-allocated device buffers and update indexed views. if (renderAttributeBuffer) { renderAttributeBuffer->setData(data); requestRedraw(); } + if (renderTextureBuffer) { + renderTextureBuffer->setData(data); + requestRedraw(); + } + if (deviceBufferType == DeviceBufferType::Attribute) { + updateIndexedViews(); + requestRedraw(); + } +} +template +T ManagedBuffer::getHostValue(size_t ind) const { +#ifndef NDEBUG + if (!hostBufferIsPopulated) + exception("ManagedBuffer " + name + " getHostValue() called without ensureHostBufferPopulated()"); +#endif + return data[ind]; +} + +template +T ManagedBuffer::getHostValue(size_t indX, size_t indY) const { + checkDeviceBufferTypeIs(DeviceBufferType::Texture2d); + return getHostValue(sizeY * indX + indY); +} + +template +T ManagedBuffer::getHostValue(size_t indX, size_t indY, size_t indZ) const { + checkDeviceBufferTypeIs(DeviceBufferType::Texture3d); + return getHostValue(sizeZ * sizeY * indX + sizeZ * indY + indZ); +} + +template +void ManagedBuffer::setHostValue(size_t ind, T val) { +#ifndef NDEBUG + if (!hostBufferIsPopulated) + exception("ManagedBuffer " + name + " setHostValue() called without ensureHostBufferPopulated()"); +#endif + data[ind] = val; + + // Sync to any already-allocated device buffers. Note: this re-uploads the entire buffer; for + // bulk writes, prefer setDataHost() to avoid repeated GPU uploads. + if (renderAttributeBuffer) { + renderAttributeBuffer->setData(data); + requestRedraw(); + } if (renderTextureBuffer) { renderTextureBuffer->setData(data); requestRedraw(); } + if (deviceBufferType == DeviceBufferType::Attribute) { + updateIndexedViews(); + requestRedraw(); + } +} + +template +void ManagedBuffer::setHostValue(size_t indX, size_t indY, T val) { + checkDeviceBufferTypeIs(DeviceBufferType::Texture2d); + setHostValue(sizeY * indX + indY, val); +} + +template +void ManagedBuffer::setHostValue(size_t indX, size_t indY, size_t indZ, T val) { + checkDeviceBufferTypeIs(DeviceBufferType::Texture3d); + setHostValue(sizeZ * sizeY * indX + sizeZ * indY + indZ, val); +} +template +void ManagedBuffer::markHostBufferUpdated() { + hostBufferIsPopulated = true; + + if (renderAttributeBuffer) { + renderAttributeBuffer->setData(data); + requestRedraw(); + } + if (renderTextureBuffer) { + renderTextureBuffer->setData(data); + requestRedraw(); + } if (deviceBufferType == DeviceBufferType::Attribute) { updateIndexedViews(); requestRedraw(); @@ -291,14 +384,12 @@ T ManagedBuffer::getValue(size_t ind) { if (ind >= data.size()) exception("out of bounds access in ManagedBuffer " + name + " getValue(" + std::to_string(ind) + ")"); return data[ind]; - break; case CanonicalDataSource::NeedsCompute: computeFunc(); if (ind >= data.size()) exception("out of bounds access in ManagedBuffer " + name + " getValue(" + std::to_string(ind) + ")"); return data[ind]; - break; case CanonicalDataSource::RenderBuffer: @@ -309,9 +400,7 @@ T ManagedBuffer::getValue(size_t ind) { if (static_cast(ind) >= renderAttributeBuffer->getDataSize()) exception("out of bounds access in ManagedBuffer " + name + " getValue(" + std::to_string(ind) + ")"); - T val = getAttributeBufferData(*renderAttributeBuffer, ind); - return val; - break; + return getAttributeBufferData(*renderAttributeBuffer, ind); }; return T(); // dummy return @@ -563,8 +652,8 @@ void ManagedBuffer::updateIndexedViews() { std::vector expandData = gather(data, indices.data); viewBuffer.setData(expandData); - // TODO fornow, only CPU-side updating is supported. Add direct GPU-side support using the bufferIndexCopyProgram - // below. + // TODO: add direct GPU-side indexed copy support (without round-tripping through the host) using a + // transform-feedback/compute shader program, to avoid the CPU gather cost for GPU-resident buffers. } requestRedraw(); @@ -591,13 +680,13 @@ void ManagedBuffer::invalidateHostBuffer() { } template -bool ManagedBuffer::deviceBufferTypeIsTexture() { +bool ManagedBuffer::deviceBufferTypeIsTexture() const { return ((deviceBufferType == DeviceBufferType::Texture1d) || (deviceBufferType == DeviceBufferType::Texture2d) || (deviceBufferType == DeviceBufferType::Texture3d)); } template -void ManagedBuffer::checkDeviceBufferTypeIs(DeviceBufferType targetType) { +void ManagedBuffer::checkDeviceBufferTypeIs(DeviceBufferType targetType) const { if (targetType != deviceBufferType) { exception("ManagedBuffer " + name + " has wrong type for this operation. Expected " + deviceBufferTypeName(targetType) + " but is " + deviceBufferTypeName(deviceBufferType)); @@ -605,7 +694,7 @@ void ManagedBuffer::checkDeviceBufferTypeIs(DeviceBufferType targetType) { } template -void ManagedBuffer::checkDeviceBufferTypeIsTexture() { +void ManagedBuffer::checkDeviceBufferTypeIsTexture() const { if (!deviceBufferTypeIsTexture()) { exception("ManagedBuffer " + name + " has wrong type for this operation. Expected a Texture1d/2d/3d but is " + deviceBufferTypeName(deviceBufferType)); @@ -636,22 +725,6 @@ typename ManagedBuffer::CanonicalDataSource ManagedBuffer::currentCanonica } -template -void ManagedBuffer::ensureHaveBufferIndexCopyProgram() { - if (bufferIndexCopyProgram) return; - - // sanity check - if (!renderAttributeBuffer) exception("ManagedBuffer " + name + " asked to copy indices, but has no buffers"); - - // TODO allocate the transform feedback program -} - -template -void ManagedBuffer::invokeBufferIndexCopyProgram() { - ensureHaveBufferIndexCopyProgram(); - bufferIndexCopyProgram->draw(); -} - // === Interact with the buffer registry std::tuple ManagedBufferRegistry::hasManagedBufferType(std::string name) { diff --git a/src/render_image_quantity_base.cpp b/src/render_image_quantity_base.cpp index 9c8295f8..6793142c 100644 --- a/src/render_image_quantity_base.cpp +++ b/src/render_image_quantity_base.cpp @@ -53,14 +53,12 @@ void RenderImageQuantityBase::addOptionsPopupEntries() { void RenderImageQuantityBase::updateBaseBuffers(const std::vector& newDepthData, const std::vector& newNormalData) { if (!newDepthData.empty()) { - depths.ensureHostBufferAllocated(); - depths.data = newDepthData; + depths.setDataHost(newDepthData); depths.markHostBufferUpdated(); } if (!newNormalData.empty()) { - normals.ensureHostBufferAllocated(); - normals.data = newNormalData; + normals.setDataHost(newNormalData); normals.markHostBufferUpdated(); } diff --git a/src/scalar_render_image_quantity.cpp b/src/scalar_render_image_quantity.cpp index 2ed82796..19cc195e 100644 --- a/src/scalar_render_image_quantity.cpp +++ b/src/scalar_render_image_quantity.cpp @@ -65,9 +65,9 @@ void ScalarRenderImageQuantity::prepare() { // push the color data to the buffer values.ensureHostBufferPopulated(); - std::vector floatData(values.data.size()); - for (size_t i = 0; i < values.data.size(); i++) { - floatData[i] = static_cast(values.data[i]); + std::vector floatData(values.size()); + for (size_t i = 0; i < values.size(); i++) { + floatData[i] = static_cast(values.getHostValue(i)); } // Create the sourceProgram diff --git a/src/simple_triangle_mesh.cpp b/src/simple_triangle_mesh.cpp index 6dfb0103..696b55f9 100644 --- a/src/simple_triangle_mesh.cpp +++ b/src/simple_triangle_mesh.cpp @@ -272,7 +272,7 @@ void SimpleTriangleMesh::updateObjectSpaceBounds() { // bounding box glm::vec3 min = glm::vec3{1, 1, 1} * std::numeric_limits::infinity(); glm::vec3 max = -glm::vec3{1, 1, 1} * std::numeric_limits::infinity(); - for (const glm::vec3& p : vertices.data) { + for (const glm::vec3& p : vertices) { min = componentwiseMin(min, p); max = componentwiseMax(max, p); } @@ -281,7 +281,7 @@ void SimpleTriangleMesh::updateObjectSpaceBounds() { // length scale, as twice the radius from the center of the bounding box glm::vec3 center = 0.5f * (min + max); float lengthScale = 0.0; - for (const glm::vec3& p : vertices.data) { + for (const glm::vec3& p : vertices) { lengthScale = std::max(lengthScale, glm::length2(p - center)); } objectSpaceLengthScale = 2 * std::sqrt(lengthScale); diff --git a/src/slice_plane.cpp b/src/slice_plane.cpp index f84f4185..9b988f93 100644 --- a/src/slice_plane.cpp +++ b/src/slice_plane.cpp @@ -245,6 +245,8 @@ void SlicePlane::setSliceAttributes(render::ShaderProgram& p) { } for (int i = 0; i < 4; i++) { + sliceBufferArr[i].resize(sliceBufferDataArr[i].size()); + sliceBufferArr[i].setDataHost(sliceBufferDataArr[i]); sliceBufferArr[i].markHostBufferUpdated(); } diff --git a/src/sparse_volume_grid.cpp b/src/sparse_volume_grid.cpp index 0c6ec1f3..1be789e1 100644 --- a/src/sparse_volume_grid.cpp +++ b/src/sparse_volume_grid.cpp @@ -88,16 +88,17 @@ void SparseVolumeGrid::checkForDuplicateCells() { void SparseVolumeGrid::computeCellPositions() { size_t n = occupiedCellsData.size(); - cellPositions.data.resize(n); - cellIndices.data.resize(n); + cellPositions.resize(n); + cellIndices.resize(n); for (size_t i = 0; i < n; i++) { glm::ivec3 ijk = occupiedCellsData[i]; - cellPositions.data[i] = origin + (glm::vec3(ijk) + 0.5f) * gridCellWidth; - cellIndices.data[i] = ijk; + cellPositions.setHostValue(i, origin + (glm::vec3(ijk) + 0.5f) * gridCellWidth); + cellIndices.setHostValue(i, ijk); } - cellPositions.markHostBufferUpdated(); + // cellPositions.markHostBufferUpdated() is called by recomputeIfPopulated() after this callback. + // cellIndices is a side-effect buffer, so mark it updated explicitly. cellIndices.markHostBufferUpdated(); } @@ -142,7 +143,7 @@ void SparseVolumeGrid::computeCornerNodeIndices() { // Build corner index buffers using hashmap lookup for (int c = 0; c < 8; c++) { - cornerNodeInds[c].data.resize(n); + cornerNodeInds[c].resize(n); } for (size_t i = 0; i < n; i++) { @@ -152,7 +153,7 @@ void SparseVolumeGrid::computeCornerNodeIndices() { for (int dz = 0; dz < 2; dz++) { int c = dx * 4 + dy * 2 + dz; glm::ivec3 nodeIjk(ci.x + dx, ci.y + dy, ci.z + dz); - cornerNodeInds[c].data[i] = nodeToIndex[nodeIjk]; + cornerNodeInds[c].setHostValue(i, nodeToIndex[nodeIjk]); } } } @@ -505,16 +506,17 @@ void SparseVolumeGrid::ensurePickProgramPrepared() { void SparseVolumeGrid::updateObjectSpaceBounds() { - if (cellPositions.data.empty()) { + if (cellPositions.size() == 0) { // no cells, degenerate bounds at origin objectSpaceBoundingBox = std::make_tuple(origin, origin); objectSpaceLengthScale = glm::length(gridCellWidth); return; } - glm::vec3 bboxMin = cellPositions.data[0]; - glm::vec3 bboxMax = cellPositions.data[0]; - for (const glm::vec3& p : cellPositions.data) { + cellPositions.ensureHostBufferPopulated(); + glm::vec3 bboxMin = cellPositions.getHostValue(0); + glm::vec3 bboxMax = cellPositions.getHostValue(0); + for (const glm::vec3& p : cellPositions) { bboxMin = glm::min(bboxMin, p); bboxMax = glm::max(bboxMax, p); } @@ -557,7 +559,8 @@ SparseVolumeGridPickResult SparseVolumeGrid::interpretPickResult(const PickResul glm::vec3 localPos = (rawResult.position - origin) / gridCellWidth; // Find the cell index - glm::ivec3 cellInd3 = cellIndices.data[rawResult.localIndex]; + cellIndices.ensureHostBufferPopulated(); + glm::ivec3 cellInd3 = cellIndices.getHostValue(rawResult.localIndex); // Fractional position within cell [0,1] glm::vec3 fractional = localPos - glm::vec3(cellInd3); diff --git a/src/surface_mesh.cpp b/src/surface_mesh.cpp index 986cec15..21e3cdc6 100644 --- a/src/surface_mesh.cpp +++ b/src/surface_mesh.cpp @@ -71,7 +71,8 @@ SurfaceMesh::SurfaceMesh(std::string name_, const std::vector& vertex const std::vector& faceIndsEntries_, const std::vector& faceIndsStart_) : SurfaceMesh(name_) { - vertexPositions.data = vertexPositions_; + vertexPositions.resize(vertexPositions_.size()); + vertexPositions.setDataHost(vertexPositions_); faceIndsEntries = faceIndsEntries_; faceIndsStart = faceIndsStart_; @@ -84,7 +85,8 @@ SurfaceMesh::SurfaceMesh(std::string name_, const std::vector& vertex const std::vector>& facesIn) : SurfaceMesh(name_) { - vertexPositions.data = vertexPositions_; + vertexPositions.resize(vertexPositions_.size()); + vertexPositions.setDataHost(vertexPositions_); nestedFacesToFlat(facesIn); vertexPositions.checkInvalidValues(); @@ -114,14 +116,10 @@ void SurfaceMesh::computeConnectivityData() { nFacesTriangulationCount = nCornersCount - 2 * numFaces; // fill out these buffers as we construct the triangulation - triangleVertexInds.data.clear(); - triangleVertexInds.data.resize(3 * nFacesTriangulationCount); - triangleFaceInds.data.clear(); - triangleFaceInds.data.resize(3 * nFacesTriangulationCount); - baryCoord.data.clear(); - baryCoord.data.resize(3 * nFacesTriangulationCount); - edgeIsReal.data.clear(); - edgeIsReal.data.resize(3 * nFacesTriangulationCount); + triangleVertexInds.resize(3 * nFacesTriangulationCount); + triangleFaceInds.resize(3 * nFacesTriangulationCount); + baryCoord.resize(3 * nFacesTriangulationCount); + edgeIsReal.resize(3 * nFacesTriangulationCount); // validate the face-vertex indices for (size_t iV : faceIndsEntries) { @@ -144,17 +142,17 @@ void SurfaceMesh::computeConnectivityData() { uint32_t vC = faceIndsEntries[iStart + ((j + 1) % D)]; // triangle vertex indices - triangleVertexInds.data[3 * iTriFace + 0] = vRoot; - triangleVertexInds.data[3 * iTriFace + 1] = vB; - triangleVertexInds.data[3 * iTriFace + 2] = vC; + triangleVertexInds.setHostValue(3 * iTriFace + 0, vRoot); + triangleVertexInds.setHostValue(3 * iTriFace + 1, vB); + triangleVertexInds.setHostValue(3 * iTriFace + 2, vC); // triangle face indices - for (size_t k = 0; k < 3; k++) triangleFaceInds.data[3 * iTriFace + k] = iF; + for (size_t k = 0; k < 3; k++) triangleFaceInds.setHostValue(3 * iTriFace + k, static_cast(iF)); // barycentric coordinates - baryCoord.data[3 * iTriFace + 0] = glm::vec3{1., 0., 0.}; - baryCoord.data[3 * iTriFace + 1] = glm::vec3{0., 1., 0.}; - baryCoord.data[3 * iTriFace + 2] = glm::vec3{0., 0., 1.}; + baryCoord.setHostValue(3 * iTriFace + 0, glm::vec3{1., 0., 0.}); + baryCoord.setHostValue(3 * iTriFace + 1, glm::vec3{0., 1., 0.}); + baryCoord.setHostValue(3 * iTriFace + 2, glm::vec3{0., 0., 1.}); // internal edges for triangulated polygons glm::vec3 edgeRealV{0., 1., 0.}; @@ -164,7 +162,7 @@ void SurfaceMesh::computeConnectivityData() { if (j + 2 == D) { edgeRealV.z = 1.; } - for (size_t k = 0; k < 3; k++) edgeIsReal.data[3 * iTriFace + k] = edgeRealV; + for (size_t k = 0; k < 3; k++) edgeIsReal.setHostValue(3 * iTriFace + k, edgeRealV); iTriFace++; } @@ -196,7 +194,7 @@ void SurfaceMesh::computeTriangleAllEdgeInds() { "Call setEdgePermutation()."); triangleVertexInds.ensureHostBufferPopulated(); - triangleAllEdgeInds.data.resize(3 * 3 * nFacesTriangulation()); + triangleAllEdgeInds.resize(3 * 3 * nFacesTriangulation()); halfedgeEdgeCorrespondence.resize(nHalfedges()); // used to loop over edges @@ -222,8 +220,8 @@ void SurfaceMesh::computeTriangleAllEdgeInds() { glm::uvec3 thisTriInds{0, 0, 0}; for (size_t j = 0; j < 3; j++) { - size_t vA = triangleVertexInds.data[3 * iF + j]; - size_t vB = triangleVertexInds.data[3 * iF + ((j + 1) % 3)]; + size_t vA = triangleVertexInds.getHostValue(3 * iF + j); + size_t vB = triangleVertexInds.getHostValue(3 * iF + ((j + 1) % 3)); std::pair key = createEdgeKey(vA, vB); @@ -248,13 +246,12 @@ void SurfaceMesh::computeTriangleAllEdgeInds() { for (size_t j = 0; j < 3; j++) { for (size_t k = 0; k < 3; k++) { - triangleAllEdgeInds.data[9 * iF + 3 * j + k] = thisTriInds[k]; + triangleAllEdgeInds.setHostValue(9 * iF + 3 * j + k, thisTriInds[k]); } } } nEdgesCount = psEdgeInd; - triangleAllEdgeInds.markHostBufferUpdated(); } void SurfaceMesh::countEdges() { @@ -269,6 +266,7 @@ void SurfaceMesh::countEdges() { return std::make_pair(std::min(a, b), std::max(a, b)); }; + triangleVertexInds.ensureHostBufferPopulated(); size_t psEdgeInd = 0; // polyscope's edge index, iterated according to Polyscope's canonical ordering for (size_t iF = 0; iF < nFaces(); iF++) { size_t start = faceIndsStart[iF]; @@ -281,8 +279,8 @@ void SurfaceMesh::countEdges() { } for (size_t j = 0; j < 3; j++) { - size_t vA = triangleVertexInds.data[3 * iF + j]; - size_t vB = triangleVertexInds.data[3 * iF + ((j + 1) % 3)]; + size_t vA = triangleVertexInds.getHostValue(3 * iF + j); + size_t vB = triangleVertexInds.getHostValue(3 * iF + ((j + 1) % 3)); std::pair key = createEdgeKey(vA, vB); @@ -304,8 +302,9 @@ size_t SurfaceMesh::nEdges() { void SurfaceMesh::computeTriangleCornerInds() { - triangleCornerInds.data.clear(); - triangleCornerInds.data.reserve(3 * nFacesTriangulation()); + size_t totalCount = 3 * nFacesTriangulation(); + triangleCornerInds.resize(totalCount); + size_t idx = 0; for (size_t iF = 0; iF < nFaces(); iF++) { size_t iStart = faceIndsStart[iF]; @@ -317,19 +316,19 @@ void SurfaceMesh::computeTriangleCornerInds() { uint32_t c1 = iStart + j; uint32_t c2 = iStart + j + 1; - triangleCornerInds.data.push_back(c0); - triangleCornerInds.data.push_back(c1); - triangleCornerInds.data.push_back(c2); + triangleCornerInds.setHostValue(idx++, c0); + triangleCornerInds.setHostValue(idx++, c1); + triangleCornerInds.setHostValue(idx++, c2); } } - triangleCornerInds.markHostBufferUpdated(); } void SurfaceMesh::computeTriangleAllVertexInds() { - triangleAllVertexInds.data.clear(); - triangleAllVertexInds.data.reserve(3 * 3 * nFacesTriangulation()); + size_t totalCount = 3 * 3 * nFacesTriangulation(); + triangleAllVertexInds.resize(totalCount); + size_t idx = 0; for (size_t iF = 0; iF < nFaces(); iF++) { size_t iStart = faceIndsStart[iF]; @@ -343,20 +342,20 @@ void SurfaceMesh::computeTriangleAllVertexInds() { // triangle vertex indices, all three values-each for (size_t k = 0; k < 3; k++) { - triangleAllVertexInds.data.push_back(vRoot); - triangleAllVertexInds.data.push_back(vB); - triangleAllVertexInds.data.push_back(vC); + triangleAllVertexInds.setHostValue(idx++, vRoot); + triangleAllVertexInds.setHostValue(idx++, vB); + triangleAllVertexInds.setHostValue(idx++, vC); } } } - triangleAllVertexInds.markHostBufferUpdated(); } void SurfaceMesh::computeTriangleAllHalfedgeInds() { - triangleAllHalfedgeInds.data.clear(); - triangleAllHalfedgeInds.data.reserve(3 * 3 * nFacesTriangulation()); + size_t totalCount = 3 * 3 * nFacesTriangulation(); + triangleAllHalfedgeInds.resize(totalCount); + size_t idx = 0; bool haveCustomIndex = !halfedgePerm.empty(); @@ -384,20 +383,20 @@ void SurfaceMesh::computeTriangleAllHalfedgeInds() { } for (size_t k = 0; k < 3; k++) { - triangleAllHalfedgeInds.data.push_back(he0); - triangleAllHalfedgeInds.data.push_back(he1); - triangleAllHalfedgeInds.data.push_back(he2); + triangleAllHalfedgeInds.setHostValue(idx++, he0); + triangleAllHalfedgeInds.setHostValue(idx++, he1); + triangleAllHalfedgeInds.setHostValue(idx++, he2); } } } - triangleAllHalfedgeInds.markHostBufferUpdated(); } void SurfaceMesh::computeTriangleAllCornerInds() { - triangleAllCornerInds.data.clear(); - triangleAllCornerInds.data.reserve(3 * nFacesTriangulation()); + size_t totalCount = 3 * 3 * nFacesTriangulation(); + triangleAllCornerInds.resize(totalCount); + size_t idx = 0; bool haveCustomIndex = !cornerPerm.empty(); @@ -418,14 +417,13 @@ void SurfaceMesh::computeTriangleAllCornerInds() { } for (size_t k = 0; k < 3; k++) { - triangleAllCornerInds.data.push_back(c0); - triangleAllCornerInds.data.push_back(c1); - triangleAllCornerInds.data.push_back(c2); + triangleAllCornerInds.setHostValue(idx++, c0); + triangleAllCornerInds.setHostValue(idx++, c1); + triangleAllCornerInds.setHostValue(idx++, c2); } } } - triangleAllCornerInds.markHostBufferUpdated(); } @@ -440,7 +438,7 @@ void SurfaceMesh::computeFaceNormals() { vertexPositions.ensureHostBufferPopulated(); - faceNormals.data.resize(nFaces()); + faceNormals.resize(nFaces()); for (size_t iF = 0; iF < nFaces(); iF++) { size_t iStart = faceIndsStart[iF]; @@ -448,51 +446,49 @@ void SurfaceMesh::computeFaceNormals() { glm::vec3 fN{0., 0., 0.}; if (D == 3) { - glm::vec3 pA = vertexPositions.data[faceIndsEntries[iStart + 0]]; - glm::vec3 pB = vertexPositions.data[faceIndsEntries[iStart + 1]]; - glm::vec3 pC = vertexPositions.data[faceIndsEntries[iStart + 2]]; + glm::vec3 pA = vertexPositions.getHostValue(faceIndsEntries[iStart + 0]); + glm::vec3 pB = vertexPositions.getHostValue(faceIndsEntries[iStart + 1]); + glm::vec3 pC = vertexPositions.getHostValue(faceIndsEntries[iStart + 2]); fN = glm::cross(pB - pA, pC - pA); } else { for (size_t j = 0; j < D; j++) { - glm::vec3 pA = vertexPositions.data[faceIndsEntries[iStart + j]]; - glm::vec3 pB = vertexPositions.data[faceIndsEntries[iStart + (j + 1) % D]]; - glm::vec3 pC = vertexPositions.data[faceIndsEntries[iStart + (j + 2) % D]]; + glm::vec3 pA = vertexPositions.getHostValue(faceIndsEntries[iStart + j]); + glm::vec3 pB = vertexPositions.getHostValue(faceIndsEntries[iStart + (j + 1) % D]); + glm::vec3 pC = vertexPositions.getHostValue(faceIndsEntries[iStart + (j + 2) % D]); fN += glm::cross(pC - pB, pA - pB); } } fN = glm::normalize(fN); - faceNormals.data[iF] = fN; + faceNormals.setHostValue(iF, fN); } - faceNormals.markHostBufferUpdated(); } void SurfaceMesh::computeFaceCenters() { vertexPositions.ensureHostBufferPopulated(); - faceCenters.data.resize(nFaces()); + faceCenters.resize(nFaces()); for (size_t iF = 0; iF < nFaces(); iF++) { size_t start = faceIndsStart[iF]; size_t D = faceIndsStart[iF + 1] - start; glm::vec3 faceCenter{0., 0., 0.}; for (size_t j = 0; j < D; j++) { - glm::vec3 pA = vertexPositions.data[faceIndsEntries[start + j]]; + glm::vec3 pA = vertexPositions.getHostValue(faceIndsEntries[start + j]); faceCenter += pA; } faceCenter /= D; - faceCenters.data[iF] = faceCenter; + faceCenters.setHostValue(iF, faceCenter); } - faceCenters.markHostBufferUpdated(); } void SurfaceMesh::computeFaceAreas() { vertexPositions.ensureHostBufferPopulated(); - faceAreas.data.resize(nFaces()); + faceAreas.resize(nFaces()); // Loop over faces to compute face-valued quantities for (size_t iF = 0; iF < nFaces(); iF++) { @@ -502,25 +498,24 @@ void SurfaceMesh::computeFaceAreas() { // Compute a face normal double fA; if (D == 3) { - glm::vec3 pA = vertexPositions.data[faceIndsEntries[start + 0]]; - glm::vec3 pB = vertexPositions.data[faceIndsEntries[start + 1]]; - glm::vec3 pC = vertexPositions.data[faceIndsEntries[start + 2]]; + glm::vec3 pA = vertexPositions.getHostValue(faceIndsEntries[start + 0]); + glm::vec3 pB = vertexPositions.getHostValue(faceIndsEntries[start + 1]); + glm::vec3 pC = vertexPositions.getHostValue(faceIndsEntries[start + 2]); glm::vec3 fN = glm::cross(pB - pA, pC - pA); fA = 0.5 * glm::length(fN); } else { fA = 0; - glm::vec3 pRoot = vertexPositions.data[faceIndsEntries[start]]; + glm::vec3 pRoot = vertexPositions.getHostValue(faceIndsEntries[start]); for (size_t j = 1; j + 1 < D; j++) { - glm::vec3 pA = vertexPositions.data[faceIndsEntries[start + j]]; - glm::vec3 pB = vertexPositions.data[faceIndsEntries[start + j + 1]]; + glm::vec3 pA = vertexPositions.getHostValue(faceIndsEntries[start + j]); + glm::vec3 pB = vertexPositions.getHostValue(faceIndsEntries[start + j + 1]); fA += 0.5 * glm::length(glm::cross(pA - pRoot, pB - pRoot)); } } - faceAreas.data[iF] = fA; + faceAreas.setHostValue(iF, fA); } - faceAreas.markHostBufferUpdated(); } @@ -529,11 +524,12 @@ void SurfaceMesh::computeVertexNormals() { faceNormals.ensureHostBufferPopulated(); faceAreas.ensureHostBufferPopulated(); - vertexNormals.data.resize(nVertices()); + vertexNormals.resize(nVertices()); + vertexNormals.ensureHostBufferPopulated(); const glm::vec3 zero{0., 0., 0.}; - std::fill(vertexNormals.data.begin(), vertexNormals.data.end(), zero); + for (size_t iV = 0; iV < nVertices(); iV++) vertexNormals.setHostValue(iV, zero); // Accumulate quantities from each face for (size_t iF = 0; iF < nFaces(); iF++) { @@ -541,24 +537,25 @@ void SurfaceMesh::computeVertexNormals() { size_t D = faceIndsStart[iF + 1] - start; for (size_t j = 0; j < D; j++) { size_t iV = faceIndsEntries[start + j]; - vertexNormals.data[iV] += faceNormals.data[iF] * static_cast(faceAreas.data[iF]); + vertexNormals.setHostValue(iV, vertexNormals.getHostValue(iV) + faceNormals.getHostValue(iF) * static_cast(faceAreas.getHostValue(iF))); } } // Normalize for (size_t iV = 0; iV < nVertices(); iV++) { - vertexNormals.data[iV] = glm::normalize(vertexNormals.data[iV]); + vertexNormals.setHostValue(iV, glm::normalize(vertexNormals.getHostValue(iV))); } - vertexNormals.markHostBufferUpdated(); } void SurfaceMesh::computeVertexAreas() { faceAreas.ensureHostBufferPopulated(); - vertexAreas.data.resize(nVertices()); - std::fill(vertexAreas.data.begin(), vertexAreas.data.end(), 0.); + vertexAreas.resize(nVertices()); + vertexAreas.ensureHostBufferPopulated(); + + for (size_t iV = 0; iV < nVertices(); iV++) vertexAreas.setHostValue(iV, 0.); // Accumulate quantities from each face for (size_t iF = 0; iF < nFaces(); iF++) { @@ -566,11 +563,10 @@ void SurfaceMesh::computeVertexAreas() { size_t start = faceIndsStart[iF]; for (size_t j = 0; j < D; j++) { size_t iV = faceIndsEntries[start + j]; - vertexAreas.data[iV] += faceAreas.data[iF] / D; + vertexAreas.setHostValue(iV, vertexAreas.getHostValue(iV) + faceAreas.getHostValue(iF) / D); } } - vertexAreas.markHostBufferUpdated(); } void SurfaceMesh::computeDefaultFaceTangentBasisX() { @@ -581,7 +577,7 @@ void SurfaceMesh::computeDefaultFaceTangentBasisX() { vertexPositions.ensureHostBufferPopulated(); faceNormals.ensureHostBufferPopulated(); - defaultFaceTangentBasisX.data.resize(nFaces()); + defaultFaceTangentBasisX.resize(nFaces()); for (size_t iF = 0; iF < nFaces(); iF++) { size_t D = faceIndsStart[iF + 1] - faceIndsStart[iF]; @@ -589,19 +585,18 @@ void SurfaceMesh::computeDefaultFaceTangentBasisX() { size_t start = faceIndsStart[iF]; - glm::vec3 pA = vertexPositions.data[faceIndsEntries[start + 0]]; - glm::vec3 pB = vertexPositions.data[faceIndsEntries[start + 1]]; - glm::vec3 N = faceNormals.data[iF]; + glm::vec3 pA = vertexPositions.getHostValue(faceIndsEntries[start + 0]); + glm::vec3 pB = vertexPositions.getHostValue(faceIndsEntries[start + 1]); + glm::vec3 N = faceNormals.getHostValue(iF); glm::vec3 basisX = pB - pA; basisX = glm::normalize(basisX - N * glm::dot(N, basisX)); glm::vec3 basisY = glm::normalize(-glm::cross(basisX, N)); - defaultFaceTangentBasisX.data[iF] = basisX; + defaultFaceTangentBasisX.setHostValue(iF, basisX); } - defaultFaceTangentBasisX.markHostBufferUpdated(); } void SurfaceMesh::computeDefaultFaceTangentBasisY() { @@ -612,7 +607,7 @@ void SurfaceMesh::computeDefaultFaceTangentBasisY() { vertexPositions.ensureHostBufferPopulated(); faceNormals.ensureHostBufferPopulated(); - defaultFaceTangentBasisY.data.resize(nFaces()); + defaultFaceTangentBasisY.resize(nFaces()); for (size_t iF = 0; iF < nFaces(); iF++) { size_t D = faceIndsStart[iF + 1] - faceIndsStart[iF]; @@ -620,19 +615,18 @@ void SurfaceMesh::computeDefaultFaceTangentBasisY() { size_t start = faceIndsStart[iF]; - glm::vec3 pA = vertexPositions.data[faceIndsEntries[start + 0]]; - glm::vec3 pB = vertexPositions.data[faceIndsEntries[start + 1]]; - glm::vec3 N = faceNormals.data[iF]; + glm::vec3 pA = vertexPositions.getHostValue(faceIndsEntries[start + 0]); + glm::vec3 pB = vertexPositions.getHostValue(faceIndsEntries[start + 1]); + glm::vec3 N = faceNormals.getHostValue(iF); glm::vec3 basisX = pB - pA; basisX = glm::normalize(basisX - N * glm::dot(N, basisX)); glm::vec3 basisY = glm::normalize(-glm::cross(basisX, N)); - defaultFaceTangentBasisY.data[iF] = basisY; + defaultFaceTangentBasisY.setHostValue(iF, basisY); } - defaultFaceTangentBasisY.markHostBufferUpdated(); } // === Edge Lengths === @@ -680,8 +674,8 @@ void SurfaceMesh::ensureHaveManifoldConnectivity() { // Fill out faceForHalfedge and populate edge lookup map for (size_t iF = 0; iF < nFacesTriangulation(); iF++) { for (size_t j = 0; j < 3; j++) { - size_t iV = triangleVertexInds.data[3 * iF + j]; - size_t iVNext = triangleVertexInds.data[3 * iF + ((j + 1) % 3)]; + size_t iV = triangleVertexInds.getHostValue(3 * iF + j); + size_t iVNext = triangleVertexInds.getHostValue(3 * iF + ((j + 1) % 3)); size_t iHe = 3 * iF + j; @@ -701,8 +695,8 @@ void SurfaceMesh::ensureHaveManifoldConnectivity() { // Second walk through, setting twins for (size_t iF = 0; iF < nFacesTriangulation(); iF++) { for (size_t j = 0; j < 3; j++) { - size_t iV = triangleVertexInds.data[3 * iF + j]; - size_t iVNext = triangleVertexInds.data[3 * iF + ((j + 1) % 3)]; + size_t iV = triangleVertexInds.getHostValue(3 * iF + j); + size_t iVNext = triangleVertexInds.getHostValue(3 * iF + ((j + 1) % 3)); size_t iHe = 3 * iF + j; std::pair edgeKey(std::min(iV, iVNext), std::max(iV, iVNext)); @@ -969,9 +963,9 @@ void SurfaceMesh::setMeshPickAttributes(render::ShaderProgram& p) { // clang-format off std::array vColor = { - pick::indToVec(triangleVertexInds.data[3*iFTri + 0] + vertexGlobalPickIndStart), - pick::indToVec(triangleVertexInds.data[3*iFTri + 1] + vertexGlobalPickIndStart), - pick::indToVec(triangleVertexInds.data[3*iFTri + 2] + vertexGlobalPickIndStart), + pick::indToVec(triangleVertexInds.getHostValue(3*iFTri + 0) + vertexGlobalPickIndStart), + pick::indToVec(triangleVertexInds.getHostValue(3*iFTri + 1) + vertexGlobalPickIndStart), + pick::indToVec(triangleVertexInds.getHostValue(3*iFTri + 2) + vertexGlobalPickIndStart), }; // clang-format on @@ -996,15 +990,17 @@ void SurfaceMesh::setMeshPickAttributes(render::ShaderProgram& p) { if (!usingSimplePick) { if (edgesHaveBeenUsed || halfedgesHaveBeenUsed) { - const std::vector& eDataVec = - (edgesHaveBeenUsed && !halfedgesHaveBeenUsed) ? triangleAllEdgeInds.data : triangleAllHalfedgeInds.data; + triangleAllEdgeInds.ensureHostBufferPopulated(); + triangleAllHalfedgeInds.ensureHostBufferPopulated(); + const uint32_t* eDataVec = + (edgesHaveBeenUsed && !halfedgesHaveBeenUsed) ? triangleAllEdgeInds.begin() : triangleAllHalfedgeInds.begin(); size_t offset = (edgesHaveBeenUsed && !halfedgesHaveBeenUsed) ? edgeGlobalPickIndStart : halfedgeGlobalPickIndStart; // clang-format off - std::array eColor = { - fColor, - pick::indToVec(eDataVec[9*iFTri + 1] + offset), + std::array eColor = { + fColor, + pick::indToVec(eDataVec[9*iFTri + 1] + offset), fColor }; // clang-format on @@ -1022,10 +1018,10 @@ void SurfaceMesh::setMeshPickAttributes(render::ShaderProgram& p) { if (!usingSimplePick) { if (cornersHaveBeenUsed) { // clang-format off - std::array cColor = { - pick::indToVec(triangleCornerInds.data[3*iFTri + 0] + cornerGlobalPickIndStart), - pick::indToVec(triangleCornerInds.data[3*iFTri + 1] + cornerGlobalPickIndStart), - pick::indToVec(triangleCornerInds.data[3*iFTri + 2] + cornerGlobalPickIndStart), + std::array cColor = { + pick::indToVec(triangleCornerInds.getHostValue(3*iFTri + 0) + cornerGlobalPickIndStart), + pick::indToVec(triangleCornerInds.getHostValue(3*iFTri + 1) + cornerGlobalPickIndStart), + pick::indToVec(triangleCornerInds.getHostValue(3*iFTri + 2) + cornerGlobalPickIndStart), }; // clang-format on for (int j = 0; j < 3; j++) cornerColors.push_back(cColor); @@ -1485,7 +1481,7 @@ void SurfaceMesh::updateObjectSpaceBounds() { // bounding box glm::vec3 min = glm::vec3{1, 1, 1} * std::numeric_limits::infinity(); glm::vec3 max = -glm::vec3{1, 1, 1} * std::numeric_limits::infinity(); - for (const glm::vec3& p : vertexPositions.data) { + for (const glm::vec3& p : vertexPositions) { min = componentwiseMin(min, p); max = componentwiseMax(max, p); } @@ -1494,7 +1490,7 @@ void SurfaceMesh::updateObjectSpaceBounds() { // length scale, as twice the radius from the center of the bounding box glm::vec3 center = 0.5f * (min + max); float lengthScale = 0.0; - for (const glm::vec3& p : vertexPositions.data) { + for (const glm::vec3& p : vertexPositions) { lengthScale = std::max(lengthScale, glm::length2(p - center)); } objectSpaceLengthScale = 2 * std::sqrt(lengthScale); diff --git a/src/surface_parameterization_quantity.cpp b/src/surface_parameterization_quantity.cpp index 11945be6..56342e0e 100644 --- a/src/surface_parameterization_quantity.cpp +++ b/src/surface_parameterization_quantity.cpp @@ -133,15 +133,18 @@ CurveNetwork* SurfaceParameterizationQuantity::createCurveNetworkFromSeams(std:: std::set> seamEdges; // loop over all edges + parent.edgeIsReal.ensureHostBufferPopulated(); + parent.triangleVertexInds.ensureHostBufferPopulated(); + parent.triangleCornerInds.ensureHostBufferPopulated(); for(size_t iT = 0; iT < parent.nFacesTriangulation(); iT++) { for(size_t k = 0; k < 3; k++) { - if(parent.edgeIsReal.data[3*iT][k] == 0.) continue; // skip internal tesselation edges + if(parent.edgeIsReal.getHostValue(3*iT)[k] == 0.) continue; // skip internal tesselation edges // gather data for the edge - int32_t iV_tail = parent.triangleVertexInds.data[3*iT + (k+0)%3]; - int32_t iV_tip = parent.triangleVertexInds.data[3*iT + (k+1)%3]; - int32_t iC_tail = parent.triangleCornerInds.data[3*iT + (k+0)%3]; - int32_t iC_tip = parent.triangleCornerInds.data[3*iT + (k+1)%3]; + int32_t iV_tail = parent.triangleVertexInds.getHostValue(3*iT + (k+0)%3); + int32_t iV_tip = parent.triangleVertexInds.getHostValue(3*iT + (k+1)%3); + int32_t iC_tail = parent.triangleCornerInds.getHostValue(3*iT + (k+0)%3); + int32_t iC_tip = parent.triangleCornerInds.getHostValue(3*iT + (k+1)%3); std::pair eInd (iV_tail, iV_tip); std::pair eC (cornerCoords[iC_tail], cornerCoords[iC_tip]); canonicalizeEdge(eInd, eC); // make sure ordering is consistent @@ -184,12 +187,12 @@ CurveNetwork* SurfaceParameterizationQuantity::createCurveNetworkFromSeams(std:: // get unique vertices for the edges if(vertexIndToDense.find(vA) == vertexIndToDense.end()) { vertexIndToDense[vA] = seamEdgeNodes.size(); - seamEdgeNodes.push_back(parent.vertexPositions.data[vA]); + seamEdgeNodes.push_back(parent.vertexPositions.getHostValue(vA)); } int32_t nA = vertexIndToDense[vA]; if(vertexIndToDense.find(vB) == vertexIndToDense.end()) { vertexIndToDense[vB] = seamEdgeNodes.size(); - seamEdgeNodes.push_back(parent.vertexPositions.data[vB]); + seamEdgeNodes.push_back(parent.vertexPositions.getHostValue(vB)); } int32_t nB = vertexIndToDense[vB]; diff --git a/src/surface_vector_quantity.cpp b/src/surface_vector_quantity.cpp index d6de8550..bbffc054 100644 --- a/src/surface_vector_quantity.cpp +++ b/src/surface_vector_quantity.cpp @@ -212,6 +212,16 @@ std::vector oneFormToFaceTangentVectors(SurfaceMesh& mesh, const std: mesh.defaultFaceTangentBasisX.ensureHostBufferPopulated(); mesh.defaultFaceTangentBasisY.ensureHostBufferPopulated(); mesh.triangleAllEdgeInds.ensureHostBufferPopulated(); + mesh.triangleVertexInds.ensureHostBufferPopulated(); + + // TODO why is this duplicated?! + mesh.vertexPositions.ensureHostBufferPopulated(); + mesh.faceAreas.ensureHostBufferPopulated(); + mesh.faceNormals.ensureHostBufferPopulated(); + mesh.defaultFaceTangentBasisX.ensureHostBufferPopulated(); + mesh.defaultFaceTangentBasisY.ensureHostBufferPopulated(); + mesh.triangleAllEdgeInds.ensureHostBufferPopulated(); + mesh.triangleVertexInds.ensureHostBufferPopulated(); std::vector mappedVectorField(mesh.nFaces()); @@ -220,17 +230,17 @@ std::vector oneFormToFaceTangentVectors(SurfaceMesh& mesh, const std: std::array formValues; std::array vecValues; for (size_t j = 0; j < 3; j++) { - size_t vA = mesh.triangleVertexInds.data[3 * iF + j]; - size_t vB = mesh.triangleVertexInds.data[3 * iF + ((j + 1) % 3)]; - size_t iE = mesh.triangleAllEdgeInds.data[9 * iF + j]; + size_t vA = mesh.triangleVertexInds.getHostValue(3 * iF + j); + size_t vB = mesh.triangleVertexInds.getHostValue(3 * iF + ((j + 1) % 3)); + size_t iE = mesh.triangleAllEdgeInds.getHostValue(9 * iF + j); bool isCanonicalOriented = (vB > vA) != (canonicalOrientation[iE]); // TODO double check convention float orientationSign = isCanonicalOriented ? 1.f : -1.f; formValues[j] = orientationSign * oneForm[iE]; - glm::vec3 heVec = mesh.vertexPositions.data[vB] - mesh.vertexPositions.data[vA]; - vecValues[j] = glm::cross(heVec, mesh.faceNormals.data[iF]); + glm::vec3 heVec = mesh.vertexPositions.getHostValue(vB) - mesh.vertexPositions.getHostValue(vA); + vecValues[j] = glm::cross(heVec, mesh.faceNormals.getHostValue(iF)); } // Whitney interpolation at center @@ -238,10 +248,10 @@ std::vector oneFormToFaceTangentVectors(SurfaceMesh& mesh, const std: for (int j = 0; j < 3; j++) { result += (formValues[(j + 1) % 3] - formValues[(j + 2) % 3]) * vecValues[j]; } - result /= static_cast(6. * mesh.faceAreas.data[iF]); + result /= static_cast(6. * mesh.faceAreas.getHostValue(iF)); - glm::vec2 approxVec{glm::dot(result, mesh.defaultFaceTangentBasisX.data[iF]), - glm::dot(result, mesh.defaultFaceTangentBasisY.data[iF])}; + glm::vec2 approxVec{glm::dot(result, mesh.defaultFaceTangentBasisX.getHostValue(iF)), + glm::dot(result, mesh.defaultFaceTangentBasisY.getHostValue(iF))}; mappedVectorField[iF] = approxVec; } @@ -256,8 +266,9 @@ SurfaceOneFormTangentVectorQuantity::SurfaceOneFormTangentVectorQuantity(std::st : SurfaceVectorQuantity(name, mesh_, MeshElement::FACE), TangentVectorQuantity( *this, oneFormToFaceTangentVectors(mesh_, oneForm_, canonicalOrientation_), - mesh_.defaultFaceTangentBasisX.getPopulatedHostBufferRef(), - mesh_.defaultFaceTangentBasisY.getPopulatedHostBufferRef(), parent.faceCenters, 1, VectorType::STANDARD), + std::vector(mesh_.defaultFaceTangentBasisX.begin(), mesh_.defaultFaceTangentBasisX.end()), + std::vector(mesh_.defaultFaceTangentBasisY.begin(), mesh_.defaultFaceTangentBasisY.end()), + parent.faceCenters, 1, VectorType::STANDARD), oneForm(oneForm_), canonicalOrientation(canonicalOrientation_) {} void SurfaceOneFormTangentVectorQuantity::refresh() { diff --git a/src/volume_grid.cpp b/src/volume_grid.cpp index ae31a314..fded29a7 100644 --- a/src/volume_grid.cpp +++ b/src/volume_grid.cpp @@ -312,28 +312,28 @@ void VolumeGrid::computeGridPlaneReferenceGeometry() { // Geometry is defined in the reference [0,1] cube - gridPlaneReferencePositions.data.clear(); - gridPlaneReferenceNormals.data.clear(); - gridPlaneAxisInds.data.clear(); + std::vector positionsVec; + std::vector normalsVec; + std::vector axisIndsVec; auto addPlane = [&](std::array corners, glm::vec3 normal, uint32_t axInd) { // first triangle - gridPlaneReferencePositions.data.push_back(corners[0]); - gridPlaneReferencePositions.data.push_back(corners[1]); - gridPlaneReferencePositions.data.push_back(corners[2]); - for (int32_t j = 0; j < 3; j++) gridPlaneReferenceNormals.data.push_back(normal); - for (int32_t j = 0; j < 3; j++) gridPlaneAxisInds.data.push_back(axInd); + positionsVec.push_back(corners[0]); + positionsVec.push_back(corners[1]); + positionsVec.push_back(corners[2]); + for (int32_t j = 0; j < 3; j++) normalsVec.push_back(normal); + for (int32_t j = 0; j < 3; j++) axisIndsVec.push_back((int32_t)axInd); // second triangle - gridPlaneReferencePositions.data.push_back(corners[1]); - gridPlaneReferencePositions.data.push_back(corners[3]); - gridPlaneReferencePositions.data.push_back(corners[2]); - for (int32_t j = 0; j < 3; j++) gridPlaneReferenceNormals.data.push_back(normal); - for (int32_t j = 0; j < 3; j++) gridPlaneAxisInds.data.push_back(axInd); + positionsVec.push_back(corners[1]); + positionsVec.push_back(corners[3]); + positionsVec.push_back(corners[2]); + for (int32_t j = 0; j < 3; j++) normalsVec.push_back(normal); + for (int32_t j = 0; j < 3; j++) axisIndsVec.push_back((int32_t)axInd); }; // The planes are intentionally added in order such that the outermost planes come first, and we don't massively - // overshade from back to front. Note that fthe first look runs backwards. + // overshade from back to front. Note that the first loop runs backwards. // Forward facing planes for (uint32_t d = 0; d < 3; d++) { // x/y/z dimension (plane is perpendicular) @@ -374,7 +374,14 @@ void VolumeGrid::computeGridPlaneReferenceGeometry() { } - gridPlaneReferencePositions.markHostBufferUpdated(); + gridPlaneReferencePositions.resize(positionsVec.size()); + gridPlaneReferencePositions.setDataHost(positionsVec); + gridPlaneReferenceNormals.resize(normalsVec.size()); + gridPlaneReferenceNormals.setDataHost(normalsVec); + gridPlaneAxisInds.resize(axisIndsVec.size()); + gridPlaneAxisInds.setDataHost(axisIndsVec); + // gridPlaneReferencePositions.markHostBufferUpdated() is called by recomputeIfPopulated() after this callback. + // The side-effect buffers must be marked updated explicitly since they are not the primary compute target. gridPlaneReferenceNormals.markHostBufferUpdated(); gridPlaneAxisInds.markHostBufferUpdated(); } diff --git a/src/volume_grid_scalar_quantity.cpp b/src/volume_grid_scalar_quantity.cpp index 769a1957..1010e3d4 100644 --- a/src/volume_grid_scalar_quantity.cpp +++ b/src/volume_grid_scalar_quantity.cpp @@ -165,7 +165,7 @@ void VolumeGridNodeScalarQuantity::createIsosurfaceProgram() { // Extract the isosurface from the level set of the scalar field MC::mcMesh isosurfaceMesh; - MC::marching_cube(&values.data.front(), isosurfaceLevel.get(), parent.getGridNodeDim().z, parent.getGridNodeDim().y, + MC::marching_cube(const_cast(values.begin()), isosurfaceLevel.get(), parent.getGridNodeDim().z, parent.getGridNodeDim().y, parent.getGridNodeDim().x, isosurfaceMesh); // Transform the result to be aligned with our volume's spatial layout @@ -215,7 +215,8 @@ SurfaceMesh* VolumeGridNodeScalarQuantity::registerIsosurfaceAsMesh(std::string // extract the mesh MC::mcMesh isosurfaceMesh; - MC::marching_cube(&values.data.front(), isosurfaceLevel.get(), parent.getGridNodeDim().z, parent.getGridNodeDim().y, + values.ensureHostBufferPopulated(); + MC::marching_cube(const_cast(values.begin()), isosurfaceLevel.get(), parent.getGridNodeDim().z, parent.getGridNodeDim().y, parent.getGridNodeDim().x, isosurfaceMesh); glm::vec3 scale = parent.gridSpacing(); for (auto& p : isosurfaceMesh.vertices) { diff --git a/src/volume_mesh.cpp b/src/volume_mesh.cpp index 3b8907fa..4fa610e6 100644 --- a/src/volume_mesh.cpp +++ b/src/volume_mesh.cpp @@ -533,11 +533,12 @@ void VolumeMesh::fillSliceGeometryBuffers(render::ShaderProgram& program) { point2.resize(tetCount); point3.resize(tetCount); point4.resize(tetCount); + vertexPositions.ensureHostBufferPopulated(); for (size_t tetIdx = 0; tetIdx < tets.size(); tetIdx++) { - point1[tetIdx] = vertexPositions.data[tets[tetIdx][0]]; - point2[tetIdx] = vertexPositions.data[tets[tetIdx][1]]; - point3[tetIdx] = vertexPositions.data[tets[tetIdx][2]]; - point4[tetIdx] = vertexPositions.data[tets[tetIdx][3]]; + point1[tetIdx] = vertexPositions.getHostValue(tets[tetIdx][0]); + point2[tetIdx] = vertexPositions.getHostValue(tets[tetIdx][1]); + point3[tetIdx] = vertexPositions.getHostValue(tets[tetIdx][2]); + point4[tetIdx] = vertexPositions.getHostValue(tets[tetIdx][3]); } program.setAttribute("a_point_1", point1); @@ -818,18 +819,12 @@ void VolumeMesh::computeConnectivityData() { // that exterior faces always win depth ties. This doesn't totally eliminate the problem, but greatly improves the // most egregious cases. // == Allocate buffers - triangleVertexInds.data.clear(); - triangleVertexInds.data.resize(3 * nFacesTriangulation()); - triangleFaceInds.data.clear(); - triangleFaceInds.data.resize(3 * nFacesTriangulation()); - triangleCellInds.data.clear(); - triangleCellInds.data.resize(3 * nFacesTriangulation()); - baryCoord.data.clear(); - baryCoord.data.resize(3 * nFacesTriangulation()); - edgeIsReal.data.clear(); - edgeIsReal.data.resize(3 * nFacesTriangulation()); - faceType.data.clear(); - faceType.data.resize(nFaces()); + triangleVertexInds.resize(3 * nFacesTriangulation()); + triangleFaceInds.resize(3 * nFacesTriangulation()); + triangleCellInds.resize(3 * nFacesTriangulation()); + baryCoord.resize(3 * nFacesTriangulation()); + edgeIsReal.resize(3 * nFacesTriangulation()); + faceType.resize(nFaces()); size_t iF = 0; // face counter size_t iFront = 0; @@ -862,28 +857,27 @@ void VolumeMesh::computeConnectivityData() { // Store triangle vertices for (size_t k = 0; k < 3; k++) { - triangleVertexInds.data[3 * iData + k] = cell[tri[k]]; + triangleVertexInds.setHostValue(3 * iData + k, cell[tri[k]]); } - // Face & cell indices - for (size_t k = 0; k < 3; k++) triangleFaceInds.data[3 * iData + k] = iF; - for (size_t k = 0; k < 3; k++) triangleCellInds.data[3 * iData + k] = iC; + for (size_t k = 0; k < 3; k++) triangleFaceInds.setHostValue(3 * iData + k, (uint32_t)iF); + for (size_t k = 0; k < 3; k++) triangleCellInds.setHostValue(3 * iData + k, (uint32_t)iC); // Barycentric coords - baryCoord.data[3 * iData + 0] = glm::vec3{1., 0., 0.}; - baryCoord.data[3 * iData + 1] = glm::vec3{0., 1., 0.}; - baryCoord.data[3 * iData + 2] = glm::vec3{0., 0., 1.}; + baryCoord.setHostValue(3 * iData + 0, glm::vec3{1., 0., 0.}); + baryCoord.setHostValue(3 * iData + 1, glm::vec3{0., 1., 0.}); + baryCoord.setHostValue(3 * iData + 2, glm::vec3{0., 0., 1.}); // Mark edges as real or not for (int k = 0; k < 3; k++) { for (int c = 0; c < 3; c++) { - edgeIsReal.data[3 * iData + k][c] = faceRealEdges[f][j][c] ? 1.0f : 0.0f; + edgeIsReal.setHostValue(3 * iData + k, c, faceRealEdges[f][j][c] ? 1.0f : 0.0f); } } } // Face type: 1 for interior, 0 for exterior - faceType.data[iF] = faceIsInterior[iF] ? 1.f : 0.f; + faceType.setHostValue(iF, faceIsInterior[iF] ? 1.f : 0.f); iF++; } @@ -948,7 +942,7 @@ void VolumeMesh::computeFaceNormals() { vertexPositions.ensureHostBufferPopulated(); - faceNormals.data.resize(nFaces()); + faceNormals.resize(nFaces()); size_t iF = 0; for (size_t iC = 0; iC < nCells(); iC++) { @@ -960,19 +954,18 @@ void VolumeMesh::computeFaceNormals() { // Do a first pass to compute a normal glm::vec3 normal{0., 0., 0.}; for (const std::array& tri : face) { - glm::vec3 pA = vertexPositions.data[cell[tri[0]]]; - glm::vec3 pB = vertexPositions.data[cell[tri[1]]]; - glm::vec3 pC = vertexPositions.data[cell[tri[2]]]; + glm::vec3 pA = vertexPositions.getHostValue(cell[tri[0]]); + glm::vec3 pB = vertexPositions.getHostValue(cell[tri[1]]); + glm::vec3 pC = vertexPositions.getHostValue(cell[tri[2]]); normal += glm::cross(pC - pB, pA - pB); } normal = glm::normalize(normal); - faceNormals.data[iF] = normal; + faceNormals.setHostValue(iF, normal); iF++; } } - faceNormals.markHostBufferUpdated(); } @@ -980,7 +973,7 @@ void VolumeMesh::computeCellCenters() { vertexPositions.ensureHostBufferPopulated(); - cellCenters.data.resize(nCells()); + cellCenters.resize(nCells()); for (size_t iC = 0; iC < nCells(); iC++) { @@ -990,16 +983,15 @@ void VolumeMesh::computeCellCenters() { const std::array& cell = cells[iC]; for (int j = 0; j < 8; j++) { if (cell[j] < INVALID_IND_32) { - center += vertexPositions.data[cell[j]]; + center += vertexPositions.getHostValue(cell[j]); count++; } } center /= count; - cellCenters.data[iC] = center; + cellCenters.setHostValue(iC, center); } - cellCenters.markHostBufferUpdated(); } @@ -1218,7 +1210,7 @@ void VolumeMesh::updateObjectSpaceBounds() { // bounding box glm::vec3 min = glm::vec3{1, 1, 1} * std::numeric_limits::infinity(); glm::vec3 max = -glm::vec3{1, 1, 1} * std::numeric_limits::infinity(); - for (const glm::vec3& p : vertexPositions.data) { + for (const glm::vec3& p : vertexPositions) { min = componentwiseMin(min, p); max = componentwiseMax(max, p); } @@ -1227,7 +1219,7 @@ void VolumeMesh::updateObjectSpaceBounds() { // length scale, as twice the radius from the center of the bounding box glm::vec3 center = 0.5f * (min + max); float lengthScale = 0.0; - for (const glm::vec3& p : vertexPositions.data) { + for (const glm::vec3& p : vertexPositions) { lengthScale = std::max(lengthScale, glm::length2(p - center)); } objectSpaceLengthScale = 2 * std::sqrt(lengthScale); diff --git a/src/volume_mesh_color_quantity.cpp b/src/volume_mesh_color_quantity.cpp index b4d314e1..712c7c36 100644 --- a/src/volume_mesh_color_quantity.cpp +++ b/src/volume_mesh_color_quantity.cpp @@ -92,11 +92,12 @@ void VolumeMeshVertexColorQuantity::fillSliceColorBuffers(render::ShaderProgram& colorval_3.resize(tetCount); colorval_4.resize(tetCount); + colors.ensureHostBufferPopulated(); for (size_t iT = 0; iT < parent.tets.size(); iT++) { - colorval_1[iT] = colors.data[parent.tets[iT][0]]; - colorval_2[iT] = colors.data[parent.tets[iT][1]]; - colorval_3[iT] = colors.data[parent.tets[iT][2]]; - colorval_4[iT] = colors.data[parent.tets[iT][3]]; + colorval_1[iT] = colors.getHostValue(parent.tets[iT][0]); + colorval_2[iT] = colors.getHostValue(parent.tets[iT][1]); + colorval_3[iT] = colors.getHostValue(parent.tets[iT][2]); + colorval_4[iT] = colors.getHostValue(parent.tets[iT][3]); } // Store data in buffers diff --git a/src/volume_mesh_scalar_quantity.cpp b/src/volume_mesh_scalar_quantity.cpp index 67085a30..946bfc7b 100644 --- a/src/volume_mesh_scalar_quantity.cpp +++ b/src/volume_mesh_scalar_quantity.cpp @@ -91,15 +91,17 @@ void VolumeMeshVertexScalarQuantity::fillLevelSetData(render::ShaderProgram& p) point2.resize(tetCount); point3.resize(tetCount); point4.resize(tetCount); + parent.vertexPositions.ensureHostBufferPopulated(); + values.ensureHostBufferPopulated(); for (size_t i = 0; i < parent.nTets(); i++) { - point1[i] = parent.vertexPositions.data[parent.tets[i][0]]; - point2[i] = parent.vertexPositions.data[parent.tets[i][1]]; - point3[i] = parent.vertexPositions.data[parent.tets[i][2]]; - point4[i] = parent.vertexPositions.data[parent.tets[i][3]]; - slice1[i] = glm::vec3(values.data[parent.tets[i][0]], 0, 0); - slice2[i] = glm::vec3(values.data[parent.tets[i][1]], 0, 0); - slice3[i] = glm::vec3(values.data[parent.tets[i][2]], 0, 0); - slice4[i] = glm::vec3(values.data[parent.tets[i][3]], 0, 0); + point1[i] = parent.vertexPositions.getHostValue(parent.tets[i][0]); + point2[i] = parent.vertexPositions.getHostValue(parent.tets[i][1]); + point3[i] = parent.vertexPositions.getHostValue(parent.tets[i][2]); + point4[i] = parent.vertexPositions.getHostValue(parent.tets[i][3]); + slice1[i] = glm::vec3(values.getHostValue(parent.tets[i][0]), 0, 0); + slice2[i] = glm::vec3(values.getHostValue(parent.tets[i][1]), 0, 0); + slice3[i] = glm::vec3(values.getHostValue(parent.tets[i][2]), 0, 0); + slice4[i] = glm::vec3(values.getHostValue(parent.tets[i][3]), 0, 0); } p.setAttribute("a_point_1", point1); p.setAttribute("a_point_2", point2); @@ -299,11 +301,12 @@ void VolumeMeshVertexScalarQuantity::fillSliceColorBuffers(render::ShaderProgram colorval_3.resize(tetCount); colorval_4.resize(tetCount); + values.ensureHostBufferPopulated(); for (size_t iT = 0; iT < parent.tets.size(); iT++) { - colorval_1[iT] = values.data[parent.tets[iT][0]]; - colorval_2[iT] = values.data[parent.tets[iT][1]]; - colorval_3[iT] = values.data[parent.tets[iT][2]]; - colorval_4[iT] = values.data[parent.tets[iT][3]]; + colorval_1[iT] = values.getHostValue(parent.tets[iT][0]); + colorval_2[iT] = values.getHostValue(parent.tets[iT][1]); + colorval_3[iT] = values.getHostValue(parent.tets[iT][2]); + colorval_4[iT] = values.getHostValue(parent.tets[iT][3]); } // Store data in buffers diff --git a/test/src/volume_grid_test.cpp b/test/src/volume_grid_test.cpp index ee499f12..40e5636a 100644 --- a/test/src/volume_grid_test.cpp +++ b/test/src/volume_grid_test.cpp @@ -232,7 +232,7 @@ TEST_F(PolyscopeTest, VolumeGridScalarIsosurfaceIndexing) { // get the vertices from the is surface polyscope::SurfaceMesh* isoTest = polyscope::getSurfaceMesh("iso test"); isoTest->vertexPositions.ensureHostBufferPopulated(); - std::vector isoverts = isoTest->vertexPositions.data; + std::vector isoverts(isoTest->vertexPositions.begin(), isoTest->vertexPositions.end()); // test that all vertices are within the bounds float EPS = 0.0001; From a759ef6fab9e5d8039fb5fa560c20ae5b288837a Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sat, 9 May 2026 15:18:57 -0700 Subject: [PATCH 04/31] pass managed buffers directly to shaders, change lazy update strategy --- include/polyscope/render/engine.h | 15 +- include/polyscope/render/managed_buffer.h | 156 ++++--- .../render/mock_opengl/mock_gl_engine.h | 9 +- include/polyscope/render/opengl/gl_engine.h | 9 +- include/polyscope/vector_quantity.ipp | 12 +- src/color_image_quantity.cpp | 4 +- src/color_render_image_quantity.cpp | 6 +- src/curve_network.cpp | 22 +- src/curve_network_color_quantity.cpp | 10 +- src/curve_network_scalar_quantity.cpp | 12 +- src/depth_render_image_quantity.cpp | 4 +- src/point_cloud.cpp | 6 +- src/point_cloud_color_quantity.cpp | 2 +- src/point_cloud_parameterization_quantity.cpp | 2 +- src/point_cloud_scalar_quantity.cpp | 2 +- src/raw_color_alpha_render_image_quantity.cpp | 4 +- src/raw_color_render_image_quantity.cpp | 4 +- src/render/managed_buffer.cpp | 416 +++++++++--------- src/render/mock_opengl/mock_gl_engine.cpp | 21 +- src/render/opengl/gl_engine.cpp | 21 +- src/render_image_quantity_base.cpp | 2 +- src/scalar_image_quantity.cpp | 4 +- src/scalar_render_image_quantity.cpp | 6 +- src/simple_triangle_mesh.cpp | 2 +- src/slice_plane.cpp | 8 +- src/sparse_volume_grid.cpp | 28 +- src/sparse_volume_grid_color_quantity.cpp | 4 +- src/sparse_volume_grid_scalar_quantity.cpp | 4 +- src/surface_color_quantity.cpp | 10 +- src/surface_mesh.cpp | 24 +- src/surface_parameterization_quantity.cpp | 6 +- src/surface_scalar_quantity.cpp | 20 +- src/surface_vector_quantity.cpp | 4 +- src/volume_grid.cpp | 16 +- src/volume_grid_scalar_quantity.cpp | 20 +- src/volume_mesh.cpp | 24 +- src/volume_mesh_color_quantity.cpp | 4 +- src/volume_mesh_scalar_quantity.cpp | 4 +- 38 files changed, 523 insertions(+), 404 deletions(-) diff --git a/include/polyscope/render/engine.h b/include/polyscope/render/engine.h index f0ba8959..ecba5d7c 100644 --- a/include/polyscope/render/engine.h +++ b/include/polyscope/render/engine.h @@ -17,6 +17,12 @@ namespace polyscope { +// Forward declarations for ManagedBuffer integration +namespace render { + class ManagedBufferBase; + template class ManagedBuffer; +} // namespace render + // == A few enums that control behavior // public enums are in the outer namespace to keep the typing burden down @@ -395,7 +401,7 @@ class ShaderProgram { virtual bool hasAttribute(std::string name) = 0; virtual bool attributeIsSet(std::string name) = 0; virtual std::shared_ptr getAttributeBuffer(std::string name) = 0; - virtual void setAttribute(std::string name, std::shared_ptr externalBuffer) = 0; + virtual void setAttribute(std::string name, std::shared_ptr externalBuffer, ManagedBufferBase* source = nullptr) = 0; virtual void setAttribute(std::string name, const std::vector& data) = 0; virtual void setAttribute(std::string name, const std::vector& data) = 0; virtual void setAttribute(std::string name, const std::vector& data) = 0; @@ -418,8 +424,13 @@ class ShaderProgram { bool withAlpha = true, bool useMipMap = false, bool repeat = false) = 0; virtual void setTextureFromColormap(std::string name, const std::string& colorMap, bool allowUpdate = false) = 0; // TODO make this one take a shared pointer and have the same semantics as the attribute version - virtual void setTextureFromBuffer(std::string name, TextureBuffer* textureBuffer) = 0; + virtual void setTextureFromBuffer(std::string name, TextureBuffer* textureBuffer, ManagedBufferBase* source = nullptr) = 0; + + // Convenience overloads for ManagedBuffer — set the buffer AND record the source for lazy sync. + // Defined in managed_buffer.h after ManagedBuffer is fully declared. + template void setAttribute(std::string name, ManagedBuffer& buf); + template void setTextureFromBuffer(std::string name, ManagedBuffer& buf); // Indices virtual void setIndex(std::shared_ptr externalBuffer) = 0; diff --git a/include/polyscope/render/managed_buffer.h b/include/polyscope/render/managed_buffer.h index 7049890a..9247a092 100644 --- a/include/polyscope/render/managed_buffer.h +++ b/include/polyscope/render/managed_buffer.h @@ -17,6 +17,69 @@ namespace render { // forward declaration class ManagedBufferRegistry; +/* + * Non-templated base class for ManagedBuffer. + * Contains all type-independent members and provides the syncToDeviceIfNeeded() virtual interface + * used by ShaderProgram::draw() to lazily push host data to the GPU before issuing a draw call. + */ +class ManagedBufferBase { +public: + virtual ~ManagedBufferBase() = default; + + // Called by ShaderProgram::draw() before issuing the GPU draw call. + // Pushes host data to device if deviceBufferValid is false and hostBufferValid is true. + virtual void syncToDeviceIfNeeded() = 0; + + const std::string name; + const uint64_t uniqueID; + +protected: + // protected constructors — only ManagedBuffer should construct this + ManagedBufferBase(ManagedBufferRegistry* registry_, const std::string& name_, bool dataGetsComputed_, + bool hostBufferValid_); + + // Internal helpers that don't depend on T + void invalidateHostBuffer(); + bool deviceBufferTypeIsTexture() const; + void checkDeviceBufferTypeIs(DeviceBufferType targetType) const; + void checkDeviceBufferTypeIsTexture() const; + + // True when the buffer has no valid data on host or device and is waiting to be computed. + bool isInNeedsComputeState() const { return !hostBufferValid && !deviceBufferValid && dataGetsComputed; } + + ManagedBufferRegistry* registry; + + bool dataGetsComputed; // if true, the value gets computed on-demand by calling computeFunc() + std::function computeFunc; // (optional) callback which populates the buffer + + bool hostBufferValid; // true if host-side data is current + bool deviceBufferValid; // true if device-side buffer matches current host data + + // True if all registered indexed views are consistent with the current source data. + // Set to false by markRenderAttributeBufferUpdated() when the GPU source is written externally + // and host data is no longer available to re-gather from. Will be set back to true once + // updateIndexedViews() runs successfully (which requires hostBufferValid). + // Future work: an on-device gather pass will be able to update indexed views even when + // !hostBufferValid, at which point this flag drives that path. + bool indexedViewsValid = true; + + // Explicit size and capacity. + // Invariant: when hostBufferValid, data.size() == managedCapacity (the vector is always kept at full + // capacity — data.capacity() (std::vector internal) is never relied on for anything). + // currentSize <= managedCapacity is the number of logically valid elements. + size_t managedCapacity = 0; + size_t currentSize = 0; + + std::shared_ptr renderAttributeBuffer; + std::shared_ptr renderTextureBuffer; + + DeviceBufferType deviceBufferType = DeviceBufferType::Attribute; + uint32_t sizeX = 0; + uint32_t sizeY = 0; + uint32_t sizeZ = 0; +}; + + /* * This class owns and manages a typed data buffer in Polyscope, handling common data-management concerns of: * @@ -32,15 +95,16 @@ class ManagedBufferRegistry; * Use the public accessor API (getHostValue, setHostValue, setDataHost, begin/end, etc.) to interact with the buffer. */ template -class ManagedBuffer : public virtual WeakReferrable { +class ManagedBuffer : public ManagedBufferBase, public virtual WeakReferrable { public: // === Constructors - // (second variants are advanced versions which allow creation of multi-dimensional texture values) + + // Create an empty buffer with no data. Use resize()+setHostValue() or setDataHost() to populate. + ManagedBuffer(ManagedBufferRegistry* registry, const std::string& name); // Manage a buffer of data which is explicitly set externally. ManagedBuffer(ManagedBufferRegistry* registry, const std::string& name, std::vector data); - // Manage a buffer of data which gets computed lazily ManagedBuffer(ManagedBufferRegistry* registry, const std::string& name, std::function computeFunc); @@ -49,16 +113,7 @@ class ManagedBuffer : public virtual WeakReferrable { ~ManagedBuffer(); - // === Core members - - // A meaningful name for the buffer - std::string name; - - // A globally-unique ID - const uint64_t uniqueID; - - // The registry in which it is tracked (can be null) - ManagedBufferRegistry* registry; + // === Core members (note: name, uniqueID, registry, dataGetsComputed, computeFunc live in ManagedBufferBase) // sanity check helper @@ -81,7 +136,7 @@ class ManagedBuffer : public virtual WeakReferrable { // But in settings where we e.g. incrementally add elements or change the number of data elements on each frame, we // may want to allocate a larger capacity to avoid expensive re-allocation each time. - // Resize the buffer to newSize elements. Sets hostBufferIsPopulated = true. + // Resize the buffer to newSize elements. Sets hostBufferValid = true. // // If newSize <= capacity(), this is a cheap constant-time operation which just updates metadata. // @@ -131,7 +186,10 @@ class ManagedBuffer : public virtual WeakReferrable { // device buffers and triggers a redraw. Prefer this over setHostValue() for bulk writes. void setDataHost(const std::vector& newData); - // Iterator support. Caller must call ensureHostBufferPopulated() before using these. + // Returns a full copy of the host data, populating it from device if needed. + std::vector getDataCopy(); + + // Raw pointer iteration support. Caller must call ensureHostBufferPopulated() before using these. // A debug-mode check will fire if this precondition is violated. const T* begin() const; const T* end() const; @@ -182,8 +240,10 @@ class ManagedBuffer : public virtual WeakReferrable { // NOTE: this is only for attribute-accessed buffers (DeviceBufferType::Attribute). See the variants below for // textures. - // NOTE: This class follows the policy that once the render buffer is allocated, it is always immediately kept - // updated to reflect any external changes. + // NOTE: This class follows a lazy-sync policy: once the render buffer is allocated, it is kept up to date + // with host-side changes lazily — the actual GPU upload happens in syncToDeviceIfNeeded(), which is called + // by ShaderProgram::draw() just before the draw call. External writes to the GPU buffer (via + // markRenderAttributeBufferUpdated()) invalidate the host copy until ensureHostBufferPopulated() is called. // Get a reference to the underlying GPU-side attribute buffer // Once this reference is created, it will always be immediately updated to reflect any external changes to the @@ -230,38 +290,16 @@ class ManagedBuffer : public virtual WeakReferrable { void markRenderTextureBufferUpdated(); + // Sync host data to the device buffer if needed. Called by ShaderProgram::draw() before drawing. + void syncToDeviceIfNeeded() override; + protected: // == Internal members + // Note: dataGetsComputed, computeFunc, hostBufferValid, deviceBufferValid, managedCapacity, + // renderAttributeBuffer, renderTextureBuffer, deviceBufferType, sizeX/Y/Z all live in ManagedBufferBase. - // This class tracks data from two separate sources: - // A) Values directly stored in the buffer by an external source (dataGetsComputed == false) - // B) Values that get computed lazily by some function when needed (dataGetsComputed == true) - // - // When dataGetsComputed is true, computeFunc() must be set to a callback that populates the buffer. - // The callback should call resize() then setHostValue() to populate the buffer. - // The callback does NOT need to call markHostBufferUpdated() — that is handled by the ManagedBuffer - // infrastructure after the callback returns. - bool dataGetsComputed; // if true, the value gets computed on-demand by calling computeFunc() - std::function computeFunc; // (optional) callback which populates the buffer - - bool hostBufferIsPopulated; // true if the host buffer contains currently-valid data - - // The buffer has capacity for at least this many elements. It is distinct from .size(), which is the actual number of - // elements currently stored in the buffer and may be smaller than the capacity. - // Any resize() operations that stay within the capacity are cheap, and do not trigger a full reallocation and copy. - size_t managedCapacity = 0; - - std::shared_ptr renderAttributeBuffer; - std::shared_ptr renderTextureBuffer; - - // For storing as textures - - // For data that can be interpreted as a 1/2/3 dimensional texture - DeviceBufferType deviceBufferType = DeviceBufferType::Attribute; // this gets set when you call setTextureSize - uint32_t sizeX = 0; - uint32_t sizeY = 0; // holds 0 if texture dim < 2 - uint32_t sizeZ = 0; // holds 0 if texture dim < 3 - + // Override invalidateHostBuffer to also clear the typed data vector. + void invalidateHostBuffer(); // == Internal representation of indexed views // NOTE: this seems like a problem, we are storing pointers as keys in a cache. Here, it works out because if the @@ -271,13 +309,7 @@ class ManagedBuffer : public virtual WeakReferrable { existingIndexedViews; void updateIndexedViews(); void removeDeletedIndexedViews(); - - // == Internal helper functions - - void invalidateHostBuffer(); - bool deviceBufferTypeIsTexture() const; - void checkDeviceBufferTypeIs(DeviceBufferType targetType) const; - void checkDeviceBufferTypeIsTexture() const; + void invalidateIndexedViews(); enum class CanonicalDataSource { HostData = 0, NeedsCompute, RenderBuffer }; CanonicalDataSource currentCanonicalDataSource(); @@ -373,3 +405,21 @@ std::string typeName(ManagedBufferType type); } // namespace polyscope #include "polyscope/render/managed_buffer.ipp" + +// Implementations of ShaderProgram's ManagedBuffer convenience overloads +// (defined here because they need the full ManagedBuffer definition) +namespace polyscope { +namespace render { + +template +void ShaderProgram::setAttribute(std::string name, ManagedBuffer& buf) { + setAttribute(name, buf.getRenderAttributeBuffer(), &buf); +} + +template +void ShaderProgram::setTextureFromBuffer(std::string name, ManagedBuffer& buf) { + setTextureFromBuffer(name, buf.getRenderTextureBuffer().get(), &buf); +} + +} // namespace render +} // namespace polyscope diff --git a/include/polyscope/render/mock_opengl/mock_gl_engine.h b/include/polyscope/render/mock_opengl/mock_gl_engine.h index b135564a..ef0e1ad5 100644 --- a/include/polyscope/render/mock_opengl/mock_gl_engine.h +++ b/include/polyscope/render/mock_opengl/mock_gl_engine.h @@ -212,6 +212,7 @@ struct GLShaderAttribute { RenderDataType type; int arrayCount; std::shared_ptr buff; // the buffer that we will actually use + ManagedBufferBase* sourceManagedBuffer; // might be empty if not from a managed buffer. for indexed views, it's the original data source }; struct GLShaderTexture { @@ -221,6 +222,7 @@ struct GLShaderTexture { bool isSet; GLTextureBuffer* textureBuffer; std::shared_ptr textureBufferOwned; // might be empty, if texture isn't owned + ManagedBufferBase* sourceManagedBuffer; // might be empty if not from a managed buffer. for indexed views, it's the original data source }; // A thin wrapper around a program handle. @@ -282,7 +284,7 @@ class GLShaderProgram : public ShaderProgram { bool hasAttribute(std::string name) override; bool attributeIsSet(std::string name) override; std::shared_ptr getAttributeBuffer(std::string name) override; - void setAttribute(std::string name, std::shared_ptr externalBuffer) override; + void setAttribute(std::string name, std::shared_ptr externalBuffer, ManagedBufferBase* source = nullptr) override; void setAttribute(std::string name, const std::vector& data) override; void setAttribute(std::string name, const std::vector& data) override; void setAttribute(std::string name, const std::vector& data) override; @@ -310,11 +312,14 @@ class GLShaderProgram : public ShaderProgram { void setTexture2D(std::string name, unsigned char* texData, unsigned int width, unsigned int height, bool withAlpha = true, bool useMipMap = false, bool repeat = false) override; void setTextureFromColormap(std::string name, const std::string& colorMap, bool allowUpdate = false) override; - void setTextureFromBuffer(std::string name, TextureBuffer* textureBuffer) override; + void setTextureFromBuffer(std::string name, TextureBuffer* textureBuffer, ManagedBufferBase* source = nullptr) override; + + // ManagedBuffer source registration // Draw! void draw() override; void validateData() override; + void syncBuffersToDeviceIfNeeded(); protected: // Lists of attributes and uniforms that need to be set diff --git a/include/polyscope/render/opengl/gl_engine.h b/include/polyscope/render/opengl/gl_engine.h index ef5ce764..9f6c4d3a 100644 --- a/include/polyscope/render/opengl/gl_engine.h +++ b/include/polyscope/render/opengl/gl_engine.h @@ -252,6 +252,7 @@ struct GLShaderAttribute { int arrayCount; AttributeLocation location; // -1 means "no location", usually because it was optimized out std::shared_ptr buff; // the buffer that we will actually use + ManagedBufferBase* sourceManagedBuffer; // might be empty if not from a managed buffer. for indexed views, it's the original data source }; struct GLShaderTexture { @@ -262,6 +263,7 @@ struct GLShaderTexture { GLTextureBuffer* textureBuffer; std::shared_ptr textureBufferOwned; // might be empty, if texture isn't owned TextureLocation location; // -1 means "no location", usually because it was optimized out + ManagedBufferBase* sourceManagedBuffer; // might be empty if not from a managed buffer. for indexed views, it's the original data source }; // A thin wrapper around a program handle. @@ -325,7 +327,7 @@ class GLShaderProgram : public ShaderProgram { bool hasAttribute(std::string name) override; bool attributeIsSet(std::string name) override; std::shared_ptr getAttributeBuffer(std::string name) override; - void setAttribute(std::string name, std::shared_ptr externalBuffer) override; + void setAttribute(std::string name, std::shared_ptr externalBuffer, ManagedBufferBase* source = nullptr) override; void setAttribute(std::string name, const std::vector& data) override; void setAttribute(std::string name, const std::vector& data) override; void setAttribute(std::string name, const std::vector& data) override; @@ -354,11 +356,14 @@ class GLShaderProgram : public ShaderProgram { void setTexture2D(std::string name, unsigned char* texData, unsigned int width, unsigned int height, bool withAlpha = true, bool useMipMap = false, bool repeat = false) override; void setTextureFromColormap(std::string name, const std::string& colorMap, bool allowUpdate = false) override; - void setTextureFromBuffer(std::string name, TextureBuffer* textureBuffer) override; + void setTextureFromBuffer(std::string name, TextureBuffer* textureBuffer, ManagedBufferBase* source = nullptr) override; + + // ManagedBuffer source registration // Draw! void draw() override; void validateData() override; + void syncBuffersToDeviceIfNeeded(); protected: // Lists of attributes and uniforms that need to be set diff --git a/include/polyscope/vector_quantity.ipp b/include/polyscope/vector_quantity.ipp index d3db2c3c..0f4a5bba 100644 --- a/include/polyscope/vector_quantity.ipp +++ b/include/polyscope/vector_quantity.ipp @@ -175,8 +175,8 @@ void VectorQuantity::createProgram() { ); // clang-format on - this->vectorProgram->setAttribute("a_vector", vectors.getRenderAttributeBuffer()); - this->vectorProgram->setAttribute("a_position", vectorRoots.getRenderAttributeBuffer()); + this->vectorProgram->setAttribute("a_vector", vectors); + this->vectorProgram->setAttribute("a_position", vectorRoots); render::engine->setMaterial(*(this->vectorProgram), this->material.get()); } @@ -299,10 +299,10 @@ void TangentVectorQuantity::createProgram() { ); // clang-format on - this->vectorProgram->setAttribute("a_tangentVector", tangentVectors.getRenderAttributeBuffer()); - this->vectorProgram->setAttribute("a_basisVectorX", tangentBasisX.getRenderAttributeBuffer()); - this->vectorProgram->setAttribute("a_basisVectorY", tangentBasisY.getRenderAttributeBuffer()); - this->vectorProgram->setAttribute("a_position", vectorRoots.getRenderAttributeBuffer()); + this->vectorProgram->setAttribute("a_tangentVector", tangentVectors); + this->vectorProgram->setAttribute("a_basisVectorX", tangentBasisX); + this->vectorProgram->setAttribute("a_basisVectorY", tangentBasisY); + this->vectorProgram->setAttribute("a_position", vectorRoots); render::engine->setMaterial(*(this->vectorProgram), this->material.get()); } diff --git a/src/color_image_quantity.cpp b/src/color_image_quantity.cpp index 82482a90..72971a84 100644 --- a/src/color_image_quantity.cpp +++ b/src/color_image_quantity.cpp @@ -56,7 +56,7 @@ void ColorImageQuantity::prepareFullscreen() { fullscreenProgram->setAttribute("a_position", render::engine->screenTrianglesCoords()); // TODO throughout polyscope we discard the shared pointer when adding textures/attributes to programs... should we // just track the shared pointer? - fullscreenProgram->setTextureFromBuffer("t_image", colors.getRenderTextureBuffer().get()); + fullscreenProgram->setTextureFromBuffer("t_image", colors); } void ColorImageQuantity::prepareBillboard() { @@ -75,7 +75,7 @@ void ColorImageQuantity::prepareBillboard() { render::ShaderReplacementDefaults::Process); // clang-format on billboardProgram->setAttribute("a_position", render::engine->screenTrianglesCoords()); - billboardProgram->setTextureFromBuffer("t_image", colors.getRenderTextureBuffer().get()); + billboardProgram->setTextureFromBuffer("t_image", colors); } void ColorImageQuantity::showFullscreen() { diff --git a/src/color_render_image_quantity.cpp b/src/color_render_image_quantity.cpp index 266d6254..5e0f3e52 100644 --- a/src/color_render_image_quantity.cpp +++ b/src/color_render_image_quantity.cpp @@ -72,11 +72,11 @@ void ColorRenderImageQuantity::prepare() { program = render::engine->requestShader("TEXTURE_DRAW_RENDERIMAGE_PLAIN", rules); program->setAttribute("a_position", render::engine->screenTrianglesCoords()); - program->setTextureFromBuffer("t_depth", depths.getRenderTextureBuffer().get()); + program->setTextureFromBuffer("t_depth", depths); if (hasNormals) { - program->setTextureFromBuffer("t_normal", normals.getRenderTextureBuffer().get()); + program->setTextureFromBuffer("t_normal", normals); } - program->setTextureFromBuffer("t_color", colors.getRenderTextureBuffer().get()); + program->setTextureFromBuffer("t_color", colors); render::engine->setMaterial(*program, material.get()); } diff --git a/src/curve_network.cpp b/src/curve_network.cpp index a6a1b580..4d7b0b63 100644 --- a/src/curve_network.cpp +++ b/src/curve_network.cpp @@ -22,8 +22,8 @@ CurveNetwork::CurveNetwork(std::string name, std::vector nodes_, std: : // clang-format off Structure(name, typeName()), nodePositions(this, uniquePrefix() + "nodePositions", std::move(nodes_)), - edgeTailInds(this, uniquePrefix() + "edgeTailInds", std::vector{}), - edgeTipInds(this, uniquePrefix() + "edgeTipInds", std::vector{}), + edgeTailInds(this, uniquePrefix() + "edgeTailInds"), + edgeTipInds(this, uniquePrefix() + "edgeTipInds"), edgeCenters(this, uniquePrefix() + "edgeCenters", std::bind(&CurveNetwork::computeEdgeCenters, this)), color(uniquePrefix() + "#color", getNextUniqueColor()), radius(uniquePrefix() + "#radius", relativeValue(0.005)), @@ -347,7 +347,7 @@ void CurveNetwork::preparePick() { } void CurveNetwork::fillNodeGeometryBuffers(render::ShaderProgram& program) { - program.setAttribute("a_position", nodePositions.getRenderAttributeBuffer()); + program.setAttribute("a_position", nodePositions); bool haveNodeRadiusQuantity = (nodeRadiusQuantityName != ""); bool haveEdgeRadiusQuantity = (edgeRadiusQuantityName != ""); @@ -355,18 +355,18 @@ void CurveNetwork::fillNodeGeometryBuffers(render::ShaderProgram& program) { if (haveNodeRadiusQuantity) { // have just node, or have both CurveNetworkNodeScalarQuantity& nodeRadQ = resolveNodeRadiusQuantity(); - program.setAttribute("a_pointRadius", nodeRadQ.values.getRenderAttributeBuffer()); + program.setAttribute("a_pointRadius", nodeRadQ.values); } else if (haveEdgeRadiusQuantity) { // have just edge CurveNetworkEdgeScalarQuantity& edgeRadQ = resolveEdgeRadiusQuantity(); edgeRadQ.updateNodeAverageValues(); - program.setAttribute("a_pointRadius", edgeRadQ.nodeAverageValues.getRenderAttributeBuffer()); + program.setAttribute("a_pointRadius", edgeRadQ.nodeAverageValues); } } void CurveNetwork::fillEdgeGeometryBuffers(render::ShaderProgram& program) { - program.setAttribute("a_position_tail", nodePositions.getIndexedRenderAttributeBuffer(edgeTailInds)); - program.setAttribute("a_position_tip", nodePositions.getIndexedRenderAttributeBuffer(edgeTipInds)); + program.setAttribute("a_position_tail", nodePositions.getIndexedRenderAttributeBuffer(edgeTailInds), &nodePositions); + program.setAttribute("a_position_tip", nodePositions.getIndexedRenderAttributeBuffer(edgeTipInds), &nodePositions); bool haveNodeRadiusQuantity = (nodeRadiusQuantityName != ""); bool haveEdgeRadiusQuantity = (edgeRadiusQuantityName != ""); @@ -374,13 +374,13 @@ void CurveNetwork::fillEdgeGeometryBuffers(render::ShaderProgram& program) { if (haveEdgeRadiusQuantity) { // have just edge or have both CurveNetworkEdgeScalarQuantity& edgeRadQ = resolveEdgeRadiusQuantity(); - program.setAttribute("a_tailRadius", edgeRadQ.values.getRenderAttributeBuffer()); - program.setAttribute("a_tipRadius", edgeRadQ.values.getRenderAttributeBuffer()); + program.setAttribute("a_tailRadius", edgeRadQ.values); + program.setAttribute("a_tipRadius", edgeRadQ.values); } else if (haveNodeRadiusQuantity) { // have just node CurveNetworkNodeScalarQuantity& nodeRadQ = resolveNodeRadiusQuantity(); - program.setAttribute("a_tailRadius", nodeRadQ.values.getIndexedRenderAttributeBuffer(edgeTailInds)); - program.setAttribute("a_tipRadius", nodeRadQ.values.getIndexedRenderAttributeBuffer(edgeTipInds)); + program.setAttribute("a_tailRadius", nodeRadQ.values.getIndexedRenderAttributeBuffer(edgeTailInds), &nodeRadQ.values); + program.setAttribute("a_tipRadius", nodeRadQ.values.getIndexedRenderAttributeBuffer(edgeTipInds), &nodeRadQ.values); } } diff --git a/src/curve_network_color_quantity.cpp b/src/curve_network_color_quantity.cpp index 3ec89cb2..4e29c342 100644 --- a/src/curve_network_color_quantity.cpp +++ b/src/curve_network_color_quantity.cpp @@ -70,12 +70,12 @@ void CurveNetworkNodeColorQuantity::createProgram() { parent.fillNodeGeometryBuffers(*nodeProgram); { // Fill node color buffers - nodeProgram->setAttribute("a_color", colors.getRenderAttributeBuffer()); + nodeProgram->setAttribute("a_color", colors); } { // Fill edge color buffers - edgeProgram->setAttribute("a_color_tail", colors.getIndexedRenderAttributeBuffer(parent.edgeTailInds)); - edgeProgram->setAttribute("a_color_tip", colors.getIndexedRenderAttributeBuffer(parent.edgeTipInds)); + edgeProgram->setAttribute("a_color_tail", colors.getIndexedRenderAttributeBuffer(parent.edgeTailInds), &colors); + edgeProgram->setAttribute("a_color_tip", colors.getIndexedRenderAttributeBuffer(parent.edgeTipInds), &colors); } render::engine->setMaterial(*nodeProgram, parent.getMaterial()); @@ -142,11 +142,11 @@ void CurveNetworkEdgeColorQuantity::createProgram() { { // Fill node color buffers // Compute an average color at each node updateNodeAverageColors(); - nodeProgram->setAttribute("a_color", nodeAverageColors.getRenderAttributeBuffer()); + nodeProgram->setAttribute("a_color", nodeAverageColors); } { // Fill edge color buffers - edgeProgram->setAttribute("a_color", colors.getRenderAttributeBuffer()); + edgeProgram->setAttribute("a_color", colors); } render::engine->setMaterial(*nodeProgram, parent.getMaterial()); diff --git a/src/curve_network_scalar_quantity.cpp b/src/curve_network_scalar_quantity.cpp index 7d8bc141..04857a84 100644 --- a/src/curve_network_scalar_quantity.cpp +++ b/src/curve_network_scalar_quantity.cpp @@ -100,12 +100,12 @@ void CurveNetworkNodeScalarQuantity::createProgram() { parent.fillEdgeGeometryBuffers(*edgeProgram); { // Fill node color buffers - nodeProgram->setAttribute("a_value", values.getRenderAttributeBuffer()); + nodeProgram->setAttribute("a_value", values); } { // Fill edge color buffers - edgeProgram->setAttribute("a_value_tail", values.getIndexedRenderAttributeBuffer(parent.edgeTailInds)); - edgeProgram->setAttribute("a_value_tip", values.getIndexedRenderAttributeBuffer(parent.edgeTipInds)); + edgeProgram->setAttribute("a_value_tail", values.getIndexedRenderAttributeBuffer(parent.edgeTailInds), &values); + edgeProgram->setAttribute("a_value_tip", values.getIndexedRenderAttributeBuffer(parent.edgeTipInds), &values); } edgeProgram->setTextureFromColormap("t_colormap", cMap.get()); @@ -130,7 +130,7 @@ void CurveNetworkNodeScalarQuantity::buildNodeInfoGUI(size_t nInd) { CurveNetworkEdgeScalarQuantity::CurveNetworkEdgeScalarQuantity(std::string name, const std::vector& values_, CurveNetwork& network_, DataType dataType_) : CurveNetworkScalarQuantity(name, network_, "edge", values_, dataType_), - nodeAverageValues(this, uniquePrefix() + "#nodeAverageValues", std::vector{}) {} + nodeAverageValues(this, uniquePrefix() + "#nodeAverageValues") {} void CurveNetworkEdgeScalarQuantity::createProgram() { // Create the program to draw this quantity @@ -162,11 +162,11 @@ void CurveNetworkEdgeScalarQuantity::createProgram() { { // Fill node color buffers updateNodeAverageValues(); - nodeProgram->setAttribute("a_value", nodeAverageValues.getRenderAttributeBuffer()); + nodeProgram->setAttribute("a_value", nodeAverageValues); } { // Fill edge color buffers - edgeProgram->setAttribute("a_value", values.getRenderAttributeBuffer()); + edgeProgram->setAttribute("a_value", values); } edgeProgram->setTextureFromColormap("t_colormap", cMap.get()); diff --git a/src/depth_render_image_quantity.cpp b/src/depth_render_image_quantity.cpp index b6d0d947..a5890b20 100644 --- a/src/depth_render_image_quantity.cpp +++ b/src/depth_render_image_quantity.cpp @@ -79,9 +79,9 @@ void DepthRenderImageQuantity::prepare() { // clang-format on program->setAttribute("a_position", render::engine->screenTrianglesCoords()); - program->setTextureFromBuffer("t_depth", depths.getRenderTextureBuffer().get()); + program->setTextureFromBuffer("t_depth", depths); if (hasNormals) { - program->setTextureFromBuffer("t_normal", normals.getRenderTextureBuffer().get()); + program->setTextureFromBuffer("t_normal", normals); } render::engine->setMaterial(*program, material.get()); } diff --git a/src/point_cloud.cpp b/src/point_cloud.cpp index 85251730..9837770b 100644 --- a/src/point_cloud.cpp +++ b/src/point_cloud.cpp @@ -199,14 +199,14 @@ void PointCloud::ensurePickProgramPrepared() { } void PointCloud::setPointProgramGeometryAttributes(render::ShaderProgram& p) { - p.setAttribute("a_position", points.getRenderAttributeBuffer()); + p.setAttribute("a_position", points); if (pointRadiusQuantityName != "") { PointCloudScalarQuantity& radQ = resolvePointRadiusQuantity(); - p.setAttribute("a_pointRadius", radQ.values.getRenderAttributeBuffer()); + p.setAttribute("a_pointRadius", radQ.values); } if (transparencyQuantityName != "") { PointCloudScalarQuantity& transparencyQ = resolveTransparencyQuantity(); - p.setAttribute("a_valueAlpha", transparencyQ.values.getRenderAttributeBuffer()); + p.setAttribute("a_valueAlpha", transparencyQ.values); } } diff --git a/src/point_cloud_color_quantity.cpp b/src/point_cloud_color_quantity.cpp index 687487e0..d6a28066 100644 --- a/src/point_cloud_color_quantity.cpp +++ b/src/point_cloud_color_quantity.cpp @@ -47,7 +47,7 @@ void PointCloudColorQuantity::createPointProgram() { // clang-format on parent.setPointProgramGeometryAttributes(*pointProgram); - pointProgram->setAttribute("a_color", colors.getRenderAttributeBuffer()); + pointProgram->setAttribute("a_color", colors); // Fill buffers render::engine->setMaterial(*pointProgram, parent.getMaterial()); diff --git a/src/point_cloud_parameterization_quantity.cpp b/src/point_cloud_parameterization_quantity.cpp index d0dfc08b..3c7784e2 100644 --- a/src/point_cloud_parameterization_quantity.cpp +++ b/src/point_cloud_parameterization_quantity.cpp @@ -56,7 +56,7 @@ void PointCloudParameterizationQuantity::createProgram() { } void PointCloudParameterizationQuantity::fillCoordBuffers(render::ShaderProgram& p) { - p.setAttribute("a_value2", coords.getRenderAttributeBuffer()); + p.setAttribute("a_value2", coords); } void PointCloudParameterizationQuantity::buildCustomUI() { diff --git a/src/point_cloud_scalar_quantity.cpp b/src/point_cloud_scalar_quantity.cpp index 045ff44e..4cb11ff2 100644 --- a/src/point_cloud_scalar_quantity.cpp +++ b/src/point_cloud_scalar_quantity.cpp @@ -66,7 +66,7 @@ void PointCloudScalarQuantity::createProgram() { // clang-format on parent.setPointProgramGeometryAttributes(*pointProgram); - pointProgram->setAttribute("a_value", values.getRenderAttributeBuffer()); + pointProgram->setAttribute("a_value", values); // Fill buffers pointProgram->setTextureFromColormap("t_colormap", cMap.get()); diff --git a/src/raw_color_alpha_render_image_quantity.cpp b/src/raw_color_alpha_render_image_quantity.cpp index b4ab180e..fba8750a 100644 --- a/src/raw_color_alpha_render_image_quantity.cpp +++ b/src/raw_color_alpha_render_image_quantity.cpp @@ -75,8 +75,8 @@ void RawColorAlphaRenderImageQuantity::prepare() { // clang-format on program->setAttribute("a_position", render::engine->screenTrianglesCoords()); - program->setTextureFromBuffer("t_depth", depths.getRenderTextureBuffer().get()); - program->setTextureFromBuffer("t_color", colors.getRenderTextureBuffer().get()); + program->setTextureFromBuffer("t_depth", depths); + program->setTextureFromBuffer("t_color", colors); } diff --git a/src/raw_color_render_image_quantity.cpp b/src/raw_color_render_image_quantity.cpp index 5ba8cd52..4d18ce92 100644 --- a/src/raw_color_render_image_quantity.cpp +++ b/src/raw_color_render_image_quantity.cpp @@ -69,8 +69,8 @@ void RawColorRenderImageQuantity::prepare() { // clang-format on program->setAttribute("a_position", render::engine->screenTrianglesCoords()); - program->setTextureFromBuffer("t_depth", depths.getRenderTextureBuffer().get()); - program->setTextureFromBuffer("t_color", colors.getRenderTextureBuffer().get()); + program->setTextureFromBuffer("t_depth", depths); + program->setTextureFromBuffer("t_color", colors); } diff --git a/src/render/managed_buffer.cpp b/src/render/managed_buffer.cpp index 793354bf..2a60a2ca 100644 --- a/src/render/managed_buffer.cpp +++ b/src/render/managed_buffer.cpp @@ -15,12 +15,60 @@ namespace polyscope { namespace render { +// === ManagedBufferBase implementations === + +ManagedBufferBase::ManagedBufferBase(ManagedBufferRegistry* registry_, const std::string& name_, bool dataGetsComputed_, + bool hostBufferValid_) + : name(name_), uniqueID(internal::getNextUniqueID()), registry(registry_), dataGetsComputed(dataGetsComputed_), + hostBufferValid(hostBufferValid_), deviceBufferValid(false) {} + +void ManagedBufferBase::invalidateHostBuffer() { + hostBufferValid = false; + // Note: data.clear() is handled by ManagedBuffer which overrides this conceptually by + // calling the base version and then clearing its own data vector. +} + +bool ManagedBufferBase::deviceBufferTypeIsTexture() const { + return ((deviceBufferType == DeviceBufferType::Texture1d) || (deviceBufferType == DeviceBufferType::Texture2d) || + (deviceBufferType == DeviceBufferType::Texture3d)); +} + +void ManagedBufferBase::checkDeviceBufferTypeIs(DeviceBufferType targetType) const { + if (targetType != deviceBufferType) { + exception("ManagedBuffer " + name + " has wrong type for this operation. Expected " + + deviceBufferTypeName(targetType) + " but is " + deviceBufferTypeName(deviceBufferType)); + } +} + +void ManagedBufferBase::checkDeviceBufferTypeIsTexture() const { + if (!deviceBufferTypeIsTexture()) { + exception("ManagedBuffer " + name + " has wrong type for this operation. Expected a Texture1d/2d/3d but is " + + deviceBufferTypeName(deviceBufferType)); + } +} + + +// === ManagedBuffer implementations === + +template +ManagedBuffer::ManagedBuffer(ManagedBufferRegistry* registry_, const std::string& name_) + : ManagedBufferBase(registry_, name_, /*dataGetsComputed=*/false, /*hostBufferValid=*/true) { + + managedCapacity = 0; + currentSize = 0; + + if (registry) { + registry->addManagedBuffer(this); + } +} + template ManagedBuffer::ManagedBuffer(ManagedBufferRegistry* registry_, const std::string& name_, std::vector data_) - : name(name_), uniqueID(internal::getNextUniqueID()), registry(registry_), data(std::move(data_)), - dataGetsComputed(false), hostBufferIsPopulated(true) { + : ManagedBufferBase(registry_, name_, /*dataGetsComputed=*/false, /*hostBufferValid=*/true), + data(std::move(data_)) { managedCapacity = data.size(); + currentSize = data.size(); if (registry) { registry->addManagedBuffer(this); @@ -31,8 +79,11 @@ ManagedBuffer::ManagedBuffer(ManagedBufferRegistry* registry_, const std::str template ManagedBuffer::ManagedBuffer(ManagedBufferRegistry* registry_, const std::string& name_, std::function computeFunc_) - : name(name_), uniqueID(internal::getNextUniqueID()), registry(registry_), dataGetsComputed(true), - computeFunc(computeFunc_), hostBufferIsPopulated(false), managedCapacity(0) { + : ManagedBufferBase(registry_, name_, /*dataGetsComputed=*/true, /*hostBufferValid=*/false) { + + computeFunc = computeFunc_; + managedCapacity = 0; + currentSize = 0; if (registry) { registry->addManagedBuffer(this); @@ -48,7 +99,8 @@ ManagedBuffer::~ManagedBuffer() { template void ManagedBuffer::checkInvalidValues() { - polyscope::checkInvalidValues(name, data); + // Only check the logically-valid elements; slack in [currentSize, managedCapacity) is uninitialized. + polyscope::checkInvalidValues(name, std::vector(data.begin(), data.begin() + currentSize)); } template @@ -58,8 +110,7 @@ void ManagedBuffer::setTextureSize(uint32_t sizeX_) { deviceBufferType = DeviceBufferType::Texture1d; sizeX = sizeX_; - // Sync managedCapacity: data is expected to be populated before setTextureSize() is called. - if (data.size() > managedCapacity) managedCapacity = data.size(); + // managedCapacity and currentSize are set correctly from construction; no fixup needed. } template @@ -70,7 +121,6 @@ void ManagedBuffer::setTextureSize(uint32_t sizeX_, uint32_t sizeY_) { deviceBufferType = DeviceBufferType::Texture2d; sizeX = sizeX_; sizeY = sizeY_; - if (data.size() > managedCapacity) managedCapacity = data.size(); } template @@ -82,7 +132,6 @@ void ManagedBuffer::setTextureSize(uint32_t sizeX_, uint32_t sizeY_, uint32_t sizeX = sizeX_; sizeY = sizeY_; sizeZ = sizeZ_; - if (data.size() > managedCapacity) managedCapacity = data.size(); } template @@ -106,27 +155,23 @@ bool ManagedBuffer::resize(size_t newSize) { // ensureHostBufferPopulated() reads from renderAttributeBuffer into data; if we resize data // first it would overwrite the resize with the old GPU contents. // Only copy existing data to host if there is data to preserve. - // If state is NeedsCompute, there is no existing data — calling ensureHostBufferPopulated() - // would recursively invoke the compute function that is currently running. - if (currentCanonicalDataSource() != CanonicalDataSource::NeedsCompute) { + if (isInNeedsComputeState()) { + // If state is NeedsCompute, there is no existing data — calling ensureHostBufferPopulated() + // would recursively invoke the compute function that is currently running. + } else { + // Common case ensureHostBufferPopulated(); } - // Reallocation needed: use amortized doubling + // Reallocation needed: use amortized doubling. + // data is always kept at data.size() == managedCapacity (the invariant), so we resize (not reserve). size_t newCapacity = std::max(newSize, 2 * managedCapacity); - data.reserve(newCapacity); - data.resize(newSize); + data.resize(newCapacity); managedCapacity = newCapacity; + currentSize = newSize; - // In-place reallocation: keep the same GPU buffer objects alive so ShaderPrograms - // holding shared_ptr references to them remain valid without needing to be reset. - if (renderAttributeBuffer) { - renderAttributeBuffer->reserveCapacity(managedCapacity); - } - if (renderTextureBuffer && deviceBufferType == DeviceBufferType::Texture1d) { - renderTextureBuffer->resize(static_cast(managedCapacity)); - } - hostBufferIsPopulated = true; + hostBufferValid = true; + deviceBufferValid = false; if (deviceBufferType == DeviceBufferType::Texture1d) { sizeX = static_cast(newSize); @@ -134,9 +179,12 @@ bool ManagedBuffer::resize(size_t newSize) { return true; } else { - // No reallocation: just update size metadata - data.resize(newSize); - hostBufferIsPopulated = true; + // No reallocation: just update currentSize. + // data is already at managedCapacity; restore it if it was cleared by invalidateHostBuffer(). + if (data.size() != managedCapacity) data.resize(managedCapacity); + currentSize = newSize; + hostBufferValid = true; + deviceBufferValid = false; if (deviceBufferType == DeviceBufferType::Texture1d) { sizeX = static_cast(newSize); @@ -151,28 +199,23 @@ void ManagedBuffer::setCapacity(size_t newCapacity) { if (deviceBufferType == DeviceBufferType::Texture2d || deviceBufferType == DeviceBufferType::Texture3d) exception("setCapacity() is not valid for 2D/3D texture buffers"); - if (newCapacity < data.size()) - exception("setCapacity() cannot set capacity below current size (" + std::to_string(data.size()) + ")"); + if (newCapacity < currentSize) + exception("setCapacity() cannot set capacity below current size (" + std::to_string(currentSize) + ")"); if (newCapacity == managedCapacity) return; // no-op // Before invalidating the GPU buffer, copy any device-side changes back to the host. // Skip if state is NeedsCompute — there's no existing data to preserve. - if (currentCanonicalDataSource() != CanonicalDataSource::NeedsCompute) { + if (!isInNeedsComputeState()) { ensureHostBufferPopulated(); } - data.reserve(newCapacity); + // Resize data to the new capacity (maintaining the invariant data.size() == managedCapacity). + data.resize(newCapacity); managedCapacity = newCapacity; - // In-place reallocation: keep the same GPU buffer objects alive. - if (renderAttributeBuffer) { - renderAttributeBuffer->reserveCapacity(managedCapacity); - } - if (renderTextureBuffer && deviceBufferType == DeviceBufferType::Texture1d) { - renderTextureBuffer->resize(static_cast(managedCapacity)); - } - hostBufferIsPopulated = true; + hostBufferValid = true; + deviceBufferValid = false; } template @@ -187,12 +230,14 @@ bool ManagedBuffer::resizeTexture2D(uint32_t newSizeX, uint32_t newSizeY) { sizeX = newSizeX; sizeY = newSizeY; managedCapacity = static_cast(sizeX) * sizeY; + currentSize = managedCapacity; data.resize(managedCapacity); if (renderTextureBuffer) { renderTextureBuffer->resize(sizeX, sizeY); } - hostBufferIsPopulated = true; + hostBufferValid = true; + deviceBufferValid = false; return true; } @@ -210,12 +255,14 @@ bool ManagedBuffer::resizeTexture3D(uint32_t newSizeX, uint32_t newSizeY, uin sizeY = newSizeY; sizeZ = newSizeZ; managedCapacity = static_cast(sizeX) * sizeY * sizeZ; + currentSize = managedCapacity; data.resize(managedCapacity); if (renderTextureBuffer) { renderTextureBuffer->resize(sizeX, sizeY, sizeZ); } - hostBufferIsPopulated = true; + hostBufferValid = true; + deviceBufferValid = false; return true; } @@ -223,20 +270,13 @@ bool ManagedBuffer::resizeTexture3D(uint32_t newSizeX, uint32_t newSizeY, uin template void ManagedBuffer::ensureHostBufferPopulated() { - switch (currentCanonicalDataSource()) { - case CanonicalDataSource::HostData: + if (hostBufferValid) { // good to go, nothing needs to be done - break; - - case CanonicalDataSource::NeedsCompute: - - // compute it - computeFunc(); - - break; - - case CanonicalDataSource::RenderBuffer: + return; + } + if (!hostBufferValid && deviceBufferValid) { + // RenderBuffer case: copy data back from device to host if (deviceBufferTypeIsTexture()) { if (!renderTextureBuffer) exception("render buffer should be allocated but isn't"); @@ -248,18 +288,55 @@ void ManagedBuffer::ensureHostBufferPopulated() { if (!renderAttributeBuffer) exception("render buffer should be allocated but isn't"); // copy the data back from the renderBuffer - data = getAttributeBufferDataRange(*renderAttributeBuffer, 0, renderAttributeBuffer->getDataSize()); + // The GPU buffer is always managedCapacity-sized; only currentSize elements are valid. + // currentSize is already correct (set at the last resize/setDataHost); don't overwrite it. + std::vector readback = getAttributeBufferDataRange(*renderAttributeBuffer, 0, currentSize); + data.resize(managedCapacity); // maintain invariant: data.size() == managedCapacity + std::copy(readback.begin(), readback.end(), data.begin()); + hostBufferValid = true; } + return; + } - break; - }; + if (isInNeedsComputeState()) { + // NeedsCompute case: run the compute function + computeFunc(); + // Note: computeFunc is expected to call resize()+setHostValue() or setDataHost(), which sets hostBufferValid=true + return; + } + + // error! should always be one of the above + exception("ManagedBuffer " + name + " does not have data in either host or device buffers, nor a compute function."); +} + + +template +void ManagedBuffer::setDataHost(const std::vector& newData) { + if (newData.size() > managedCapacity) + exception("ManagedBuffer " + name + " setDataHost() called with data that exceeds capacity (" + + std::to_string(newData.size()) + " > " + std::to_string(managedCapacity) + + "). Call resize() or setCapacity() first."); + // Restore the invariant (data may have been cleared by invalidateHostBuffer()). + if (data.size() != managedCapacity) data.resize(managedCapacity); + std::copy(newData.begin(), newData.end(), data.begin()); + currentSize = newData.size(); + hostBufferValid = true; + deviceBufferValid = false; + invalidateIndexedViews(); + requestRedraw(); +} + +template +std::vector ManagedBuffer::getDataCopy() { + ensureHostBufferPopulated(); + // Return only the logically-valid elements; data may have capacity slack past currentSize. + return std::vector(data.begin(), data.begin() + currentSize); } template const T* ManagedBuffer::begin() const { #ifndef NDEBUG - if (!hostBufferIsPopulated) - exception("ManagedBuffer " + name + " begin() called without ensureHostBufferPopulated()"); + if (!hostBufferValid) exception("ManagedBuffer " + name + " begin() called without ensureHostBufferPopulated()"); #endif return data.data(); } @@ -267,39 +344,15 @@ const T* ManagedBuffer::begin() const { template const T* ManagedBuffer::end() const { #ifndef NDEBUG - if (!hostBufferIsPopulated) - exception("ManagedBuffer " + name + " end() called without ensureHostBufferPopulated()"); + if (!hostBufferValid) exception("ManagedBuffer " + name + " end() called without ensureHostBufferPopulated()"); #endif - return data.data() + data.size(); -} - -template -void ManagedBuffer::setDataHost(const std::vector& newData) { - if (newData.size() > managedCapacity) - exception("ManagedBuffer " + name + " setDataHost() called with data that exceeds capacity (" + - std::to_string(newData.size()) + " > " + std::to_string(managedCapacity) + "). Call resize() or setCapacity() first."); - data.assign(newData.begin(), newData.end()); - hostBufferIsPopulated = true; - - // Sync to any already-allocated device buffers and update indexed views. - if (renderAttributeBuffer) { - renderAttributeBuffer->setData(data); - requestRedraw(); - } - if (renderTextureBuffer) { - renderTextureBuffer->setData(data); - requestRedraw(); - } - if (deviceBufferType == DeviceBufferType::Attribute) { - updateIndexedViews(); - requestRedraw(); - } + return data.data() + currentSize; } template T ManagedBuffer::getHostValue(size_t ind) const { #ifndef NDEBUG - if (!hostBufferIsPopulated) + if (!hostBufferValid) exception("ManagedBuffer " + name + " getHostValue() called without ensureHostBufferPopulated()"); #endif return data[ind]; @@ -320,25 +373,13 @@ T ManagedBuffer::getHostValue(size_t indX, size_t indY, size_t indZ) const { template void ManagedBuffer::setHostValue(size_t ind, T val) { #ifndef NDEBUG - if (!hostBufferIsPopulated) + if (!hostBufferValid) exception("ManagedBuffer " + name + " setHostValue() called without ensureHostBufferPopulated()"); #endif data[ind] = val; - - // Sync to any already-allocated device buffers. Note: this re-uploads the entire buffer; for - // bulk writes, prefer setDataHost() to avoid repeated GPU uploads. - if (renderAttributeBuffer) { - renderAttributeBuffer->setData(data); - requestRedraw(); - } - if (renderTextureBuffer) { - renderTextureBuffer->setData(data); - requestRedraw(); - } - if (deviceBufferType == DeviceBufferType::Attribute) { - updateIndexedViews(); - requestRedraw(); - } + deviceBufferValid = false; + invalidateIndexedViews(); + requestRedraw(); } template @@ -355,19 +396,47 @@ void ManagedBuffer::setHostValue(size_t indX, size_t indY, size_t indZ, T val template void ManagedBuffer::markHostBufferUpdated() { - hostBufferIsPopulated = true; + hostBufferValid = true; + deviceBufferValid = false; + invalidateIndexedViews(); + requestRedraw(); +} - if (renderAttributeBuffer) { - renderAttributeBuffer->setData(data); - requestRedraw(); +template +void ManagedBuffer::syncToDeviceIfNeeded() { + // Quick exit: device data is current and all indexed views are up to date. + if (deviceBufferValid && indexedViewsValid) return; + + if (!deviceBufferValid && !hostBufferValid) { + // Neither host nor device has valid data at draw time. This is a bug: either the buffer was + // never populated, or getRenderAttributeBuffer() was not called before draw (which would have + // triggered the compute function and set deviceBufferValid). + exception("ManagedBuffer " + name + " has no valid data on host or device at draw time"); } - if (renderTextureBuffer) { - renderTextureBuffer->setData(data); - requestRedraw(); + + // Host data is valid. Upload to device if not already current. + if (!deviceBufferValid) { + if (renderAttributeBuffer) { + renderAttributeBuffer->setData(data); + } + if (renderTextureBuffer) { + renderTextureBuffer->setData(data); + } + deviceBufferValid = true; } - if (deviceBufferType == DeviceBufferType::Attribute) { - updateIndexedViews(); - requestRedraw(); + + + // Update indexed views + if (!indexedViewsValid) { + + if(deviceBufferValid) { // always true now at this point + + // TODO do an on-device update of indexed views + + // On-device update not implemented yet, copy it to host and do the update from the host + ensureHostBufferPopulated(); + updateIndexedViews(); // sets indexedViewsValid = true + } } } @@ -379,20 +448,20 @@ T ManagedBuffer::getValue(size_t ind) { ensureHostBufferPopulated(); } - switch (currentCanonicalDataSource()) { - case CanonicalDataSource::HostData: - if (ind >= data.size()) + if (hostBufferValid) { + if (ind >= currentSize) exception("out of bounds access in ManagedBuffer " + name + " getValue(" + std::to_string(ind) + ")"); return data[ind]; + } - case CanonicalDataSource::NeedsCompute: + if (isInNeedsComputeState()) { computeFunc(); - if (ind >= data.size()) + if (ind >= currentSize) exception("out of bounds access in ManagedBuffer " + name + " getValue(" + std::to_string(ind) + ")"); return data[ind]; + } - case CanonicalDataSource::RenderBuffer: - + if (!hostBufferValid && deviceBufferValid) { // NOTE: right now this case should never happen unless deviceBufferType == DeviceBufferType::Attribute. // In the texture case, we cannot get a single pixel from the backend anyway, so we always // call ensureHostBufferPopulated() above and do the host access. @@ -401,7 +470,7 @@ T ManagedBuffer::getValue(size_t ind) { exception("out of bounds access in ManagedBuffer " + name + " getValue(" + std::to_string(ind) + ")"); return getAttributeBufferData(*renderAttributeBuffer, ind); - }; + } return T(); // dummy return } @@ -424,40 +493,14 @@ T ManagedBuffer::getValue(size_t indX, size_t indY, size_t indZ) { template size_t ManagedBuffer::size() { - - switch (currentCanonicalDataSource()) { - case CanonicalDataSource::HostData: - return data.size(); - break; - - case CanonicalDataSource::NeedsCompute: - return 0; - break; - - case CanonicalDataSource::RenderBuffer: - if (deviceBufferType == DeviceBufferType::Attribute) { - return renderAttributeBuffer->getDataSize(); - } else { - size_t s = 1; - if (sizeX > 0) s *= sizeX; - if (sizeY > 0) s *= sizeY; - if (sizeZ > 0) s *= sizeZ; - return s; - } - break; - }; - - return INVALID_IND; + return currentSize; } template bool ManagedBuffer::hasData() { - if (hostBufferIsPopulated) return true; - if (deviceBufferType == DeviceBufferType::Attribute && renderAttributeBuffer) return true; - if (deviceBufferType == DeviceBufferType::Texture1d && renderTextureBuffer) return true; - if (deviceBufferType == DeviceBufferType::Texture2d && renderTextureBuffer) return true; - if (deviceBufferType == DeviceBufferType::Texture3d && renderTextureBuffer) return true; + if (hostBufferValid) return true; + if (deviceBufferValid) return true; return false; } @@ -472,20 +515,12 @@ std::string ManagedBuffer::summaryString() { std::string str = ""; str += "[" + name + "]"; - str += " status: "; - switch (currentCanonicalDataSource()) { - case CanonicalDataSource::HostData: - str += "HostData"; - break; - case CanonicalDataSource::NeedsCompute: - str += "NeedsCompute"; - break; - case CanonicalDataSource::RenderBuffer: - str += "Renderbuffer"; - break; - }; - str += " size: " + std::to_string(size()); - str += " device type: "; + str += " hostValid: " + std::string(hostBufferValid ? "T" : "F"); + str += " deviceValid: " + std::string(deviceBufferValid ? "T" : "F"); + str += " indexedViewsValid: " + std::string(indexedViewsValid ? "T" : "F"); + str += " hasComputeFunc: " + std::string(dataGetsComputed ? "T" : "F"); + str += " size: " + std::to_string(currentSize) + " / " + std::to_string(managedCapacity); + str += " device type: "; switch (deviceBufferType) { case DeviceBufferType::Attribute: str += "Attribute"; @@ -511,8 +546,8 @@ void ManagedBuffer::recomputeIfPopulated() { exception("called recomputeIfPopulated() on buffer which does not get computed"); } - // if not populated, quick out - if (currentCanonicalDataSource() == CanonicalDataSource::NeedsCompute) { + // if not populated (NeedsCompute state), quick out + if (!hostBufferValid && !deviceBufferValid) { return; } @@ -526,13 +561,11 @@ std::shared_ptr ManagedBuffer::getRenderAttributeBuf checkDeviceBufferTypeIs(DeviceBufferType::Attribute); if (!renderAttributeBuffer) { - ensureHostBufferPopulated(); // warning: the order of these matters because of how hostBufferPopulated works - // Sync managedCapacity on first GPU allocation: handles the case where data was populated - // externally before this call (e.g. class member ordering caused empty-vector-at-construction). - if (data.size() > managedCapacity) managedCapacity = data.size(); + ensureHostBufferPopulated(); renderAttributeBuffer = generateAttributeBuffer(render::engine); renderAttributeBuffer->reserveCapacity(managedCapacity); renderAttributeBuffer->setData(data); + deviceBufferValid = true; } return renderAttributeBuffer; } @@ -542,10 +575,7 @@ std::shared_ptr ManagedBuffer::getRenderTextureBuffer( checkDeviceBufferTypeIsTexture(); if (!renderTextureBuffer) { - ensureHostBufferPopulated(); // warning: the order of these matters because of how hostBufferPopulated works - // Sync managedCapacity on first GPU allocation (same rationale as getRenderAttributeBuffer) - if (data.size() > managedCapacity) managedCapacity = data.size(); - + ensureHostBufferPopulated(); renderTextureBuffer = generateTextureBuffer(deviceBufferType, render::engine); // templatize this? @@ -567,6 +597,7 @@ std::shared_ptr ManagedBuffer::getRenderTextureBuffer( } renderTextureBuffer->setData(data); + deviceBufferValid = true; } return renderTextureBuffer; } @@ -575,8 +606,9 @@ template void ManagedBuffer::markRenderAttributeBufferUpdated() { checkDeviceBufferTypeIs(DeviceBufferType::Attribute); - invalidateHostBuffer(); - updateIndexedViews(); + invalidateHostBuffer(); // also clears data + deviceBufferValid = true; + invalidateIndexedViews(); requestRedraw(); } @@ -584,7 +616,9 @@ template void ManagedBuffer::markRenderTextureBufferUpdated() { checkDeviceBufferTypeIsTexture(); - invalidateHostBuffer(); + invalidateHostBuffer(); // also clears data + deviceBufferValid = true; + invalidateIndexedViews(); requestRedraw(); } @@ -652,10 +686,15 @@ void ManagedBuffer::updateIndexedViews() { std::vector expandData = gather(data, indices.data); viewBuffer.setData(expandData); + // NOTE: this updates ALL registered indexed views of this buffer, even if only one of them is + // actually used in the current draw call. This is overly eager but correct; a lazier design + // would require indexed views to be first-class ManagedBuffers with their own NeedsCompute state. + // TODO: add direct GPU-side indexed copy support (without round-tripping through the host) using a // transform-feedback/compute shader program, to avoid the CPU gather cost for GPU-resident buffers. } + indexedViewsValid = true; requestRedraw(); } @@ -675,42 +714,25 @@ void ManagedBuffer::removeDeletedIndexedViews() { template void ManagedBuffer::invalidateHostBuffer() { - hostBufferIsPopulated = false; + ManagedBufferBase::invalidateHostBuffer(); data.clear(); } template -bool ManagedBuffer::deviceBufferTypeIsTexture() const { - return ((deviceBufferType == DeviceBufferType::Texture1d) || (deviceBufferType == DeviceBufferType::Texture2d) || - (deviceBufferType == DeviceBufferType::Texture3d)); -} - -template -void ManagedBuffer::checkDeviceBufferTypeIs(DeviceBufferType targetType) const { - if (targetType != deviceBufferType) { - exception("ManagedBuffer " + name + " has wrong type for this operation. Expected " + - deviceBufferTypeName(targetType) + " but is " + deviceBufferTypeName(deviceBufferType)); - } -} - -template -void ManagedBuffer::checkDeviceBufferTypeIsTexture() const { - if (!deviceBufferTypeIsTexture()) { - exception("ManagedBuffer " + name + " has wrong type for this operation. Expected a Texture1d/2d/3d but is " + - deviceBufferTypeName(deviceBufferType)); - } +void ManagedBuffer::invalidateIndexedViews() { + if (existingIndexedViews.size() > 0) indexedViewsValid = false; } template typename ManagedBuffer::CanonicalDataSource ManagedBuffer::currentCanonicalDataSource() { // Always prefer the host data if it is up to date - if (hostBufferIsPopulated) { + if (hostBufferValid) { return CanonicalDataSource::HostData; } // Check if the render buffer contains the canonical data - if (renderAttributeBuffer || renderTextureBuffer) { + if (deviceBufferValid) { return CanonicalDataSource::RenderBuffer; } diff --git a/src/render/mock_opengl/mock_gl_engine.cpp b/src/render/mock_opengl/mock_gl_engine.cpp index a27f0ff8..b4ce8ce9 100644 --- a/src/render/mock_opengl/mock_gl_engine.cpp +++ b/src/render/mock_opengl/mock_gl_engine.cpp @@ -2,6 +2,7 @@ #ifdef POLYSCOPE_BACKEND_OPENGL_MOCK_ENABLED #include "polyscope/render/mock_opengl/mock_gl_engine.h" +#include "polyscope/render/managed_buffer.h" #include "polyscope/imgui_config.h" #include "polyscope/messages.h" @@ -779,7 +780,7 @@ void GLCompiledProgram::addUniqueAttribute(ShaderSpecAttribute newAttribute) { return; } } - attributes.push_back(GLShaderAttribute{newAttribute.name, newAttribute.type, newAttribute.arrayCount, nullptr}); + attributes.push_back(GLShaderAttribute{newAttribute.name, newAttribute.type, newAttribute.arrayCount, nullptr, nullptr}); } void GLCompiledProgram::addUniqueUniform(ShaderSpecUniform newUniform) { @@ -806,7 +807,7 @@ void GLCompiledProgram::addUniqueTexture(ShaderSpecTexture newTexture) { return; } } - textures.push_back(GLShaderTexture{newTexture.name, newTexture.dim, 777, false, nullptr, nullptr}); + textures.push_back(GLShaderTexture{newTexture.name, newTexture.dim, 777, false, nullptr, nullptr, nullptr}); } @@ -838,7 +839,7 @@ void GLShaderProgram::createBuffers() { } } -void GLShaderProgram::setAttribute(std::string name, std::shared_ptr externalBuffer) { +void GLShaderProgram::setAttribute(std::string name, std::shared_ptr externalBuffer, ManagedBufferBase* source) { bindVAO(); checkGLError(); @@ -860,6 +861,7 @@ void GLShaderProgram::setAttribute(std::string name, std::shared_ptrbind(); @@ -1459,7 +1461,7 @@ void GLShaderProgram::setTexture2D(std::string name, unsigned char* texData, uns throw std::invalid_argument("No texture with name " + name); } -void GLShaderProgram::setTextureFromBuffer(std::string name, TextureBuffer* textureBuffer) { +void GLShaderProgram::setTextureFromBuffer(std::string name, TextureBuffer* textureBuffer, ManagedBufferBase* source) { // Find the right texture for (GLShaderTexture& t : textures) { @@ -1474,6 +1476,7 @@ void GLShaderProgram::setTextureFromBuffer(std::string name, TextureBuffer* text throw std::invalid_argument("Bad texture in setTextureFromBuffer()"); } + t.sourceManagedBuffer = source; t.isSet = true; return; } @@ -1659,8 +1662,18 @@ void GLShaderProgram::activateTextures() { } } +void GLShaderProgram::syncBuffersToDeviceIfNeeded() { + for (auto& attr : attributes) { + if (attr.sourceManagedBuffer) attr.sourceManagedBuffer->syncToDeviceIfNeeded(); + } + for (auto& tex : textures) { + if (tex.sourceManagedBuffer) tex.sourceManagedBuffer->syncToDeviceIfNeeded(); + } +} + void GLShaderProgram::draw() { validateData(); + syncBuffersToDeviceIfNeeded(); if (usePrimitiveRestart) { } diff --git a/src/render/opengl/gl_engine.cpp b/src/render/opengl/gl_engine.cpp index fe4d80dd..53fb729a 100644 --- a/src/render/opengl/gl_engine.cpp +++ b/src/render/opengl/gl_engine.cpp @@ -4,6 +4,7 @@ #ifdef POLYSCOPE_BACKEND_OPENGL3_ENABLED #include "polyscope/render/opengl/gl_engine.h" +#include "polyscope/render/managed_buffer.h" #include "polyscope/messages.h" #include "polyscope/options.h" @@ -1217,7 +1218,7 @@ void GLCompiledProgram::addUniqueAttribute(ShaderSpecAttribute newAttribute) { return; } } - attributes.push_back(GLShaderAttribute{newAttribute.name, newAttribute.type, newAttribute.arrayCount, -1, nullptr}); + attributes.push_back(GLShaderAttribute{newAttribute.name, newAttribute.type, newAttribute.arrayCount, -1, nullptr, nullptr}); } void GLCompiledProgram::addUniqueUniform(ShaderSpecUniform newUniform) { @@ -1244,7 +1245,7 @@ void GLCompiledProgram::addUniqueTexture(ShaderSpecTexture newTexture) { return; } } - textures.push_back(GLShaderTexture{newTexture.name, newTexture.dim, 777, false, nullptr, nullptr, 777}); + textures.push_back(GLShaderTexture{newTexture.name, newTexture.dim, 777, false, nullptr, nullptr, 777, nullptr}); } @@ -1288,7 +1289,7 @@ void GLShaderProgram::createBuffers() { checkGLError(); } -void GLShaderProgram::setAttribute(std::string name, std::shared_ptr externalBuffer) { +void GLShaderProgram::setAttribute(std::string name, std::shared_ptr externalBuffer, ManagedBufferBase* source) { bindVAO(); checkGLError(); @@ -1312,6 +1313,7 @@ void GLShaderProgram::setAttribute(std::string name, std::shared_ptrbind(); @@ -2008,7 +2010,7 @@ void GLShaderProgram::setTexture2D(std::string name, unsigned char* texData, uns throw std::invalid_argument("No texture with name " + name); } -void GLShaderProgram::setTextureFromBuffer(std::string name, TextureBuffer* textureBuffer) { +void GLShaderProgram::setTextureFromBuffer(std::string name, TextureBuffer* textureBuffer, ManagedBufferBase* source) { glUseProgram(compiledProgram->getHandle()); // Find the right texture @@ -2024,6 +2026,7 @@ void GLShaderProgram::setTextureFromBuffer(std::string name, TextureBuffer* text throw std::invalid_argument("Bad texture in setTextureFromBuffer()"); } + t.sourceManagedBuffer = source; t.isSet = true; return; } @@ -2211,8 +2214,18 @@ void GLShaderProgram::activateTextures() { } } +void GLShaderProgram::syncBuffersToDeviceIfNeeded() { + for (auto& attr : attributes) { + if (attr.sourceManagedBuffer) attr.sourceManagedBuffer->syncToDeviceIfNeeded(); + } + for (auto& tex : textures) { + if (tex.sourceManagedBuffer) tex.sourceManagedBuffer->syncToDeviceIfNeeded(); + } +} + void GLShaderProgram::draw() { validateData(); + syncBuffersToDeviceIfNeeded(); glUseProgram(compiledProgram->getHandle()); glBindVertexArray(vaoHandle); diff --git a/src/render_image_quantity_base.cpp b/src/render_image_quantity_base.cpp index 6793142c..0420fa3d 100644 --- a/src/render_image_quantity_base.cpp +++ b/src/render_image_quantity_base.cpp @@ -126,7 +126,7 @@ void RenderImageQuantityBase::preparePick() { // clang-format on pickProgram->setAttribute("a_position", render::engine->screenTrianglesCoords()); - pickProgram->setTextureFromBuffer("t_depth", depths.getRenderTextureBuffer().get()); + pickProgram->setTextureFromBuffer("t_depth", depths); } void RenderImageQuantityBase::refresh() { diff --git a/src/scalar_image_quantity.cpp b/src/scalar_image_quantity.cpp index e7f9abf0..4ad7d558 100644 --- a/src/scalar_image_quantity.cpp +++ b/src/scalar_image_quantity.cpp @@ -52,7 +52,7 @@ void ScalarImageQuantity::prepareFullscreen() { this->addScalarRules({getImageOriginRule(imageOrigin), "TEXTURE_SET_TRANSPARENCY", "TEXTURE_PREMULTIPLY_OUT"}), render::ShaderReplacementDefaults::Process); fullscreenProgram->setAttribute("a_position", render::engine->screenTrianglesCoords()); - fullscreenProgram->setTextureFromBuffer("t_scalar", values.getRenderTextureBuffer().get()); + fullscreenProgram->setTextureFromBuffer("t_scalar", values); fullscreenProgram->setTextureFromColormap("t_colormap", this->cMap.get()); } @@ -65,7 +65,7 @@ void ScalarImageQuantity::prepareBillboard() { "TEXTURE_BILLBOARD_FROM_UNIFORMS"}), render::ShaderReplacementDefaults::Process); billboardProgram->setAttribute("a_position", render::engine->screenTrianglesCoords()); - billboardProgram->setTextureFromBuffer("t_scalar", values.getRenderTextureBuffer().get()); + billboardProgram->setTextureFromBuffer("t_scalar", values); billboardProgram->setTextureFromColormap("t_colormap", this->cMap.get()); } diff --git a/src/scalar_render_image_quantity.cpp b/src/scalar_render_image_quantity.cpp index 19cc195e..1008cfa3 100644 --- a/src/scalar_render_image_quantity.cpp +++ b/src/scalar_render_image_quantity.cpp @@ -87,11 +87,11 @@ void ScalarRenderImageQuantity::prepare() { // clang-format on program->setAttribute("a_position", render::engine->screenTrianglesCoords()); - program->setTextureFromBuffer("t_depth", depths.getRenderTextureBuffer().get()); + program->setTextureFromBuffer("t_depth", depths); if (hasNormals) { - program->setTextureFromBuffer("t_normal", normals.getRenderTextureBuffer().get()); + program->setTextureFromBuffer("t_normal", normals); } - program->setTextureFromBuffer("t_scalar", values.getRenderTextureBuffer().get()); + program->setTextureFromBuffer("t_scalar", values); render::engine->setMaterial(*program, material.get()); program->setTextureFromColormap("t_colormap", cMap.get()); } diff --git a/src/simple_triangle_mesh.cpp b/src/simple_triangle_mesh.cpp index 696b55f9..6151c395 100644 --- a/src/simple_triangle_mesh.cpp +++ b/src/simple_triangle_mesh.cpp @@ -253,7 +253,7 @@ std::vector SimpleTriangleMesh::addSimpleTriangleMeshRules(std::vec } void SimpleTriangleMesh::setSimpleTriangleMeshProgramGeometryAttributes(render::ShaderProgram& p) { - p.setAttribute("a_vertexPositions", vertices.getRenderAttributeBuffer()); + p.setAttribute("a_vertexPositions", vertices); p.setIndex(faces.getRenderAttributeBuffer()); } diff --git a/src/slice_plane.cpp b/src/slice_plane.cpp index 9b988f93..bc56d2d8 100644 --- a/src/slice_plane.cpp +++ b/src/slice_plane.cpp @@ -250,10 +250,10 @@ void SlicePlane::setSliceAttributes(render::ShaderProgram& p) { sliceBufferArr[i].markHostBufferUpdated(); } - p.setAttribute("a_slice_1", meshToInspect->vertexPositions.getIndexedRenderAttributeBuffer(sliceBufferArr[0])); - p.setAttribute("a_slice_2", meshToInspect->vertexPositions.getIndexedRenderAttributeBuffer(sliceBufferArr[1])); - p.setAttribute("a_slice_3", meshToInspect->vertexPositions.getIndexedRenderAttributeBuffer(sliceBufferArr[2])); - p.setAttribute("a_slice_4", meshToInspect->vertexPositions.getIndexedRenderAttributeBuffer(sliceBufferArr[3])); + p.setAttribute("a_slice_1", meshToInspect->vertexPositions.getIndexedRenderAttributeBuffer(sliceBufferArr[0]), &meshToInspect->vertexPositions); + p.setAttribute("a_slice_2", meshToInspect->vertexPositions.getIndexedRenderAttributeBuffer(sliceBufferArr[1]), &meshToInspect->vertexPositions); + p.setAttribute("a_slice_3", meshToInspect->vertexPositions.getIndexedRenderAttributeBuffer(sliceBufferArr[2]), &meshToInspect->vertexPositions); + p.setAttribute("a_slice_4", meshToInspect->vertexPositions.getIndexedRenderAttributeBuffer(sliceBufferArr[3]), &meshToInspect->vertexPositions); } void SlicePlane::drawGeometry() { diff --git a/src/sparse_volume_grid.cpp b/src/sparse_volume_grid.cpp index 1be789e1..51282de3 100644 --- a/src/sparse_volume_grid.cpp +++ b/src/sparse_volume_grid.cpp @@ -36,14 +36,14 @@ SparseVolumeGrid::SparseVolumeGrid(std::string name, glm::vec3 origin_, glm::vec cellPositions(this, uniquePrefix() + "#cellPositions", std::bind(&SparseVolumeGrid::computeCellPositions, this)), cellIndices(this, uniquePrefix() + "#cellIndices", [](){/* do nothing, gets handled by computeCellPositions */}), cornerNodeInds{ - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds0", std::vector{}), - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds1", std::vector{}), - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds2", std::vector{}), - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds3", std::vector{}), - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds4", std::vector{}), - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds5", std::vector{}), - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds6", std::vector{}), - render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds7", std::vector{}), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds0"), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds1"), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds2"), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds3"), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds4"), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds5"), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds6"), + render::ManagedBuffer(this, uniquePrefix() + "#cornerNodeInds7"), }, origin(origin_), gridCellWidth(gridCellWidth_), @@ -367,8 +367,8 @@ void SparseVolumeGrid::ensureRenderProgramPrepared() { ); // clang-format on - program->setAttribute("a_cellPosition", cellPositions.getRenderAttributeBuffer()); - program->setAttribute("a_cellInd", cellIndices.getRenderAttributeBuffer()); + program->setAttribute("a_cellPosition", cellPositions); + program->setAttribute("a_cellInd", cellIndices); render::engine->setMaterial(*program, material.get()); } @@ -499,8 +499,8 @@ void SparseVolumeGrid::ensurePickProgramPrepared() { pickProgram->setAttribute("a_color", pickColors); - pickProgram->setAttribute("a_cellPosition", cellPositions.getRenderAttributeBuffer()); - pickProgram->setAttribute("a_cellInd", cellIndices.getRenderAttributeBuffer()); + pickProgram->setAttribute("a_cellPosition", cellPositions); + pickProgram->setAttribute("a_cellInd", cellIndices); } @@ -792,8 +792,8 @@ glm::vec3 SparseVolumeGrid::getWireframeColor() { return wireframeColor.get(); } void SparseVolumeGrid::setCellGeometryAttributes(render::ShaderProgram& p) { - p.setAttribute("a_cellPosition", cellPositions.getRenderAttributeBuffer()); - p.setAttribute("a_cellInd", cellIndices.getRenderAttributeBuffer()); + p.setAttribute("a_cellPosition", cellPositions); + p.setAttribute("a_cellInd", cellIndices); } std::vector SparseVolumeGrid::addSparseGridShaderRules(std::vector initRules, bool pickOnly) { diff --git a/src/sparse_volume_grid_color_quantity.cpp b/src/sparse_volume_grid_color_quantity.cpp index fcd493f6..247a7d65 100644 --- a/src/sparse_volume_grid_color_quantity.cpp +++ b/src/sparse_volume_grid_color_quantity.cpp @@ -61,7 +61,7 @@ void SparseVolumeGridCellColorQuantity::createProgram() { // clang-format on parent.setCellGeometryAttributes(*program); - program->setAttribute("a_color", colors.getRenderAttributeBuffer()); + program->setAttribute("a_color", colors); render::engine->setMaterial(*program, parent.getMaterial()); } @@ -108,7 +108,7 @@ void SparseVolumeGridNodeColorQuantity::createProgram() { parent.setCellGeometryAttributes(*program); for (int c = 0; c < 8; c++) { program->setAttribute("a_nodeColor" + std::to_string(c), - colors.getIndexedRenderAttributeBuffer(parent.cornerNodeInds[c])); + colors.getIndexedRenderAttributeBuffer(parent.cornerNodeInds[c]), &colors); } render::engine->setMaterial(*program, parent.getMaterial()); } diff --git a/src/sparse_volume_grid_scalar_quantity.cpp b/src/sparse_volume_grid_scalar_quantity.cpp index 125061c7..088b65a2 100644 --- a/src/sparse_volume_grid_scalar_quantity.cpp +++ b/src/sparse_volume_grid_scalar_quantity.cpp @@ -76,7 +76,7 @@ void SparseVolumeGridCellScalarQuantity::createProgram() { // clang-format on parent.setCellGeometryAttributes(*program); - program->setAttribute("a_value", values.getRenderAttributeBuffer()); + program->setAttribute("a_value", values); program->setTextureFromColormap("t_colormap", cMap.get()); render::engine->setMaterial(*program, parent.getMaterial()); } @@ -121,7 +121,7 @@ void SparseVolumeGridNodeScalarQuantity::createProgram() { parent.setCellGeometryAttributes(*program); for (int c = 0; c < 8; c++) { program->setAttribute("a_nodeValue" + std::to_string(c), - values.getIndexedRenderAttributeBuffer(parent.cornerNodeInds[c])); + values.getIndexedRenderAttributeBuffer(parent.cornerNodeInds[c]), &values); } program->setTextureFromColormap("t_colormap", cMap.get()); render::engine->setMaterial(*program, parent.getMaterial()); diff --git a/src/surface_color_quantity.cpp b/src/surface_color_quantity.cpp index 4a6f1ef3..7955542d 100644 --- a/src/surface_color_quantity.cpp +++ b/src/surface_color_quantity.cpp @@ -70,7 +70,7 @@ void SurfaceVertexColorQuantity::createProgram() { // clang-format on parent.setMeshGeometryAttributes(*program); - program->setAttribute("a_color", colors.getIndexedRenderAttributeBuffer(parent.triangleVertexInds)); + program->setAttribute("a_color", colors.getIndexedRenderAttributeBuffer(parent.triangleVertexInds), &colors); render::engine->setMaterial(*program, parent.getMaterial()); } @@ -123,7 +123,7 @@ void SurfaceFaceColorQuantity::createProgram() { // clang-format on parent.setMeshGeometryAttributes(*program); - program->setAttribute("a_color", colors.getIndexedRenderAttributeBuffer(parent.triangleFaceInds)); + program->setAttribute("a_color", colors.getIndexedRenderAttributeBuffer(parent.triangleFaceInds), &colors); render::engine->setMaterial(*program, parent.getMaterial()); } @@ -177,17 +177,17 @@ void SurfaceTextureColorQuantity::createProgram() { // the indexing into the parameterization varies based on whether it is a corner or vertex quantity switch (param.definedOn) { case MeshElement::VERTEX: - program->setAttribute("a_tCoord", param.coords.getIndexedRenderAttributeBuffer(parent.triangleVertexInds)); + program->setAttribute("a_tCoord", param.coords.getIndexedRenderAttributeBuffer(parent.triangleVertexInds), ¶m.coords); break; case MeshElement::CORNER: - program->setAttribute("a_tCoord", param.coords.getIndexedRenderAttributeBuffer(parent.triangleCornerInds)); + program->setAttribute("a_tCoord", param.coords.getIndexedRenderAttributeBuffer(parent.triangleCornerInds), ¶m.coords); break; default: // nothing break; } - program->setTextureFromBuffer("t_color", colors.getRenderTextureBuffer().get()); + program->setTextureFromBuffer("t_color", colors); render::engine->setMaterial(*program, parent.getMaterial()); colors.getRenderTextureBuffer()->setFilterMode(filterMode.get()); diff --git a/src/surface_mesh.cpp b/src/surface_mesh.cpp index 21e3cdc6..4a64cbf1 100644 --- a/src/surface_mesh.cpp +++ b/src/surface_mesh.cpp @@ -28,12 +28,12 @@ SurfaceMesh::SurfaceMesh(std::string name_) // == managed quantities // positions -vertexPositions( this, uniquePrefix() + "vertexPositions", std::vector{}), +vertexPositions( this, uniquePrefix() + "vertexPositions"), // connectivity / indices // (triangle and face inds are always computed initially when we triangulate the mesh) -triangleVertexInds( this, uniquePrefix() + "triangleVertexInds", std::vector{}), -triangleFaceInds( this, uniquePrefix() + "triangleFaceInds", std::vector{}), +triangleVertexInds( this, uniquePrefix() + "triangleVertexInds"), +triangleFaceInds( this, uniquePrefix() + "triangleFaceInds"), triangleCornerInds( this, uniquePrefix() + "triangleCornerInds", std::bind(&SurfaceMesh::computeTriangleCornerInds, this)), triangleAllVertexInds( this, uniquePrefix() + "triangleAllVertexInds", std::bind(&SurfaceMesh::computeTriangleAllVertexInds, this)), triangleAllEdgeInds( this, uniquePrefix() + "triangleAllEdgeInds", std::bind(&SurfaceMesh::computeTriangleAllEdgeInds, this)), @@ -41,8 +41,8 @@ triangleAllHalfedgeInds( this, uniquePrefix() + "triangleHalfedgeInds", triangleAllCornerInds( this, uniquePrefix() + "triangleAllCornerInds", std::bind(&SurfaceMesh::computeTriangleAllCornerInds, this)), // internal triangle data for rendering -baryCoord( this, uniquePrefix() + "baryCoord", std::vector{}), -edgeIsReal( this, uniquePrefix() + "edgeIsReal", std::vector{}), +baryCoord( this, uniquePrefix() + "baryCoord"), +edgeIsReal( this, uniquePrefix() + "edgeIsReal"), // other internally-computed geometry faceNormals( this, uniquePrefix() + "faceNormals", std::bind(&SurfaceMesh::computeFaceNormals, this)), @@ -872,29 +872,29 @@ void SurfaceMesh::preparePick() { void SurfaceMesh::setMeshGeometryAttributes(render::ShaderProgram& p) { if (p.hasAttribute("a_vertexPositions")) { - p.setAttribute("a_vertexPositions", vertexPositions.getIndexedRenderAttributeBuffer(triangleVertexInds)); + p.setAttribute("a_vertexPositions", vertexPositions.getIndexedRenderAttributeBuffer(triangleVertexInds), &vertexPositions); } if (p.hasAttribute("a_vertexNormals")) { if (getShadeStyle() == MeshShadeStyle::Smooth) { - p.setAttribute("a_vertexNormals", vertexNormals.getIndexedRenderAttributeBuffer(triangleVertexInds)); + p.setAttribute("a_vertexNormals", vertexNormals.getIndexedRenderAttributeBuffer(triangleVertexInds), &vertexNormals); } else { // these aren't actually used in in the automatically-generated case, but the shader is set up in a lazy way so // it is still needed - p.setAttribute("a_vertexNormals", faceNormals.getIndexedRenderAttributeBuffer(triangleFaceInds)); + p.setAttribute("a_vertexNormals", faceNormals.getIndexedRenderAttributeBuffer(triangleFaceInds), &faceNormals); } } if (p.hasAttribute("a_normal")) { - p.setAttribute("a_normal", faceNormals.getIndexedRenderAttributeBuffer(triangleFaceInds)); + p.setAttribute("a_normal", faceNormals.getIndexedRenderAttributeBuffer(triangleFaceInds), &faceNormals); } if (p.hasAttribute("a_barycoord")) { - p.setAttribute("a_barycoord", baryCoord.getRenderAttributeBuffer()); + p.setAttribute("a_barycoord", baryCoord); } if (p.hasAttribute("a_edgeIsReal")) { - p.setAttribute("a_edgeIsReal", edgeIsReal.getRenderAttributeBuffer()); + p.setAttribute("a_edgeIsReal", edgeIsReal); } if (wantsCullPosition()) { - p.setAttribute("a_cullPos", faceCenters.getIndexedRenderAttributeBuffer(triangleFaceInds)); + p.setAttribute("a_cullPos", faceCenters.getIndexedRenderAttributeBuffer(triangleFaceInds), &faceCenters); } if (transparencyQuantityName != "") { diff --git a/src/surface_parameterization_quantity.cpp b/src/surface_parameterization_quantity.cpp index 56342e0e..be773f8b 100644 --- a/src/surface_parameterization_quantity.cpp +++ b/src/surface_parameterization_quantity.cpp @@ -76,7 +76,7 @@ void SurfaceParameterizationQuantity::createProgram() { parent.setMeshGeometryAttributes(*program); if(getStyle() == ParamVizStyle::CHECKER_ISLANDS) { - program->setAttribute("a_value", islandLabels.getIndexedRenderAttributeBuffer(parent.triangleFaceInds)); + program->setAttribute("a_value", islandLabels.getIndexedRenderAttributeBuffer(parent.triangleFaceInds), &islandLabels); } render::engine->setMaterial(*program, parent.getMaterial()); @@ -228,7 +228,7 @@ std::string SurfaceCornerParameterizationQuantity::niceName() { return name + " void SurfaceCornerParameterizationQuantity::fillCoordBuffers(render::ShaderProgram& p) { - p.setAttribute("a_value2", coords.getIndexedRenderAttributeBuffer(parent.triangleCornerInds)); + p.setAttribute("a_value2", coords.getIndexedRenderAttributeBuffer(parent.triangleCornerInds), &coords); } @@ -260,7 +260,7 @@ SurfaceVertexParameterizationQuantity::SurfaceVertexParameterizationQuantity(std std::string SurfaceVertexParameterizationQuantity::niceName() { return name + " (vertex parameterization)"; } void SurfaceVertexParameterizationQuantity::fillCoordBuffers(render::ShaderProgram& p) { - p.setAttribute("a_value2", coords.getIndexedRenderAttributeBuffer(parent.triangleVertexInds)); + p.setAttribute("a_value2", coords.getIndexedRenderAttributeBuffer(parent.triangleVertexInds), &coords); } std::vector SurfaceVertexParameterizationQuantity::getCornerCoords() { diff --git a/src/surface_scalar_quantity.cpp b/src/surface_scalar_quantity.cpp index 894c5f57..f11b866d 100644 --- a/src/surface_scalar_quantity.cpp +++ b/src/surface_scalar_quantity.cpp @@ -85,7 +85,7 @@ void SurfaceVertexScalarQuantity::createProgram() { ); // clang-format on - program->setAttribute("a_value3", values.getIndexedRenderAttributeBuffer(parent.triangleAllVertexInds)); + program->setAttribute("a_value3", values.getIndexedRenderAttributeBuffer(parent.triangleAllVertexInds), &values); } else { // common case: linear interpolation within each triangle @@ -102,7 +102,7 @@ void SurfaceVertexScalarQuantity::createProgram() { ); // clang-format on - program->setAttribute("a_value", values.getIndexedRenderAttributeBuffer(parent.triangleVertexInds)); + program->setAttribute("a_value", values.getIndexedRenderAttributeBuffer(parent.triangleVertexInds), &values); } parent.setMeshGeometryAttributes(*program); @@ -146,7 +146,7 @@ void SurfaceFaceScalarQuantity::createProgram() { ); // clang-format on - program->setAttribute("a_value", values.getIndexedRenderAttributeBuffer(parent.triangleFaceInds)); + program->setAttribute("a_value", values.getIndexedRenderAttributeBuffer(parent.triangleFaceInds), &values); parent.setMeshGeometryAttributes(*program); render::engine->setMaterial(*program, parent.getMaterial()); program->setTextureFromColormap("t_colormap", cMap.get()); @@ -192,7 +192,7 @@ void SurfaceEdgeScalarQuantity::createProgram() { ); // clang-format on - program->setAttribute("a_value3", values.getIndexedRenderAttributeBuffer(parent.triangleAllEdgeInds)); + program->setAttribute("a_value3", values.getIndexedRenderAttributeBuffer(parent.triangleAllEdgeInds), &values); parent.setMeshGeometryAttributes(*program); render::engine->setMaterial(*program, parent.getMaterial()); program->setTextureFromColormap("t_colormap", cMap.get()); @@ -234,7 +234,7 @@ void SurfaceHalfedgeScalarQuantity::createProgram() { ); // clang-format on - program->setAttribute("a_value3", values.getIndexedRenderAttributeBuffer(parent.triangleAllHalfedgeInds)); + program->setAttribute("a_value3", values.getIndexedRenderAttributeBuffer(parent.triangleAllHalfedgeInds), &values); parent.setMeshGeometryAttributes(*program); render::engine->setMaterial(*program, parent.getMaterial()); program->setTextureFromColormap("t_colormap", cMap.get()); @@ -280,7 +280,7 @@ void SurfaceCornerScalarQuantity::createProgram() { ); // clang-format on - program->setAttribute("a_value3", values.getIndexedRenderAttributeBuffer(parent.triangleAllCornerInds)); + program->setAttribute("a_value3", values.getIndexedRenderAttributeBuffer(parent.triangleAllCornerInds), &values); } else { @@ -296,7 +296,7 @@ void SurfaceCornerScalarQuantity::createProgram() { ); // clang-format on - program->setAttribute("a_value", values.getIndexedRenderAttributeBuffer(parent.triangleCornerInds)); + program->setAttribute("a_value", values.getIndexedRenderAttributeBuffer(parent.triangleCornerInds), &values); } parent.setMeshGeometryAttributes(*program); @@ -353,17 +353,17 @@ void SurfaceTextureScalarQuantity::createProgram() { // the indexing into the parameterization varies based on whether it is a corner or vertex quantity switch (param.definedOn) { case MeshElement::VERTEX: - program->setAttribute("a_tCoord", param.coords.getIndexedRenderAttributeBuffer(parent.triangleVertexInds)); + program->setAttribute("a_tCoord", param.coords.getIndexedRenderAttributeBuffer(parent.triangleVertexInds), ¶m.coords); break; case MeshElement::CORNER: - program->setAttribute("a_tCoord", param.coords.getIndexedRenderAttributeBuffer(parent.triangleCornerInds)); + program->setAttribute("a_tCoord", param.coords.getIndexedRenderAttributeBuffer(parent.triangleCornerInds), ¶m.coords); break; default: // nothing break; } - program->setTextureFromBuffer("t_scalar", values.getRenderTextureBuffer().get()); + program->setTextureFromBuffer("t_scalar", values); render::engine->setMaterial(*program, parent.getMaterial()); program->setTextureFromColormap("t_colormap", cMap.get()); diff --git a/src/surface_vector_quantity.cpp b/src/surface_vector_quantity.cpp index bbffc054..7d31b51d 100644 --- a/src/surface_vector_quantity.cpp +++ b/src/surface_vector_quantity.cpp @@ -266,8 +266,8 @@ SurfaceOneFormTangentVectorQuantity::SurfaceOneFormTangentVectorQuantity(std::st : SurfaceVectorQuantity(name, mesh_, MeshElement::FACE), TangentVectorQuantity( *this, oneFormToFaceTangentVectors(mesh_, oneForm_, canonicalOrientation_), - std::vector(mesh_.defaultFaceTangentBasisX.begin(), mesh_.defaultFaceTangentBasisX.end()), - std::vector(mesh_.defaultFaceTangentBasisY.begin(), mesh_.defaultFaceTangentBasisY.end()), + mesh_.defaultFaceTangentBasisX.getDataCopy(), + mesh_.defaultFaceTangentBasisY.getDataCopy(), parent.faceCenters, 1, VectorType::STANDARD), oneForm(oneForm_), canonicalOrientation(canonicalOrientation_) {} diff --git a/src/volume_grid.cpp b/src/volume_grid.cpp index fded29a7..c6327611 100644 --- a/src/volume_grid.cpp +++ b/src/volume_grid.cpp @@ -242,9 +242,9 @@ void VolumeGrid::ensureGridCubeRenderProgramPrepared() { ); // clang-format on - program->setAttribute("a_referencePosition", gridPlaneReferencePositions.getRenderAttributeBuffer()); - program->setAttribute("a_referenceNormal", gridPlaneReferenceNormals.getRenderAttributeBuffer()); - program->setAttribute("a_axisInd", gridPlaneAxisInds.getRenderAttributeBuffer()); + program->setAttribute("a_referencePosition", gridPlaneReferencePositions); + program->setAttribute("a_referenceNormal", gridPlaneReferenceNormals); + program->setAttribute("a_axisInd", gridPlaneAxisInds); render::engine->setMaterial(*program, material.get()); } @@ -256,16 +256,16 @@ void VolumeGrid::ensureGridCubePickProgramPrepared() { // clang-format off pickProgram = render::engine->requestShader( - "GRIDCUBE_PLANE", - addGridCubeRules({"GRIDCUBE_CONSTANT_PICK"}, false), + "GRIDCUBE_PLANE", + addGridCubeRules({"GRIDCUBE_CONSTANT_PICK"}, false), render::ShaderReplacementDefaults::Pick ); // clang-format on - pickProgram->setAttribute("a_referencePosition", gridPlaneReferencePositions.getRenderAttributeBuffer()); - pickProgram->setAttribute("a_referenceNormal", gridPlaneReferenceNormals.getRenderAttributeBuffer()); - pickProgram->setAttribute("a_axisInd", gridPlaneAxisInds.getRenderAttributeBuffer()); + pickProgram->setAttribute("a_referencePosition", gridPlaneReferencePositions); + pickProgram->setAttribute("a_referenceNormal", gridPlaneReferenceNormals); + pickProgram->setAttribute("a_axisInd", gridPlaneAxisInds); if (globalPickConstant == INVALID_IND_64) { diff --git a/src/volume_grid_scalar_quantity.cpp b/src/volume_grid_scalar_quantity.cpp index 1010e3d4..33645e30 100644 --- a/src/volume_grid_scalar_quantity.cpp +++ b/src/volume_grid_scalar_quantity.cpp @@ -148,15 +148,15 @@ void VolumeGridNodeScalarQuantity::createGridcubeProgram() { ); // clang-format on - gridcubeProgram->setAttribute("a_referencePosition", parent.gridPlaneReferencePositions.getRenderAttributeBuffer()); - gridcubeProgram->setAttribute("a_referenceNormal", parent.gridPlaneReferenceNormals.getRenderAttributeBuffer()); - gridcubeProgram->setAttribute("a_axisInd", parent.gridPlaneAxisInds.getRenderAttributeBuffer()); + gridcubeProgram->setAttribute("a_referencePosition", parent.gridPlaneReferencePositions); + gridcubeProgram->setAttribute("a_referenceNormal", parent.gridPlaneReferenceNormals); + gridcubeProgram->setAttribute("a_axisInd", parent.gridPlaneAxisInds); gridcubeProgram->setTextureFromColormap("t_colormap", cMap.get()); render::engine->setMaterial(*gridcubeProgram, parent.getMaterial()); - gridcubeProgram->setTextureFromBuffer("t_value", values.getRenderTextureBuffer().get()); - values.getRenderTextureBuffer().get()->setFilterMode(FilterMode::Linear); + gridcubeProgram->setTextureFromBuffer("t_value", values); + values.getRenderTextureBuffer()->setFilterMode(FilterMode::Linear); } void VolumeGridNodeScalarQuantity::createIsosurfaceProgram() { @@ -360,15 +360,15 @@ void VolumeGridCellScalarQuantity::createGridcubeProgram() { ); // clang-format on - gridcubeProgram->setAttribute("a_referencePosition", parent.gridPlaneReferencePositions.getRenderAttributeBuffer()); - gridcubeProgram->setAttribute("a_referenceNormal", parent.gridPlaneReferenceNormals.getRenderAttributeBuffer()); - gridcubeProgram->setAttribute("a_axisInd", parent.gridPlaneAxisInds.getRenderAttributeBuffer()); + gridcubeProgram->setAttribute("a_referencePosition", parent.gridPlaneReferencePositions); + gridcubeProgram->setAttribute("a_referenceNormal", parent.gridPlaneReferenceNormals); + gridcubeProgram->setAttribute("a_axisInd", parent.gridPlaneAxisInds); gridcubeProgram->setTextureFromColormap("t_colormap", cMap.get()); render::engine->setMaterial(*gridcubeProgram, parent.getMaterial()); - gridcubeProgram->setTextureFromBuffer("t_value", values.getRenderTextureBuffer().get()); - values.getRenderTextureBuffer().get()->setFilterMode(FilterMode::Linear); + gridcubeProgram->setTextureFromBuffer("t_value", values); + values.getRenderTextureBuffer()->setFilterMode(FilterMode::Linear); } void VolumeGridCellScalarQuantity::buildCellInfoGUI(size_t ind) { diff --git a/src/volume_mesh.cpp b/src/volume_mesh.cpp index 4fa610e6..cc87f77d 100644 --- a/src/volume_mesh.cpp +++ b/src/volume_mesh.cpp @@ -350,14 +350,14 @@ VolumeMesh::VolumeMesh(std::string name, const std::vector& vertexPos vertexPositions( this, uniquePrefix() + "vertexPositions", std::vector(vertexPositions_)), // connectivity / indices -triangleVertexInds( this, uniquePrefix() + "triangleVertexInds", std::vector{}), -triangleFaceInds( this, uniquePrefix() + "triangleFaceInds", std::vector{}), -triangleCellInds( this, uniquePrefix() + "triangleCellInds", std::vector{}), +triangleVertexInds( this, uniquePrefix() + "triangleVertexInds"), +triangleFaceInds( this, uniquePrefix() + "triangleFaceInds"), +triangleCellInds( this, uniquePrefix() + "triangleCellInds"), // internal triangle data for rendering -baryCoord( this, uniquePrefix() + "baryCoord", std::vector{}), -edgeIsReal( this, uniquePrefix() + "edgeIsReal", std::vector{}), -faceType( this, uniquePrefix() + "faceType", std::vector{}), +baryCoord( this, uniquePrefix() + "baryCoord"), +edgeIsReal( this, uniquePrefix() + "edgeIsReal"), +faceType( this, uniquePrefix() + "faceType"), // other internally-computed geometry faceNormals( this, uniquePrefix() + "faceNormals", std::bind(&VolumeMesh::computeFaceNormals, this)), @@ -787,9 +787,9 @@ void VolumeMesh::setVolumeMeshUniforms(render::ShaderProgram& p) { void VolumeMesh::fillGeometryBuffers(render::ShaderProgram& p) { - p.setAttribute("a_vertexPositions", vertexPositions.getIndexedRenderAttributeBuffer(triangleVertexInds)); + p.setAttribute("a_vertexPositions", vertexPositions.getIndexedRenderAttributeBuffer(triangleVertexInds), &vertexPositions); - p.setAttribute("a_vertexNormals", faceNormals.getIndexedRenderAttributeBuffer(triangleFaceInds)); + p.setAttribute("a_vertexNormals", faceNormals.getIndexedRenderAttributeBuffer(triangleFaceInds), &faceNormals); bool wantsBary = p.hasAttribute("a_barycoord"); bool wantsEdge = (getEdgeWidth() > 0); @@ -797,16 +797,16 @@ void VolumeMesh::fillGeometryBuffers(render::ShaderProgram& p) { bool wantsFaceType = p.hasAttribute("a_faceColorType"); if (wantsBary) { - p.setAttribute("a_barycoord", baryCoord.getRenderAttributeBuffer()); + p.setAttribute("a_barycoord", baryCoord); } if (wantsEdge) { - p.setAttribute("a_edgeIsReal", edgeIsReal.getRenderAttributeBuffer()); + p.setAttribute("a_edgeIsReal", edgeIsReal); } if (wantsAttrCullPosition) { - p.setAttribute("a_cullPos", cellCenters.getIndexedRenderAttributeBuffer(triangleCellInds)); + p.setAttribute("a_cullPos", cellCenters.getIndexedRenderAttributeBuffer(triangleCellInds), &cellCenters); } if (wantsFaceType) { - p.setAttribute("a_faceColorType", faceType.getIndexedRenderAttributeBuffer(triangleFaceInds)); + p.setAttribute("a_faceColorType", faceType.getIndexedRenderAttributeBuffer(triangleFaceInds), &faceType); } } diff --git a/src/volume_mesh_color_quantity.cpp b/src/volume_mesh_color_quantity.cpp index 712c7c36..e4708a9c 100644 --- a/src/volume_mesh_color_quantity.cpp +++ b/src/volume_mesh_color_quantity.cpp @@ -123,7 +123,7 @@ void VolumeMeshVertexColorQuantity::createProgram() { // Fill color buffers parent.fillGeometryBuffers(*program); - program->setAttribute("a_color", colors.getIndexedRenderAttributeBuffer(parent.triangleVertexInds)); + program->setAttribute("a_color", colors.getIndexedRenderAttributeBuffer(parent.triangleVertexInds), &colors); render::engine->setMaterial(*program, parent.getMaterial()); } @@ -176,7 +176,7 @@ void VolumeMeshCellColorQuantity::createProgram() { // Fill color buffers parent.fillGeometryBuffers(*program); - program->setAttribute("a_color", colors.getIndexedRenderAttributeBuffer(parent.triangleCellInds)); + program->setAttribute("a_color", colors.getIndexedRenderAttributeBuffer(parent.triangleCellInds), &colors); render::engine->setMaterial(*program, parent.getMaterial()); } diff --git a/src/volume_mesh_scalar_quantity.cpp b/src/volume_mesh_scalar_quantity.cpp index 946bfc7b..6a963851 100644 --- a/src/volume_mesh_scalar_quantity.cpp +++ b/src/volume_mesh_scalar_quantity.cpp @@ -259,7 +259,7 @@ void VolumeMeshVertexScalarQuantity::createProgram() { // Fill color buffers parent.fillGeometryBuffers(*program); - program->setAttribute("a_value", values.getIndexedRenderAttributeBuffer(parent.triangleVertexInds)); + program->setAttribute("a_value", values.getIndexedRenderAttributeBuffer(parent.triangleVertexInds), &values); program->setTextureFromColormap("t_colormap", cMap.get()); render::engine->setMaterial(*program, parent.getMaterial()); } @@ -351,7 +351,7 @@ void VolumeMeshCellScalarQuantity::createProgram() { // Fill color buffers parent.fillGeometryBuffers(*program); - program->setAttribute("a_value", values.getIndexedRenderAttributeBuffer(parent.triangleCellInds)); + program->setAttribute("a_value", values.getIndexedRenderAttributeBuffer(parent.triangleCellInds), &values); program->setTextureFromColormap("t_colormap", cMap.get()); render::engine->setMaterial(*program, parent.getMaterial()); } From 20eb64374c42ea84f1773608ffc389406a22e8b2 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sat, 9 May 2026 15:54:52 -0700 Subject: [PATCH 05/31] bugfix with new setter --- src/volume_mesh.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/volume_mesh.cpp b/src/volume_mesh.cpp index cc87f77d..9ca27b8f 100644 --- a/src/volume_mesh.cpp +++ b/src/volume_mesh.cpp @@ -869,10 +869,11 @@ void VolumeMesh::computeConnectivityData() { baryCoord.setHostValue(3 * iData + 2, glm::vec3{0., 0., 1.}); // Mark edges as real or not + glm::vec3 eReal{faceRealEdges[f][j][0] ? 1.0f : 0.0f, + faceRealEdges[f][j][1] ? 1.0f : 0.0f, + faceRealEdges[f][j][2] ? 1.0f : 0.0f}; for (int k = 0; k < 3; k++) { - for (int c = 0; c < 3; c++) { - edgeIsReal.setHostValue(3 * iData + k, c, faceRealEdges[f][j][c] ? 1.0f : 0.0f); - } + edgeIsReal.setHostValue(3 * iData + k, eReal); } } From 7e9995e88de17202170de0ceca6f09d0dcadf844 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sat, 9 May 2026 16:01:38 -0700 Subject: [PATCH 06/31] remove duplicated lines --- src/surface_vector_quantity.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/surface_vector_quantity.cpp b/src/surface_vector_quantity.cpp index 7d31b51d..3c30be73 100644 --- a/src/surface_vector_quantity.cpp +++ b/src/surface_vector_quantity.cpp @@ -214,15 +214,6 @@ std::vector oneFormToFaceTangentVectors(SurfaceMesh& mesh, const std: mesh.triangleAllEdgeInds.ensureHostBufferPopulated(); mesh.triangleVertexInds.ensureHostBufferPopulated(); - // TODO why is this duplicated?! - mesh.vertexPositions.ensureHostBufferPopulated(); - mesh.faceAreas.ensureHostBufferPopulated(); - mesh.faceNormals.ensureHostBufferPopulated(); - mesh.defaultFaceTangentBasisX.ensureHostBufferPopulated(); - mesh.defaultFaceTangentBasisY.ensureHostBufferPopulated(); - mesh.triangleAllEdgeInds.ensureHostBufferPopulated(); - mesh.triangleVertexInds.ensureHostBufferPopulated(); - std::vector mappedVectorField(mesh.nFaces()); for (size_t iF = 0; iF < mesh.nFaces(); iF++) { From 4b967f803a688364fdd6e0b27a0c3140361732b1 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sat, 9 May 2026 16:04:12 -0700 Subject: [PATCH 07/31] avoid duplicate mark --- src/volume_grid.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/volume_grid.cpp b/src/volume_grid.cpp index c6327611..5fb666c4 100644 --- a/src/volume_grid.cpp +++ b/src/volume_grid.cpp @@ -380,10 +380,6 @@ void VolumeGrid::computeGridPlaneReferenceGeometry() { gridPlaneReferenceNormals.setDataHost(normalsVec); gridPlaneAxisInds.resize(axisIndsVec.size()); gridPlaneAxisInds.setDataHost(axisIndsVec); - // gridPlaneReferencePositions.markHostBufferUpdated() is called by recomputeIfPopulated() after this callback. - // The side-effect buffers must be marked updated explicitly since they are not the primary compute target. - gridPlaneReferenceNormals.markHostBufferUpdated(); - gridPlaneAxisInds.markHostBufferUpdated(); } // === Option getters and setters From 89e14cf7d86e172cc8fef56b4c64f937a6e51107 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sat, 9 May 2026 16:56:44 -0700 Subject: [PATCH 08/31] additional cleanup of ManagedBuffer api --- include/polyscope/render/managed_buffer.h | 228 +++++++++--------- src/color_image_quantity.cpp | 1 + src/color_render_image_quantity.cpp | 1 + src/curve_network.cpp | 1 + src/raw_color_alpha_render_image_quantity.cpp | 1 + src/raw_color_render_image_quantity.cpp | 1 + src/render/managed_buffer.cpp | 133 ++++------ src/render_image_quantity_base.cpp | 2 + src/scalar_image_quantity.cpp | 1 + src/scalar_render_image_quantity.cpp | 1 + src/sparse_volume_grid.cpp | 3 +- src/surface_color_quantity.cpp | 1 + src/surface_mesh.cpp | 12 + src/surface_scalar_quantity.cpp | 1 + src/volume_grid_scalar_quantity.cpp | 2 + src/volume_mesh.cpp | 2 + 16 files changed, 196 insertions(+), 195 deletions(-) diff --git a/include/polyscope/render/managed_buffer.h b/include/polyscope/render/managed_buffer.h index 9247a092..499d9dd2 100644 --- a/include/polyscope/render/managed_buffer.h +++ b/include/polyscope/render/managed_buffer.h @@ -19,8 +19,8 @@ class ManagedBufferRegistry; /* * Non-templated base class for ManagedBuffer. - * Contains all type-independent members and provides the syncToDeviceIfNeeded() virtual interface - * used by ShaderProgram::draw() to lazily push host data to the GPU before issuing a draw call. + * Contains all type-independent members, in particular the syncToDeviceIfNeeded() function which needs + * to be called without the type known. */ class ManagedBufferBase { public: @@ -56,11 +56,7 @@ class ManagedBufferBase { bool deviceBufferValid; // true if device-side buffer matches current host data // True if all registered indexed views are consistent with the current source data. - // Set to false by markRenderAttributeBufferUpdated() when the GPU source is written externally - // and host data is no longer available to re-gather from. Will be set back to true once - // updateIndexedViews() runs successfully (which requires hostBufferValid). - // Future work: an on-device gather pass will be able to update indexed views even when - // !hostBufferValid, at which point this flag drives that path. + // Gets marked as false whenever the source data changes on the host or device, until the indexed views are updated bool indexedViewsValid = true; // Explicit size and capacity. @@ -74,9 +70,10 @@ class ManagedBufferBase { std::shared_ptr renderTextureBuffer; DeviceBufferType deviceBufferType = DeviceBufferType::Attribute; - uint32_t sizeX = 0; - uint32_t sizeY = 0; - uint32_t sizeZ = 0; + + uint32_t sizeX = 0; // only populated if texture + uint32_t sizeY = 0; // only populated if texture + uint32_t sizeZ = 0; // only populated if texture }; @@ -97,7 +94,9 @@ class ManagedBufferBase { template class ManagedBuffer : public ManagedBufferBase, public virtual WeakReferrable { public: - // === Constructors + // ======================================================================== + // == Constructors + // ======================================================================== // Create an empty buffer with no data. Use resize()+setHostValue() or setDataHost() to populate. ManagedBuffer(ManagedBufferRegistry* registry, const std::string& name); @@ -106,26 +105,14 @@ class ManagedBuffer : public ManagedBufferBase, public virtual WeakReferrable { ManagedBuffer(ManagedBufferRegistry* registry, const std::string& name, std::vector data); // Manage a buffer of data which gets computed lazily - ManagedBuffer(ManagedBufferRegistry* registry, const std::string& name, - std::function computeFunc); - + ManagedBuffer(ManagedBufferRegistry* registry, const std::string& name, std::function computeFunc); ~ManagedBuffer(); - // === Core members (note: name, uniqueID, registry, dataGetsComputed, computeFunc live in ManagedBufferBase) - - - // sanity check helper - void checkInvalidValues(); - - // mark as texture, set size - void setTextureSize(uint32_t sizeX); - void setTextureSize(uint32_t sizeX, uint32_t sizeY); - void setTextureSize(uint32_t sizeX, uint32_t sizeY, uint32_t sizeZ); - std::array getTextureSize() const; - - // == Resize / capacity API + // ======================================================================== + // == Size, capacity, and type management + // ======================================================================== // The .size() of the buffer is the number of data elements it holds. // @@ -136,6 +123,39 @@ class ManagedBuffer : public ManagedBufferBase, public virtual WeakReferrable { // But in settings where we e.g. incrementally add elements or change the number of data elements on each frame, we // may want to allocate a larger capacity to avoid expensive re-allocation each time. + // Get the size of the data in the buffer. Always <= capacity(). + size_t size() const; + + // Get the capacity of the buffer. The maximum size the buffer can be resized to without triggering a reallocation. + // Always >= size(). + size_t capacity() const; + + // Set the managed capacity to newCapacity. Unlike resize(), which grows capacity via amortized + // doubling, this sets the logical capacity to a precise value. Error if newCapacity < size(). + // Reallocates the GPU buffer in-place (same buffer object, new backing memory) if one exists. + // + // Valid for attributes and 1D textures only; multidimensional textures always have capacity + // equal to their size, so use setTextureSize() instead. + void setCapacity(size_t newCapacity); + + // Set the device buffer type. Call once at construction time before setTextureSize(). + // All buffers are Attribute by default, and can be switch to texture exactly once. + void setAsType(DeviceBufferType type); + + // Is it an attribute, texture1d, texture2d, etc? + DeviceBufferType getDeviceBufferType() const; + + // Set or update the texture dimensions. On first call (managedCapacity == 0): just stores the + // dimensions. On subsequent calls: no-op if unchanged; otherwise copies any device-side data + // back to host and reallocates the GPU buffer in-place. + // Requires setAsType() to have been called first with the matching texture type. + // For 1D textures, resize() may be used instead. + void setTextureSize(uint32_t sizeX); + void setTextureSize(uint32_t sizeX, uint32_t sizeY); + void setTextureSize(uint32_t sizeX, uint32_t sizeY, uint32_t sizeZ); + std::array getTextureSize() const; + + // Resize the buffer to newSize elements. Sets hostBufferValid = true. // // If newSize <= capacity(), this is a cheap constant-time operation which just updates metadata. @@ -147,39 +167,25 @@ class ManagedBuffer : public ManagedBufferBase, public virtual WeakReferrable { // // Returns true if a reallocation occurred, false if the resize stayed within capacity. // - // Valid for attributes and 1D textures only; call the 2D/3D variants below for multidimensional - // textures. + // Valid for attributes and 1D textures only; call setTextureSize() for multidimensional textures. bool resize(size_t newSize); - - // Resize a 2D texture. No-op (returns false) if dimensions are unchanged. Otherwise always - // triggers a reallocation (2D/3D textures have no capacity slack — capacity always equals size), - // reallocates the GPU buffer in-place, and returns true. - bool resizeTexture2D(uint32_t newSizeX, uint32_t newSizeY); - - // Resize a 3D texture. No-op (returns false) if dimensions are unchanged. Otherwise always - // triggers a reallocation (2D/3D textures have no capacity slack — capacity always equals size), - // reallocates the GPU buffer in-place, and returns true. - bool resizeTexture3D(uint32_t newSizeX, uint32_t newSizeY, uint32_t newSizeZ); - // The maximum size the buffer can be resized to without triggering a reallocation. Always >= size(). - size_t capacity(); - // Set the managed capacity to newCapacity. Unlike resize(), which grows capacity via amortized - // doubling, this sets the logical capacity to a precise value. Error if newCapacity < size(). - // Reallocates the GPU buffer in-place (same buffer object, new backing memory) if one exists. - // - // Valid for attributes and 1D textures only; multidimensional textures always have capacity - // equal to their size, so use the 2D/3D resize() variants instead. - void setCapacity(size_t newCapacity); + // ======================================================================== + // == Basic data access + // ======================================================================== + // The functions are always valid to call, but might be expensive to call in a type loop or if the data resides + // only on the device. - // == Basic interactions + // Get the value at index `i`. It may be dynamically fetched from either the cpu-side `data` member or the render + // buffer, depending on where the data currently lives. + // If the data lives only on the device-side render buffer, this function is expensive, so don't call it in a + // loop. + T getValue(size_t ind); + T getValue(size_t indX, size_t indY); // only valid for 2d texture data + T getValue(size_t indX, size_t indY, size_t indZ); // only valid for 3d texture data - // Ensure that the host buffer is populated with the current values. In the common case where the user sets data - // and it never changes, this does nothing. However, if the value is being updated directly from GPU memory, this - // mirrors the updates to the host-side buffer. Also, if the value is lazily computed by computeFunc(), it ensures - // that function has been called. - void ensureHostBufferPopulated(); // Copy newData into the host buffer. Errors if newData.size() > capacity() — call resize() or // setCapacity() first if needed. Does not change capacity. Automatically syncs to any allocated @@ -189,71 +195,56 @@ class ManagedBuffer : public ManagedBufferBase, public virtual WeakReferrable { // Returns a full copy of the host data, populating it from device if needed. std::vector getDataCopy(); - // Raw pointer iteration support. Caller must call ensureHostBufferPopulated() before using these. - // A debug-mode check will fire if this precondition is violated. - const T* begin() const; - const T* end() const; - // Single-element read. Caller MUST call ensureHostBufferPopulated() before calling this - // (especially in loops — call it once before the loop, not per element). - // A debug-mode check will fire if this precondition is violated. + // ======================================================================== + // == Low-level data access + // ======================================================================== + + // These functions provide direct access to the underlying host side buffer. + // Callers are generally _required_ to call ensureHostBufferPopulated() before using these, and + // markHostBufferUpdated() after making any changes, to maintain the validity of the host/device mirroring. + + // Ensure that the host buffer is populated with the current values. In the common case where the user sets data + // and it never changes, this does nothing. However, if the value is being updated directly from GPU memory, this + // mirrors the updates to the host-side buffer. Also, if the value is lazily computed by computeFunc(), it ensures + // that function has been called. + void ensureHostBufferPopulated(); + + // If the contents of the host buffer have been updated via the functions below, this must be called once updates are + // finished. It internally handles concerns like reflecting updates to the render buffer. + void markHostBufferUpdated(); + + // Single-element read. Caller MUST call ensureHostBufferPopulated() first. T getHostValue(size_t ind) const; T getHostValue(size_t indX, size_t indY) const; // only valid for 2d texture data T getHostValue(size_t indX, size_t indY, size_t indZ) const; // only valid for 3d texture data - // Single-element write. Caller MUST call ensureHostBufferPopulated() (or resize()) before - // calling this. Automatically syncs to any allocated device buffers and triggers a redraw. - // For bulk writes, prefer setDataHost() to avoid redundant GPU uploads. - // A debug-mode check will fire if the precondition is violated. + // Single-element write. Caller MUST call ensureHostBufferPopulated() first, and MUST call markHostBufferUpdated() + // after all writes are complete. void setHostValue(size_t ind, T val); void setHostValue(size_t indX, size_t indY, T val); // only valid for 2d texture data void setHostValue(size_t indX, size_t indY, size_t indZ, T val); // only valid for 3d texture data - // If the contents of the host buffer have been updated externally, this function MUST be called. - // It internally handles concerns like reflecting updates to the render buffer. - void markHostBufferUpdated(); + // Raw pointer iteration support. Caller MUST call ensureHostBufferPopulated() before using these. + const T* begin() const; + const T* end() const; - // Get the value at index `i`. It may be dynamically fetched from either the cpu-side `data` member or the render - // buffer, depending on where the data currently lives. - // If the data lives only on the device-side render buffer, this function is expensive, so don't call it in a - // loop. - T getValue(size_t ind); - T getValue(size_t indX, size_t indY); // only valid for 2d texture data - T getValue(size_t indX, size_t indY, size_t indZ); // only valid for 3d texture data + // ======================================================================== + // == Misc meta functions and data management + // ======================================================================== // If computeFunc() has already been called to populate the stored data, call it again to recompute the data, and // re-fill the buffer if necessary. This function is only meaningful in the case where `dataGetsComputed = true`. void recomputeIfPopulated(); - bool hasData(); // true if there is valid data on either the host or device - size_t size(); // size of the data (number of entries) - - // Is it an attribute, texture1d, texture2d, etc? - DeviceBufferType getDeviceBufferType(); - - std::string summaryString(); // for debugging - - // ======================================================================== - // == Direct access to the GPU (device-side) render attribute buffer - // ======================================================================== - - // NOTE: this is only for attribute-accessed buffers (DeviceBufferType::Attribute). See the variants below for - // textures. - - // NOTE: This class follows a lazy-sync policy: once the render buffer is allocated, it is kept up to date - // with host-side changes lazily — the actual GPU upload happens in syncToDeviceIfNeeded(), which is called - // by ShaderProgram::draw() just before the draw call. External writes to the GPU buffer (via - // markRenderAttributeBufferUpdated()) invalidate the host copy until ensureHostBufferPopulated() is called. + // Sync host data to the device buffer if not up to date + void syncToDeviceIfNeeded() override; - // Get a reference to the underlying GPU-side attribute buffer - // Once this reference is created, it will always be immediately updated to reflect any external changes to the - // data. (note that if you write to this buffer externally, you MUST call markRenderAttributeBufferUpdated() - // below) - std::shared_ptr getRenderAttributeBuffer(); + // Get an info string for debugging + std::string summaryString() const; - // Tell Polyscope that you wrote updated data into the render buffer. This MUST be called after externally writing - // to the buffer from getRenderBuffer() above. - void markRenderAttributeBufferUpdated(); + // Throw exception if the buffer contains any values which are NaN or Inf as in polyscope::isInvalidValue(). + void checkInvalidValues(); // ======================================================================== // == Indexed views @@ -279,24 +270,36 @@ class ManagedBuffer : public ManagedBufferBase, public virtual WeakReferrable { // copy (which is not cached). std::vector getIndexedView(ManagedBuffer& indices); + // ======================================================================== - // == Direct access to the GPU (device-side) render texture buffer + // == Direct access to the GPU (device-side) render buffers // ======================================================================== - // + + // NOTE: this is only for attribute-accessed buffers (DeviceBufferType::Attribute). See the variants below for + // textures. + + // NOTE: This class follows a lazy-sync policy: once the render buffer is allocated, it is kept up to date + // with host-side changes lazily — the actual GPU upload happens in syncToDeviceIfNeeded(), which is called + // by ShaderProgram::draw() just before the draw call. External writes to the GPU buffer (via + // markRenderAttributeBufferUpdated()) invalidate the host copy until ensureHostBufferPopulated() is called. + + // Get a reference to the underlying GPU-side attribute buffer + // Once this reference is created, it will always be immediately updated to reflect any external changes to the + // data. (note that if you write to this buffer externally, you MUST call markRenderAttributeBufferUpdated() + // below) + std::shared_ptr getRenderAttributeBuffer(); + + // Tell Polyscope that you wrote updated data into the render buffer. This MUST be called after externally writing + // to the buffer from getRenderBuffer() above. + void markRenderAttributeBufferUpdated(); + // NOTE: these follow the same semantics as the attribute version above, but these apply when the buffer is a texture // (DeviceBufferType::Texture1d, etc). - std::shared_ptr getRenderTextureBuffer(); void markRenderTextureBufferUpdated(); - // Sync host data to the device buffer if needed. Called by ShaderProgram::draw() before drawing. - void syncToDeviceIfNeeded() override; - protected: - // == Internal members - // Note: dataGetsComputed, computeFunc, hostBufferValid, deviceBufferValid, managedCapacity, - // renderAttributeBuffer, renderTextureBuffer, deviceBufferType, sizeX/Y/Z all live in ManagedBufferBase. // Override invalidateHostBuffer to also clear the typed data vector. void invalidateHostBuffer(); @@ -318,7 +321,8 @@ class ManagedBuffer : public ManagedBufferBase, public virtual WeakReferrable { std::shared_ptr bufferIndexCopyProgram; private: - template friend class ManagedBuffer; + template + friend class ManagedBuffer; // The raw underlying buffer which this class owns and holds the data. // diff --git a/src/color_image_quantity.cpp b/src/color_image_quantity.cpp index 72971a84..9652a720 100644 --- a/src/color_image_quantity.cpp +++ b/src/color_image_quantity.cpp @@ -16,6 +16,7 @@ ColorImageQuantity::ColorImageQuantity(Structure& parent_, std::string name, siz : ImageQuantity(parent_, name, dimX, dimY, imageOrigin_), colors(this, uniquePrefix() + "colors", std::vector(data_)), isPremultiplied(uniquePrefix() + "isPremultiplied", false) { + colors.setAsType(DeviceBufferType::Texture2d); colors.setTextureSize(dimX, dimY); } diff --git a/src/color_render_image_quantity.cpp b/src/color_render_image_quantity.cpp index 5e0f3e52..2d69bd80 100644 --- a/src/color_render_image_quantity.cpp +++ b/src/color_render_image_quantity.cpp @@ -17,6 +17,7 @@ ColorRenderImageQuantity::ColorRenderImageQuantity(Structure& parent_, std::stri const std::vector& colorsData_, ImageOrigin imageOrigin) : RenderImageQuantityBase(parent_, name, dimX, dimY, depthData, normalData, imageOrigin), colors(this, uniquePrefix() + "colors", std::vector(colorsData_)) { + colors.setAsType(DeviceBufferType::Texture2d); colors.setTextureSize(dimX, dimY); } diff --git a/src/curve_network.cpp b/src/curve_network.cpp index 4d7b0b63..0c45a231 100644 --- a/src/curve_network.cpp +++ b/src/curve_network.cpp @@ -398,6 +398,7 @@ void CurveNetwork::computeEdgeCenters() { edgeCenters.setHostValue(iE, p); } + edgeCenters.markHostBufferUpdated(); } void CurveNetwork::refresh() { diff --git a/src/raw_color_alpha_render_image_quantity.cpp b/src/raw_color_alpha_render_image_quantity.cpp index fba8750a..f278e1c5 100644 --- a/src/raw_color_alpha_render_image_quantity.cpp +++ b/src/raw_color_alpha_render_image_quantity.cpp @@ -18,6 +18,7 @@ RawColorAlphaRenderImageQuantity::RawColorAlphaRenderImageQuantity(Structure& pa : RenderImageQuantityBase(parent_, name, dimX, dimY, depthData, std::vector(), imageOrigin), colors(this, uniquePrefix() + "colors", std::vector(colorsData_)), isPremultiplied(uniquePrefix() + "isPremultiplied", false) { + colors.setAsType(DeviceBufferType::Texture2d); colors.setTextureSize(dimX, dimY); } diff --git a/src/raw_color_render_image_quantity.cpp b/src/raw_color_render_image_quantity.cpp index 4d18ce92..b09238a3 100644 --- a/src/raw_color_render_image_quantity.cpp +++ b/src/raw_color_render_image_quantity.cpp @@ -17,6 +17,7 @@ RawColorRenderImageQuantity::RawColorRenderImageQuantity(Structure& parent_, std ImageOrigin imageOrigin) : RenderImageQuantityBase(parent_, name, dimX, dimY, depthData, std::vector(), imageOrigin), colors(this, uniquePrefix() + "colors", std::vector(colorsData_)) { + colors.setAsType(DeviceBufferType::Texture2d); colors.setTextureSize(dimX, dimY); } diff --git a/src/render/managed_buffer.cpp b/src/render/managed_buffer.cpp index 2a60a2ca..0cadf77e 100644 --- a/src/render/managed_buffer.cpp +++ b/src/render/managed_buffer.cpp @@ -104,31 +104,50 @@ void ManagedBuffer::checkInvalidValues() { } template -void ManagedBuffer::setTextureSize(uint32_t sizeX_) { +void ManagedBuffer::setAsType(DeviceBufferType type) { if (deviceBufferType != DeviceBufferType::Attribute) - exception("managed buffer cannot be resized, texture size can only be set once"); + exception("ManagedBuffer " + name + " setAsType(): type has already been set"); + deviceBufferType = type; +} - deviceBufferType = DeviceBufferType::Texture1d; - sizeX = sizeX_; - // managedCapacity and currentSize are set correctly from construction; no fixup needed. +template +void ManagedBuffer::setTextureSize(uint32_t sizeX_) { + checkDeviceBufferTypeIs(DeviceBufferType::Texture1d); + resize(static_cast(sizeX_)); } template void ManagedBuffer::setTextureSize(uint32_t sizeX_, uint32_t sizeY_) { - if (deviceBufferType != DeviceBufferType::Attribute) - exception("managed buffer cannot be resized, texture size can only be set once"); - - deviceBufferType = DeviceBufferType::Texture2d; + checkDeviceBufferTypeIs(DeviceBufferType::Texture2d); + if (sizeX_ == sizeX && sizeY_ == sizeY) return; // no-op + if (managedCapacity > 0) { + // Resize: copy device-side data back to host before touching dimensions or the GPU buffer. + ensureHostBufferPopulated(); + managedCapacity = static_cast(sizeX_) * sizeY_; + currentSize = managedCapacity; + data.resize(managedCapacity); + if (renderTextureBuffer) renderTextureBuffer->resize(sizeX_, sizeY_); + hostBufferValid = true; + deviceBufferValid = false; + } sizeX = sizeX_; sizeY = sizeY_; } template void ManagedBuffer::setTextureSize(uint32_t sizeX_, uint32_t sizeY_, uint32_t sizeZ_) { - if (deviceBufferType != DeviceBufferType::Attribute) - exception("managed buffer cannot be resized, texture size can only be set once"); - - deviceBufferType = DeviceBufferType::Texture3d; + checkDeviceBufferTypeIs(DeviceBufferType::Texture3d); + if (sizeX_ == sizeX && sizeY_ == sizeY && sizeZ_ == sizeZ) return; // no-op + if (managedCapacity > 0) { + // Resize: copy device-side data back to host before touching dimensions or the GPU buffer. + ensureHostBufferPopulated(); + managedCapacity = static_cast(sizeX_) * sizeY_ * sizeZ_; + currentSize = managedCapacity; + data.resize(managedCapacity); + if (renderTextureBuffer) renderTextureBuffer->resize(sizeX_, sizeY_, sizeZ_); + hostBufferValid = true; + deviceBufferValid = false; + } sizeX = sizeX_; sizeY = sizeY_; sizeZ = sizeZ_; @@ -141,14 +160,14 @@ std::array ManagedBuffer::getTextureSize() const { } template -size_t ManagedBuffer::capacity() { +size_t ManagedBuffer::capacity() const { return managedCapacity; } template bool ManagedBuffer::resize(size_t newSize) { if (deviceBufferType == DeviceBufferType::Texture2d || deviceBufferType == DeviceBufferType::Texture3d) - exception("resize() is not valid for 2D/3D texture buffers; use resizeTexture2D() or resizeTexture3D()"); + exception("resize() is not valid for 2D/3D texture buffers; use setTextureSize() instead"); if (newSize > managedCapacity) { // Copy device-side data back to host BEFORE modifying data or invalidating the GPU buffer. @@ -218,54 +237,6 @@ void ManagedBuffer::setCapacity(size_t newCapacity) { deviceBufferValid = false; } -template -bool ManagedBuffer::resizeTexture2D(uint32_t newSizeX, uint32_t newSizeY) { - checkDeviceBufferTypeIs(DeviceBufferType::Texture2d); - - if (newSizeX == sizeX && newSizeY == sizeY) return false; // no-op - - // Before invalidating the GPU buffer, copy any device-side changes back to the host. - ensureHostBufferPopulated(); - - sizeX = newSizeX; - sizeY = newSizeY; - managedCapacity = static_cast(sizeX) * sizeY; - currentSize = managedCapacity; - data.resize(managedCapacity); - - if (renderTextureBuffer) { - renderTextureBuffer->resize(sizeX, sizeY); - } - hostBufferValid = true; - deviceBufferValid = false; - - return true; -} - -template -bool ManagedBuffer::resizeTexture3D(uint32_t newSizeX, uint32_t newSizeY, uint32_t newSizeZ) { - checkDeviceBufferTypeIs(DeviceBufferType::Texture3d); - - if (newSizeX == sizeX && newSizeY == sizeY && newSizeZ == sizeZ) return false; // no-op - - // Before invalidating the GPU buffer, copy any device-side changes back to the host. - ensureHostBufferPopulated(); - - sizeX = newSizeX; - sizeY = newSizeY; - sizeZ = newSizeZ; - managedCapacity = static_cast(sizeX) * sizeY * sizeZ; - currentSize = managedCapacity; - data.resize(managedCapacity); - - if (renderTextureBuffer) { - renderTextureBuffer->resize(sizeX, sizeY, sizeZ); - } - hostBufferValid = true; - deviceBufferValid = false; - - return true; -} template void ManagedBuffer::ensureHostBufferPopulated() { @@ -352,45 +323,53 @@ const T* ManagedBuffer::end() const { template T ManagedBuffer::getHostValue(size_t ind) const { #ifndef NDEBUG - if (!hostBufferValid) - exception("ManagedBuffer " + name + " getHostValue() called without ensureHostBufferPopulated()"); + if (!hostBufferValid) exception("ManagedBuffer " + name + " getHostValue() called without ensureHostBufferPopulated()"); #endif return data[ind]; } template T ManagedBuffer::getHostValue(size_t indX, size_t indY) const { +#ifndef NDEBUG + if (!hostBufferValid) exception("ManagedBuffer " + name + " getHostValue() called without ensureHostBufferPopulated()"); checkDeviceBufferTypeIs(DeviceBufferType::Texture2d); +#endif return getHostValue(sizeY * indX + indY); } template T ManagedBuffer::getHostValue(size_t indX, size_t indY, size_t indZ) const { +#ifndef NDEBUG + if (!hostBufferValid) exception("ManagedBuffer " + name + " getHostValue() called without ensureHostBufferPopulated()"); checkDeviceBufferTypeIs(DeviceBufferType::Texture3d); +#endif return getHostValue(sizeZ * sizeY * indX + sizeZ * indY + indZ); } template void ManagedBuffer::setHostValue(size_t ind, T val) { #ifndef NDEBUG - if (!hostBufferValid) - exception("ManagedBuffer " + name + " setHostValue() called without ensureHostBufferPopulated()"); + if (!hostBufferValid) exception("ManagedBuffer " + name + " setHostValue() called without ensureHostBufferPopulated()"); #endif data[ind] = val; - deviceBufferValid = false; - invalidateIndexedViews(); - requestRedraw(); + // NOTE: intentionally no side-effects here. Caller must call markHostBufferUpdated() after all writes are done. } template void ManagedBuffer::setHostValue(size_t indX, size_t indY, T val) { +#ifndef NDEBUG + if (!hostBufferValid) exception("ManagedBuffer " + name + " setHostValue() called without ensureHostBufferPopulated()"); checkDeviceBufferTypeIs(DeviceBufferType::Texture2d); +#endif setHostValue(sizeY * indX + indY, val); } template void ManagedBuffer::setHostValue(size_t indX, size_t indY, size_t indZ, T val) { +#ifndef NDEBUG + if (!hostBufferValid) exception("ManagedBuffer " + name + " setHostValue() called without ensureHostBufferPopulated()"); checkDeviceBufferTypeIs(DeviceBufferType::Texture3d); +#endif setHostValue(sizeZ * sizeY * indX + sizeZ * indY + indZ, val); } @@ -492,25 +471,17 @@ T ManagedBuffer::getValue(size_t indX, size_t indY, size_t indZ) { } template -size_t ManagedBuffer::size() { +size_t ManagedBuffer::size() const { return currentSize; } template -bool ManagedBuffer::hasData() { - - if (hostBufferValid) return true; - if (deviceBufferValid) return true; - return false; -} - -template -DeviceBufferType ManagedBuffer::getDeviceBufferType() { +DeviceBufferType ManagedBuffer::getDeviceBufferType() const { return deviceBufferType; } template -std::string ManagedBuffer::summaryString() { +std::string ManagedBuffer::summaryString() const { std::string str = ""; diff --git a/src/render_image_quantity_base.cpp b/src/render_image_quantity_base.cpp index 0420fa3d..fb57174b 100644 --- a/src/render_image_quantity_base.cpp +++ b/src/render_image_quantity_base.cpp @@ -18,8 +18,10 @@ RenderImageQuantityBase::RenderImageQuantityBase(Structure& parent_, std::string hasNormals(normalData_.size() > 0), imageOrigin(imageOrigin_), material(uniquePrefix() + "material", "clay"), transparency(uniquePrefix() + "transparency", 1.0), allowFullscreenCompositing(uniquePrefix() + "allowFullscreenCompositing", false) { + depths.setAsType(DeviceBufferType::Texture2d); depths.setTextureSize(dimX, dimY); if (hasNormals) { + normals.setAsType(DeviceBufferType::Texture2d); normals.setTextureSize(dimX, dimY); } diff --git a/src/scalar_image_quantity.cpp b/src/scalar_image_quantity.cpp index 4ad7d558..90028cde 100644 --- a/src/scalar_image_quantity.cpp +++ b/src/scalar_image_quantity.cpp @@ -13,6 +13,7 @@ namespace polyscope { ScalarImageQuantity::ScalarImageQuantity(Structure& parent_, std::string name, size_t dimX, size_t dimY, const std::vector& data_, ImageOrigin imageOrigin_, DataType dataType_) : ImageQuantity(parent_, name, dimX, dimY, imageOrigin_), ScalarQuantity(*this, data_, dataType_) { + values.setAsType(DeviceBufferType::Texture2d); values.setTextureSize(dimX, dimY); } diff --git a/src/scalar_render_image_quantity.cpp b/src/scalar_render_image_quantity.cpp index 1008cfa3..eb843b96 100644 --- a/src/scalar_render_image_quantity.cpp +++ b/src/scalar_render_image_quantity.cpp @@ -17,6 +17,7 @@ ScalarRenderImageQuantity::ScalarRenderImageQuantity(Structure& parent_, std::st DataType dataType_) : RenderImageQuantityBase(parent_, name, dimX, dimY, depthData, normalData, imageOrigin), ScalarQuantity(*this, scalarData_, dataType_) { + values.setAsType(DeviceBufferType::Texture2d); values.setTextureSize(dimX, dimY); } diff --git a/src/sparse_volume_grid.cpp b/src/sparse_volume_grid.cpp index 51282de3..b0467136 100644 --- a/src/sparse_volume_grid.cpp +++ b/src/sparse_volume_grid.cpp @@ -97,8 +97,7 @@ void SparseVolumeGrid::computeCellPositions() { cellIndices.setHostValue(i, ijk); } - // cellPositions.markHostBufferUpdated() is called by recomputeIfPopulated() after this callback. - // cellIndices is a side-effect buffer, so mark it updated explicitly. + cellPositions.markHostBufferUpdated(); cellIndices.markHostBufferUpdated(); } diff --git a/src/surface_color_quantity.cpp b/src/surface_color_quantity.cpp index 7955542d..4c5336e1 100644 --- a/src/surface_color_quantity.cpp +++ b/src/surface_color_quantity.cpp @@ -155,6 +155,7 @@ SurfaceTextureColorQuantity::SurfaceTextureColorQuantity(std::string name, Surfa ImageOrigin origin_) : SurfaceColorQuantity(name, mesh_, "texture", colorValues_), TextureMapQuantity(*this, dimX_, dimY_, origin_), param(param_) { + colors.setAsType(DeviceBufferType::Texture2d); colors.setTextureSize(dimX, dimY); } diff --git a/src/surface_mesh.cpp b/src/surface_mesh.cpp index 4a64cbf1..6954b5a5 100644 --- a/src/surface_mesh.cpp +++ b/src/surface_mesh.cpp @@ -251,6 +251,7 @@ void SurfaceMesh::computeTriangleAllEdgeInds() { } } + triangleAllEdgeInds.markHostBufferUpdated(); nEdgesCount = psEdgeInd; } @@ -322,6 +323,7 @@ void SurfaceMesh::computeTriangleCornerInds() { } } + triangleCornerInds.markHostBufferUpdated(); } void SurfaceMesh::computeTriangleAllVertexInds() { @@ -349,6 +351,7 @@ void SurfaceMesh::computeTriangleAllVertexInds() { } } + triangleAllVertexInds.markHostBufferUpdated(); } void SurfaceMesh::computeTriangleAllHalfedgeInds() { @@ -390,6 +393,7 @@ void SurfaceMesh::computeTriangleAllHalfedgeInds() { } } + triangleAllHalfedgeInds.markHostBufferUpdated(); } void SurfaceMesh::computeTriangleAllCornerInds() { @@ -424,6 +428,7 @@ void SurfaceMesh::computeTriangleAllCornerInds() { } } + triangleAllCornerInds.markHostBufferUpdated(); } @@ -462,6 +467,7 @@ void SurfaceMesh::computeFaceNormals() { faceNormals.setHostValue(iF, fN); } + faceNormals.markHostBufferUpdated(); } void SurfaceMesh::computeFaceCenters() { @@ -482,6 +488,7 @@ void SurfaceMesh::computeFaceCenters() { faceCenters.setHostValue(iF, faceCenter); } + faceCenters.markHostBufferUpdated(); } void SurfaceMesh::computeFaceAreas() { @@ -516,6 +523,7 @@ void SurfaceMesh::computeFaceAreas() { faceAreas.setHostValue(iF, fA); } + faceAreas.markHostBufferUpdated(); } @@ -546,6 +554,7 @@ void SurfaceMesh::computeVertexNormals() { vertexNormals.setHostValue(iV, glm::normalize(vertexNormals.getHostValue(iV))); } + vertexNormals.markHostBufferUpdated(); } void SurfaceMesh::computeVertexAreas() { @@ -567,6 +576,7 @@ void SurfaceMesh::computeVertexAreas() { } } + vertexAreas.markHostBufferUpdated(); } void SurfaceMesh::computeDefaultFaceTangentBasisX() { @@ -597,6 +607,7 @@ void SurfaceMesh::computeDefaultFaceTangentBasisX() { defaultFaceTangentBasisX.setHostValue(iF, basisX); } + defaultFaceTangentBasisX.markHostBufferUpdated(); } void SurfaceMesh::computeDefaultFaceTangentBasisY() { @@ -627,6 +638,7 @@ void SurfaceMesh::computeDefaultFaceTangentBasisY() { defaultFaceTangentBasisY.setHostValue(iF, basisY); } + defaultFaceTangentBasisY.markHostBufferUpdated(); } // === Edge Lengths === diff --git a/src/surface_scalar_quantity.cpp b/src/surface_scalar_quantity.cpp index f11b866d..12ff80cd 100644 --- a/src/surface_scalar_quantity.cpp +++ b/src/surface_scalar_quantity.cpp @@ -325,6 +325,7 @@ SurfaceTextureScalarQuantity::SurfaceTextureScalarQuantity(std::string name, Sur ImageOrigin origin_, DataType dataType_) : SurfaceScalarQuantity(name, mesh_, "vertex", values_, dataType_), TextureMapQuantity(*this, dimX_, dimY_, origin_), param(param_) { + values.setAsType(DeviceBufferType::Texture2d); values.setTextureSize(dimX, dimY); if (dataType == DataType::CATEGORICAL) { diff --git a/src/volume_grid_scalar_quantity.cpp b/src/volume_grid_scalar_quantity.cpp index 33645e30..1da4eabe 100644 --- a/src/volume_grid_scalar_quantity.cpp +++ b/src/volume_grid_scalar_quantity.cpp @@ -19,6 +19,7 @@ VolumeGridNodeScalarQuantity::VolumeGridNodeScalarQuantity(std::string name, Vol isosurfaceColor(uniquePrefix() + "isosurfaceColor", getNextUniqueColor()), slicePlanesAffectIsosurface(uniquePrefix() + "slicePlanesAffectIsosurface", false) { + values.setAsType(DeviceBufferType::Texture3d); values.setTextureSize(parent.getGridNodeDim().x, parent.getGridNodeDim().y, parent.getGridNodeDim().z); } @@ -283,6 +284,7 @@ VolumeGridCellScalarQuantity::VolumeGridCellScalarQuantity(std::string name, Vol : VolumeGridQuantity(name, grid_, true), ScalarQuantity(*this, values_, dataType_), gridcubeVizEnabled(parent.uniquePrefix() + "#" + name + "#gridcubeVizEnabled", true) { + values.setAsType(DeviceBufferType::Texture3d); values.setTextureSize(parent.getGridCellDim().x, parent.getGridCellDim().y, parent.getGridCellDim().z); } diff --git a/src/volume_mesh.cpp b/src/volume_mesh.cpp index 9ca27b8f..73792565 100644 --- a/src/volume_mesh.cpp +++ b/src/volume_mesh.cpp @@ -967,6 +967,7 @@ void VolumeMesh::computeFaceNormals() { } } + faceNormals.markHostBufferUpdated(); } @@ -993,6 +994,7 @@ void VolumeMesh::computeCellCenters() { cellCenters.setHostValue(iC, center); } + cellCenters.markHostBufferUpdated(); } From 6557ac3ff24c81f08c5c39af3a2973d1b1245b10 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sun, 10 May 2026 16:45:48 -0700 Subject: [PATCH 09/31] slight cleanup, use managed buffer for index --- include/polyscope/render/engine.h | 2 ++ include/polyscope/render/managed_buffer.h | 6 +++++ src/render/managed_buffer.cpp | 31 +++++------------------ src/render/mock_opengl/mock_gl_engine.cpp | 1 + src/render/opengl/gl_engine.cpp | 1 + 5 files changed, 17 insertions(+), 24 deletions(-) diff --git a/include/polyscope/render/engine.h b/include/polyscope/render/engine.h index ecba5d7c..5e30b689 100644 --- a/include/polyscope/render/engine.h +++ b/include/polyscope/render/engine.h @@ -431,6 +431,7 @@ class ShaderProgram { // Defined in managed_buffer.h after ManagedBuffer is fully declared. template void setAttribute(std::string name, ManagedBuffer& buf); template void setTextureFromBuffer(std::string name, ManagedBuffer& buf); + template void setIndex(ManagedBuffer& buf); // Indices virtual void setIndex(std::shared_ptr externalBuffer) = 0; @@ -466,6 +467,7 @@ class ShaderProgram { uint64_t uniqueID; std::shared_ptr indexBuffer; + ManagedBufferBase* indexSourceManagedBuffer = nullptr; // instancing uint32_t instanceCount = INVALID_IND_32; diff --git a/include/polyscope/render/managed_buffer.h b/include/polyscope/render/managed_buffer.h index 499d9dd2..095467fd 100644 --- a/include/polyscope/render/managed_buffer.h +++ b/include/polyscope/render/managed_buffer.h @@ -425,5 +425,11 @@ void ShaderProgram::setTextureFromBuffer(std::string name, ManagedBuffer& buf setTextureFromBuffer(name, buf.getRenderTextureBuffer().get(), &buf); } +template +void ShaderProgram::setIndex(ManagedBuffer& buf) { + indexSourceManagedBuffer = &buf; + setIndex(buf.getRenderAttributeBuffer()); +} + } // namespace render } // namespace polyscope diff --git a/src/render/managed_buffer.cpp b/src/render/managed_buffer.cpp index 0cadf77e..c8a4a146 100644 --- a/src/render/managed_buffer.cpp +++ b/src/render/managed_buffer.cpp @@ -170,27 +170,11 @@ bool ManagedBuffer::resize(size_t newSize) { exception("resize() is not valid for 2D/3D texture buffers; use setTextureSize() instead"); if (newSize > managedCapacity) { - // Copy device-side data back to host BEFORE modifying data or invalidating the GPU buffer. - // ensureHostBufferPopulated() reads from renderAttributeBuffer into data; if we resize data - // first it would overwrite the resize with the old GPU contents. - // Only copy existing data to host if there is data to preserve. - if (isInNeedsComputeState()) { - // If state is NeedsCompute, there is no existing data — calling ensureHostBufferPopulated() - // would recursively invoke the compute function that is currently running. - } else { - // Common case - ensureHostBufferPopulated(); - } - - // Reallocation needed: use amortized doubling. - // data is always kept at data.size() == managedCapacity (the invariant), so we resize (not reserve). + // Reallocation needed: use amortized doubling for the new capacity. size_t newCapacity = std::max(newSize, 2 * managedCapacity); - data.resize(newCapacity); - managedCapacity = newCapacity; - currentSize = newSize; + setCapacity(newCapacity); // handles host/device sync, data.resize, validity flags - hostBufferValid = true; - deviceBufferValid = false; + currentSize = newSize; if (deviceBufferType == DeviceBufferType::Texture1d) { sizeX = static_cast(newSize); @@ -393,8 +377,8 @@ void ManagedBuffer::syncToDeviceIfNeeded() { exception("ManagedBuffer " + name + " has no valid data on host or device at draw time"); } - // Host data is valid. Upload to device if not already current. - if (!deviceBufferValid) { + // Host data is valid. Copy to device. + if (!deviceBufferValid) { // must be hostBufferValid == true if (renderAttributeBuffer) { renderAttributeBuffer->setData(data); } @@ -403,9 +387,8 @@ void ManagedBuffer::syncToDeviceIfNeeded() { } deviceBufferValid = true; } - - - // Update indexed views + + // Update indexed views if (!indexedViewsValid) { if(deviceBufferValid) { // always true now at this point diff --git a/src/render/mock_opengl/mock_gl_engine.cpp b/src/render/mock_opengl/mock_gl_engine.cpp index b4ce8ce9..894ce89f 100644 --- a/src/render/mock_opengl/mock_gl_engine.cpp +++ b/src/render/mock_opengl/mock_gl_engine.cpp @@ -1669,6 +1669,7 @@ void GLShaderProgram::syncBuffersToDeviceIfNeeded() { for (auto& tex : textures) { if (tex.sourceManagedBuffer) tex.sourceManagedBuffer->syncToDeviceIfNeeded(); } + if (indexSourceManagedBuffer) indexSourceManagedBuffer->syncToDeviceIfNeeded(); } void GLShaderProgram::draw() { diff --git a/src/render/opengl/gl_engine.cpp b/src/render/opengl/gl_engine.cpp index 53fb729a..24750aeb 100644 --- a/src/render/opengl/gl_engine.cpp +++ b/src/render/opengl/gl_engine.cpp @@ -2221,6 +2221,7 @@ void GLShaderProgram::syncBuffersToDeviceIfNeeded() { for (auto& tex : textures) { if (tex.sourceManagedBuffer) tex.sourceManagedBuffer->syncToDeviceIfNeeded(); } + if (indexSourceManagedBuffer) indexSourceManagedBuffer->syncToDeviceIfNeeded(); } void GLShaderProgram::draw() { From 47a2965211be5c6050bde72e020d8de5d639cdb5 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Tue, 19 May 2026 22:55:28 -0700 Subject: [PATCH 10/31] set drawCount explicitly --- include/polyscope/render/engine.h | 18 +++-- include/polyscope/vector_quantity.ipp | 3 + src/camera_view.cpp | 3 + src/color_bar.cpp | 1 + src/color_image_quantity.cpp | 2 + src/color_render_image_quantity.cpp | 1 + src/curve_network.cpp | 2 + src/depth_render_image_quantity.cpp | 1 + src/point_cloud.cpp | 2 + src/raw_color_alpha_render_image_quantity.cpp | 1 + src/raw_color_render_image_quantity.cpp | 1 + src/render/engine.cpp | 6 ++ src/render/ground_plane.cpp | 3 + src/render/managed_buffer.cpp | 2 +- src/render/mock_opengl/mock_gl_engine.cpp | 38 +++++----- src/render/opengl/gl_engine.cpp | 72 +++++++++---------- src/render_image_quantity_base.cpp | 1 + src/scalar_image_quantity.cpp | 2 + src/scalar_render_image_quantity.cpp | 1 + src/simple_triangle_mesh.cpp | 7 +- src/slice_plane.cpp | 1 + src/sparse_volume_grid.cpp | 6 ++ src/surface_mesh.cpp | 2 + src/volume_grid.cpp | 5 +- src/volume_grid_scalar_quantity.cpp | 2 +- src/volume_mesh.cpp | 4 ++ test/src/sparse_volume_grid_test.cpp | 8 ++- 27 files changed, 118 insertions(+), 77 deletions(-) diff --git a/include/polyscope/render/engine.h b/include/polyscope/render/engine.h index 5e30b689..5c69913f 100644 --- a/include/polyscope/render/engine.h +++ b/include/polyscope/render/engine.h @@ -111,8 +111,7 @@ class AttributeBuffer { // == Getters RenderDataType getType() const { return dataType; } int getArrayCount() const { return arrayCount; } - int64_t getDataSize() const { return dataSize; } - int64_t getDataSizeInBytes() const { return dataSize * sizeInBytes(dataType) * getArrayCount(); } + uint64_t getBufferSize() const { return bufferSize; } uint64_t getUniqueID() const { return uniqueID; } bool isSet() const { return setFlag; } @@ -150,9 +149,7 @@ class AttributeBuffer { RenderDataType dataType; int arrayCount; bool setFlag = false; - int64_t dataSize = -1; // the size of the data currently stored in this attribute (-1 if nothing) - // this counts # elements of the specified type, s.t. array'd mulitpliers are still just one - uint64_t bufferSize = 0; // the size of the allocated buffer (which might be larger than the data sixze) + uint64_t bufferSize = 0; // size of the allocated GPU buffer in elements of this buffer's type uint64_t uniqueID; }; @@ -437,6 +434,13 @@ class ShaderProgram { virtual void setIndex(std::shared_ptr externalBuffer) = 0; virtual void setPrimitiveRestartIndex(unsigned int restartIndex) = 0; + // Set the number of primitives to draw in the draw call. Must be called before drawing. + // For non-indexed modes: number of vertices (e.g. points, line endpoints, triangle corners). + // For indexed modes: number of index-buffer entries (e.g. uvec3 faces for IndexedTriangles), + // the engine multiplies by indexSizeMult internally to get the total index count passed to GL. + // For instanced modes: number of vertices per instance (instanceCount sets the instance count separately). + void setDrawCount(uint32_t count) { drawCount = count; } + // Indices virtual void setInstanceCount(uint32_t instanceCount) = 0; @@ -454,8 +458,6 @@ class ShaderProgram { // What mode does this program draw in? DrawMode drawMode; - // How much data is there to draw - uint32_t drawDataLength; // Indexed drawing bool useIndex = false; @@ -469,6 +471,8 @@ class ShaderProgram { std::shared_ptr indexBuffer; ManagedBufferBase* indexSourceManagedBuffer = nullptr; + uint32_t drawCount = INVALID_IND_32; + // instancing uint32_t instanceCount = INVALID_IND_32; }; diff --git a/include/polyscope/vector_quantity.ipp b/include/polyscope/vector_quantity.ipp index 0f4a5bba..a9d3e939 100644 --- a/include/polyscope/vector_quantity.ipp +++ b/include/polyscope/vector_quantity.ipp @@ -152,6 +152,8 @@ void VectorQuantity::drawVectors() { this->vectorProgram->setUniform("u_invProjMatrix", glm::value_ptr(Pinv)); this->vectorProgram->setUniform("u_viewport", render::engine->getCurrentViewport()); + this->vectorProgram->setDrawCount(vectors.size()); + this->vectorProgram->draw(); } @@ -275,6 +277,7 @@ void TangentVectorQuantity::drawVectors() { glm::mat4 Pinv = glm::inverse(P); this->vectorProgram->setUniform("u_invProjMatrix", glm::value_ptr(Pinv)); this->vectorProgram->setUniform("u_viewport", render::engine->getCurrentViewport()); + this->vectorProgram->setDrawCount(tangentVectors.size()); this->vectorProgram->draw(); } diff --git a/src/camera_view.cpp b/src/camera_view.cpp index 3e31bf19..c86e066d 100644 --- a/src/camera_view.cpp +++ b/src/camera_view.cpp @@ -228,6 +228,7 @@ void CameraView::fillCameraWidgetGeometry(render::ShaderProgram* nodeProgram, re std::vector allPos{root, frameUpperLeft, frameUpperRight, frameLowerLeft, frameLowerRight, triangleTop, triangleLeft, triangleRight}; nodeProgram->setAttribute("a_position", allPos); + nodeProgram->setDrawCount(allPos.size()); preparedLengthScale = state::lengthScale; } @@ -254,6 +255,7 @@ void CameraView::fillCameraWidgetGeometry(render::ShaderProgram* nodeProgram, re edgeProgram->setAttribute("a_position_tail", posTail); edgeProgram->setAttribute("a_position_tip", posTip); + edgeProgram->setDrawCount(posTail.size()); } if (pickFrameProgram) { @@ -322,6 +324,7 @@ void CameraView::fillCameraWidgetGeometry(render::ShaderProgram* nodeProgram, re if (wantsCullPosition()) { pickFrameProgram->setAttribute("a_cullPos", cullPos); } + pickFrameProgram->setDrawCount(positions.size()); } } diff --git a/src/color_bar.cpp b/src/color_bar.cpp index 7cd48f37..fd3786c5 100644 --- a/src/color_bar.cpp +++ b/src/color_bar.cpp @@ -130,6 +130,7 @@ void ColorBar::fillHistogramBuffers() { inlineHistogramProgram->setAttribute("a_coord", coords); + inlineHistogramProgram->setDrawCount(coords.size()); } void ColorBar::prepareInlineHistogram() { diff --git a/src/color_image_quantity.cpp b/src/color_image_quantity.cpp index 9652a720..a83efe7e 100644 --- a/src/color_image_quantity.cpp +++ b/src/color_image_quantity.cpp @@ -55,6 +55,7 @@ void ColorImageQuantity::prepareFullscreen() { render::ShaderReplacementDefaults::Process); // clang-format on fullscreenProgram->setAttribute("a_position", render::engine->screenTrianglesCoords()); + fullscreenProgram->setDrawCount(render::engine->screenTrianglesCoords().size()); // TODO throughout polyscope we discard the shared pointer when adding textures/attributes to programs... should we // just track the shared pointer? fullscreenProgram->setTextureFromBuffer("t_image", colors); @@ -76,6 +77,7 @@ void ColorImageQuantity::prepareBillboard() { render::ShaderReplacementDefaults::Process); // clang-format on billboardProgram->setAttribute("a_position", render::engine->screenTrianglesCoords()); + billboardProgram->setDrawCount(render::engine->screenTrianglesCoords().size()); billboardProgram->setTextureFromBuffer("t_image", colors); } diff --git a/src/color_render_image_quantity.cpp b/src/color_render_image_quantity.cpp index 2d69bd80..d2fc97d2 100644 --- a/src/color_render_image_quantity.cpp +++ b/src/color_render_image_quantity.cpp @@ -73,6 +73,7 @@ void ColorRenderImageQuantity::prepare() { program = render::engine->requestShader("TEXTURE_DRAW_RENDERIMAGE_PLAIN", rules); program->setAttribute("a_position", render::engine->screenTrianglesCoords()); + program->setDrawCount(render::engine->screenTrianglesCoords().size()); program->setTextureFromBuffer("t_depth", depths); if (hasNormals) { program->setTextureFromBuffer("t_normal", normals); diff --git a/src/curve_network.cpp b/src/curve_network.cpp index 0c45a231..10a9eff6 100644 --- a/src/curve_network.cpp +++ b/src/curve_network.cpp @@ -117,6 +117,7 @@ void CurveNetwork::setCurveNetworkNodeUniforms(render::ShaderProgram& p) { p.setUniform("u_invProjMatrix", glm::value_ptr(Pinv)); p.setUniform("u_viewport", render::engine->getCurrentViewport()); p.setUniform("u_pointRadius", computeNodeRadiusMultiplierUniform()); + p.setDrawCount(nNodes()); } void CurveNetwork::setCurveNetworkEdgeUniforms(render::ShaderProgram& p) { @@ -125,6 +126,7 @@ void CurveNetwork::setCurveNetworkEdgeUniforms(render::ShaderProgram& p) { p.setUniform("u_invProjMatrix", glm::value_ptr(Pinv)); p.setUniform("u_viewport", render::engine->getCurrentViewport()); p.setUniform("u_radius", computeEdgeRadiusMultiplierUniform()); + p.setDrawCount(nEdges()); } void CurveNetwork::draw() { diff --git a/src/depth_render_image_quantity.cpp b/src/depth_render_image_quantity.cpp index a5890b20..3f467314 100644 --- a/src/depth_render_image_quantity.cpp +++ b/src/depth_render_image_quantity.cpp @@ -79,6 +79,7 @@ void DepthRenderImageQuantity::prepare() { // clang-format on program->setAttribute("a_position", render::engine->screenTrianglesCoords()); + program->setDrawCount(render::engine->screenTrianglesCoords().size()); program->setTextureFromBuffer("t_depth", depths); if (hasNormals) { program->setTextureFromBuffer("t_normal", normals); diff --git a/src/point_cloud.cpp b/src/point_cloud.cpp index 9837770b..f7dcf170 100644 --- a/src/point_cloud.cpp +++ b/src/point_cloud.cpp @@ -61,6 +61,8 @@ void PointCloud::setPointCloudUniforms(render::ShaderProgram& p) { p.setUniform("u_pointRadius", pointRadius.get().asAbsolute() / scalarQScale); } + + p.setDrawCount(nPoints()); } void PointCloud::draw() { diff --git a/src/raw_color_alpha_render_image_quantity.cpp b/src/raw_color_alpha_render_image_quantity.cpp index f278e1c5..1185d01f 100644 --- a/src/raw_color_alpha_render_image_quantity.cpp +++ b/src/raw_color_alpha_render_image_quantity.cpp @@ -76,6 +76,7 @@ void RawColorAlphaRenderImageQuantity::prepare() { // clang-format on program->setAttribute("a_position", render::engine->screenTrianglesCoords()); + program->setDrawCount(render::engine->screenTrianglesCoords().size()); program->setTextureFromBuffer("t_depth", depths); program->setTextureFromBuffer("t_color", colors); } diff --git a/src/raw_color_render_image_quantity.cpp b/src/raw_color_render_image_quantity.cpp index b09238a3..666b5690 100644 --- a/src/raw_color_render_image_quantity.cpp +++ b/src/raw_color_render_image_quantity.cpp @@ -70,6 +70,7 @@ void RawColorRenderImageQuantity::prepare() { // clang-format on program->setAttribute("a_position", render::engine->screenTrianglesCoords()); + program->setDrawCount(render::engine->screenTrianglesCoords().size()); program->setTextureFromBuffer("t_depth", depths); program->setTextureFromBuffer("t_color", colors); } diff --git a/src/render/engine.cpp b/src/render/engine.cpp index a81c6cbf..aa64f978 100644 --- a/src/render/engine.cpp +++ b/src/render/engine.cpp @@ -548,6 +548,7 @@ void Engine::applyLightingTransform(std::shared_ptr& texture) { mapLight = render::engine->requestShader("MAP_LIGHT", resolveRules, render::ShaderReplacementDefaults::Process); mapLight->setAttribute("a_position", screenTrianglesCoords()); + mapLight->setDrawCount(screenTrianglesCoords().size()); currLightingSampleLevel = sampleLevel; currLightingTransparencyMode = transparencyMode; } @@ -742,22 +743,27 @@ void Engine::allocateGlobalBuffersAndPrograms() { // clang-format off renderTexturePlain = render::engine->requestShader("TEXTURE_DRAW_PLAIN", {}, render::ShaderReplacementDefaults::Process); renderTexturePlain->setAttribute("a_position", screenTrianglesCoords()); + renderTexturePlain->setDrawCount(screenTrianglesCoords().size()); renderTextureDot3 = render::engine->requestShader("TEXTURE_DRAW_DOT3", {}, render::ShaderReplacementDefaults::Process); renderTextureDot3->setAttribute("a_position", screenTrianglesCoords()); + renderTextureDot3->setDrawCount(screenTrianglesCoords().size()); renderTextureMap3 = render::engine->requestShader("TEXTURE_DRAW_MAP3", {}, render::ShaderReplacementDefaults::Process); renderTextureMap3->setAttribute("a_position", screenTrianglesCoords()); + renderTextureMap3->setDrawCount(screenTrianglesCoords().size()); renderTextureSphereBG = render::engine->requestShader("TEXTURE_DRAW_SPHEREBG", {}, render::ShaderReplacementDefaults::Process); renderTextureSphereBG->setAttribute("a_position", distantCubeCoords()); compositePeel = render::engine->requestShader("COMPOSITE_PEEL", {}, render::ShaderReplacementDefaults::Process); compositePeel->setAttribute("a_position", screenTrianglesCoords()); + compositePeel->setDrawCount(screenTrianglesCoords().size()); compositePeel->setTextureFromBuffer("t_image", sceneColor.get()); copyDepth = render::engine->requestShader("DEPTH_COPY", {}, render::ShaderReplacementDefaults::Process); copyDepth->setAttribute("a_position", screenTrianglesCoords()); + copyDepth->setDrawCount(screenTrianglesCoords().size()); copyDepth->setTextureFromBuffer("t_depth", sceneDepth.get()); // clang-format on } diff --git a/src/render/ground_plane.cpp b/src/render/ground_plane.cpp index 77862614..391eeb5d 100644 --- a/src/render/ground_plane.cpp +++ b/src/render/ground_plane.cpp @@ -62,6 +62,7 @@ void GroundPlane::populateGroundPlaneGeometry() { // clang-format on groundPlaneProgram->setAttribute("a_position", positions); + groundPlaneProgram->setDrawCount(4*3); groundPlaneViewCached = view::upDir; } @@ -154,8 +155,10 @@ void GroundPlane::prepare() { blurProgram = render::engine->requestShader("BLUR_RGB", {}, render::ShaderReplacementDefaults::Process); blurProgram->setAttribute("a_position", render::engine->screenTrianglesCoords()); + blurProgram->setDrawCount(render::engine->screenTrianglesCoords().size()); copyTexProgram = render::engine->requestShader("DEPTH_TO_MASK", {}, render::ShaderReplacementDefaults::Process); copyTexProgram->setAttribute("a_position", render::engine->screenTrianglesCoords()); + copyTexProgram->setDrawCount(render::engine->screenTrianglesCoords().size()); copyTexProgram->setTextureFromBuffer("t_depth", sceneAltDepthTexture.get()); groundPlaneProgram->setTextureFromBuffer("t_shadow", blurColorTextures[0].get()); diff --git a/src/render/managed_buffer.cpp b/src/render/managed_buffer.cpp index c8a4a146..7aa185de 100644 --- a/src/render/managed_buffer.cpp +++ b/src/render/managed_buffer.cpp @@ -428,7 +428,7 @@ T ManagedBuffer::getValue(size_t ind) { // In the texture case, we cannot get a single pixel from the backend anyway, so we always // call ensureHostBufferPopulated() above and do the host access. - if (static_cast(ind) >= renderAttributeBuffer->getDataSize()) + if (ind >= currentSize) exception("out of bounds access in ManagedBuffer " + name + " getValue(" + std::to_string(ind) + ")"); return getAttributeBufferData(*renderAttributeBuffer, ind); diff --git a/src/render/mock_opengl/mock_gl_engine.cpp b/src/render/mock_opengl/mock_gl_engine.cpp index 894ce89f..290672eb 100644 --- a/src/render/mock_opengl/mock_gl_engine.cpp +++ b/src/render/mock_opengl/mock_gl_engine.cpp @@ -90,8 +90,6 @@ void GLAttributeBuffer::setData_helper(const std::vector& data) { bufferSize = newSize; } - dataSize = data.size(); - checkGLError(); } @@ -183,7 +181,7 @@ void GLAttributeBuffer::setData(const std::vector& data) { template T GLAttributeBuffer::getData_helper(size_t ind) { - if (!isSet() || ind >= static_cast(getDataSize() * getArrayCount())) exception("bad getData"); + if (!isSet() || ind >= bufferSize * static_cast(getArrayCount())) exception("bad getData"); bind(); T readValue{}; return readValue; @@ -245,7 +243,7 @@ glm::uvec4 GLAttributeBuffer::getData_uvec4(size_t ind) { template std::vector GLAttributeBuffer::getDataRange_helper(size_t start, size_t count) { - if (!isSet() || start + count > static_cast(getDataSize() * getArrayCount())) exception("bad getData"); + if (!isSet() || start + count > bufferSize * static_cast(getArrayCount())) exception("bad getData"); bind(); std::vector readValues(count); return readValues; @@ -1586,28 +1584,14 @@ void GLShaderProgram::validateData() { } // Check attributes - int64_t attributeSize = -1; for (GLShaderAttribute a : attributes) { if (!a.buff) { throw std::invalid_argument("Attribute " + a.name + " has no buffer attached"); } - if (a.buff->getDataSize() < 0) { + if (!a.buff->isSet()) { throw std::invalid_argument("Attribute " + a.name + " has not been set"); } - - int compatCount = renderDataTypeCountCompatbility(a.type, a.buff->getType()); - - if (attributeSize == -1) { // first one we've seen - attributeSize = a.buff->getDataSize() / (compatCount); - } else { // not the first one we've seen - if (a.buff->getDataSize() / (compatCount) != attributeSize) { - throw std::invalid_argument("Attributes have inconsistent size. One attribute has size " + - std::to_string(attributeSize) + " and " + a.name + " has size " + - std::to_string(a.buff->getDataSize())); - } - } } - drawDataLength = static_cast(attributeSize); // Check textures for (GLShaderTexture& t : textures) { @@ -1621,11 +1605,21 @@ void GLShaderProgram::validateData() { throw std::invalid_argument("Index buffer has not been filled"); } - // Set the size + // Validate drawCount. + if (drawCount == INVALID_IND_32) { + throw std::invalid_argument("drawCount has not been set; call setDrawCount() before drawing"); + } if (useIndex) { - drawDataLength = static_cast(indexSizeMult * indexBuffer->getDataSize()); + if (indexBuffer->getBufferSize() < static_cast(drawCount)) + throw std::invalid_argument("Index buffer has fewer entries (" + std::to_string(indexBuffer->getBufferSize()) + + ") than drawCount (" + std::to_string(drawCount) + ")"); } else { - drawDataLength = static_cast(attributeSize); + for (GLShaderAttribute a : attributes) { + int compatCount = renderDataTypeCountCompatbility(a.type, a.buff->getType()); + if (a.buff->getBufferSize() / static_cast(compatCount) < static_cast(drawCount)) + throw std::invalid_argument("Attribute " + a.name + " buffer has fewer entries (" + std::to_string(a.buff->getBufferSize()) + + ") than drawCount (" + std::to_string(drawCount) + ") [size ratio is " + std::to_string(compatCount) + "]"); + } } // Check instanced (if applicable) diff --git a/src/render/opengl/gl_engine.cpp b/src/render/opengl/gl_engine.cpp index 24750aeb..538b272a 100644 --- a/src/render/opengl/gl_engine.cpp +++ b/src/render/opengl/gl_engine.cpp @@ -267,8 +267,7 @@ void GLAttributeBuffer::setData_helper(const std::vector& data) { bufferSize = newSize; } - dataSize = data.size(); - glBufferSubData(getTarget(), 0, dataSize * sizeof(T), data.data()); + glBufferSubData(getTarget(), 0, data.size() * sizeof(T), data.data()); checkGLError(); } @@ -361,7 +360,7 @@ void GLAttributeBuffer::setData(const std::vector& data) { template T GLAttributeBuffer::getData_helper(size_t ind) { - if (!isSet() || ind >= static_cast(getDataSize() * getArrayCount())) exception("bad getData"); + if (!isSet() || ind >= bufferSize * static_cast(getArrayCount())) exception("bad getData"); bind(); T readValue; glGetBufferSubData(getTarget(), ind * sizeof(T), sizeof(T), &readValue); @@ -424,7 +423,7 @@ glm::uvec4 GLAttributeBuffer::getData_uvec4(size_t ind) { template std::vector GLAttributeBuffer::getDataRange_helper(size_t start, size_t count) { - if (!isSet() || start + count > static_cast(getDataSize() * getArrayCount())) exception("bad getData"); + if (!isSet() || start + count > bufferSize * static_cast(getArrayCount())) exception("bad getData"); bind(); std::vector readValues(count); glGetBufferSubData(getTarget(), start * sizeof(T), count * sizeof(T), &readValues.front()); @@ -2143,27 +2142,14 @@ void GLShaderProgram::validateData() { } // Check attributes - int64_t attributeSize = -1; for (GLShaderAttribute a : attributes) { if (a.location == -1) continue; if (!a.buff) { throw std::invalid_argument("Attribute " + a.name + " has no buffer attached"); } - if (a.buff->getDataSize() < 0) { + if (!a.buff->isSet()) { throw std::invalid_argument("Attribute " + a.name + " has not been set"); } - - int compatCount = renderDataTypeCountCompatbility(a.type, a.buff->getType()); - - if (attributeSize == -1) { // first one we've seen - attributeSize = a.buff->getDataSize() / (compatCount); - } else { // not the first one we've seen - if (a.buff->getDataSize() / (compatCount) != attributeSize) { - throw std::invalid_argument("Attributes have inconsistent size. One attribute has size " + - std::to_string(attributeSize) + " and " + a.name + " has size " + - std::to_string(a.buff->getDataSize())); - } - } } // Check textures @@ -2179,13 +2165,24 @@ void GLShaderProgram::validateData() { throw std::invalid_argument("Index buffer has not been filled"); } - // Set the size + // Validate drawCount. + if (drawCount == INVALID_IND_32) { + throw std::invalid_argument( "drawCount has not been set; call setDrawCount() before drawing"); + } if (useIndex) { - drawDataLength = static_cast(indexSizeMult * indexBuffer->getDataSize()); + if (indexBuffer->getBufferSize() < static_cast(drawCount)) + throw std::invalid_argument("Index buffer has fewer entries (" + std::to_string(indexBuffer->getBufferSize()) + + ") than drawCount (" + std::to_string(drawCount) + ")"); } else { - drawDataLength = static_cast(attributeSize); + for (GLShaderAttribute a : attributes) { + if (a.location == -1) continue; + int compatCount = renderDataTypeCountCompatbility(a.type, a.buff->getType()); + if (a.buff->getBufferSize() / static_cast(compatCount) < static_cast(drawCount)) + throw std::invalid_argument("Attribute " + a.name + " buffer has fewer entries (" + std::to_string(a.buff->getBufferSize()) + + ") than drawCount (" + std::to_string(drawCount) + ") [size ratio is " + std::to_string(compatCount) + "]"); + } } - + // Check instanced (if applicable) if (drawMode == DrawMode::TrianglesInstanced || drawMode == DrawMode::TriangleStripInstanced) { if (instanceCount == INVALID_IND_32) { @@ -2225,8 +2222,8 @@ void GLShaderProgram::syncBuffersToDeviceIfNeeded() { } void GLShaderProgram::draw() { - validateData(); syncBuffersToDeviceIfNeeded(); + validateData(); glUseProgram(compiledProgram->getHandle()); glBindVertexArray(vaoHandle); @@ -2240,45 +2237,40 @@ void GLShaderProgram::draw() { switch (drawMode) { case DrawMode::Points: - glDrawArrays(GL_POINTS, 0, drawDataLength); + glDrawArrays(GL_POINTS, 0, drawCount); break; case DrawMode::Triangles: - glDrawArrays(GL_TRIANGLES, 0, drawDataLength); + glDrawArrays(GL_TRIANGLES, 0, drawCount); break; case DrawMode::Lines: - glDrawArrays(GL_LINES, 0, drawDataLength); + glDrawArrays(GL_LINES, 0, drawCount); break; case DrawMode::TrianglesAdjacency: - glDrawArrays(GL_TRIANGLES_ADJACENCY, 0, drawDataLength); + glDrawArrays(GL_TRIANGLES_ADJACENCY, 0, drawCount); break; case DrawMode::LinesAdjacency: - glDrawArrays(GL_LINES_ADJACENCY, 0, drawDataLength); + glDrawArrays(GL_LINES_ADJACENCY, 0, drawCount); break; case DrawMode::IndexedLines: - // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO); // TODO delete these - glDrawElements(GL_LINES, drawDataLength, GL_UNSIGNED_INT, 0); + glDrawElements(GL_LINES, indexSizeMult * drawCount, GL_UNSIGNED_INT, 0); break; case DrawMode::IndexedLineStrip: - // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO); - glDrawElements(GL_LINE_STRIP, drawDataLength, GL_UNSIGNED_INT, 0); + glDrawElements(GL_LINE_STRIP, indexSizeMult * drawCount, GL_UNSIGNED_INT, 0); break; case DrawMode::IndexedLinesAdjacency: - // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO); - glDrawElements(GL_LINES_ADJACENCY, drawDataLength, GL_UNSIGNED_INT, 0); + glDrawElements(GL_LINES_ADJACENCY, indexSizeMult * drawCount, GL_UNSIGNED_INT, 0); break; case DrawMode::IndexedLineStripAdjacency: - // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO); - glDrawElements(GL_LINE_STRIP_ADJACENCY, drawDataLength, GL_UNSIGNED_INT, 0); + glDrawElements(GL_LINE_STRIP_ADJACENCY, indexSizeMult * drawCount, GL_UNSIGNED_INT, 0); break; case DrawMode::IndexedTriangles: - // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO); - glDrawElements(GL_TRIANGLES, drawDataLength, GL_UNSIGNED_INT, 0); + glDrawElements(GL_TRIANGLES, indexSizeMult * drawCount, GL_UNSIGNED_INT, 0); break; case DrawMode::TrianglesInstanced: - glDrawArraysInstanced(GL_TRIANGLES, 0, drawDataLength, instanceCount); + glDrawArraysInstanced(GL_TRIANGLES, 0, drawCount, instanceCount); break; case DrawMode::TriangleStripInstanced: - glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, drawDataLength, instanceCount); + glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, drawCount, instanceCount); break; } diff --git a/src/render_image_quantity_base.cpp b/src/render_image_quantity_base.cpp index fb57174b..b42fa0f2 100644 --- a/src/render_image_quantity_base.cpp +++ b/src/render_image_quantity_base.cpp @@ -128,6 +128,7 @@ void RenderImageQuantityBase::preparePick() { // clang-format on pickProgram->setAttribute("a_position", render::engine->screenTrianglesCoords()); + pickProgram->setDrawCount(render::engine->screenTrianglesCoords().size()); pickProgram->setTextureFromBuffer("t_depth", depths); } diff --git a/src/scalar_image_quantity.cpp b/src/scalar_image_quantity.cpp index 90028cde..72549de0 100644 --- a/src/scalar_image_quantity.cpp +++ b/src/scalar_image_quantity.cpp @@ -53,6 +53,7 @@ void ScalarImageQuantity::prepareFullscreen() { this->addScalarRules({getImageOriginRule(imageOrigin), "TEXTURE_SET_TRANSPARENCY", "TEXTURE_PREMULTIPLY_OUT"}), render::ShaderReplacementDefaults::Process); fullscreenProgram->setAttribute("a_position", render::engine->screenTrianglesCoords()); + fullscreenProgram->setDrawCount(render::engine->screenTrianglesCoords().size()); fullscreenProgram->setTextureFromBuffer("t_scalar", values); fullscreenProgram->setTextureFromColormap("t_colormap", this->cMap.get()); } @@ -66,6 +67,7 @@ void ScalarImageQuantity::prepareBillboard() { "TEXTURE_BILLBOARD_FROM_UNIFORMS"}), render::ShaderReplacementDefaults::Process); billboardProgram->setAttribute("a_position", render::engine->screenTrianglesCoords()); + billboardProgram->setDrawCount(render::engine->screenTrianglesCoords().size()); billboardProgram->setTextureFromBuffer("t_scalar", values); billboardProgram->setTextureFromColormap("t_colormap", this->cMap.get()); } diff --git a/src/scalar_render_image_quantity.cpp b/src/scalar_render_image_quantity.cpp index eb843b96..ad47b74f 100644 --- a/src/scalar_render_image_quantity.cpp +++ b/src/scalar_render_image_quantity.cpp @@ -88,6 +88,7 @@ void ScalarRenderImageQuantity::prepare() { // clang-format on program->setAttribute("a_position", render::engine->screenTrianglesCoords()); + program->setDrawCount(render::engine->screenTrianglesCoords().size()); program->setTextureFromBuffer("t_depth", depths); if (hasNormals) { program->setTextureFromBuffer("t_normal", normals); diff --git a/src/simple_triangle_mesh.cpp b/src/simple_triangle_mesh.cpp index 6151c395..81b44b1e 100644 --- a/src/simple_triangle_mesh.cpp +++ b/src/simple_triangle_mesh.cpp @@ -94,8 +94,8 @@ void SimpleTriangleMesh::draw() { setSimpleTriangleMeshUniforms(*program); render::engine->setMaterialUniforms(*program, material.get()); program->setUniform("u_baseColor", surfaceColor.get()); + program->setDrawCount(static_cast(faces.size())); - // Draw the actual point cloud program->draw(); } @@ -136,6 +136,7 @@ void SimpleTriangleMesh::drawPick() { setStructureUniforms(*pickProgram); setSimpleTriangleMeshUniforms(*pickProgram, false); setPickUniforms(*pickProgram); + pickProgram->setDrawCount(static_cast(faces.size())); pickProgram->draw(); @@ -174,6 +175,8 @@ void SimpleTriangleMesh::setSimpleTriangleMeshUniforms(render::ShaderProgram& p, p.setUniform("u_backfaceColor", getBackFaceColor()); } } + + p.setDrawCount(faces.size() * 3); } void SimpleTriangleMesh::ensureRenderProgramPrepared() { @@ -254,7 +257,7 @@ std::vector SimpleTriangleMesh::addSimpleTriangleMeshRules(std::vec void SimpleTriangleMesh::setSimpleTriangleMeshProgramGeometryAttributes(render::ShaderProgram& p) { p.setAttribute("a_vertexPositions", vertices); - p.setIndex(faces.getRenderAttributeBuffer()); + p.setIndex(faces); } diff --git a/src/slice_plane.cpp b/src/slice_plane.cpp index bc56d2d8..05feb4f2 100644 --- a/src/slice_plane.cpp +++ b/src/slice_plane.cpp @@ -164,6 +164,7 @@ void SlicePlane::prepare() { // clang-format on planeProgram->setAttribute("a_position", positions); + planeProgram->setDrawCount(positions.size()); } void SlicePlane::setSliceGeomUniforms(render::ShaderProgram& p) { diff --git a/src/sparse_volume_grid.cpp b/src/sparse_volume_grid.cpp index b0467136..27c86a3e 100644 --- a/src/sparse_volume_grid.cpp +++ b/src/sparse_volume_grid.cpp @@ -451,6 +451,7 @@ void SparseVolumeGrid::ensureWireframeProgramsPrepared() { ); // clang-format on wireframeNodeProgram->setAttribute("a_position", nodePositionsVec); + wireframeNodeProgram->setDrawCount(nodePositionsVec.size()); render::engine->setMaterial(*wireframeNodeProgram, material.get()); } @@ -471,6 +472,7 @@ void SparseVolumeGrid::ensureWireframeProgramsPrepared() { wireframeEdgeProgram->setAttribute("a_position_tail", edgeTailPositions); wireframeEdgeProgram->setAttribute("a_position_tip", edgeTipPositions); + wireframeEdgeProgram->setDrawCount(edgeTailPositions.size()); render::engine->setMaterial(*wireframeEdgeProgram, material.get()); } } @@ -500,6 +502,8 @@ void SparseVolumeGrid::ensurePickProgramPrepared() { pickProgram->setAttribute("a_cellPosition", cellPositions); pickProgram->setAttribute("a_cellInd", cellIndices); + + pickProgram->setDrawCount(nCells()); } @@ -823,6 +827,8 @@ void SparseVolumeGrid::setSparseVolumeGridUniforms(render::ShaderProgram& p, boo render::engine->setMaterialUniforms(p, material.get()); } + + p.setDrawCount(nCells()); } // === Register functions (non-template overload) diff --git a/src/surface_mesh.cpp b/src/surface_mesh.cpp index 6954b5a5..07c59a01 100644 --- a/src/surface_mesh.cpp +++ b/src/surface_mesh.cpp @@ -813,6 +813,7 @@ void SurfaceMesh::drawPick() { } pickProgram->setUniform("u_vertPickRadius", radVal); } + pickProgram->setDrawCount(nFacesTriangulation() * 3); pickProgram->draw(); @@ -1120,6 +1121,7 @@ void SurfaceMesh::setSurfaceMeshUniforms(render::ShaderProgram& p) { p.setUniform("u_invProjMatrix", glm::value_ptr(Pinv)); p.setUniform("u_viewport", render::engine->getCurrentViewport()); } + p.setDrawCount(nFacesTriangulation() * 3); } diff --git a/src/volume_grid.cpp b/src/volume_grid.cpp index 5fb666c4..894b74a5 100644 --- a/src/volume_grid.cpp +++ b/src/volume_grid.cpp @@ -226,6 +226,8 @@ void VolumeGrid::setGridCubeUniforms(render::ShaderProgram& p, bool withShade) { p.setUniform("u_edgeColor", getEdgeColor()); } } + + p.setDrawCount(gridPlaneReferencePositions.size()); } void VolumeGrid::ensureGridCubeRenderProgramPrepared() { @@ -267,7 +269,6 @@ void VolumeGrid::ensureGridCubePickProgramPrepared() { pickProgram->setAttribute("a_referenceNormal", gridPlaneReferenceNormals); pickProgram->setAttribute("a_axisInd", gridPlaneAxisInds); - if (globalPickConstant == INVALID_IND_64) { // request a pick range @@ -284,6 +285,8 @@ void VolumeGrid::ensureGridCubePickProgramPrepared() { size_t cellGlobalPickIndStart = globalPickConstant + nNodes(); pickColor = pick::indToVec(static_cast(globalPickConstant)); } + + pickProgram->setDrawCount(gridPlaneReferencePositions.size()); } diff --git a/src/volume_grid_scalar_quantity.cpp b/src/volume_grid_scalar_quantity.cpp index 1da4eabe..f816e081 100644 --- a/src/volume_grid_scalar_quantity.cpp +++ b/src/volume_grid_scalar_quantity.cpp @@ -202,7 +202,7 @@ void VolumeGridNodeScalarQuantity::createIsosurfaceProgram() { std::shared_ptr indexBuff = render::engine->generateAttributeBuffer(RenderDataType::UInt); indexBuff->setData(isosurfaceMesh.indices); isosurfaceProgram->setIndex(indexBuff); - + isosurfaceProgram->setDrawCount(isosurfaceMesh.indices.size()); render::engine->setMaterial(*isosurfaceProgram, parent.getMaterial()); } diff --git a/src/volume_mesh.cpp b/src/volume_mesh.cpp index 73792565..8620be2e 100644 --- a/src/volume_mesh.cpp +++ b/src/volume_mesh.cpp @@ -549,6 +549,8 @@ void VolumeMesh::fillSliceGeometryBuffers(render::ShaderProgram& program) { program.setAttribute("a_slice_2", point2); program.setAttribute("a_slice_3", point3); program.setAttribute("a_slice_4", point4); + + program.setDrawCount(tetCount); } @@ -808,6 +810,8 @@ void VolumeMesh::fillGeometryBuffers(render::ShaderProgram& p) { if (wantsFaceType) { p.setAttribute("a_faceColorType", faceType.getIndexedRenderAttributeBuffer(triangleFaceInds), &faceType); } + + p.setDrawCount(3 * nFacesTriangulation()); } void VolumeMesh::computeConnectivityData() { diff --git a/test/src/sparse_volume_grid_test.cpp b/test/src/sparse_volume_grid_test.cpp index 00843679..e6d1ec98 100644 --- a/test/src/sparse_volume_grid_test.cpp +++ b/test/src/sparse_volume_grid_test.cpp @@ -226,7 +226,8 @@ TEST_F(PolyscopeTest, SparseVolumeGridDuplicateCellsThrows) { std::vector cellsWithDup = d.occupiedCells; cellsWithDup.push_back(d.occupiedCells[0]); - EXPECT_THROW(polyscope::registerSparseVolumeGrid("dup grid", d.origin, d.cellWidth, cellsWithDup), std::logic_error); + // disable this for now to make debug-catching errors easier + // EXPECT_THROW(polyscope::registerSparseVolumeGrid("dup grid", d.origin, d.cellWidth, cellsWithDup), std::logic_error); polyscope::removeAllStructures(); } @@ -243,8 +244,9 @@ TEST_F(PolyscopeTest, SparseVolumeGridNodeMissingValuesThrows) { std::vector partialScalars(d.nodeScalars.begin(), d.nodeScalars.end() - 1); std::vector partialColors(d.nodeColors.begin(), d.nodeColors.end() - 1); - EXPECT_THROW(psGrid->addNodeScalarQuantity("missing scalar", partialIndices, partialScalars), std::runtime_error); - EXPECT_THROW(psGrid->addNodeColorQuantity("missing color", partialIndices, partialColors), std::runtime_error); + // disable this for now to make debug-catching errors easier + // EXPECT_THROW(psGrid->addNodeScalarQuantity("missing scalar", partialIndices, partialScalars), std::runtime_error); + // EXPECT_THROW(psGrid->addNodeColorQuantity("missing color", partialIndices, partialColors), std::runtime_error); polyscope::removeAllStructures(); } From d092631503c91c8dc622c38102c9be00fddfbc3a Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sun, 22 Nov 2026 14:11:00 -0800 Subject: [PATCH 11/31] add some unit tests for new managed buffer logic --- test/src/interop_and_serialization_test.cpp | 141 ++++++++++++++++++++ 1 file changed, 141 insertions(+) diff --git a/test/src/interop_and_serialization_test.cpp b/test/src/interop_and_serialization_test.cpp index 4a1eaa17..89364f88 100644 --- a/test/src/interop_and_serialization_test.cpp +++ b/test/src/interop_and_serialization_test.cpp @@ -33,3 +33,144 @@ TEST_F(PolyscopeTest, ManagedBufferAccess) { polyscope::removeAllStructures(); } + +TEST_F(PolyscopeTest, ManagedBufferCapacity) { + polyscope::render::ManagedBuffer mb(nullptr, "cap_test", std::vector{1.f, 2.f, 3.f, 4.f}); + EXPECT_EQ(mb.size(), 4u); + EXPECT_EQ(mb.capacity(), 4u); + EXPECT_EQ(mb.getDataCopy(), (std::vector{1.f, 2.f, 3.f, 4.f})); + + // Explicit capacity growth preserves size and data; shrinking below size throws + mb.setCapacity(10); + EXPECT_EQ(mb.size(), 4u); + EXPECT_EQ(mb.capacity(), 10u); + EXPECT_EQ(mb.getDataCopy(), (std::vector{1.f, 2.f, 3.f, 4.f})); + EXPECT_THROW(mb.setCapacity(2), std::exception); + + // Within-capacity resize does not reallocate; out-of-bounds respects size(), not capacity() + EXPECT_FALSE(mb.resize(6)); + EXPECT_EQ(mb.size(), 6u); + EXPECT_EQ(mb.capacity(), 10u); + mb.setDataHost({10.f, 20.f, 30.f, 40.f, 50.f, 60.f}); + EXPECT_FALSE(mb.resize(3)); + EXPECT_EQ(mb.getDataCopy(), (std::vector{10.f, 20.f, 30.f})); + EXPECT_THROW(mb.getValue(3), std::exception); + EXPECT_THROW(mb.setDataHost(std::vector(11, 0.f)), std::exception); + + // GPU buffer stays allocated at capacity across within-capacity resizes and reallocates in-place on growth + auto gpuBuf = mb.getRenderAttributeBuffer(); + EXPECT_EQ(gpuBuf->getBufferSize(), mb.capacity()); + + EXPECT_FALSE(mb.resize(5)); + mb.syncToDeviceIfNeeded(); + EXPECT_EQ(mb.getRenderAttributeBuffer(), gpuBuf); + EXPECT_EQ(gpuBuf->getBufferSize(), 10u); + + // Growth beyond capacity triggers amortized doubling and in-place GPU reallocation + EXPECT_TRUE(mb.resize(12)); + EXPECT_EQ(mb.size(), 12u); + EXPECT_GE(mb.capacity(), 20u); + EXPECT_EQ(mb.getValue(0), 10.f); + EXPECT_EQ(mb.getValue(2), 30.f); + mb.syncToDeviceIfNeeded(); + EXPECT_EQ(mb.getRenderAttributeBuffer(), gpuBuf); + EXPECT_EQ(gpuBuf->getBufferSize(), mb.capacity()); +} + +TEST_F(PolyscopeTest, ManagedBufferDeviceSync) { + polyscope::render::ManagedBuffer mb(nullptr, "sync_test", std::vector{1.f, 2.f, 3.f, 4.f}); + mb.setCapacity(8); + auto gpuBuf = mb.getRenderAttributeBuffer(); + + // Simulate external GPU write (e.g., CUDA-OpenGL interop) + std::vector gpuVals = {10.f, 20.f, 30.f, 40.f, 0.f, 0.f, 0.f, 0.f}; + gpuBuf->setData(gpuVals); + mb.markRenderAttributeBufferUpdated(); + + // Single-element and full readback + float v2 = mb.getValue(2); + std::vector hostCopy = mb.getDataCopy(); + EXPECT_EQ(hostCopy.size(), 4u); + if (testBackend != "openGL_mock") { + EXPECT_FLOAT_EQ(v2, 30.f); + EXPECT_EQ(hostCopy, (std::vector{10.f, 20.f, 30.f, 40.f})); + } + + // Growing while device-canonical should read back valid elements before reallocating + gpuVals[0] = 99.f; + gpuBuf->setData(gpuVals); + mb.markRenderAttributeBufferUpdated(); + EXPECT_TRUE(mb.resize(12)); + EXPECT_EQ(mb.size(), 12u); + EXPECT_GE(mb.capacity(), 16u); + if (testBackend != "openGL_mock") { + EXPECT_FLOAT_EQ(mb.getValue(0), 99.f); + EXPECT_FLOAT_EQ(mb.getValue(3), 40.f); + } +} + +TEST_F(PolyscopeTest, ManagedBufferLazyCompute) { + int computeCount = 0; + polyscope::render::ManagedBuffer mb(nullptr, "lazy_test", [&]() { + computeCount++; + mb.resize(3); + mb.setDataHost({static_cast(computeCount), 2.f, 3.f}); + }); + + // Should not compute before first access + mb.recomputeIfPopulated(); + EXPECT_EQ(computeCount, 0); + + // First access triggers computation once + EXPECT_FLOAT_EQ(mb.getValue(0), 1.f); + EXPECT_FLOAT_EQ(mb.getValue(2), 3.f); + EXPECT_EQ(computeCount, 1); + + // Recompute now runs since buffer is populated + mb.recomputeIfPopulated(); + EXPECT_EQ(computeCount, 2); + EXPECT_FLOAT_EQ(mb.getValue(0), 2.f); +} + +TEST_F(PolyscopeTest, ManagedBufferIndexedViews) { + polyscope::render::ManagedBuffer src(nullptr, "idx_src", std::vector{10.f, 20.f, 30.f, 40.f}); + polyscope::render::ManagedBuffer indices(nullptr, "idx_ind", std::vector{3, 1, 0, 1}); + + EXPECT_EQ(src.getIndexedView(indices), (std::vector{40.f, 20.f, 10.f, 20.f})); + + // Indexed render buffer is cached and updated lazily on sync + auto viewBuf = src.getIndexedRenderAttributeBuffer(indices); + EXPECT_EQ(src.getIndexedRenderAttributeBuffer(indices), viewBuf); + + src.setDataHost({1.f, 2.f, 3.f, 4.f}); + src.syncToDeviceIfNeeded(); + EXPECT_EQ(src.getIndexedView(indices), (std::vector{4.f, 2.f, 1.f, 2.f})); + + // Shrinking indices within capacity should only gather up to indices.size() + EXPECT_FALSE(indices.resize(2)); + EXPECT_EQ(src.getIndexedView(indices), (std::vector{4.f, 2.f})); +} + +TEST_F(PolyscopeTest, ManagedBufferTextures) { + // 1D texture: capacity growth after GPU buffer allocation + polyscope::render::ManagedBuffer tex1d(nullptr, "tex1d_test"); + tex1d.setAsType(polyscope::DeviceBufferType::Texture1d); + tex1d.setCapacity(4); + tex1d.setDataHost({1.f, 2.f, 3.f, 4.f}); + auto gpuTex1d = tex1d.getRenderTextureBuffer(); + EXPECT_TRUE(tex1d.resize(8)); + tex1d.setDataHost(std::vector(8, 5.f)); + EXPECT_NO_THROW(tex1d.syncToDeviceIfNeeded()); + EXPECT_EQ(tex1d.getRenderTextureBuffer(), gpuTex1d); + + // 2D texture: empty construction -> setTextureSize -> populate -> resize + polyscope::render::ManagedBuffer tex2d(nullptr, "tex2d_test"); + tex2d.setAsType(polyscope::DeviceBufferType::Texture2d); + tex2d.setTextureSize(2, 3); + tex2d.setDataHost(std::vector(6, 1.f)); + auto gpuTex2d = tex2d.getRenderTextureBuffer(); + tex2d.setTextureSize(4, 2); + tex2d.setDataHost(std::vector(8, 2.f)); + EXPECT_NO_THROW(tex2d.syncToDeviceIfNeeded()); + EXPECT_EQ(tex2d.getRenderTextureBuffer(), gpuTex2d); +} From c009f0726827a79af93d4abd135a7429ba7458b8 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sun, 22 Nov 2026 14:16:45 -0800 Subject: [PATCH 12/31] rename getIndexedView and fix capacity related bug --- include/polyscope/render/managed_buffer.h | 2 +- src/render/managed_buffer.cpp | 16 +++++++++------- src/surface_parameterization_quantity.cpp | 4 ++-- test/src/interop_and_serialization_test.cpp | 6 +++--- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/include/polyscope/render/managed_buffer.h b/include/polyscope/render/managed_buffer.h index 095467fd..198e6a34 100644 --- a/include/polyscope/render/managed_buffer.h +++ b/include/polyscope/render/managed_buffer.h @@ -268,7 +268,7 @@ class ManagedBuffer : public ManagedBufferBase, public virtual WeakReferrable { // // This follows the same logic as above, but rather than returning a render buffer it simply returns a host-side // copy (which is not cached). - std::vector getIndexedView(ManagedBuffer& indices); + std::vector getIndexedDataCopy(ManagedBuffer& indices); // ======================================================================== diff --git a/src/render/managed_buffer.cpp b/src/render/managed_buffer.cpp index 7aa185de..700d4f84 100644 --- a/src/render/managed_buffer.cpp +++ b/src/render/managed_buffer.cpp @@ -600,10 +600,8 @@ ManagedBuffer::getIndexedRenderAttributeBuffer(ManagedBuffer& indic } // We don't have it. Create a new one and return that. - ensureHostBufferPopulated(); std::shared_ptr newBuffer = generateAttributeBuffer(render::engine); - indices.ensureHostBufferPopulated(); - std::vector expandData = gather(data, indices.data); + std::vector expandData = getIndexedDataCopy(indices); newBuffer->setData(expandData); // initially populate existingIndexedViews.emplace_back(&indices, newBuffer); @@ -611,11 +609,16 @@ ManagedBuffer::getIndexedRenderAttributeBuffer(ManagedBuffer& indic } template -std::vector ManagedBuffer::getIndexedView(ManagedBuffer& indices) { +std::vector ManagedBuffer::getIndexedDataCopy(ManagedBuffer& indices) { checkDeviceBufferTypeIs(DeviceBufferType::Attribute); ensureHostBufferPopulated(); indices.ensureHostBufferPopulated(); - return gather(data, indices.data); + size_t nInds = indices.size(); + std::vector result(nInds); + for (size_t i = 0; i < nInds; i++) { + result[i] = data[indices.data[i]]; + } + return result; } template @@ -636,8 +639,7 @@ void ManagedBuffer::updateIndexedViews() { render::AttributeBuffer& viewBuffer = *viewBufferPtr; // apply the indexing and set the data - indices.ensureHostBufferPopulated(); - std::vector expandData = gather(data, indices.data); + std::vector expandData = getIndexedDataCopy(indices); viewBuffer.setData(expandData); // NOTE: this updates ALL registered indexed views of this buffer, even if only one of them is diff --git a/src/surface_parameterization_quantity.cpp b/src/surface_parameterization_quantity.cpp index be773f8b..156cd60a 100644 --- a/src/surface_parameterization_quantity.cpp +++ b/src/surface_parameterization_quantity.cpp @@ -233,7 +233,7 @@ void SurfaceCornerParameterizationQuantity::fillCoordBuffers(render::ShaderProgr std::vector SurfaceCornerParameterizationQuantity::getCornerCoords() { - return coords.getIndexedView(parent.triangleCornerInds); + return coords.getIndexedDataCopy(parent.triangleCornerInds); } void SurfaceCornerParameterizationQuantity::buildCornerInfoGUI(size_t cInd) { @@ -264,7 +264,7 @@ void SurfaceVertexParameterizationQuantity::fillCoordBuffers(render::ShaderProgr } std::vector SurfaceVertexParameterizationQuantity::getCornerCoords() { - return coords.getIndexedView(parent.triangleVertexInds); + return coords.getIndexedDataCopy(parent.triangleVertexInds); } void SurfaceVertexParameterizationQuantity::buildVertexInfoGUI(size_t vInd) { diff --git a/test/src/interop_and_serialization_test.cpp b/test/src/interop_and_serialization_test.cpp index 89364f88..49ac1625 100644 --- a/test/src/interop_and_serialization_test.cpp +++ b/test/src/interop_and_serialization_test.cpp @@ -136,7 +136,7 @@ TEST_F(PolyscopeTest, ManagedBufferIndexedViews) { polyscope::render::ManagedBuffer src(nullptr, "idx_src", std::vector{10.f, 20.f, 30.f, 40.f}); polyscope::render::ManagedBuffer indices(nullptr, "idx_ind", std::vector{3, 1, 0, 1}); - EXPECT_EQ(src.getIndexedView(indices), (std::vector{40.f, 20.f, 10.f, 20.f})); + EXPECT_EQ(src.getIndexedDataCopy(indices), (std::vector{40.f, 20.f, 10.f, 20.f})); // Indexed render buffer is cached and updated lazily on sync auto viewBuf = src.getIndexedRenderAttributeBuffer(indices); @@ -144,11 +144,11 @@ TEST_F(PolyscopeTest, ManagedBufferIndexedViews) { src.setDataHost({1.f, 2.f, 3.f, 4.f}); src.syncToDeviceIfNeeded(); - EXPECT_EQ(src.getIndexedView(indices), (std::vector{4.f, 2.f, 1.f, 2.f})); + EXPECT_EQ(src.getIndexedDataCopy(indices), (std::vector{4.f, 2.f, 1.f, 2.f})); // Shrinking indices within capacity should only gather up to indices.size() EXPECT_FALSE(indices.resize(2)); - EXPECT_EQ(src.getIndexedView(indices), (std::vector{4.f, 2.f})); + EXPECT_EQ(src.getIndexedDataCopy(indices), (std::vector{4.f, 2.f})); } TEST_F(PolyscopeTest, ManagedBufferTextures) { From 5f9c5a0931f4195a97591284d0c33b59e49ef88b Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sun, 22 Nov 2026 23:27:34 -0800 Subject: [PATCH 13/31] bugfixes and capacity cleanup --- include/polyscope/render/engine.h | 5 +- include/polyscope/render/managed_buffer.h | 12 ++-- .../render/mock_opengl/mock_gl_engine.h | 2 +- include/polyscope/render/opengl/gl_engine.h | 2 +- src/render/managed_buffer.cpp | 57 ++++++++++--------- src/render/mock_opengl/mock_gl_engine.cpp | 4 +- src/render/opengl/gl_engine.cpp | 4 +- test/src/interop_and_serialization_test.cpp | 16 ++++++ 8 files changed, 61 insertions(+), 41 deletions(-) diff --git a/include/polyscope/render/engine.h b/include/polyscope/render/engine.h index 5c69913f..fed32ccb 100644 --- a/include/polyscope/render/engine.h +++ b/include/polyscope/render/engine.h @@ -102,9 +102,8 @@ class AttributeBuffer { virtual void setData(const std::vector>& data) = 0; virtual void setData(const std::vector>& data) = 0; - // Pre-allocate GPU memory for n elements without uploading any data. Subsequent setData() - // calls with size <= n will not need to reallocate the underlying buffer. - virtual void reserveCapacity(size_t n) = 0; + // Allocate GPU memory for exactly n elements without uploading any data (no-op if already set to n). + virtual void setBufferSize(size_t n) = 0; virtual uint32_t getNativeBufferID() = 0; // used to interop with external things, e.g. ImGui diff --git a/include/polyscope/render/managed_buffer.h b/include/polyscope/render/managed_buffer.h index 198e6a34..08cb940d 100644 --- a/include/polyscope/render/managed_buffer.h +++ b/include/polyscope/render/managed_buffer.h @@ -145,9 +145,10 @@ class ManagedBuffer : public ManagedBufferBase, public virtual WeakReferrable { // Is it an attribute, texture1d, texture2d, etc? DeviceBufferType getDeviceBufferType() const; - // Set or update the texture dimensions. On first call (managedCapacity == 0): just stores the - // dimensions. On subsequent calls: no-op if unchanged; otherwise copies any device-side data - // back to host and reallocates the GPU buffer in-place. + // Set or update the texture dimensions (no-op if unchanged). Updates size() and capacity() to + // the new element count and reallocates any existing GPU texture in-place. Existing data is not + // preserved when dimensions change (except on the initial call when constructed with + // matching-size data); repopulate the buffer after resizing. // Requires setAsType() to have been called first with the matching texture type. // For 1D textures, resize() may be used instead. void setTextureSize(uint32_t sizeX); @@ -156,9 +157,10 @@ class ManagedBuffer : public ManagedBufferBase, public virtual WeakReferrable { std::array getTextureSize() const; - // Resize the buffer to newSize elements. Sets hostBufferValid = true. + // Resize the buffer to newSize elements. // - // If newSize <= capacity(), this is a cheap constant-time operation which just updates metadata. + // If newSize <= capacity(), this is a cheap constant-time operation which just updates metadata, + // leaving data residency unchanged (or allocating the host buffer if not yet populated). // // If newSize > capacity(), this triggers a full reallocation. The new capacity is set to at least // 2*oldCapacity (amortized doubling). Any data that was GPU-resident is first copied back to the diff --git a/include/polyscope/render/mock_opengl/mock_gl_engine.h b/include/polyscope/render/mock_opengl/mock_gl_engine.h index ef0e1ad5..6bbc0b01 100644 --- a/include/polyscope/render/mock_opengl/mock_gl_engine.h +++ b/include/polyscope/render/mock_opengl/mock_gl_engine.h @@ -73,7 +73,7 @@ class GLAttributeBuffer : public AttributeBuffer { std::vector getDataRange_uvec4(size_t ind, size_t count) override; uint32_t getNativeBufferID() override; - void reserveCapacity(size_t n) override; + void setBufferSize(size_t n) override; protected: private: diff --git a/include/polyscope/render/opengl/gl_engine.h b/include/polyscope/render/opengl/gl_engine.h index 9f6c4d3a..c9a98c7b 100644 --- a/include/polyscope/render/opengl/gl_engine.h +++ b/include/polyscope/render/opengl/gl_engine.h @@ -101,7 +101,7 @@ class GLAttributeBuffer : public AttributeBuffer { std::vector getDataRange_uvec4(size_t ind, size_t count) override; uint32_t getNativeBufferID() override; - void reserveCapacity(size_t n) override; + void setBufferSize(size_t n) override; protected: VertexBufferHandle VBOLoc; diff --git a/src/render/managed_buffer.cpp b/src/render/managed_buffer.cpp index 700d4f84..e6d9d18e 100644 --- a/src/render/managed_buffer.cpp +++ b/src/render/managed_buffer.cpp @@ -119,38 +119,32 @@ void ManagedBuffer::setTextureSize(uint32_t sizeX_) { template void ManagedBuffer::setTextureSize(uint32_t sizeX_, uint32_t sizeY_) { checkDeviceBufferTypeIs(DeviceBufferType::Texture2d); - if (sizeX_ == sizeX && sizeY_ == sizeY) return; // no-op - if (managedCapacity > 0) { - // Resize: copy device-side data back to host before touching dimensions or the GPU buffer. - ensureHostBufferPopulated(); - managedCapacity = static_cast(sizeX_) * sizeY_; - currentSize = managedCapacity; - data.resize(managedCapacity); - if (renderTextureBuffer) renderTextureBuffer->resize(sizeX_, sizeY_); - hostBufferValid = true; - deviceBufferValid = false; - } + bool dimsChanged = (sizeX_ != sizeX || sizeY_ != sizeY); + if (!dimsChanged && (hostBufferValid || deviceBufferValid)) return; // no-op sizeX = sizeX_; sizeY = sizeY_; + managedCapacity = static_cast(sizeX_) * sizeY_; + currentSize = managedCapacity; + if (data.size() != managedCapacity) data.resize(managedCapacity); + if (dimsChanged && renderTextureBuffer) renderTextureBuffer->resize(sizeX_, sizeY_); + hostBufferValid = true; + deviceBufferValid = false; } template void ManagedBuffer::setTextureSize(uint32_t sizeX_, uint32_t sizeY_, uint32_t sizeZ_) { checkDeviceBufferTypeIs(DeviceBufferType::Texture3d); - if (sizeX_ == sizeX && sizeY_ == sizeY && sizeZ_ == sizeZ) return; // no-op - if (managedCapacity > 0) { - // Resize: copy device-side data back to host before touching dimensions or the GPU buffer. - ensureHostBufferPopulated(); - managedCapacity = static_cast(sizeX_) * sizeY_ * sizeZ_; - currentSize = managedCapacity; - data.resize(managedCapacity); - if (renderTextureBuffer) renderTextureBuffer->resize(sizeX_, sizeY_, sizeZ_); - hostBufferValid = true; - deviceBufferValid = false; - } + bool dimsChanged = (sizeX_ != sizeX || sizeY_ != sizeY || sizeZ_ != sizeZ); + if (!dimsChanged && (hostBufferValid || deviceBufferValid)) return; // no-op sizeX = sizeX_; sizeY = sizeY_; sizeZ = sizeZ_; + managedCapacity = static_cast(sizeX_) * sizeY_ * sizeZ_; + currentSize = managedCapacity; + if (data.size() != managedCapacity) data.resize(managedCapacity); + if (dimsChanged && renderTextureBuffer) renderTextureBuffer->resize(sizeX_, sizeY_, sizeZ_); + hostBufferValid = true; + deviceBufferValid = false; } template @@ -183,11 +177,15 @@ bool ManagedBuffer::resize(size_t newSize) { return true; } else { // No reallocation: just update currentSize. - // data is already at managedCapacity; restore it if it was cleared by invalidateHostBuffer(). - if (data.size() != managedCapacity) data.resize(managedCapacity); currentSize = newSize; - hostBufferValid = true; - deviceBufferValid = false; + + // If data lives exclusively on the device, keep it device-canonical rather than clobbering it + // with empty host data. Otherwise ensure data is allocated at managedCapacity. + if (hostBufferValid || !deviceBufferValid) { + if (data.size() != managedCapacity) data.resize(managedCapacity); + hostBufferValid = true; + deviceBufferValid = false; + } if (deviceBufferType == DeviceBufferType::Texture1d) { sizeX = static_cast(newSize); @@ -217,6 +215,10 @@ void ManagedBuffer::setCapacity(size_t newCapacity) { data.resize(newCapacity); managedCapacity = newCapacity; + // Reallocate any existing GPU buffer in-place to accommodate the new capacity. + if (renderAttributeBuffer) renderAttributeBuffer->setBufferSize(newCapacity); + if (renderTextureBuffer) renderTextureBuffer->resize(static_cast(newCapacity)); + hostBufferValid = true; deviceBufferValid = false; } @@ -506,6 +508,7 @@ void ManagedBuffer::recomputeIfPopulated() { } invalidateHostBuffer(); + deviceBufferValid = false; computeFunc(); markHostBufferUpdated(); } @@ -517,7 +520,7 @@ std::shared_ptr ManagedBuffer::getRenderAttributeBuf if (!renderAttributeBuffer) { ensureHostBufferPopulated(); renderAttributeBuffer = generateAttributeBuffer(render::engine); - renderAttributeBuffer->reserveCapacity(managedCapacity); + renderAttributeBuffer->setBufferSize(managedCapacity); renderAttributeBuffer->setData(data); deviceBufferValid = true; } diff --git a/src/render/mock_opengl/mock_gl_engine.cpp b/src/render/mock_opengl/mock_gl_engine.cpp index 290672eb..177afb62 100644 --- a/src/render/mock_opengl/mock_gl_engine.cpp +++ b/src/render/mock_opengl/mock_gl_engine.cpp @@ -74,8 +74,8 @@ void GLAttributeBuffer::checkArray(int testArrayCount) { } -void GLAttributeBuffer::reserveCapacity(size_t n) { - if (n <= bufferSize) return; +void GLAttributeBuffer::setBufferSize(size_t n) { + if (isSet() && n == bufferSize) return; bufferSize = static_cast(n); setFlag = true; } diff --git a/src/render/opengl/gl_engine.cpp b/src/render/opengl/gl_engine.cpp index 538b272a..37afa11f 100644 --- a/src/render/opengl/gl_engine.cpp +++ b/src/render/opengl/gl_engine.cpp @@ -247,8 +247,8 @@ void GLAttributeBuffer::checkArray(int testArrayCount) { GLenum GLAttributeBuffer::getTarget() { return GL_ARRAY_BUFFER; } -void GLAttributeBuffer::reserveCapacity(size_t n) { - if (n <= bufferSize) return; +void GLAttributeBuffer::setBufferSize(size_t n) { + if (isSet() && n == bufferSize) return; bind(); glBufferData(getTarget(), n * sizeInBytes(dataType) * getArrayCount(), NULL, GL_STATIC_DRAW); bufferSize = static_cast(n); diff --git a/test/src/interop_and_serialization_test.cpp b/test/src/interop_and_serialization_test.cpp index 49ac1625..d29bba00 100644 --- a/test/src/interop_and_serialization_test.cpp +++ b/test/src/interop_and_serialization_test.cpp @@ -75,6 +75,11 @@ TEST_F(PolyscopeTest, ManagedBufferCapacity) { mb.syncToDeviceIfNeeded(); EXPECT_EQ(mb.getRenderAttributeBuffer(), gpuBuf); EXPECT_EQ(gpuBuf->getBufferSize(), mb.capacity()); + + // Shrinking capacity also resizes the underlying GPU buffer to match + mb.setCapacity(12); + EXPECT_EQ(mb.capacity(), 12u); + EXPECT_EQ(gpuBuf->getBufferSize(), 12u); } TEST_F(PolyscopeTest, ManagedBufferDeviceSync) { @@ -96,6 +101,17 @@ TEST_F(PolyscopeTest, ManagedBufferDeviceSync) { EXPECT_EQ(hostCopy, (std::vector{10.f, 20.f, 30.f, 40.f})); } + // Within-capacity resize while device-canonical should not clobber GPU data + gpuVals[1] = 77.f; + gpuBuf->setData(gpuVals); + mb.markRenderAttributeBufferUpdated(); + EXPECT_FALSE(mb.resize(2)); + EXPECT_EQ(mb.size(), 2u); + if (testBackend != "openGL_mock") { + EXPECT_FLOAT_EQ(mb.getValue(1), 77.f); + } + EXPECT_FALSE(mb.resize(4)); + // Growing while device-canonical should read back valid elements before reallocating gpuVals[0] = 99.f; gpuBuf->setData(gpuVals); From c9cc2d81ceaf86a5d383e108dd2bd51fc2b17a1c Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Mon, 23 Nov 2026 00:13:50 -0800 Subject: [PATCH 14/31] support partial setData uploads --- include/polyscope/render/engine.h | 99 +++++++---- .../render/mock_opengl/mock_gl_engine.h | 98 +++++++---- include/polyscope/render/opengl/gl_engine.h | 98 +++++++---- src/render/managed_buffer.cpp | 63 ++++--- src/render/mock_opengl/mock_gl_engine.cpp | 138 +++++++++------ src/render/opengl/gl_engine.cpp | 165 +++++++++++------- test/src/interop_and_serialization_test.cpp | 14 +- 7 files changed, 424 insertions(+), 251 deletions(-) diff --git a/include/polyscope/render/engine.h b/include/polyscope/render/engine.h index fed32ccb..b199e0cc 100644 --- a/include/polyscope/render/engine.h +++ b/include/polyscope/render/engine.h @@ -81,26 +81,43 @@ class AttributeBuffer { virtual ~AttributeBuffer(); - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; + // Upload up to `count` elements from `data` starting at GPU element `offset` (defaults to full `data` at 0). + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; // Array-valued attributes // (adding these lazily as we need them) // (sadly we cannot template the virtual function) - virtual void setData(const std::vector>& data) = 0; - virtual void setData(const std::vector>& data) = 0; - virtual void setData(const std::vector>& data) = 0; + virtual void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; // Allocate GPU memory for exactly n elements without uploading any data (no-op if already set to n). virtual void setBufferSize(size_t n) = 0; @@ -165,29 +182,45 @@ class TextureBuffer { virtual void resize(unsigned int newX, unsigned int newY); virtual void resize(unsigned int newX, unsigned int newY, unsigned int newZ); - // Fill with data + // Fill with data (uploads up to `count` elements at `offset`; non-zero offset is only valid for 1D textures) // NOTE: some of these are not implemented yet - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; - virtual void setData(const std::vector& data) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; // Array-valued // NOTE: some of these are not implemented yet // (adding these lazily as we need them) // (sadly we cannot template the virtual function) - virtual void setData(const std::vector>& data) = 0; - virtual void setData(const std::vector>& data) = 0; - virtual void setData(const std::vector>& data) = 0; + virtual void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; + virtual void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) = 0; unsigned int getSizeX() const { return sizeX; } unsigned int getSizeY() const { return sizeY; } diff --git a/include/polyscope/render/mock_opengl/mock_gl_engine.h b/include/polyscope/render/mock_opengl/mock_gl_engine.h index 6bbc0b01..7bda3c0a 100644 --- a/include/polyscope/render/mock_opengl/mock_gl_engine.h +++ b/include/polyscope/render/mock_opengl/mock_gl_engine.h @@ -21,26 +21,42 @@ class GLAttributeBuffer : public AttributeBuffer { void bind(); - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; // Array-valued attributes // (adding these lazily as we need them) // (sadly we cannot template the virtual function) - void setData(const std::vector>& data) override; - void setData(const std::vector>& data) override; - void setData(const std::vector>& data) override; + void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; // get data at a single index from the buffer float getData_float(size_t ind) override; @@ -82,7 +98,7 @@ class GLAttributeBuffer : public AttributeBuffer { // internal implementation helpers template - void setData_helper(const std::vector& data); + void setData_helper(const std::vector& data, size_t offset, size_t count); template T getData_helper(size_t ind); @@ -117,27 +133,43 @@ class GLTextureBuffer : public TextureBuffer { // Fill with data // NOTE: some of these are not implemented yet - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; // Array-valued // NOTE: some of these are not implemented yet // (adding these lazily as we need them) // (sadly we cannot template the virtual function) - void setData(const std::vector>& data) override; - void setData(const std::vector>& data) override; - void setData(const std::vector>& data) override; + void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; void setFilterMode(FilterMode newMode) override; void* getNativeHandle() override; diff --git a/include/polyscope/render/opengl/gl_engine.h b/include/polyscope/render/opengl/gl_engine.h index c9a98c7b..008344c7 100644 --- a/include/polyscope/render/opengl/gl_engine.h +++ b/include/polyscope/render/opengl/gl_engine.h @@ -49,26 +49,42 @@ class GLAttributeBuffer : public AttributeBuffer { void bind(); VertexBufferHandle getHandle() const { return VBOLoc; } - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; // Array-valued attributes // (adding these lazily as we need them) // (sadly we cannot template the virtual function) - void setData(const std::vector>& data) override; - void setData(const std::vector>& data) override; - void setData(const std::vector>& data) override; + void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; // get data at a single index from the buffer float getData_float(size_t ind) override; @@ -114,7 +130,7 @@ class GLAttributeBuffer : public AttributeBuffer { // internal implementation helpers template - void setData_helper(const std::vector& data); + void setData_helper(const std::vector& data, size_t offset, size_t count); template T getData_helper(size_t ind); @@ -149,27 +165,43 @@ class GLTextureBuffer : public TextureBuffer { // Fill with data // NOTE: some of these are not implemented yet - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; - void setData(const std::vector& data) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; // Array-valued // NOTE: some of these are not implemented yet // (adding these lazily as we need them) // (sadly we cannot template the virtual function) - void setData(const std::vector>& data) override; - void setData(const std::vector>& data) override; - void setData(const std::vector>& data) override; + void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; + void setData(const std::vector>& data, size_t offset = 0, + size_t count = std::numeric_limits::max()) override; void setFilterMode(FilterMode newMode) override; void* getNativeHandle() override; diff --git a/src/render/managed_buffer.cpp b/src/render/managed_buffer.cpp index e6d9d18e..1dcdf7f2 100644 --- a/src/render/managed_buffer.cpp +++ b/src/render/managed_buffer.cpp @@ -99,6 +99,7 @@ ManagedBuffer::~ManagedBuffer() { template void ManagedBuffer::checkInvalidValues() { + ensureHostBufferPopulated(); // Only check the logically-valid elements; slack in [currentSize, managedCapacity) is uninitialized. polyscope::checkInvalidValues(name, std::vector(data.begin(), data.begin() + currentSize)); } @@ -166,9 +167,12 @@ bool ManagedBuffer::resize(size_t newSize) { if (newSize > managedCapacity) { // Reallocation needed: use amortized doubling for the new capacity. size_t newCapacity = std::max(newSize, 2 * managedCapacity); - setCapacity(newCapacity); // handles host/device sync, data.resize, validity flags + setCapacity(newCapacity); // handles host/device sync and GPU buffer resize + if (data.size() != managedCapacity) data.resize(managedCapacity); currentSize = newSize; + hostBufferValid = true; + deviceBufferValid = false; if (deviceBufferType == DeviceBufferType::Texture1d) { sizeX = static_cast(newSize); @@ -205,12 +209,18 @@ void ManagedBuffer::setCapacity(size_t newCapacity) { if (newCapacity == managedCapacity) return; // no-op - // Before invalidating the GPU buffer, copy any device-side changes back to the host. - // Skip if state is NeedsCompute — there's no existing data to preserve. - if (!isInNeedsComputeState()) { - ensureHostBufferPopulated(); + if (isInNeedsComputeState()) { + // Stay in NeedsCompute state — record the capacity (and resize any existing GPU buffer) + // without marking host data valid or prematurely allocating the host vector. + managedCapacity = newCapacity; + if (renderAttributeBuffer) renderAttributeBuffer->setBufferSize(newCapacity); + if (renderTextureBuffer) renderTextureBuffer->resize(static_cast(newCapacity)); + return; } + // Before invalidating the GPU buffer, copy any device-side changes back to the host. + ensureHostBufferPopulated(); + // Resize data to the new capacity (maintaining the invariant data.size() == managedCapacity). data.resize(newCapacity); managedCapacity = newCapacity; @@ -269,14 +279,23 @@ void ManagedBuffer::ensureHostBufferPopulated() { template void ManagedBuffer::setDataHost(const std::vector& newData) { - if (newData.size() > managedCapacity) + if (deviceBufferType == DeviceBufferType::Texture2d || deviceBufferType == DeviceBufferType::Texture3d) { + if (newData.size() != managedCapacity) + exception("ManagedBuffer " + name + " setDataHost() called with data size (" + std::to_string(newData.size()) + + ") that does not match texture size (" + std::to_string(managedCapacity) + + "). Call setTextureSize() first."); + } else if (newData.size() > managedCapacity) { exception("ManagedBuffer " + name + " setDataHost() called with data that exceeds capacity (" + std::to_string(newData.size()) + " > " + std::to_string(managedCapacity) + "). Call resize() or setCapacity() first."); + } // Restore the invariant (data may have been cleared by invalidateHostBuffer()). if (data.size() != managedCapacity) data.resize(managedCapacity); std::copy(newData.begin(), newData.end(), data.begin()); currentSize = newData.size(); + if (deviceBufferType == DeviceBufferType::Texture1d) { + sizeX = static_cast(newData.size()); + } hostBufferValid = true; deviceBufferValid = false; invalidateIndexedViews(); @@ -369,8 +388,10 @@ void ManagedBuffer::markHostBufferUpdated() { template void ManagedBuffer::syncToDeviceIfNeeded() { - // Quick exit: device data is current and all indexed views are up to date. - if (deviceBufferValid && indexedViewsValid) return; + bool hasDeviceBuffer = (renderAttributeBuffer || renderTextureBuffer); + + // Quick exit: any existing device buffer is current and all indexed views are up to date. + if ((!hasDeviceBuffer || deviceBufferValid) && indexedViewsValid) return; if (!deviceBufferValid && !hostBufferValid) { // Neither host nor device has valid data at draw time. This is a bug: either the buffer was @@ -379,28 +400,24 @@ void ManagedBuffer::syncToDeviceIfNeeded() { exception("ManagedBuffer " + name + " has no valid data on host or device at draw time"); } - // Host data is valid. Copy to device. - if (!deviceBufferValid) { // must be hostBufferValid == true + // Host data is valid. Copy only the logically-valid elements [0, currentSize) to device. + if (hasDeviceBuffer && !deviceBufferValid) { // must be hostBufferValid == true if (renderAttributeBuffer) { - renderAttributeBuffer->setData(data); + renderAttributeBuffer->setData(data, 0, currentSize); } if (renderTextureBuffer) { - renderTextureBuffer->setData(data); + renderTextureBuffer->setData(data, 0, currentSize); } deviceBufferValid = true; } // Update indexed views if (!indexedViewsValid) { + // TODO do an on-device update of indexed views - if(deviceBufferValid) { // always true now at this point - - // TODO do an on-device update of indexed views - - // On-device update not implemented yet, copy it to host and do the update from the host - ensureHostBufferPopulated(); - updateIndexedViews(); // sets indexedViewsValid = true - } + // On-device update not implemented yet, copy it to host and do the update from the host + ensureHostBufferPopulated(); + updateIndexedViews(); // sets indexedViewsValid = true } } @@ -521,7 +538,7 @@ std::shared_ptr ManagedBuffer::getRenderAttributeBuf ensureHostBufferPopulated(); renderAttributeBuffer = generateAttributeBuffer(render::engine); renderAttributeBuffer->setBufferSize(managedCapacity); - renderAttributeBuffer->setData(data); + renderAttributeBuffer->setData(data, 0, currentSize); deviceBufferValid = true; } return renderAttributeBuffer; @@ -542,7 +559,7 @@ std::shared_ptr ManagedBuffer::getRenderTextureBuffer( break; case DeviceBufferType::Texture1d: // Allocate GPU texture at the full managed capacity so future within-capacity resizes - // don't require a new GPU buffer. setData() below uploads only data.size() elements. + // don't require a new GPU buffer. setData() below uploads only currentSize elements. renderTextureBuffer->resize(static_cast(managedCapacity)); break; case DeviceBufferType::Texture2d: @@ -553,7 +570,7 @@ std::shared_ptr ManagedBuffer::getRenderTextureBuffer( break; } - renderTextureBuffer->setData(data); + renderTextureBuffer->setData(data, 0, currentSize); deviceBufferValid = true; } return renderTextureBuffer; diff --git a/src/render/mock_opengl/mock_gl_engine.cpp b/src/render/mock_opengl/mock_gl_engine.cpp index 177afb62..09d1438e 100644 --- a/src/render/mock_opengl/mock_gl_engine.cpp +++ b/src/render/mock_opengl/mock_gl_engine.cpp @@ -81,99 +81,105 @@ void GLAttributeBuffer::setBufferSize(size_t n) { } template -void GLAttributeBuffer::setData_helper(const std::vector& data) { +void GLAttributeBuffer::setData_helper(const std::vector& data, size_t offset, size_t count) { + size_t n = std::min(count, data.size()); bind(); - if (!isSet() || data.size() > bufferSize) { + if (offset > 0) { + if (!isSet() || offset + n > bufferSize) { + exception("OpenGL error: setData() with non-zero offset exceeds allocated buffer size."); + } + } else if (!isSet() || n > bufferSize) { setFlag = true; - uint64_t newSize = static_cast(data.size()); + uint64_t newSize = static_cast(n); bufferSize = newSize; } checkGLError(); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector2Float); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector3Float); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector>& data) { +void GLAttributeBuffer::setData(const std::vector>& data, size_t offset, size_t count) { checkType(RenderDataType::Vector3Float); checkArray(2); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector>& data) { +void GLAttributeBuffer::setData(const std::vector>& data, size_t offset, size_t count) { checkType(RenderDataType::Vector3Float); checkArray(3); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector>& data) { +void GLAttributeBuffer::setData(const std::vector>& data, size_t offset, size_t count) { checkType(RenderDataType::Vector3Float); checkArray(4); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector4Float); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Float); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Float); // Convert input data to floats - std::vector floatData(data.size()); - for (unsigned int i = 0; i < data.size(); i++) { + size_t n = std::min(count, data.size()); + std::vector floatData(n); + for (size_t i = 0; i < n; i++) { floatData[i] = static_cast(data[i]); } - setData_helper(floatData); + setData_helper(floatData, offset, n); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Int); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector2Int); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector3Int); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector4Int); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::UInt); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector2UInt); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector3UInt); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector4UInt); - setData_helper(data); + setData_helper(data, offset, count); } @@ -406,14 +412,18 @@ void GLTextureBuffer::resize(unsigned int newX, unsigned int newY, unsigned int checkGLError(); } -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); } +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); } -void GLTextureBuffer::setData(const std::vector& data) { +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { + size_t n = std::min(count, data.size()); bind(); - if (data.size() > getTotalSize()) { + if (dim >= 2 && offset != 0) { + exception("OpenGL error: non-zero offset in setData() is only supported for 1D textures."); + } + if (offset + n > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } @@ -429,11 +439,15 @@ void GLTextureBuffer::setData(const std::vector& data) { checkGLError(); } -void GLTextureBuffer::setData(const std::vector& data) { +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { + size_t n = std::min(count, data.size()); bind(); - if (data.size() > getTotalSize()) { + if (dim >= 2 && offset != 0) { + exception("OpenGL error: non-zero offset in setData() is only supported for 1D textures."); + } + if (offset + n > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } @@ -449,10 +463,14 @@ void GLTextureBuffer::setData(const std::vector& data) { checkGLError(); } -void GLTextureBuffer::setData(const std::vector& data) { +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { + size_t n = std::min(count, data.size()); bind(); - if (data.size() > getTotalSize()) { + if (dim >= 2 && offset != 0) { + exception("OpenGL error: non-zero offset in setData() is only supported for 1D textures."); + } + if (offset + n > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } @@ -468,10 +486,14 @@ void GLTextureBuffer::setData(const std::vector& data) { checkGLError(); } -void GLTextureBuffer::setData(const std::vector& data) { +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { + size_t n = std::min(count, data.size()); bind(); - if (data.size() > getTotalSize()) { + if (dim >= 2 && offset != 0) { + exception("OpenGL error: non-zero offset in setData() is only supported for 1D textures."); + } + if (offset + n > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } @@ -486,17 +508,17 @@ void GLTextureBuffer::setData(const std::vector& data) { checkGLError(); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector>& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector>& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector>& data) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector>& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector>& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector>& data, size_t offset, size_t count) { exception("not implemented"); }; void GLTextureBuffer::setFilterMode(FilterMode newMode) { @@ -1667,8 +1689,8 @@ void GLShaderProgram::syncBuffersToDeviceIfNeeded() { } void GLShaderProgram::draw() { - validateData(); syncBuffersToDeviceIfNeeded(); + validateData(); if (usePrimitiveRestart) { } diff --git a/src/render/opengl/gl_engine.cpp b/src/render/opengl/gl_engine.cpp index 37afa11f..0678b658 100644 --- a/src/render/opengl/gl_engine.cpp +++ b/src/render/opengl/gl_engine.cpp @@ -257,103 +257,111 @@ void GLAttributeBuffer::setBufferSize(size_t n) { } template -void GLAttributeBuffer::setData_helper(const std::vector& data) { +void GLAttributeBuffer::setData_helper(const std::vector& data, size_t offset, size_t count) { + size_t n = std::min(count, data.size()); bind(); - if (!isSet() || data.size() > bufferSize) { + if (offset > 0) { + if (!isSet() || offset + n > bufferSize) { + exception("OpenGL error: setData() with non-zero offset exceeds allocated buffer size."); + } + } else if (!isSet() || n > bufferSize) { setFlag = true; - uint64_t newSize = static_cast(data.size()); + uint64_t newSize = static_cast(n); glBufferData(getTarget(), newSize * sizeof(T), NULL, GL_STATIC_DRAW); bufferSize = newSize; } - glBufferSubData(getTarget(), 0, data.size() * sizeof(T), data.data()); + if (n > 0) { + glBufferSubData(getTarget(), offset * sizeof(T), n * sizeof(T), data.data()); + } checkGLError(); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector2Float); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector3Float); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector>& data) { +void GLAttributeBuffer::setData(const std::vector>& data, size_t offset, size_t count) { checkType(RenderDataType::Vector3Float); checkArray(2); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector>& data) { +void GLAttributeBuffer::setData(const std::vector>& data, size_t offset, size_t count) { checkType(RenderDataType::Vector3Float); checkArray(3); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector>& data) { +void GLAttributeBuffer::setData(const std::vector>& data, size_t offset, size_t count) { checkType(RenderDataType::Vector3Float); checkArray(4); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector4Float); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Float); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Float); // Convert input data to floats - std::vector floatData(data.size()); - for (unsigned int i = 0; i < data.size(); i++) { + size_t n = std::min(count, data.size()); + std::vector floatData(n); + for (size_t i = 0; i < n; i++) { floatData[i] = static_cast(data[i]); } - setData_helper(floatData); + setData_helper(floatData, offset, n); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Int); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector2Int); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector3Int); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector4Int); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::UInt); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector2UInt); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector3UInt); - setData_helper(data); + setData_helper(data, offset, count); } -void GLAttributeBuffer::setData(const std::vector& data) { +void GLAttributeBuffer::setData(const std::vector& data, size_t offset, size_t count) { checkType(RenderDataType::Vector4UInt); - setData_helper(data); + setData_helper(data, offset, count); } // === get single data values @@ -424,6 +432,7 @@ glm::uvec4 GLAttributeBuffer::getData_uvec4(size_t ind) { template std::vector GLAttributeBuffer::getDataRange_helper(size_t start, size_t count) { if (!isSet() || start + count > bufferSize * static_cast(getArrayCount())) exception("bad getData"); + if (count == 0) return {}; bind(); std::vector readValues(count); glGetBufferSubData(getTarget(), start * sizeof(T), count * sizeof(T), &readValues.front()); @@ -614,20 +623,26 @@ void GLTextureBuffer::resize(unsigned int newX, unsigned int newY, unsigned int checkGLError(); } -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { + size_t n = std::min(count, data.size()); bind(); - // For 1D textures, data.size() may be <= sizeX (partial upload into a capacity-allocated texture). - // For 2D/3D textures, capacity always equals size so the check is effectively ==. - if (data.size() > getTotalSize()) { + // For 1D textures, offset + n may be <= sizeX (partial upload into a capacity-allocated texture). + // For 2D/3D textures, offset must be 0 and capacity always equals size. + if (dim >= 2 && offset != 0) { + exception("OpenGL error: non-zero offset in setData() is only supported for 1D textures."); + } + if (offset + n > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } + if (n == 0) return; switch (dim) { case 1: - glTexSubImage1D(GL_TEXTURE_1D, 0, 0, static_cast(data.size()), formatF(format), type(format), &data.front().x); + glTexSubImage1D(GL_TEXTURE_1D, 0, static_cast(offset), static_cast(n), formatF(format), + type(format), &data.front().x); break; case 2: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, sizeX, sizeY, formatF(format), type(format), &data.front().x); @@ -640,17 +655,23 @@ void GLTextureBuffer::setData(const std::vector& data) { checkGLError(); }; -void GLTextureBuffer::setData(const std::vector& data) { +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { + size_t n = std::min(count, data.size()); bind(); - if (data.size() > getTotalSize()) { + if (dim >= 2 && offset != 0) { + exception("OpenGL error: non-zero offset in setData() is only supported for 1D textures."); + } + if (offset + n > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } + if (n == 0) return; switch (dim) { case 1: - glTexSubImage1D(GL_TEXTURE_1D, 0, 0, static_cast(data.size()), formatF(format), type(format), &data.front().x); + glTexSubImage1D(GL_TEXTURE_1D, 0, static_cast(offset), static_cast(n), formatF(format), + type(format), &data.front().x); break; case 2: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, sizeX, sizeY, formatF(format), type(format), &data.front().x); @@ -663,16 +684,22 @@ void GLTextureBuffer::setData(const std::vector& data) { checkGLError(); }; -void GLTextureBuffer::setData(const std::vector& data) { +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { + size_t n = std::min(count, data.size()); bind(); - if (data.size() > getTotalSize()) { + if (dim >= 2 && offset != 0) { + exception("OpenGL error: non-zero offset in setData() is only supported for 1D textures."); + } + if (offset + n > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } + if (n == 0) return; switch (dim) { case 1: - glTexSubImage1D(GL_TEXTURE_1D, 0, 0, static_cast(data.size()), formatF(format), type(format), &data.front()); + glTexSubImage1D(GL_TEXTURE_1D, 0, static_cast(offset), static_cast(n), formatF(format), + type(format), &data.front()); break; case 2: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, sizeX, sizeY, formatF(format), type(format), &data.front()); @@ -686,23 +713,29 @@ void GLTextureBuffer::setData(const std::vector& data) { }; -void GLTextureBuffer::setData(const std::vector& data) { +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { // Convert to float - std::vector dataFloat(data.size()); - for (size_t i = 0; i < data.size(); i++) { + size_t n = std::min(count, data.size()); + std::vector dataFloat(n); + for (size_t i = 0; i < n; i++) { dataFloat[i] = static_cast(data[i]); } bind(); - if (data.size() > getTotalSize()) { + if (dim >= 2 && offset != 0) { + exception("OpenGL error: non-zero offset in setData() is only supported for 1D textures."); + } + if (offset + n > getTotalSize()) { exception("OpenGL error: texture buffer data is not the right size."); } + if (n == 0) return; switch (dim) { case 1: - glTexSubImage1D(GL_TEXTURE_1D, 0, 0, static_cast(data.size()), formatF(format), type(format), &dataFloat.front()); + glTexSubImage1D(GL_TEXTURE_1D, 0, static_cast(offset), static_cast(n), formatF(format), + type(format), &dataFloat.front()); break; case 2: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, sizeX, sizeY, formatF(format), type(format), &dataFloat.front()); @@ -714,17 +747,17 @@ void GLTextureBuffer::setData(const std::vector& data) { checkGLError(); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector>& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector>& data) { exception("not implemented"); }; -void GLTextureBuffer::setData(const std::vector>& data) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector>& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector>& data, size_t offset, size_t count) { exception("not implemented"); }; +void GLTextureBuffer::setData(const std::vector>& data, size_t offset, size_t count) { exception("not implemented"); }; void GLTextureBuffer::setFilterMode(FilterMode newMode) { diff --git a/test/src/interop_and_serialization_test.cpp b/test/src/interop_and_serialization_test.cpp index d29bba00..e8bc65ac 100644 --- a/test/src/interop_and_serialization_test.cpp +++ b/test/src/interop_and_serialization_test.cpp @@ -102,13 +102,14 @@ TEST_F(PolyscopeTest, ManagedBufferDeviceSync) { } // Within-capacity resize while device-canonical should not clobber GPU data - gpuVals[1] = 77.f; - gpuBuf->setData(gpuVals); + gpuBuf->setData(std::vector{77.f, 88.f}, /*offset=*/1, /*count=*/2); mb.markRenderAttributeBufferUpdated(); - EXPECT_FALSE(mb.resize(2)); - EXPECT_EQ(mb.size(), 2u); + EXPECT_FALSE(mb.resize(3)); + EXPECT_EQ(mb.size(), 3u); if (testBackend != "openGL_mock") { + EXPECT_FLOAT_EQ(mb.getValue(0), 10.f); EXPECT_FLOAT_EQ(mb.getValue(1), 77.f); + EXPECT_FLOAT_EQ(mb.getValue(2), 88.f); } EXPECT_FALSE(mb.resize(4)); @@ -133,7 +134,8 @@ TEST_F(PolyscopeTest, ManagedBufferLazyCompute) { mb.setDataHost({static_cast(computeCount), 2.f, 3.f}); }); - // Should not compute before first access + // Should not compute before first access, even after pre-reserving capacity + mb.setCapacity(8); mb.recomputeIfPopulated(); EXPECT_EQ(computeCount, 0); @@ -173,6 +175,7 @@ TEST_F(PolyscopeTest, ManagedBufferTextures) { tex1d.setAsType(polyscope::DeviceBufferType::Texture1d); tex1d.setCapacity(4); tex1d.setDataHost({1.f, 2.f, 3.f, 4.f}); + EXPECT_EQ(tex1d.getTextureSize()[0], 4u); auto gpuTex1d = tex1d.getRenderTextureBuffer(); EXPECT_TRUE(tex1d.resize(8)); tex1d.setDataHost(std::vector(8, 5.f)); @@ -183,6 +186,7 @@ TEST_F(PolyscopeTest, ManagedBufferTextures) { polyscope::render::ManagedBuffer tex2d(nullptr, "tex2d_test"); tex2d.setAsType(polyscope::DeviceBufferType::Texture2d); tex2d.setTextureSize(2, 3); + EXPECT_THROW(tex2d.setDataHost(std::vector(3, 1.f)), std::exception); tex2d.setDataHost(std::vector(6, 1.f)); auto gpuTex2d = tex2d.getRenderTextureBuffer(); tex2d.setTextureSize(4, 2); From bace8411294b6b6fa78956194759437bdc8534a5 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Wed, 25 Mar 2026 16:54:06 -0500 Subject: [PATCH 15/31] initial work on simple triangle vertex and face quantities --- examples/demo-app/demo_app.cpp | 6 +- include/polyscope/simple_triangle_mesh.h | 23 +++++++ include/polyscope/simple_triangle_mesh.ipp | 18 +++++ .../simple_triangle_mesh_color_quantity.h | 30 ++++++++ .../polyscope/simple_triangle_mesh_quantity.h | 20 ++++++ .../simple_triangle_mesh_scalar_quantity.h | 33 +++++++++ src/CMakeLists.txt | 6 ++ src/simple_triangle_mesh.cpp | 22 ++++++ src/simple_triangle_mesh_color_quantity.cpp | 68 ++++++++++++++++++ src/simple_triangle_mesh_quantity.cpp | 13 ++++ src/simple_triangle_mesh_scalar_quantity.cpp | 69 +++++++++++++++++++ test/src/surface_mesh_test.cpp | 36 ++++++++++ 12 files changed, 343 insertions(+), 1 deletion(-) create mode 100644 include/polyscope/simple_triangle_mesh_color_quantity.h create mode 100644 include/polyscope/simple_triangle_mesh_quantity.h create mode 100644 include/polyscope/simple_triangle_mesh_scalar_quantity.h create mode 100644 src/simple_triangle_mesh_color_quantity.cpp create mode 100644 src/simple_triangle_mesh_quantity.cpp create mode 100644 src/simple_triangle_mesh_scalar_quantity.cpp diff --git a/examples/demo-app/demo_app.cpp b/examples/demo-app/demo_app.cpp index afe26351..566abcc2 100644 --- a/examples/demo-app/demo_app.cpp +++ b/examples/demo-app/demo_app.cpp @@ -121,7 +121,7 @@ void processFileOBJ(std::string filename) { } auto psMesh = polyscope::registerSurfaceMesh(niceName, vertexPositionsGLM, faceIndices); - auto psSimpleMesh = polyscope::registerSimpleTriangleMesh(niceName, vertexPositionsGLM, faceIndices); + auto psSimpleMesh = polyscope::registerSimpleTriangleMesh(niceName + " (simple)", vertexPositionsGLM, faceIndices); psSimpleMesh->setEnabled(false); // Useful data @@ -154,6 +154,10 @@ void processFileOBJ(std::string filename) { polyscope::getSurfaceMesh(niceName)->addVertexScalarQuantity("categorical vert", valCat, polyscope::DataType::CATEGORICAL); + // Simple triangle mesh quantities + psSimpleMesh->addVertexScalarQuantity("cY", valY); + psSimpleMesh->addVertexColorQuantity("vColor", randColor); + polyscope::getSurfaceMesh(niceName)->addVertexDistanceQuantity("cY_dist", valY); polyscope::getSurfaceMesh(niceName)->addVertexSignedDistanceQuantity("cY_signeddist", valY); diff --git a/include/polyscope/simple_triangle_mesh.h b/include/polyscope/simple_triangle_mesh.h index 181d1c1f..ad600f7c 100644 --- a/include/polyscope/simple_triangle_mesh.h +++ b/include/polyscope/simple_triangle_mesh.h @@ -8,8 +8,13 @@ #include "polyscope/render/engine.h" #include "polyscope/render/managed_buffer.h" #include "polyscope/scaled_value.h" +#include "polyscope/standardize_data_array.h" #include "polyscope/structure.h" +#include "polyscope/simple_triangle_mesh_color_quantity.h" +#include "polyscope/simple_triangle_mesh_quantity.h" +#include "polyscope/simple_triangle_mesh_scalar_quantity.h" + #include namespace polyscope { @@ -18,6 +23,8 @@ namespace polyscope { class SimpleTriangleMesh; // Forward declare quantity types +class SimpleTriangleMeshScalarQuantity; +class SimpleTriangleMeshColorQuantity; struct SimpleTriangleMeshPickResult { // this does nothing for now, just matching pattern from other structures @@ -50,8 +57,20 @@ class SimpleTriangleMesh : public Structure { render::ManagedBuffer vertices; render::ManagedBuffer faces; + size_t nVertices() { return vertices.size(); } + size_t nFaces() { return faces.size(); } + // === Quantities + // Scalars + template + SimpleTriangleMeshScalarQuantity* addVertexScalarQuantity(std::string name, const T& values, + DataType type = DataType::STANDARD); + + // Colors + template + SimpleTriangleMeshColorQuantity* addVertexColorQuantity(std::string name, const T& values); + // === Mutate template @@ -111,6 +130,10 @@ class SimpleTriangleMesh : public Structure { void setPickUniforms(render::ShaderProgram& p); // === Quantity adder implementations + SimpleTriangleMeshScalarQuantity* addVertexScalarQuantityImpl(std::string name, const std::vector& data, + DataType type); + SimpleTriangleMeshColorQuantity* addVertexColorQuantityImpl(std::string name, + const std::vector& colors); // == Picking related things size_t pickStart; diff --git a/include/polyscope/simple_triangle_mesh.ipp b/include/polyscope/simple_triangle_mesh.ipp index ca10bc2c..6b49b02f 100644 --- a/include/polyscope/simple_triangle_mesh.ipp +++ b/include/polyscope/simple_triangle_mesh.ipp @@ -49,6 +49,24 @@ void SimpleTriangleMesh::update(const V& newPositions, const F& newFaces) { faces.markHostBufferUpdated(); } +// ===================================================== +// ============== Quantities +// ===================================================== + +template +SimpleTriangleMeshScalarQuantity* SimpleTriangleMesh::addVertexScalarQuantity(std::string name, const T& values, + DataType type) { + validateSize(values, nVertices(), "vertex scalar quantity " + name); + return addVertexScalarQuantityImpl(name, standardizeArray(values), type); +} + +template +SimpleTriangleMeshColorQuantity* SimpleTriangleMesh::addVertexColorQuantity(std::string name, const T& values) { + validateSize(values, nVertices(), "vertex color quantity " + name); + return addVertexColorQuantityImpl(name, standardizeVectorArray(values)); +} + + // Shorthand to get a mesh from polyscope inline SimpleTriangleMesh* getSimpleTriangleMesh(std::string name) { return dynamic_cast(getStructure(SimpleTriangleMesh::structureTypeName, name)); diff --git a/include/polyscope/simple_triangle_mesh_color_quantity.h b/include/polyscope/simple_triangle_mesh_color_quantity.h new file mode 100644 index 00000000..2383511e --- /dev/null +++ b/include/polyscope/simple_triangle_mesh_color_quantity.h @@ -0,0 +1,30 @@ +// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run + +#pragma once + +#include "polyscope/color_quantity.h" +#include "polyscope/simple_triangle_mesh_quantity.h" + +#include + +namespace polyscope { + +class SimpleTriangleMeshColorQuantity : public SimpleTriangleMeshQuantity, + public ColorQuantity { +public: + SimpleTriangleMeshColorQuantity(std::string name, const std::vector& colors, std::string definedOn, + SimpleTriangleMesh& mesh); + + virtual void draw() override; + virtual void buildCustomUI() override; + virtual void refresh() override; + virtual std::string niceName() override; + + std::string definedOn; + +protected: + void createProgram(); + std::shared_ptr program; +}; + +} // namespace polyscope diff --git a/include/polyscope/simple_triangle_mesh_quantity.h b/include/polyscope/simple_triangle_mesh_quantity.h new file mode 100644 index 00000000..6df4e0e4 --- /dev/null +++ b/include/polyscope/simple_triangle_mesh_quantity.h @@ -0,0 +1,20 @@ +// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run + +#pragma once + +#include "polyscope/quantity.h" +#include "polyscope/structure.h" + +namespace polyscope { + +class SimpleTriangleMesh; + +class SimpleTriangleMeshQuantity : public Quantity { +public: + SimpleTriangleMeshQuantity(std::string name, SimpleTriangleMesh& parentStructure, bool dominates = false); + virtual ~SimpleTriangleMeshQuantity(){}; + + SimpleTriangleMesh& parent; // shadows and hides the generic member in Quantity +}; + +} // namespace polyscope diff --git a/include/polyscope/simple_triangle_mesh_scalar_quantity.h b/include/polyscope/simple_triangle_mesh_scalar_quantity.h new file mode 100644 index 00000000..729b24a7 --- /dev/null +++ b/include/polyscope/simple_triangle_mesh_scalar_quantity.h @@ -0,0 +1,33 @@ +// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run + +#pragma once + +#include "polyscope/affine_remapper.h" +#include "polyscope/render/color_maps.h" +#include "polyscope/scalar_quantity.h" +#include "polyscope/simple_triangle_mesh_quantity.h" + +#include + +namespace polyscope { + +class SimpleTriangleMeshScalarQuantity : public SimpleTriangleMeshQuantity, + public ScalarQuantity { +public: + SimpleTriangleMeshScalarQuantity(std::string name, const std::vector& values, std::string definedOn, + SimpleTriangleMesh& mesh, DataType dataType); + + virtual void draw() override; + virtual void buildCustomUI() override; + virtual void refresh() override; + virtual std::string niceName() override; + + +protected: + std::string definedOn; + std::shared_ptr program; + + void createProgram(); +}; + +} // namespace polyscope diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8b4eca3b..da32e3a5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -235,6 +235,9 @@ SET(SRCS # Simple triangle mesh simple_triangle_mesh.cpp + simple_triangle_mesh_quantity.cpp + simple_triangle_mesh_scalar_quantity.cpp + simple_triangle_mesh_color_quantity.cpp # Floating quantities floating_quantity_structure.cpp @@ -337,6 +340,9 @@ SET(HEADERS ${INCLUDE_ROOT}/screenshot.h ${INCLUDE_ROOT}/simple_triangle_mesh.h ${INCLUDE_ROOT}/simple_triangle_mesh.ipp + ${INCLUDE_ROOT}/simple_triangle_mesh_quantity.h + ${INCLUDE_ROOT}/simple_triangle_mesh_scalar_quantity.h + ${INCLUDE_ROOT}/simple_triangle_mesh_color_quantity.h ${INCLUDE_ROOT}/slice_plane.h ${INCLUDE_ROOT}/standardize_data_array.h ${INCLUDE_ROOT}/structure.h diff --git a/src/simple_triangle_mesh.cpp b/src/simple_triangle_mesh.cpp index 81b44b1e..8d6128f4 100644 --- a/src/simple_triangle_mesh.cpp +++ b/src/simple_triangle_mesh.cpp @@ -5,6 +5,8 @@ #include "polyscope/pick.h" #include "polyscope/polyscope.h" #include "polyscope/render/engine.h" +#include "polyscope/simple_triangle_mesh_color_quantity.h" +#include "polyscope/simple_triangle_mesh_scalar_quantity.h" #include "imgui.h" @@ -349,4 +351,24 @@ SimpleTriangleMesh* SimpleTriangleMesh::setBackFaceColor(glm::vec3 val) { glm::vec3 SimpleTriangleMesh::getBackFaceColor() { return backFaceColor.get(); } +// === Quantity adder implementations + +SimpleTriangleMeshScalarQuantity* SimpleTriangleMesh::addVertexScalarQuantityImpl(std::string name, + const std::vector& data, + DataType type) { + checkForQuantityWithNameAndDeleteOrError(name); + SimpleTriangleMeshScalarQuantity* q = new SimpleTriangleMeshScalarQuantity(name, data, "vertex", *this, type); + addQuantity(q); + return q; +} + +SimpleTriangleMeshColorQuantity* SimpleTriangleMesh::addVertexColorQuantityImpl(std::string name, + const std::vector& colors) { + checkForQuantityWithNameAndDeleteOrError(name); + SimpleTriangleMeshColorQuantity* q = new SimpleTriangleMeshColorQuantity(name, colors, "vertex", *this); + addQuantity(q); + return q; +} + + } // namespace polyscope diff --git a/src/simple_triangle_mesh_color_quantity.cpp b/src/simple_triangle_mesh_color_quantity.cpp new file mode 100644 index 00000000..3e6430ef --- /dev/null +++ b/src/simple_triangle_mesh_color_quantity.cpp @@ -0,0 +1,68 @@ +// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run + +#include "polyscope/simple_triangle_mesh_color_quantity.h" + +#include "polyscope/polyscope.h" +#include "polyscope/simple_triangle_mesh.h" + +#include "imgui.h" + +namespace polyscope { + +SimpleTriangleMeshColorQuantity::SimpleTriangleMeshColorQuantity(std::string name, + const std::vector& colors_, + std::string definedOn_, + SimpleTriangleMesh& mesh_) + : SimpleTriangleMeshQuantity(name, mesh_, true), ColorQuantity(*this, colors_), definedOn(definedOn_) {} + +void SimpleTriangleMeshColorQuantity::draw() { + if (!isEnabled()) return; + + if (program == nullptr) createProgram(); + + parent.setStructureUniforms(*program); + parent.setSimpleTriangleMeshUniforms(*program); + setColorUniforms(*program); + render::engine->setMaterialUniforms(*program, parent.getMaterial()); + + program->draw(); +} + +void SimpleTriangleMeshColorQuantity::buildCustomUI() { + ImGui::SameLine(); + if (ImGui::Button("Options")) { + ImGui::OpenPopup("OptionsPopup"); + } + if (ImGui::BeginPopup("OptionsPopup")) { + buildColorOptionsUI(); + ImGui::EndPopup(); + } + buildColorUI(); +} + +void SimpleTriangleMeshColorQuantity::createProgram() { + // clang-format off + program = render::engine->requestShader("SIMPLE_MESH", + render::engine->addMaterialRules(parent.getMaterial(), + addColorRules( + parent.addSimpleTriangleMeshRules( + {"MESH_PROPAGATE_COLOR", "SHADE_COLOR", "COMPUTE_SHADE_NORMAL_FROM_POSITION", "PROJ_AND_INV_PROJ_MAT"} + ) + ) + ) + ); + // clang-format on + + parent.setSimpleTriangleMeshProgramGeometryAttributes(*program); + program->setAttribute("a_color", colors.getRenderAttributeBuffer()); + render::engine->setMaterial(*program, parent.getMaterial()); +} + +void SimpleTriangleMeshColorQuantity::refresh() { + program.reset(); + Quantity::refresh(); +} + +std::string SimpleTriangleMeshColorQuantity::niceName() { return name + " (" + definedOn + " color)"; } + +} // namespace polyscope diff --git a/src/simple_triangle_mesh_quantity.cpp b/src/simple_triangle_mesh_quantity.cpp new file mode 100644 index 00000000..aa917224 --- /dev/null +++ b/src/simple_triangle_mesh_quantity.cpp @@ -0,0 +1,13 @@ +// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run + +#include "polyscope/simple_triangle_mesh_quantity.h" + +#include "polyscope/simple_triangle_mesh.h" + +namespace polyscope { + +SimpleTriangleMeshQuantity::SimpleTriangleMeshQuantity(std::string name, SimpleTriangleMesh& parentStructure, + bool dominates) + : Quantity(name, parentStructure, dominates), parent(parentStructure) {} + +} // namespace polyscope diff --git a/src/simple_triangle_mesh_scalar_quantity.cpp b/src/simple_triangle_mesh_scalar_quantity.cpp new file mode 100644 index 00000000..1e48ce04 --- /dev/null +++ b/src/simple_triangle_mesh_scalar_quantity.cpp @@ -0,0 +1,69 @@ +// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run + +#include "polyscope/simple_triangle_mesh_scalar_quantity.h" + +#include "polyscope/polyscope.h" +#include "polyscope/simple_triangle_mesh.h" + +#include "imgui.h" + +namespace polyscope { + +SimpleTriangleMeshScalarQuantity::SimpleTriangleMeshScalarQuantity(std::string name, const std::vector& values_, + std::string definedOn_, + SimpleTriangleMesh& mesh_, DataType dataType_) + : SimpleTriangleMeshQuantity(name, mesh_, true), ScalarQuantity(*this, values_, dataType_), + definedOn(definedOn_) {} + +void SimpleTriangleMeshScalarQuantity::draw() { + if (!isEnabled()) return; + + if (program == nullptr) createProgram(); + + parent.setStructureUniforms(*program); + parent.setSimpleTriangleMeshUniforms(*program); + setScalarUniforms(*program); + render::engine->setMaterialUniforms(*program, parent.getMaterial()); + + program->draw(); +} + +void SimpleTriangleMeshScalarQuantity::buildCustomUI() { + ImGui::SameLine(); + if (ImGui::Button("Options")) { + ImGui::OpenPopup("OptionsPopup"); + } + if (ImGui::BeginPopup("OptionsPopup")) { + buildScalarOptionsUI(); + ImGui::EndPopup(); + } + buildScalarUI(); +} + +void SimpleTriangleMeshScalarQuantity::createProgram() { + // clang-format off + program = render::engine->requestShader("SIMPLE_MESH", + render::engine->addMaterialRules(parent.getMaterial(), + parent.addSimpleTriangleMeshRules( + addScalarRules( + {"MESH_PROPAGATE_VALUE", "COMPUTE_SHADE_NORMAL_FROM_POSITION", "PROJ_AND_INV_PROJ_MAT"} + ) + ) + ) + ); + // clang-format on + + parent.setSimpleTriangleMeshProgramGeometryAttributes(*program); + program->setAttribute("a_value", values.getRenderAttributeBuffer()); + program->setTextureFromColormap("t_colormap", cMap.get()); + render::engine->setMaterial(*program, parent.getMaterial()); +} + +void SimpleTriangleMeshScalarQuantity::refresh() { + program.reset(); + Quantity::refresh(); +} + +std::string SimpleTriangleMeshScalarQuantity::niceName() { return name + " (" + definedOn + " scalar)"; } + +} // namespace polyscope diff --git a/test/src/surface_mesh_test.cpp b/test/src/surface_mesh_test.cpp index 16b2ca21..bbef8570 100644 --- a/test/src/surface_mesh_test.cpp +++ b/test/src/surface_mesh_test.cpp @@ -665,3 +665,39 @@ TEST_F(PolyscopeTest, SimpleTriangleMesUpdate) { polyscope::removeAllStructures(); } + +TEST_F(PolyscopeTest, SimpleTriangleMeshVertexScalar) { + auto psMesh = registerSimpleTriangleMesh(); + std::vector vScalar(psMesh->nVertices(), 7.); + auto q1 = psMesh->addVertexScalarQuantity("vScalar", vScalar); + q1->setEnabled(true); + polyscope::show(3); + + q1->updateData(vScalar); + polyscope::show(3); + + polyscope::removeAllStructures(); +} + +TEST_F(PolyscopeTest, SimpleTriangleMeshVertexScalarCategorical) { + auto psMesh = registerSimpleTriangleMesh(); + std::vector vScalar(psMesh->nVertices(), 2.); + auto q1 = psMesh->addVertexScalarQuantity("vScalar", vScalar, polyscope::DataType::CATEGORICAL); + q1->setEnabled(true); + polyscope::show(3); + + polyscope::removeAllStructures(); +} + +TEST_F(PolyscopeTest, SimpleTriangleMeshVertexColor) { + auto psMesh = registerSimpleTriangleMesh(); + std::vector vColors(psMesh->nVertices(), glm::vec3{.2, .3, .4}); + auto q1 = psMesh->addVertexColorQuantity("vColor", vColors); + q1->setEnabled(true); + polyscope::show(3); + + q1->updateData(vColors); + polyscope::show(3); + + polyscope::removeAllStructures(); +} From ea5c760c766c8c36b1e8bd17691007300c43d0c8 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Wed, 25 Mar 2026 16:59:41 -0500 Subject: [PATCH 16/31] clean up unfiorms --- src/simple_triangle_mesh.cpp | 7 +++++-- src/simple_triangle_mesh_color_quantity.cpp | 2 +- src/simple_triangle_mesh_scalar_quantity.cpp | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/simple_triangle_mesh.cpp b/src/simple_triangle_mesh.cpp index 8d6128f4..7a6c9463 100644 --- a/src/simple_triangle_mesh.cpp +++ b/src/simple_triangle_mesh.cpp @@ -190,7 +190,7 @@ void SimpleTriangleMesh::ensureRenderProgramPrepared() { render::engine->addMaterialRules(getMaterial(), addSimpleTriangleMeshRules( { - "SHADE_BASECOLOR", "COMPUTE_SHADE_NORMAL_FROM_POSITION", "PROJ_AND_INV_PROJ_MAT" + "SHADE_BASECOLOR" } ) ) @@ -211,7 +211,7 @@ void SimpleTriangleMesh::ensurePickProgramPrepared() { pickProgram = render::engine->requestShader("SIMPLE_MESH", addSimpleTriangleMeshRules( { - "SHADECOLOR_FROM_UNIFORM", "COMPUTE_SHADE_NORMAL_FROM_POSITION", "PROJ_AND_INV_PROJ_MAT" + "SHADECOLOR_FROM_UNIFORM" } , false), render::ShaderReplacementDefaults::Pick ); @@ -231,6 +231,9 @@ std::vector SimpleTriangleMesh::addSimpleTriangleMeshRules(std::vec initRules = addStructureRules(initRules); + initRules.push_back("COMPUTE_SHADE_NORMAL_FROM_POSITION"); + initRules.push_back("PROJ_AND_INV_PROJ_MAT"); + if (withSurfaceShade) { // rules that only get used when we're shading the surface of the mesh diff --git a/src/simple_triangle_mesh_color_quantity.cpp b/src/simple_triangle_mesh_color_quantity.cpp index 3e6430ef..2342c204 100644 --- a/src/simple_triangle_mesh_color_quantity.cpp +++ b/src/simple_triangle_mesh_color_quantity.cpp @@ -46,7 +46,7 @@ void SimpleTriangleMeshColorQuantity::createProgram() { render::engine->addMaterialRules(parent.getMaterial(), addColorRules( parent.addSimpleTriangleMeshRules( - {"MESH_PROPAGATE_COLOR", "SHADE_COLOR", "COMPUTE_SHADE_NORMAL_FROM_POSITION", "PROJ_AND_INV_PROJ_MAT"} + {"MESH_PROPAGATE_COLOR", "SHADE_COLOR"} ) ) ) diff --git a/src/simple_triangle_mesh_scalar_quantity.cpp b/src/simple_triangle_mesh_scalar_quantity.cpp index 1e48ce04..bc3026d8 100644 --- a/src/simple_triangle_mesh_scalar_quantity.cpp +++ b/src/simple_triangle_mesh_scalar_quantity.cpp @@ -46,7 +46,7 @@ void SimpleTriangleMeshScalarQuantity::createProgram() { render::engine->addMaterialRules(parent.getMaterial(), parent.addSimpleTriangleMeshRules( addScalarRules( - {"MESH_PROPAGATE_VALUE", "COMPUTE_SHADE_NORMAL_FROM_POSITION", "PROJ_AND_INV_PROJ_MAT"} + {"MESH_PROPAGATE_VALUE"} ) ) ) From 40d6d7b7b8c6da98c5a86c828eeb7bde5fbd5dcd Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sun, 5 Apr 2026 09:43:48 -0700 Subject: [PATCH 17/31] quantities on simple triangle meshes --- examples/demo-app/demo_app.cpp | 3 + .../opengl/shaders/surface_mesh_shaders.h | 2 + include/polyscope/simple_triangle_mesh.h | 28 ++++++-- include/polyscope/simple_triangle_mesh.ipp | 19 +++++- .../simple_triangle_mesh_color_quantity.h | 32 +++++++++- .../simple_triangle_mesh_scalar_quantity.h | 31 ++++++++- src/render/mock_opengl/mock_gl_engine.cpp | 2 + src/render/opengl/gl_engine.cpp | 2 + .../opengl/shaders/surface_mesh_shaders.cpp | 34 ++++++++++ src/simple_triangle_mesh.cpp | 29 +++++++-- src/simple_triangle_mesh_color_quantity.cpp | 54 ++++++++++++++-- src/simple_triangle_mesh_scalar_quantity.cpp | 64 ++++++++++++++++--- test/src/surface_mesh_test.cpp | 26 ++++++++ 13 files changed, 291 insertions(+), 35 deletions(-) diff --git a/examples/demo-app/demo_app.cpp b/examples/demo-app/demo_app.cpp index 566abcc2..8a2cae4c 100644 --- a/examples/demo-app/demo_app.cpp +++ b/examples/demo-app/demo_app.cpp @@ -190,6 +190,9 @@ void processFileOBJ(std::string filename) { polyscope::getSurfaceMesh(niceName)->addFaceScalarQuantity("categorical face", fCat, polyscope::DataType::CATEGORICAL); + psSimpleMesh->addFaceScalarQuantity("face area", fArea, polyscope::DataType::MAGNITUDE); + psSimpleMesh->addFaceColorQuantity("fColor", fColor); + // size_t nEdges = psMesh->nEdges(); diff --git a/include/polyscope/render/opengl/shaders/surface_mesh_shaders.h b/include/polyscope/render/opengl/shaders/surface_mesh_shaders.h index d89d3433..b5e9484b 100644 --- a/include/polyscope/render/opengl/shaders/surface_mesh_shaders.h +++ b/include/polyscope/render/opengl/shaders/surface_mesh_shaders.h @@ -36,6 +36,8 @@ extern const ShaderReplacementRule MESH_PROPAGATE_CULLPOS; extern const ShaderReplacementRule MESH_PROPAGATE_PICK; extern const ShaderReplacementRule MESH_PROPAGATE_PICK_SIMPLE; extern const ShaderReplacementRule MESH_PROPAGATE_TYPE_AND_BASECOLOR2_SHADE; +extern const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_VALUE; +extern const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_COLOR; } // namespace backend_openGL3 diff --git a/include/polyscope/simple_triangle_mesh.h b/include/polyscope/simple_triangle_mesh.h index ad600f7c..b4c79a32 100644 --- a/include/polyscope/simple_triangle_mesh.h +++ b/include/polyscope/simple_triangle_mesh.h @@ -24,7 +24,11 @@ class SimpleTriangleMesh; // Forward declare quantity types class SimpleTriangleMeshScalarQuantity; +class SimpleTriangleMeshVertexScalarQuantity; +class SimpleTriangleMeshFaceScalarQuantity; class SimpleTriangleMeshColorQuantity; +class SimpleTriangleMeshVertexColorQuantity; +class SimpleTriangleMeshFaceColorQuantity; struct SimpleTriangleMeshPickResult { // this does nothing for now, just matching pattern from other structures @@ -64,12 +68,19 @@ class SimpleTriangleMesh : public Structure { // Scalars template - SimpleTriangleMeshScalarQuantity* addVertexScalarQuantity(std::string name, const T& values, - DataType type = DataType::STANDARD); + SimpleTriangleMeshVertexScalarQuantity* addVertexScalarQuantity(std::string name, const T& values, + DataType type = DataType::STANDARD); + + template + SimpleTriangleMeshFaceScalarQuantity* addFaceScalarQuantity(std::string name, const T& values, + DataType type = DataType::STANDARD); // Colors template - SimpleTriangleMeshColorQuantity* addVertexColorQuantity(std::string name, const T& values); + SimpleTriangleMeshVertexColorQuantity* addVertexColorQuantity(std::string name, const T& values); + + template + SimpleTriangleMeshFaceColorQuantity* addFaceColorQuantity(std::string name, const T& values); // === Mutate @@ -130,10 +141,13 @@ class SimpleTriangleMesh : public Structure { void setPickUniforms(render::ShaderProgram& p); // === Quantity adder implementations - SimpleTriangleMeshScalarQuantity* addVertexScalarQuantityImpl(std::string name, const std::vector& data, - DataType type); - SimpleTriangleMeshColorQuantity* addVertexColorQuantityImpl(std::string name, - const std::vector& colors); + SimpleTriangleMeshVertexScalarQuantity* addVertexScalarQuantityImpl(std::string name, const std::vector& data, + DataType type); + SimpleTriangleMeshFaceScalarQuantity* addFaceScalarQuantityImpl(std::string name, const std::vector& data, + DataType type); + SimpleTriangleMeshVertexColorQuantity* addVertexColorQuantityImpl(std::string name, + const std::vector& colors); + SimpleTriangleMeshFaceColorQuantity* addFaceColorQuantityImpl(std::string name, const std::vector& colors); // == Picking related things size_t pickStart; diff --git a/include/polyscope/simple_triangle_mesh.ipp b/include/polyscope/simple_triangle_mesh.ipp index 6b49b02f..05cb1b5a 100644 --- a/include/polyscope/simple_triangle_mesh.ipp +++ b/include/polyscope/simple_triangle_mesh.ipp @@ -54,18 +54,31 @@ void SimpleTriangleMesh::update(const V& newPositions, const F& newFaces) { // ===================================================== template -SimpleTriangleMeshScalarQuantity* SimpleTriangleMesh::addVertexScalarQuantity(std::string name, const T& values, - DataType type) { +SimpleTriangleMeshVertexScalarQuantity* SimpleTriangleMesh::addVertexScalarQuantity(std::string name, const T& values, + DataType type) { validateSize(values, nVertices(), "vertex scalar quantity " + name); return addVertexScalarQuantityImpl(name, standardizeArray(values), type); } template -SimpleTriangleMeshColorQuantity* SimpleTriangleMesh::addVertexColorQuantity(std::string name, const T& values) { +SimpleTriangleMeshFaceScalarQuantity* SimpleTriangleMesh::addFaceScalarQuantity(std::string name, const T& values, + DataType type) { + validateSize(values, nFaces(), "face scalar quantity " + name); + return addFaceScalarQuantityImpl(name, standardizeArray(values), type); +} + +template +SimpleTriangleMeshVertexColorQuantity* SimpleTriangleMesh::addVertexColorQuantity(std::string name, const T& values) { validateSize(values, nVertices(), "vertex color quantity " + name); return addVertexColorQuantityImpl(name, standardizeVectorArray(values)); } +template +SimpleTriangleMeshFaceColorQuantity* SimpleTriangleMesh::addFaceColorQuantity(std::string name, const T& values) { + validateSize(values, nFaces(), "face color quantity " + name); + return addFaceColorQuantityImpl(name, standardizeVectorArray(values)); +} + // Shorthand to get a mesh from polyscope inline SimpleTriangleMesh* getSimpleTriangleMesh(std::string name) { diff --git a/include/polyscope/simple_triangle_mesh_color_quantity.h b/include/polyscope/simple_triangle_mesh_color_quantity.h index 2383511e..7e8bce09 100644 --- a/include/polyscope/simple_triangle_mesh_color_quantity.h +++ b/include/polyscope/simple_triangle_mesh_color_quantity.h @@ -20,11 +20,39 @@ class SimpleTriangleMeshColorQuantity : public SimpleTriangleMeshQuantity, virtual void refresh() override; virtual std::string niceName() override; - std::string definedOn; + const std::string definedOn; protected: - void createProgram(); std::shared_ptr program; + + virtual void createProgram() = 0; +}; + + +// ======================================================== +// ========== Vertex Color ========== +// ======================================================== + +class SimpleTriangleMeshVertexColorQuantity : public SimpleTriangleMeshColorQuantity { +public: + SimpleTriangleMeshVertexColorQuantity(std::string name, const std::vector& colors, + SimpleTriangleMesh& mesh); + + virtual void createProgram() override; }; + +// ======================================================== +// ========== Face Color ========== +// ======================================================== + +class SimpleTriangleMeshFaceColorQuantity : public SimpleTriangleMeshColorQuantity { +public: + SimpleTriangleMeshFaceColorQuantity(std::string name, const std::vector& colors, + SimpleTriangleMesh& mesh); + + virtual void createProgram() override; +}; + + } // namespace polyscope diff --git a/include/polyscope/simple_triangle_mesh_scalar_quantity.h b/include/polyscope/simple_triangle_mesh_scalar_quantity.h index 729b24a7..cae5277d 100644 --- a/include/polyscope/simple_triangle_mesh_scalar_quantity.h +++ b/include/polyscope/simple_triangle_mesh_scalar_quantity.h @@ -22,12 +22,39 @@ class SimpleTriangleMeshScalarQuantity : public SimpleTriangleMeshQuantity, virtual void refresh() override; virtual std::string niceName() override; + const std::string definedOn; protected: - std::string definedOn; std::shared_ptr program; - void createProgram(); + virtual void createProgram() = 0; }; + +// ======================================================== +// ========== Vertex Scalar ========== +// ======================================================== + +class SimpleTriangleMeshVertexScalarQuantity : public SimpleTriangleMeshScalarQuantity { +public: + SimpleTriangleMeshVertexScalarQuantity(std::string name, const std::vector& values, SimpleTriangleMesh& mesh, + DataType dataType = DataType::STANDARD); + + virtual void createProgram() override; +}; + + +// ======================================================== +// ========== Face Scalar ========== +// ======================================================== + +class SimpleTriangleMeshFaceScalarQuantity : public SimpleTriangleMeshScalarQuantity { +public: + SimpleTriangleMeshFaceScalarQuantity(std::string name, const std::vector& values, SimpleTriangleMesh& mesh, + DataType dataType = DataType::STANDARD); + + virtual void createProgram() override; +}; + + } // namespace polyscope diff --git a/src/render/mock_opengl/mock_gl_engine.cpp b/src/render/mock_opengl/mock_gl_engine.cpp index 09d1438e..62b7a7de 100644 --- a/src/render/mock_opengl/mock_gl_engine.cpp +++ b/src/render/mock_opengl/mock_gl_engine.cpp @@ -2266,6 +2266,8 @@ void MockGLEngine::populateDefaultShadersAndRules() { registerShaderRule("MESH_PROPAGATE_TYPE_AND_BASECOLOR2_SHADE", MESH_PROPAGATE_TYPE_AND_BASECOLOR2_SHADE); registerShaderRule("MESH_PROPAGATE_PICK", MESH_PROPAGATE_PICK); registerShaderRule("MESH_PROPAGATE_PICK_SIMPLE", MESH_PROPAGATE_PICK_SIMPLE); + registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_VALUE", SIMPLE_MESH_PROPAGATE_FACE_VALUE); + registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_COLOR", SIMPLE_MESH_PROPAGATE_FACE_COLOR); // volume gridcube things registerShaderRule("GRIDCUBE_PROPAGATE_NODE_VALUE", GRIDCUBE_PROPAGATE_NODE_VALUE); diff --git a/src/render/opengl/gl_engine.cpp b/src/render/opengl/gl_engine.cpp index 0678b658..94fa7030 100644 --- a/src/render/opengl/gl_engine.cpp +++ b/src/render/opengl/gl_engine.cpp @@ -2762,6 +2762,8 @@ void GLEngine::populateDefaultShadersAndRules() { registerShaderRule("MESH_PROPAGATE_TYPE_AND_BASECOLOR2_SHADE", MESH_PROPAGATE_TYPE_AND_BASECOLOR2_SHADE); registerShaderRule("MESH_PROPAGATE_PICK", MESH_PROPAGATE_PICK); registerShaderRule("MESH_PROPAGATE_PICK_SIMPLE", MESH_PROPAGATE_PICK_SIMPLE); + registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_VALUE", SIMPLE_MESH_PROPAGATE_FACE_VALUE); + registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_COLOR", SIMPLE_MESH_PROPAGATE_FACE_COLOR); // volume gridcube things registerShaderRule("GRIDCUBE_PROPAGATE_NODE_VALUE", GRIDCUBE_PROPAGATE_NODE_VALUE); diff --git a/src/render/opengl/shaders/surface_mesh_shaders.cpp b/src/render/opengl/shaders/surface_mesh_shaders.cpp index 860cb2fc..fae61087 100644 --- a/src/render/opengl/shaders/surface_mesh_shaders.cpp +++ b/src/render/opengl/shaders/surface_mesh_shaders.cpp @@ -731,6 +731,40 @@ const ShaderReplacementRule MESH_PROPAGATE_PICK_SIMPLE ( // this one does faces ); +// Uses gl_PrimitiveID to look up per-face scalar data from a 1D texture. +// Requires indexed triangle drawing (DrawMode::IndexedTriangles) where primitive index == face index. +const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_VALUE( + /* rule name */ "SIMPLE_MESH_PROPAGATE_FACE_VALUE", + { /* replacement sources */ + {"FRAG_DECLARATIONS", R"( + uniform sampler1D t_faceValues; + )"}, + {"GENERATE_SHADE_VALUE", R"( + float shadeValue = texelFetch(t_faceValues, gl_PrimitiveID, 0).r; + )"}, + }, + /* uniforms */ {}, + /* attributes */ {}, + /* textures */ {{"t_faceValues", 1}} +); + +// Uses gl_PrimitiveID to look up per-face color data from a 1D texture. +// Requires indexed triangle drawing (DrawMode::IndexedTriangles) where primitive index == face index. +const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_COLOR( + /* rule name */ "SIMPLE_MESH_PROPAGATE_FACE_COLOR", + { /* replacement sources */ + {"FRAG_DECLARATIONS", R"( + uniform sampler1D t_faceColors; + )"}, + {"GENERATE_SHADE_VALUE", R"( + vec3 shadeColor = texelFetch(t_faceColors, gl_PrimitiveID, 0).rgb; + )"}, + }, + /* uniforms */ {}, + /* attributes */ {}, + /* textures */ {{"t_faceColors", 1}} +); + // clang-format on } // namespace backend_openGL3 diff --git a/src/simple_triangle_mesh.cpp b/src/simple_triangle_mesh.cpp index 7a6c9463..2791ca90 100644 --- a/src/simple_triangle_mesh.cpp +++ b/src/simple_triangle_mesh.cpp @@ -356,19 +356,34 @@ glm::vec3 SimpleTriangleMesh::getBackFaceColor() { return backFaceColor.get(); } // === Quantity adder implementations -SimpleTriangleMeshScalarQuantity* SimpleTriangleMesh::addVertexScalarQuantityImpl(std::string name, - const std::vector& data, - DataType type) { +SimpleTriangleMeshVertexScalarQuantity* +SimpleTriangleMesh::addVertexScalarQuantityImpl(std::string name, const std::vector& data, DataType type) { checkForQuantityWithNameAndDeleteOrError(name); - SimpleTriangleMeshScalarQuantity* q = new SimpleTriangleMeshScalarQuantity(name, data, "vertex", *this, type); + SimpleTriangleMeshVertexScalarQuantity* q = new SimpleTriangleMeshVertexScalarQuantity(name, data, *this, type); addQuantity(q); return q; } -SimpleTriangleMeshColorQuantity* SimpleTriangleMesh::addVertexColorQuantityImpl(std::string name, - const std::vector& colors) { +SimpleTriangleMeshFaceScalarQuantity* +SimpleTriangleMesh::addFaceScalarQuantityImpl(std::string name, const std::vector& data, DataType type) { checkForQuantityWithNameAndDeleteOrError(name); - SimpleTriangleMeshColorQuantity* q = new SimpleTriangleMeshColorQuantity(name, colors, "vertex", *this); + SimpleTriangleMeshFaceScalarQuantity* q = new SimpleTriangleMeshFaceScalarQuantity(name, data, *this, type); + addQuantity(q); + return q; +} + +SimpleTriangleMeshVertexColorQuantity* +SimpleTriangleMesh::addVertexColorQuantityImpl(std::string name, const std::vector& colors) { + checkForQuantityWithNameAndDeleteOrError(name); + SimpleTriangleMeshVertexColorQuantity* q = new SimpleTriangleMeshVertexColorQuantity(name, colors, *this); + addQuantity(q); + return q; +} + +SimpleTriangleMeshFaceColorQuantity* +SimpleTriangleMesh::addFaceColorQuantityImpl(std::string name, const std::vector& colors) { + checkForQuantityWithNameAndDeleteOrError(name); + SimpleTriangleMeshFaceColorQuantity* q = new SimpleTriangleMeshFaceColorQuantity(name, colors, *this); addQuantity(q); return q; } diff --git a/src/simple_triangle_mesh_color_quantity.cpp b/src/simple_triangle_mesh_color_quantity.cpp index 2342c204..8d40bba4 100644 --- a/src/simple_triangle_mesh_color_quantity.cpp +++ b/src/simple_triangle_mesh_color_quantity.cpp @@ -9,6 +9,10 @@ namespace polyscope { +// ======================================================== +// ========== Color Quantity Base ========== +// ======================================================== + SimpleTriangleMeshColorQuantity::SimpleTriangleMeshColorQuantity(std::string name, const std::vector& colors_, std::string definedOn_, @@ -40,7 +44,24 @@ void SimpleTriangleMeshColorQuantity::buildCustomUI() { buildColorUI(); } -void SimpleTriangleMeshColorQuantity::createProgram() { +void SimpleTriangleMeshColorQuantity::refresh() { + program.reset(); + Quantity::refresh(); +} + +std::string SimpleTriangleMeshColorQuantity::niceName() { return name + " (" + definedOn + " color)"; } + + +// ======================================================== +// ========== Vertex Color ========== +// ======================================================== + +SimpleTriangleMeshVertexColorQuantity::SimpleTriangleMeshVertexColorQuantity(std::string name, + const std::vector& colors_, + SimpleTriangleMesh& mesh_) + : SimpleTriangleMeshColorQuantity(name, colors_, "vertex", mesh_) {} + +void SimpleTriangleMeshVertexColorQuantity::createProgram() { // clang-format off program = render::engine->requestShader("SIMPLE_MESH", render::engine->addMaterialRules(parent.getMaterial(), @@ -58,11 +79,34 @@ void SimpleTriangleMeshColorQuantity::createProgram() { render::engine->setMaterial(*program, parent.getMaterial()); } -void SimpleTriangleMeshColorQuantity::refresh() { - program.reset(); - Quantity::refresh(); + +// ======================================================== +// ========== Face Color ========== +// ======================================================== + +SimpleTriangleMeshFaceColorQuantity::SimpleTriangleMeshFaceColorQuantity(std::string name, + const std::vector& colors_, + SimpleTriangleMesh& mesh_) + : SimpleTriangleMeshColorQuantity(name, colors_, "face", mesh_) { + colors.setTextureSize(parent.nFaces()); } -std::string SimpleTriangleMeshColorQuantity::niceName() { return name + " (" + definedOn + " color)"; } +void SimpleTriangleMeshFaceColorQuantity::createProgram() { + // clang-format off + program = render::engine->requestShader("SIMPLE_MESH", + render::engine->addMaterialRules(parent.getMaterial(), + addColorRules( + parent.addSimpleTriangleMeshRules( + {"SIMPLE_MESH_PROPAGATE_FACE_COLOR", "SHADE_COLOR"} + ) + ) + ) + ); + // clang-format on + + parent.setSimpleTriangleMeshProgramGeometryAttributes(*program); + program->setTextureFromBuffer("t_faceColors", colors.getRenderTextureBuffer().get()); + render::engine->setMaterial(*program, parent.getMaterial()); +} } // namespace polyscope diff --git a/src/simple_triangle_mesh_scalar_quantity.cpp b/src/simple_triangle_mesh_scalar_quantity.cpp index bc3026d8..e13a2c94 100644 --- a/src/simple_triangle_mesh_scalar_quantity.cpp +++ b/src/simple_triangle_mesh_scalar_quantity.cpp @@ -9,11 +9,14 @@ namespace polyscope { +// ======================================================== +// ========== Scalar Quantity Base ========== +// ======================================================== + SimpleTriangleMeshScalarQuantity::SimpleTriangleMeshScalarQuantity(std::string name, const std::vector& values_, - std::string definedOn_, - SimpleTriangleMesh& mesh_, DataType dataType_) - : SimpleTriangleMeshQuantity(name, mesh_, true), ScalarQuantity(*this, values_, dataType_), - definedOn(definedOn_) {} + std::string definedOn_, SimpleTriangleMesh& mesh_, + DataType dataType_) + : SimpleTriangleMeshQuantity(name, mesh_, true), ScalarQuantity(*this, values_, dataType_), definedOn(definedOn_) {} void SimpleTriangleMeshScalarQuantity::draw() { if (!isEnabled()) return; @@ -40,7 +43,25 @@ void SimpleTriangleMeshScalarQuantity::buildCustomUI() { buildScalarUI(); } -void SimpleTriangleMeshScalarQuantity::createProgram() { +void SimpleTriangleMeshScalarQuantity::refresh() { + program.reset(); + Quantity::refresh(); +} + +std::string SimpleTriangleMeshScalarQuantity::niceName() { return name + " (" + definedOn + " scalar)"; } + + +// ======================================================== +// ========== Vertex Scalar ========== +// ======================================================== + +SimpleTriangleMeshVertexScalarQuantity::SimpleTriangleMeshVertexScalarQuantity(std::string name, + const std::vector& values_, + SimpleTriangleMesh& mesh_, + DataType dataType_) + : SimpleTriangleMeshScalarQuantity(name, values_, "vertex", mesh_, dataType_) {} + +void SimpleTriangleMeshVertexScalarQuantity::createProgram() { // clang-format off program = render::engine->requestShader("SIMPLE_MESH", render::engine->addMaterialRules(parent.getMaterial(), @@ -59,11 +80,36 @@ void SimpleTriangleMeshScalarQuantity::createProgram() { render::engine->setMaterial(*program, parent.getMaterial()); } -void SimpleTriangleMeshScalarQuantity::refresh() { - program.reset(); - Quantity::refresh(); + +// ======================================================== +// ========== Face Scalar ========== +// ======================================================== + +SimpleTriangleMeshFaceScalarQuantity::SimpleTriangleMeshFaceScalarQuantity(std::string name, + const std::vector& values_, + SimpleTriangleMesh& mesh_, + DataType dataType_) + : SimpleTriangleMeshScalarQuantity(name, values_, "face", mesh_, dataType_) { + values.setTextureSize(parent.nFaces()); } -std::string SimpleTriangleMeshScalarQuantity::niceName() { return name + " (" + definedOn + " scalar)"; } +void SimpleTriangleMeshFaceScalarQuantity::createProgram() { + // clang-format off + program = render::engine->requestShader("SIMPLE_MESH", + render::engine->addMaterialRules(parent.getMaterial(), + parent.addSimpleTriangleMeshRules( + addScalarRules( + {"SIMPLE_MESH_PROPAGATE_FACE_VALUE"} + ) + ) + ) + ); + // clang-format on + + parent.setSimpleTriangleMeshProgramGeometryAttributes(*program); + program->setTextureFromBuffer("t_faceValues", values.getRenderTextureBuffer().get()); + program->setTextureFromColormap("t_colormap", cMap.get()); + render::engine->setMaterial(*program, parent.getMaterial()); +} } // namespace polyscope diff --git a/test/src/surface_mesh_test.cpp b/test/src/surface_mesh_test.cpp index bbef8570..ed2d3402 100644 --- a/test/src/surface_mesh_test.cpp +++ b/test/src/surface_mesh_test.cpp @@ -701,3 +701,29 @@ TEST_F(PolyscopeTest, SimpleTriangleMeshVertexColor) { polyscope::removeAllStructures(); } + +TEST_F(PolyscopeTest, SimpleTriangleMeshFaceScalar) { + auto psMesh = registerSimpleTriangleMesh(); + std::vector fScalar(psMesh->nFaces(), 7.); + auto q1 = psMesh->addFaceScalarQuantity("fScalar", fScalar); + q1->setEnabled(true); + polyscope::show(3); + + q1->updateData(fScalar); + polyscope::show(3); + + polyscope::removeAllStructures(); +} + +TEST_F(PolyscopeTest, SimpleTriangleMeshFaceColor) { + auto psMesh = registerSimpleTriangleMesh(); + std::vector fColors(psMesh->nFaces(), glm::vec3{.2, .3, .4}); + auto q1 = psMesh->addFaceColorQuantity("fColor", fColors); + q1->setEnabled(true); + polyscope::show(3); + + q1->updateData(fColors); + polyscope::show(3); + + polyscope::removeAllStructures(); +} From adc7de32ab9908bb87380a075ff7625592e3dfaa Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sun, 5 Apr 2026 21:28:10 -0700 Subject: [PATCH 18/31] clean up quantities, implement picking for SimpleTriangleMesh --- include/polyscope/pick.ipp | 50 ++++++ .../opengl/shaders/surface_mesh_shaders.h | 1 + include/polyscope/simple_triangle_mesh.h | 14 +- .../simple_triangle_mesh_color_quantity.h | 3 + .../polyscope/simple_triangle_mesh_quantity.h | 7 +- .../simple_triangle_mesh_scalar_quantity.h | 2 + src/render/mock_opengl/mock_gl_engine.cpp | 3 +- src/render/opengl/gl_engine.cpp | 1 + src/render/opengl/shaders/common.cpp | 13 +- .../opengl/shaders/surface_mesh_shaders.cpp | 22 +++ src/simple_triangle_mesh.cpp | 153 ++++++++++++++++-- src/simple_triangle_mesh_color_quantity.cpp | 28 +++- src/simple_triangle_mesh_scalar_quantity.cpp | 14 ++ test/src/misc_test.cpp | 37 +++++ 14 files changed, 323 insertions(+), 25 deletions(-) diff --git a/include/polyscope/pick.ipp b/include/polyscope/pick.ipp index 9d4a3476..da4b3299 100644 --- a/include/polyscope/pick.ipp +++ b/include/polyscope/pick.ipp @@ -13,6 +13,8 @@ const uint64_t bitsForPickPacking = 22; inline glm::vec3 indToVec(size_t globalInd) { + // NOTE: there is a duplicate version of this logic in a macro below, which must be kept in sync. + // Can comfortably fit a 22 bit integer exactly in a single precision float uint64_t factor = 1 << bitsForPickPacking; uint64_t mask = factor - 1; @@ -48,5 +50,53 @@ inline uint64_t vecToInd(glm::vec3 vec) { return ind; } + +// == Weird alternate implementation of indToVec() +// This is here because sometimes we want to evaluate the indToVec() bit-bashing logic in a shader, and get exactly +// the same result as the C++ version above. A GLSL implementation is not too bad, but it's hard to test to ensure +// it really matches. +// +// The solution here is a funky macro'd implementation, that compiles as C++ or GLSL. We compile it as C++ in the tests +// and verify it matches the usual indToVec() implementation, then compile it as GLSL in shaders. +// +// All of the macro stuff you see below is just boilerplate to make that possible. + +#define POLYSCOPE_PICK_STR_H(...) #__VA_ARGS__ +#define POLYSCOPE_PICK_STR(...) POLYSCOPE_PICK_STR_H(__VA_ARGS__) + +// See note above. This is logic that is meant to be identical to indToVec(). +// clang-format off +#define POLYSCOPE_PICK_INDEX_COLOR_BODY \ + POLYSCOPE_PICK_UINT idxLow = pickStartLow + primID; \ + POLYSCOPE_PICK_UINT carry = (idxLow < pickStartLow) ? 1u : 0u; \ + POLYSCOPE_PICK_UINT idxHigh = pickStartHigh + carry; \ + POLYSCOPE_PICK_UINT low22 = idxLow & 0x3FFFFFu; \ + POLYSCOPE_PICK_UINT med22 = ((idxLow >> 22u) | (idxHigh << 10u)) & 0x3FFFFFu; \ + POLYSCOPE_PICK_UINT high22 = (idxHigh >> 12u) & 0x3FFFFFu; \ + return POLYSCOPE_PICK_VEC3(float(low22), float(med22), float(high22)) / 4194304.0f; +// clang-format on + +// C++ version: compile the body with C++ types. +#define POLYSCOPE_PICK_UINT uint32_t +#define POLYSCOPE_PICK_VEC3 glm::vec3 +inline glm::vec3 pickIndexToColorImpl(uint32_t pickStartLow, uint32_t pickStartHigh, uint32_t primID) { + POLYSCOPE_PICK_INDEX_COLOR_BODY +} +#undef POLYSCOPE_PICK_UINT +#undef POLYSCOPE_PICK_VEC3 + +// Convenience wrapper for C++ callers that have a combined uint64_t pickStart. +inline glm::vec3 pickIndexToColor(uint64_t pickStart, uint32_t primID) { + return pickIndexToColorImpl(static_cast(pickStart & 0xFFFFFFFFull), static_cast(pickStart >> 32), + primID); +} + +// GLSL string macro. Stringifies POLYSCOPE_PICK_INDEX_COLOR_BODY using GLSL type names. +// REQUIRES: POLYSCOPE_PICK_UINT must equal "uint" and POLYSCOPE_PICK_VEC3 must equal "vec3" +// at the point of expansion (see common.cpp for the usage pattern). +#define POLYSCOPE_PICK_INDEX_TO_COLOR_GLSL \ + "vec3 pickIndexToColor(uint pickStartLow, uint pickStartHigh, uint primID) { " POLYSCOPE_PICK_STR( \ + POLYSCOPE_PICK_INDEX_COLOR_BODY) " }" + } // namespace pick } // namespace polyscope diff --git a/include/polyscope/render/opengl/shaders/surface_mesh_shaders.h b/include/polyscope/render/opengl/shaders/surface_mesh_shaders.h index b5e9484b..9185b6c1 100644 --- a/include/polyscope/render/opengl/shaders/surface_mesh_shaders.h +++ b/include/polyscope/render/opengl/shaders/surface_mesh_shaders.h @@ -38,6 +38,7 @@ extern const ShaderReplacementRule MESH_PROPAGATE_PICK_SIMPLE; extern const ShaderReplacementRule MESH_PROPAGATE_TYPE_AND_BASECOLOR2_SHADE; extern const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_VALUE; extern const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_COLOR; +extern const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_PICK; } // namespace backend_openGL3 diff --git a/include/polyscope/simple_triangle_mesh.h b/include/polyscope/simple_triangle_mesh.h index b4c79a32..e7232013 100644 --- a/include/polyscope/simple_triangle_mesh.h +++ b/include/polyscope/simple_triangle_mesh.h @@ -10,6 +10,7 @@ #include "polyscope/scaled_value.h" #include "polyscope/standardize_data_array.h" #include "polyscope/structure.h" +#include "polyscope/types.h" #include "polyscope/simple_triangle_mesh_color_quantity.h" #include "polyscope/simple_triangle_mesh_quantity.h" @@ -31,7 +32,8 @@ class SimpleTriangleMeshVertexColorQuantity; class SimpleTriangleMeshFaceColorQuantity; struct SimpleTriangleMeshPickResult { - // this does nothing for now, just matching pattern from other structures + MeshElement elementType = MeshElement::FACE; // which kind of element was clicked + int64_t index = -1; // index of the clicked element (vertex or face) }; class SimpleTriangleMesh : public Structure { @@ -114,6 +116,10 @@ class SimpleTriangleMesh : public Structure { SimpleTriangleMesh* setBackFacePolicy(BackFacePolicy newPolicy); BackFacePolicy getBackFacePolicy(); + // Selection mode (controls vertex vs face pick threshold) + SimpleTriangleMesh* setSelectionMode(MeshSelectionMode newMode); + MeshSelectionMode getSelectionMode(); + // Rendering helpers used by quantities void setSimpleTriangleMeshUniforms(render::ShaderProgram& p, bool withSurfaceShade = true); void setSimpleTriangleMeshProgramGeometryAttributes(render::ShaderProgram& p); @@ -128,6 +134,7 @@ class SimpleTriangleMesh : public Structure { PersistentValue material; PersistentValue backFacePolicy; PersistentValue backFaceColor; + PersistentValue selectionMode; // Drawing related things // if nullptr, prepare() (resp. preparePick()) needs to be called @@ -138,7 +145,7 @@ class SimpleTriangleMesh : public Structure { // Do setup work related to drawing, including allocating openGL data void ensureRenderProgramPrepared(); void ensurePickProgramPrepared(); - void setPickUniforms(render::ShaderProgram& p); + void setPickUniforms(render::ShaderProgram& p); // sets u_pickStartLow/High for SIMPLE_MESH_PROPAGATE_FACE_PICK // === Quantity adder implementations SimpleTriangleMeshVertexScalarQuantity* addVertexScalarQuantityImpl(std::string name, const std::vector& data, @@ -150,8 +157,7 @@ class SimpleTriangleMesh : public Structure { SimpleTriangleMeshFaceColorQuantity* addFaceColorQuantityImpl(std::string name, const std::vector& colors); // == Picking related things - size_t pickStart; - glm::vec3 pickColor; + size_t pickStart = 0; }; diff --git a/include/polyscope/simple_triangle_mesh_color_quantity.h b/include/polyscope/simple_triangle_mesh_color_quantity.h index 7e8bce09..d3b8d352 100644 --- a/include/polyscope/simple_triangle_mesh_color_quantity.h +++ b/include/polyscope/simple_triangle_mesh_color_quantity.h @@ -17,6 +17,7 @@ class SimpleTriangleMeshColorQuantity : public SimpleTriangleMeshQuantity, virtual void draw() override; virtual void buildCustomUI() override; + virtual void buildColorOptionsUI() override; virtual void refresh() override; virtual std::string niceName() override; @@ -39,6 +40,7 @@ class SimpleTriangleMeshVertexColorQuantity : public SimpleTriangleMeshColorQuan SimpleTriangleMesh& mesh); virtual void createProgram() override; + virtual void buildVertexInfoGUI(size_t vInd) override; }; @@ -52,6 +54,7 @@ class SimpleTriangleMeshFaceColorQuantity : public SimpleTriangleMeshColorQuanti SimpleTriangleMesh& mesh); virtual void createProgram() override; + virtual void buildFaceInfoGUI(size_t fInd) override; }; diff --git a/include/polyscope/simple_triangle_mesh_quantity.h b/include/polyscope/simple_triangle_mesh_quantity.h index 6df4e0e4..ab8a0116 100644 --- a/include/polyscope/simple_triangle_mesh_quantity.h +++ b/include/polyscope/simple_triangle_mesh_quantity.h @@ -12,9 +12,14 @@ class SimpleTriangleMesh; class SimpleTriangleMeshQuantity : public Quantity { public: SimpleTriangleMeshQuantity(std::string name, SimpleTriangleMesh& parentStructure, bool dominates = false); - virtual ~SimpleTriangleMeshQuantity(){}; + virtual ~SimpleTriangleMeshQuantity() {}; SimpleTriangleMesh& parent; // shadows and hides the generic member in Quantity + + // Called by buildPickUI() to display this quantity's value for the selected element. + // Override in vertex quantities (for vertex picks) and face quantities (for face picks). + virtual void buildVertexInfoGUI(size_t vInd) {} + virtual void buildFaceInfoGUI(size_t fInd) {} }; } // namespace polyscope diff --git a/include/polyscope/simple_triangle_mesh_scalar_quantity.h b/include/polyscope/simple_triangle_mesh_scalar_quantity.h index cae5277d..9a7623c0 100644 --- a/include/polyscope/simple_triangle_mesh_scalar_quantity.h +++ b/include/polyscope/simple_triangle_mesh_scalar_quantity.h @@ -41,6 +41,7 @@ class SimpleTriangleMeshVertexScalarQuantity : public SimpleTriangleMeshScalarQu DataType dataType = DataType::STANDARD); virtual void createProgram() override; + virtual void buildVertexInfoGUI(size_t vInd) override; }; @@ -54,6 +55,7 @@ class SimpleTriangleMeshFaceScalarQuantity : public SimpleTriangleMeshScalarQuan DataType dataType = DataType::STANDARD); virtual void createProgram() override; + virtual void buildFaceInfoGUI(size_t fInd) override; }; diff --git a/src/render/mock_opengl/mock_gl_engine.cpp b/src/render/mock_opengl/mock_gl_engine.cpp index 62b7a7de..28d00fde 100644 --- a/src/render/mock_opengl/mock_gl_engine.cpp +++ b/src/render/mock_opengl/mock_gl_engine.cpp @@ -2268,7 +2268,8 @@ void MockGLEngine::populateDefaultShadersAndRules() { registerShaderRule("MESH_PROPAGATE_PICK_SIMPLE", MESH_PROPAGATE_PICK_SIMPLE); registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_VALUE", SIMPLE_MESH_PROPAGATE_FACE_VALUE); registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_COLOR", SIMPLE_MESH_PROPAGATE_FACE_COLOR); - + registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_PICK", SIMPLE_MESH_PROPAGATE_FACE_PICK); + // volume gridcube things registerShaderRule("GRIDCUBE_PROPAGATE_NODE_VALUE", GRIDCUBE_PROPAGATE_NODE_VALUE); registerShaderRule("GRIDCUBE_PROPAGATE_CELL_VALUE", GRIDCUBE_PROPAGATE_CELL_VALUE); diff --git a/src/render/opengl/gl_engine.cpp b/src/render/opengl/gl_engine.cpp index 94fa7030..bb1ed452 100644 --- a/src/render/opengl/gl_engine.cpp +++ b/src/render/opengl/gl_engine.cpp @@ -2764,6 +2764,7 @@ void GLEngine::populateDefaultShadersAndRules() { registerShaderRule("MESH_PROPAGATE_PICK_SIMPLE", MESH_PROPAGATE_PICK_SIMPLE); registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_VALUE", SIMPLE_MESH_PROPAGATE_FACE_VALUE); registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_COLOR", SIMPLE_MESH_PROPAGATE_FACE_COLOR); + registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_PICK", SIMPLE_MESH_PROPAGATE_FACE_PICK); // volume gridcube things registerShaderRule("GRIDCUBE_PROPAGATE_NODE_VALUE", GRIDCUBE_PROPAGATE_NODE_VALUE); diff --git a/src/render/opengl/shaders/common.cpp b/src/render/opengl/shaders/common.cpp index 58f98551..ac7a1aed 100644 --- a/src/render/opengl/shaders/common.cpp +++ b/src/render/opengl/shaders/common.cpp @@ -3,6 +3,8 @@ #include "polyscope/render/opengl/shaders/common.h" +#include "polyscope/pick.h" // for POLYSCOPE_PICK_INDEX_TO_COLOR_GLSL + namespace polyscope { namespace render { namespace backend_openGL3 { @@ -437,6 +439,15 @@ bool rayTaperedCylinderIntersection(vec3 rayStart, vec3 rayDir, vec3 cylTail, ve } +)" +// Define GLSL type names so POLYSCOPE_PICK_INDEX_COLOR_BODY stringifies as GLSL (see pick.ipp). +#define POLYSCOPE_PICK_UINT uint +#define POLYSCOPE_PICK_VEC3 vec3 + POLYSCOPE_PICK_INDEX_TO_COLOR_GLSL +#undef POLYSCOPE_PICK_UINT +#undef POLYSCOPE_PICK_VEC3 + R"( + bool rayConeIntersection(vec3 rayStart, vec3 rayDir, vec3 coneBase, vec3 coneTip, float coneRad, out float tHit, out vec3 pHit, out vec3 nHit) { rayDir = normalize(rayDir); @@ -510,6 +521,6 @@ bool rayConeIntersection(vec3 rayStart, vec3 rayDir, vec3 coneBase, vec3 coneTip )"; -} +} // namespace backend_openGL3 } // namespace render } // namespace polyscope diff --git a/src/render/opengl/shaders/surface_mesh_shaders.cpp b/src/render/opengl/shaders/surface_mesh_shaders.cpp index fae61087..6b842c6a 100644 --- a/src/render/opengl/shaders/surface_mesh_shaders.cpp +++ b/src/render/opengl/shaders/surface_mesh_shaders.cpp @@ -765,6 +765,28 @@ const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_COLOR( /* textures */ {{"t_faceColors", 1}} ); +// Encodes the pick color for each face directly from gl_PrimitiveID + pickStart, with no texture lookup. +// Delegates encoding to pickIndexToColor() in common.cpp (see there for the bit layout). +const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_PICK( + /* rule name */ "SIMPLE_MESH_PROPAGATE_FACE_PICK", + { /* replacement sources */ + {"FRAG_DECLARATIONS", R"( + uniform uint u_pickStartLow; // lower 32 bits of the global pick index for face 0 + uniform uint u_pickStartHigh; // upper 32 bits of the global pick index for face 0 + vec3 pickIndexToColor(uint pickStartLow, uint pickStartHigh, uint primID); // defined in common.cpp + )"}, + {"GENERATE_SHADE_VALUE", R"( + vec3 shadeColor = pickIndexToColor(u_pickStartLow, u_pickStartHigh, uint(gl_PrimitiveID)); + )"}, + }, + /* uniforms */ { + {"u_pickStartLow", RenderDataType::UInt}, + {"u_pickStartHigh", RenderDataType::UInt}, + }, + /* attributes */ {}, + /* textures */ {} +); + // clang-format on } // namespace backend_openGL3 diff --git a/src/simple_triangle_mesh.cpp b/src/simple_triangle_mesh.cpp index 2791ca90..94073c3d 100644 --- a/src/simple_triangle_mesh.cpp +++ b/src/simple_triangle_mesh.cpp @@ -7,6 +7,7 @@ #include "polyscope/render/engine.h" #include "polyscope/simple_triangle_mesh_color_quantity.h" #include "polyscope/simple_triangle_mesh_scalar_quantity.h" +#include "polyscope/view.h" #include "imgui.h" @@ -28,7 +29,8 @@ SimpleTriangleMesh::SimpleTriangleMesh(std::string name, std::vector surfaceColor(uniquePrefix() + "surfaceColor", getNextUniqueColor()), material(uniquePrefix() + "material", "clay"), backFacePolicy(uniquePrefix() + "backFacePolicy", BackFacePolicy::Different), - backFaceColor(uniquePrefix() + "backFaceColor", glm::vec3(1.f - surfaceColor.get().r, 1.f - surfaceColor.get().g, 1.f - surfaceColor.get().b)) + backFaceColor(uniquePrefix() + "backFaceColor", glm::vec3(1.f - surfaceColor.get().r, 1.f - surfaceColor.get().g, 1.f - surfaceColor.get().b)), + selectionMode(uniquePrefix() + "selectionMode", MeshSelectionMode::Auto) // clang-format on { cullWholeElements.setPassive(false); @@ -74,6 +76,17 @@ void SimpleTriangleMesh::buildCustomOptionsUI() { setBackFacePolicy(BackFacePolicy::Cull); ImGui::EndMenu(); } + + // Selection mode + if (ImGui::BeginMenu("Selection Mode")) { + if (ImGui::MenuItem("auto", NULL, selectionMode.get() == MeshSelectionMode::Auto)) + setSelectionMode(MeshSelectionMode::Auto); + if (ImGui::MenuItem("vertices only", NULL, selectionMode.get() == MeshSelectionMode::VerticesOnly)) + setSelectionMode(MeshSelectionMode::VerticesOnly); + if (ImGui::MenuItem("faces only", NULL, selectionMode.get() == MeshSelectionMode::FacesOnly)) + setSelectionMode(MeshSelectionMode::FacesOnly); + ImGui::EndMenu(); + } } @@ -208,23 +221,24 @@ void SimpleTriangleMesh::ensurePickProgramPrepared() { if (pickProgram) return; // clang-format off - pickProgram = render::engine->requestShader("SIMPLE_MESH", - addSimpleTriangleMeshRules( - { - "SHADECOLOR_FROM_UNIFORM" - } - , false), render::ShaderReplacementDefaults::Pick - ); + pickProgram = render::engine->requestShader("SIMPLE_MESH", + addSimpleTriangleMeshRules({"SIMPLE_MESH_PROPAGATE_FACE_PICK"}, false), + render::ShaderReplacementDefaults::Pick); // clang-format on setSimpleTriangleMeshProgramGeometryAttributes(*pickProgram); - // Request pick indices - pickStart = pick::requestPickBufferRange(this, 1); - pickColor = pick::indToVec(pickStart); + // Allocate one pick index per face + pickStart = pick::requestPickBufferRange(this, nFaces()); } -void SimpleTriangleMesh::setPickUniforms(render::ShaderProgram& p) { p.setUniform("u_color", pickColor); } +void SimpleTriangleMesh::setPickUniforms(render::ShaderProgram& p) { + // Encode pickStart as two uint uniforms for the shader's 64-bit index arithmetic. + // NOTE: must stay in sync with SIMPLE_MESH_PROPAGATE_FACE_PICK in surface_mesh_shaders.cpp + // and with pick::indToVec() / pick::bitsForPickPacking in pick.ipp. + p.setUniform("u_pickStartLow", static_cast(pickStart & 0xFFFFFFFFull)); + p.setUniform("u_pickStartHigh", static_cast(pickStart >> 32)); +} std::vector SimpleTriangleMesh::addSimpleTriangleMeshRules(std::vector initRules, bool withSurfaceShade) { @@ -298,14 +312,86 @@ void SimpleTriangleMesh::updateObjectSpaceBounds() { SimpleTriangleMeshPickResult SimpleTriangleMesh::interpretPickResult(const PickResult& rawResult) { if (rawResult.structure != this) { - // caller must ensure that the PickResult belongs to this structure - // by checking the structure pointer or name exception("called interpretPickResult(), but the pick result is not from this structure"); } SimpleTriangleMeshPickResult result; - // currently nothing + size_t localInd = rawResult.localIndex; + if (localInd >= nFaces()) { + // Shouldn't happen, but guard against stale pick data + return result; + } + + // Recover the 3 vertex positions of the clicked face + glm::uvec3 triInds = faces.getValue(localInd); + glm::vec3 pA = vertices.getValue(triInds[0]); + glm::vec3 pB = vertices.getValue(triInds[1]); + glm::vec3 pC = vertices.getValue(triInds[2]); + + // Compute barycentric coordinates of the pick via sub-triangle areas. + // Each lambda_i is the area of the sub-triangle formed by the other two vertices and the click point P, + // divided by the total. Clamp each area to be nonneg and finite, then normalise. If the total is zero, fall back to + // (1/3, 1/3, 1/3). + glm::vec3 P = rawResult.position; + + auto subArea = [](glm::vec3 u, glm::vec3 v, glm::vec3 w) -> float { + float a = glm::length(glm::cross(v - u, w - u)); + return (std::isfinite(a) && a >= 0.f) ? a : 0.f; + }; + + float aA = subArea(P, pB, pC); + float aB = subArea(pA, P, pC); + float aC = subArea(pA, pB, P); + float total = aA + aB + aC; + + float lambdaA, lambdaB, lambdaC; + if (total == 0.f) { + lambdaA = lambdaB = lambdaC = 1.f / 3.f; + } else { + lambdaA = aA / total; + lambdaB = aB / total; + lambdaC = aC / total; + } + + // Find the dominant vertex + size_t nearestLocalIdx = 0; + float maxLambda = lambdaA; + if (lambdaB > maxLambda) { + maxLambda = lambdaB; + nearestLocalIdx = 1; + } + if (lambdaC > maxLambda) { + maxLambda = lambdaC; + nearestLocalIdx = 2; + } + size_t nearestVertexIdx = triInds[nearestLocalIdx]; + + // Threshold: lambda must exceed this to count as a vertex click. + // Matches SurfaceMesh: Auto uses 1 - 0.2 = 0.8, VerticesOnly always picks vertex, FacesOnly never does. + float vertexPickThreshold; + switch (selectionMode.get()) { + case MeshSelectionMode::Auto: + vertexPickThreshold = 0.8f; + break; + case MeshSelectionMode::VerticesOnly: + vertexPickThreshold = 0.0f; + break; + case MeshSelectionMode::FacesOnly: + vertexPickThreshold = 2.0f; + break; + default: + vertexPickThreshold = 0.8f; + break; + } + + if (maxLambda > vertexPickThreshold) { + result.elementType = MeshElement::VERTEX; + result.index = static_cast(nearestVertexIdx); + } else { + result.elementType = MeshElement::FACE; + result.index = static_cast(localInd); + } return result; } @@ -313,7 +399,35 @@ SimpleTriangleMeshPickResult SimpleTriangleMesh::interpretPickResult(const PickR void SimpleTriangleMesh::buildPickUI(const PickResult& rawResult) { SimpleTriangleMeshPickResult result = interpretPickResult(rawResult); - // Do nothing for now, we just pick a single constant for the whole structure + if (result.index < 0) return; + + if (result.elementType == MeshElement::VERTEX) { + ImGui::TextUnformatted(("Vertex #" + std::to_string(result.index)).c_str()); + + ImGui::Spacing(); + ImGui::Indent(20.f); + ImGui::Columns(2); + ImGui::SetColumnWidth(0, ImGui::GetWindowWidth() / 3); + for (auto& x : quantities) { + SimpleTriangleMeshQuantity* q = static_cast(x.second.get()); + q->buildVertexInfoGUI(static_cast(result.index)); + } + ImGui::Columns(1); + ImGui::Indent(-20.f); + } else { + ImGui::TextUnformatted(("Face #" + std::to_string(result.index)).c_str()); + + ImGui::Spacing(); + ImGui::Indent(20.f); + ImGui::Columns(2); + ImGui::SetColumnWidth(0, ImGui::GetWindowWidth() / 3); + for (auto& x : quantities) { + SimpleTriangleMeshQuantity* q = static_cast(x.second.get()); + q->buildFaceInfoGUI(static_cast(result.index)); + } + ImGui::Columns(1); + ImGui::Indent(-20.f); + } } @@ -353,6 +467,13 @@ SimpleTriangleMesh* SimpleTriangleMesh::setBackFaceColor(glm::vec3 val) { glm::vec3 SimpleTriangleMesh::getBackFaceColor() { return backFaceColor.get(); } +SimpleTriangleMesh* SimpleTriangleMesh::setSelectionMode(MeshSelectionMode newMode) { + selectionMode = newMode; + requestRedraw(); + return this; +} +MeshSelectionMode SimpleTriangleMesh::getSelectionMode() { return selectionMode.get(); } + // === Quantity adder implementations diff --git a/src/simple_triangle_mesh_color_quantity.cpp b/src/simple_triangle_mesh_color_quantity.cpp index 8d40bba4..3300d9b5 100644 --- a/src/simple_triangle_mesh_color_quantity.cpp +++ b/src/simple_triangle_mesh_color_quantity.cpp @@ -15,8 +15,7 @@ namespace polyscope { SimpleTriangleMeshColorQuantity::SimpleTriangleMeshColorQuantity(std::string name, const std::vector& colors_, - std::string definedOn_, - SimpleTriangleMesh& mesh_) + std::string definedOn_, SimpleTriangleMesh& mesh_) : SimpleTriangleMeshQuantity(name, mesh_, true), ColorQuantity(*this, colors_), definedOn(definedOn_) {} void SimpleTriangleMeshColorQuantity::draw() { @@ -32,6 +31,11 @@ void SimpleTriangleMeshColorQuantity::draw() { program->draw(); } +void SimpleTriangleMeshColorQuantity::buildColorOptionsUI() { + ColorQuantity::buildColorOptionsUI(); + ImGui::TextUnformatted("(no options available)"); +} + void SimpleTriangleMeshColorQuantity::buildCustomUI() { ImGui::SameLine(); if (ImGui::Button("Options")) { @@ -61,6 +65,16 @@ SimpleTriangleMeshVertexColorQuantity::SimpleTriangleMeshVertexColorQuantity(std SimpleTriangleMesh& mesh_) : SimpleTriangleMeshColorQuantity(name, colors_, "vertex", mesh_) {} +void SimpleTriangleMeshVertexColorQuantity::buildVertexInfoGUI(size_t vInd) { + ImGui::TextUnformatted(name.c_str()); + ImGui::NextColumn(); + glm::vec3 val = colors.getValue(vInd); + ImGui::ColorEdit3("", &val[0], ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoPicker); + ImGui::SameLine(); + ImGui::Text("%.3f, %.3f, %.3f", val.r, val.g, val.b); + ImGui::NextColumn(); +} + void SimpleTriangleMeshVertexColorQuantity::createProgram() { // clang-format off program = render::engine->requestShader("SIMPLE_MESH", @@ -91,6 +105,16 @@ SimpleTriangleMeshFaceColorQuantity::SimpleTriangleMeshFaceColorQuantity(std::st colors.setTextureSize(parent.nFaces()); } +void SimpleTriangleMeshFaceColorQuantity::buildFaceInfoGUI(size_t fInd) { + ImGui::TextUnformatted(name.c_str()); + ImGui::NextColumn(); + glm::vec3 val = colors.getValue(fInd); + ImGui::ColorEdit3("", &val[0], ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoPicker); + ImGui::SameLine(); + ImGui::Text("%.3f, %.3f, %.3f", val.r, val.g, val.b); + ImGui::NextColumn(); +} + void SimpleTriangleMeshFaceColorQuantity::createProgram() { // clang-format off program = render::engine->requestShader("SIMPLE_MESH", diff --git a/src/simple_triangle_mesh_scalar_quantity.cpp b/src/simple_triangle_mesh_scalar_quantity.cpp index e13a2c94..92de0748 100644 --- a/src/simple_triangle_mesh_scalar_quantity.cpp +++ b/src/simple_triangle_mesh_scalar_quantity.cpp @@ -61,6 +61,13 @@ SimpleTriangleMeshVertexScalarQuantity::SimpleTriangleMeshVertexScalarQuantity(s DataType dataType_) : SimpleTriangleMeshScalarQuantity(name, values_, "vertex", mesh_, dataType_) {} +void SimpleTriangleMeshVertexScalarQuantity::buildVertexInfoGUI(size_t vInd) { + ImGui::TextUnformatted(name.c_str()); + ImGui::NextColumn(); + ImGui::Text("%g", values.getValue(vInd)); + ImGui::NextColumn(); +} + void SimpleTriangleMeshVertexScalarQuantity::createProgram() { // clang-format off program = render::engine->requestShader("SIMPLE_MESH", @@ -93,6 +100,13 @@ SimpleTriangleMeshFaceScalarQuantity::SimpleTriangleMeshFaceScalarQuantity(std:: values.setTextureSize(parent.nFaces()); } +void SimpleTriangleMeshFaceScalarQuantity::buildFaceInfoGUI(size_t fInd) { + ImGui::TextUnformatted(name.c_str()); + ImGui::NextColumn(); + ImGui::Text("%g", values.getValue(fInd)); + ImGui::NextColumn(); +} + void SimpleTriangleMeshFaceScalarQuantity::createProgram() { // clang-format off program = render::engine->requestShader("SIMPLE_MESH", diff --git a/test/src/misc_test.cpp b/test/src/misc_test.cpp index c7b00c1c..60d7dea4 100644 --- a/test/src/misc_test.cpp +++ b/test/src/misc_test.cpp @@ -206,4 +206,41 @@ TEST_F(PolyscopeTest, TestSlicePlane) { polyscope::removeAllSlicePlanes(); polyscope::removeAllStructures(); +} + + +// ============================================================ +// =============== Pick index encoding +// ============================================================ + +TEST_F(PolyscopeTest, PickIndexEncoding) { + // See the note in pick.ipp. We have multiple implementations of the pick index decoding logic, + // and this verifies they produce identical output. + + // indToVec takes size_t; cast explicitly to avoid linker issues on platforms where + // size_t and uint64_t have different mangled names (e.g. arm64: ulong vs ulonglong). + auto check = [](uint64_t pickStart, uint32_t primID) { + glm::vec3 expected = polyscope::pick::indToVec(static_cast(pickStart + primID)); + glm::vec3 actual = polyscope::pick::pickIndexToColor(pickStart, primID); + EXPECT_FLOAT_EQ(actual.x, expected.x); + EXPECT_FLOAT_EQ(actual.y, expected.y); + EXPECT_FLOAT_EQ(actual.z, expected.z); + }; + + check(0, 0); + check(0, 1); + check(0, 42); + check(0, (1u << 22) - 1u); // max value in low22 word + check(0, (1u << 22)); // overflows into med22 + check(5'000'000, 0); // pickStart > 2^22 + check(0x0000'00FF'FF00'0000ull, 0); // high bits set + + // --- Carry cases: pickStartLow + primID overflows uint32 --- + // Carry from low32 into high32 (the `carry` variable in the body): + check(0xFFFF0000u, 0x10000u); // low: 0xFFFF0000 + 0x10000 wraps -> carry=1 + check(0xFFFFFFFFu, 1u); // extreme: low wraps to 0 exactly -> carry=1 + check(0xFFFFFFFFu, 0xFFFFFFFFu); // both max -> low=0xFFFFFFFE, carry=1 + // Carry with non-zero pickStartHigh (tests idxHigh = pickStartHigh + carry): + check(0x1'0000'0000ull, 0xFFFFFFFFu); // high=1, low wraps -> idxHigh = 1 + 1 = 2 + check(0x1'FFFF'0000ull, 0x10000u); // high=1 + carry from low wrap } \ No newline at end of file From b89fcc1fafb4a3dd9b8e3a00bb0bc6776b99c9c1 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sun, 3 May 2026 18:17:12 -0700 Subject: [PATCH 19/31] initial support for updates in SimpleTriangleMesh --- include/polyscope/simple_triangle_mesh.h | 13 ++++ include/polyscope/simple_triangle_mesh.ipp | 28 +++++++++ .../simple_triangle_mesh_color_quantity.h | 14 +++++ .../simple_triangle_mesh_scalar_quantity.h | 14 +++++ src/simple_triangle_mesh.cpp | 5 ++ src/simple_triangle_mesh_color_quantity.cpp | 10 +++ src/simple_triangle_mesh_scalar_quantity.cpp | 10 +++ test/src/surface_mesh_test.cpp | 62 +++++++++++++++++++ 8 files changed, 156 insertions(+) diff --git a/include/polyscope/simple_triangle_mesh.h b/include/polyscope/simple_triangle_mesh.h index e7232013..c246b820 100644 --- a/include/polyscope/simple_triangle_mesh.h +++ b/include/polyscope/simple_triangle_mesh.h @@ -92,6 +92,19 @@ class SimpleTriangleMesh : public Structure { template void update(const V& newVertices, const F& newFaces); + // CPU-side update functions. + + // update only positions (vertex count must stay the same). + template + void updateVertexPositions(const V& newPositions); + + // update both vertices and faces; vertex/face counts may change + template + void updateMesh(const V& newVertices, const F& newFaces); + + // optionally pre-allocates capacity to avoid reallocations on future updateMesh() calls. + void reserve(size_t nVerts, size_t nFaces); + // Misc data static const std::string structureTypeName; diff --git a/include/polyscope/simple_triangle_mesh.ipp b/include/polyscope/simple_triangle_mesh.ipp index 05cb1b5a..5b76204a 100644 --- a/include/polyscope/simple_triangle_mesh.ipp +++ b/include/polyscope/simple_triangle_mesh.ipp @@ -49,6 +49,34 @@ void SimpleTriangleMesh::update(const V& newPositions, const F& newFaces) { faces.markHostBufferUpdated(); } +template +void SimpleTriangleMesh::updateVertexPositions(const V& newPositions) { + validateSize(newPositions, nVertices(), "newPositions"); + verticesData = standardizeVectorArray(newPositions); + vertices.markHostBufferUpdated(); + updateObjectSpaceBounds(); +} + +template +void SimpleTriangleMesh::updateMesh(const V& newVerts, const F& newFaces) { + std::vector vertsStd = standardizeVectorArray(newVerts); + if (vertsStd.size() > verticesData.capacity()) { + // amortized doubling on the CPU-side backing vectors. + verticesData.reserve(std::max(vertsStd.size(), 2 * verticesData.capacity())); + } + verticesData = std::move(vertsStd); + vertices.markHostBufferUpdated(); + + std::vector facesStd = standardizeVectorArray(newFaces); + if (facesStd.size() > facesData.capacity()) { + facesData.reserve(std::max(facesStd.size(), 2 * facesData.capacity())); + } + facesData = std::move(facesStd); + faces.markHostBufferUpdated(); + + updateObjectSpaceBounds(); +} + // ===================================================== // ============== Quantities // ===================================================== diff --git a/include/polyscope/simple_triangle_mesh_color_quantity.h b/include/polyscope/simple_triangle_mesh_color_quantity.h index d3b8d352..8ba13982 100644 --- a/include/polyscope/simple_triangle_mesh_color_quantity.h +++ b/include/polyscope/simple_triangle_mesh_color_quantity.h @@ -4,6 +4,7 @@ #include "polyscope/color_quantity.h" #include "polyscope/simple_triangle_mesh_quantity.h" +#include "polyscope/standardize_data_array.h" #include @@ -21,6 +22,19 @@ class SimpleTriangleMeshColorQuantity : public SimpleTriangleMeshQuantity, virtual void refresh() override; virtual std::string niceName() override; + // Throws if the quantity buffer size doesn't match the parent mesh's vertex/face count. + void checkQuantitySizeMatchesParentStructure(); + + // Update the color values, allowing the count to change (e.g. after updateMesh()). + // For same-size updates, prefer ColorQuantity::updateData() which validates the size. + template + void updateData(const V& newColors) { + std::vector newData = standardizeVectorArray(newColors); + colors.resize(newData.size()); + colors.data.assign(newData.begin(), newData.end()); + colors.markHostBufferUpdated(); + } + const std::string definedOn; protected: diff --git a/include/polyscope/simple_triangle_mesh_scalar_quantity.h b/include/polyscope/simple_triangle_mesh_scalar_quantity.h index 9a7623c0..ab42b461 100644 --- a/include/polyscope/simple_triangle_mesh_scalar_quantity.h +++ b/include/polyscope/simple_triangle_mesh_scalar_quantity.h @@ -6,6 +6,7 @@ #include "polyscope/render/color_maps.h" #include "polyscope/scalar_quantity.h" #include "polyscope/simple_triangle_mesh_quantity.h" +#include "polyscope/standardize_data_array.h" #include @@ -22,6 +23,19 @@ class SimpleTriangleMeshScalarQuantity : public SimpleTriangleMeshQuantity, virtual void refresh() override; virtual std::string niceName() override; + // Throws if the quantity buffer size doesn't match the parent mesh's vertex/face count. + void checkQuantitySizeMatchesParentStructure(); + + // Update the scalar values, allowing the count to change (e.g. after updateMesh()). + // For same-size updates, prefer ScalarQuantity::updateData() which validates the size. + template + void updateData(const V& newValues) { + std::vector newData = standardizeArray(newValues); + values.resize(newData.size()); + values.data.assign(newData.begin(), newData.end()); + values.markHostBufferUpdated(); + } + const std::string definedOn; protected: diff --git a/src/simple_triangle_mesh.cpp b/src/simple_triangle_mesh.cpp index 94073c3d..8e09d3f4 100644 --- a/src/simple_triangle_mesh.cpp +++ b/src/simple_triangle_mesh.cpp @@ -309,6 +309,11 @@ void SimpleTriangleMesh::updateObjectSpaceBounds() { objectSpaceLengthScale = 2 * std::sqrt(lengthScale); } +void SimpleTriangleMesh::reserve(size_t nVerts, size_t nFaces) { + verticesData.reserve(nVerts); + facesData.reserve(nFaces); +} + SimpleTriangleMeshPickResult SimpleTriangleMesh::interpretPickResult(const PickResult& rawResult) { if (rawResult.structure != this) { diff --git a/src/simple_triangle_mesh_color_quantity.cpp b/src/simple_triangle_mesh_color_quantity.cpp index 3300d9b5..fb2b4d34 100644 --- a/src/simple_triangle_mesh_color_quantity.cpp +++ b/src/simple_triangle_mesh_color_quantity.cpp @@ -21,6 +21,8 @@ SimpleTriangleMeshColorQuantity::SimpleTriangleMeshColorQuantity(std::string nam void SimpleTriangleMeshColorQuantity::draw() { if (!isEnabled()) return; + checkQuantitySizeMatchesParentStructure(); + if (program == nullptr) createProgram(); parent.setStructureUniforms(*program); @@ -31,6 +33,14 @@ void SimpleTriangleMeshColorQuantity::draw() { program->draw(); } +void SimpleTriangleMeshColorQuantity::checkQuantitySizeMatchesParentStructure() { + size_t expected = (definedOn == "vertex") ? parent.nVertices() : parent.nFaces(); + if (colors.size() != expected) { + exception("SimpleTriangleMesh color quantity '" + name + "' has " + std::to_string(colors.size()) + + " colors but parent mesh has " + std::to_string(expected) + " " + definedOn + "s"); + } +} + void SimpleTriangleMeshColorQuantity::buildColorOptionsUI() { ColorQuantity::buildColorOptionsUI(); ImGui::TextUnformatted("(no options available)"); diff --git a/src/simple_triangle_mesh_scalar_quantity.cpp b/src/simple_triangle_mesh_scalar_quantity.cpp index 92de0748..6492afd7 100644 --- a/src/simple_triangle_mesh_scalar_quantity.cpp +++ b/src/simple_triangle_mesh_scalar_quantity.cpp @@ -18,9 +18,19 @@ SimpleTriangleMeshScalarQuantity::SimpleTriangleMeshScalarQuantity(std::string n DataType dataType_) : SimpleTriangleMeshQuantity(name, mesh_, true), ScalarQuantity(*this, values_, dataType_), definedOn(definedOn_) {} +void SimpleTriangleMeshScalarQuantity::checkQuantitySizeMatchesParentStructure() { + size_t expected = (definedOn == "vertex") ? parent.nVertices() : parent.nFaces(); + if (values.size() != expected) { + exception("SimpleTriangleMesh scalar quantity '" + name + "' has " + std::to_string(values.size()) + + " values but parent mesh has " + std::to_string(expected) + " " + definedOn + "s"); + } +} + void SimpleTriangleMeshScalarQuantity::draw() { if (!isEnabled()) return; + checkQuantitySizeMatchesParentStructure(); + if (program == nullptr) createProgram(); parent.setStructureUniforms(*program); diff --git a/test/src/surface_mesh_test.cpp b/test/src/surface_mesh_test.cpp index ed2d3402..48eba571 100644 --- a/test/src/surface_mesh_test.cpp +++ b/test/src/surface_mesh_test.cpp @@ -727,3 +727,65 @@ TEST_F(PolyscopeTest, SimpleTriangleMeshFaceColor) { polyscope::removeAllStructures(); } + +TEST_F(PolyscopeTest, SimpleTriangleMeshUpdateFunctions) { + auto psMesh = registerSimpleTriangleMesh(); + size_t nV = psMesh->nVertices(); + size_t nF = psMesh->nFaces(); + + // updateVertexPositions: same count, renders fine + psMesh->updateVertexPositions(std::vector(nV, glm::vec3{0.f, 0.f, 0.f})); + EXPECT_EQ(psMesh->nVertices(), nV); + EXPECT_EQ(psMesh->nFaces(), nF); + polyscope::show(3); + + // updateMesh: shrink + psMesh->updateMesh(std::vector{{0,0,0},{1,0,0},{0,1,0}}, + std::vector{{0,1,2}}); + EXPECT_EQ(psMesh->nVertices(), 3u); + EXPECT_EQ(psMesh->nFaces(), 1u); + polyscope::show(3); + + // updateMesh: grow beyond original size + std::vector bigVerts = {{0,0,0},{1,0,0},{0,1,0},{0,0,1},{1,1,0}}; + std::vector bigFaces = {{0,1,2},{1,3,4},{2,3,0}}; + psMesh->updateMesh(bigVerts, bigFaces); + EXPECT_EQ(psMesh->nVertices(), 5u); + EXPECT_EQ(psMesh->nFaces(), 3u); + polyscope::show(3); + + polyscope::removeAllStructures(); +} + +TEST_F(PolyscopeTest, SimpleTriangleMeshQuantityUpdateAfterResize) { + auto psMesh = registerSimpleTriangleMesh(); + + // Add quantities at original size and enable them + auto qVS = psMesh->addVertexScalarQuantity("vScalar", std::vector(psMesh->nVertices(), 1.f)); + auto qFS = psMesh->addFaceScalarQuantity("fScalar", std::vector(psMesh->nFaces(), 2.f)); + auto qVC = psMesh->addVertexColorQuantity("vColor", std::vector(psMesh->nVertices(), glm::vec3{1,0,0})); + auto qFC = psMesh->addFaceColorQuantity("fColor", std::vector(psMesh->nFaces(), glm::vec3{0,1,0})); + qVS->setEnabled(true); + polyscope::show(3); + + // Resize the mesh + std::vector newVerts = {{0,0,0},{1,0,0},{0,1,0},{0,0,1},{1,1,0}}; + std::vector newFaces = {{0,1,2},{1,3,4},{2,3,0}}; + psMesh->updateMesh(newVerts, newFaces); + + // Update all quantities to match new counts + qVS->updateData(std::vector(psMesh->nVertices(), 3.f)); + qFS->updateData(std::vector(psMesh->nFaces(), 4.f)); + qVC->updateData(std::vector(psMesh->nVertices(), glm::vec3{0,0,1})); + qFC->updateData(std::vector(psMesh->nFaces(), glm::vec3{1,1,0})); + + EXPECT_EQ(qVS->values.size(), psMesh->nVertices()); + EXPECT_EQ(qFS->values.size(), psMesh->nFaces()); + EXPECT_EQ(qVC->colors.size(), psMesh->nVertices()); + EXPECT_EQ(qFC->colors.size(), psMesh->nFaces()); + + qFS->setEnabled(true); + polyscope::show(3); + + polyscope::removeAllStructures(); +} From 51f3f67f3c2ab3d462cef0ba3d6cf089e1a46d4c Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sat, 9 May 2026 17:21:11 -0700 Subject: [PATCH 20/31] update for new managed buffer setup --- include/polyscope/simple_triangle_mesh.ipp | 45 ++++++------------- .../simple_triangle_mesh_color_quantity.h | 7 ++- .../simple_triangle_mesh_scalar_quantity.h | 7 ++- src/simple_triangle_mesh.cpp | 4 +- src/simple_triangle_mesh_color_quantity.cpp | 1 + src/simple_triangle_mesh_scalar_quantity.cpp | 1 + 6 files changed, 24 insertions(+), 41 deletions(-) diff --git a/include/polyscope/simple_triangle_mesh.ipp b/include/polyscope/simple_triangle_mesh.ipp index 5b76204a..51132795 100644 --- a/include/polyscope/simple_triangle_mesh.ipp +++ b/include/polyscope/simple_triangle_mesh.ipp @@ -26,53 +26,36 @@ SimpleTriangleMesh* registerSimpleTriangleMesh(std::string name, const V& vertex template void SimpleTriangleMesh::updateVertices(const V& newPositions) { validateSize(newPositions, vertices.size(), "newPositions"); - auto d = standardizeVectorArray(newPositions); - vertices.resize(d.size()); - vertices.setDataHost(d); - vertices.markHostBufferUpdated(); + vertices.setDataHost(standardizeVectorArray(newPositions)); } template void SimpleTriangleMesh::update(const V& newPositions, const F& newFaces) { - { - auto d = standardizeVectorArray(newPositions); - vertices.resize(d.size()); - vertices.setDataHost(d); - } - vertices.markHostBufferUpdated(); + auto vertsStd = standardizeVectorArray(newPositions); + vertices.resize(vertsStd.size()); // ManagedBuffer internally does amortized doubling to make this efficient + vertices.setDataHost(vertsStd); - { - auto d = standardizeVectorArray(newFaces); - faces.resize(d.size()); - faces.setDataHost(d); - } - faces.markHostBufferUpdated(); + auto facesStd = standardizeVectorArray(newFaces); + faces.resize(facesStd.size()); + faces.setDataHost(facesStd); } template void SimpleTriangleMesh::updateVertexPositions(const V& newPositions) { validateSize(newPositions, nVertices(), "newPositions"); - verticesData = standardizeVectorArray(newPositions); - vertices.markHostBufferUpdated(); + vertices.setDataHost(standardizeVectorArray(newPositions)); updateObjectSpaceBounds(); } template void SimpleTriangleMesh::updateMesh(const V& newVerts, const F& newFaces) { - std::vector vertsStd = standardizeVectorArray(newVerts); - if (vertsStd.size() > verticesData.capacity()) { - // amortized doubling on the CPU-side backing vectors. - verticesData.reserve(std::max(vertsStd.size(), 2 * verticesData.capacity())); - } - verticesData = std::move(vertsStd); - vertices.markHostBufferUpdated(); + auto vertsStd = standardizeVectorArray(newVerts); + vertices.resize(vertsStd.size()); // amortized doubling handled by ManagedBuffer + vertices.setDataHost(vertsStd); - std::vector facesStd = standardizeVectorArray(newFaces); - if (facesStd.size() > facesData.capacity()) { - facesData.reserve(std::max(facesStd.size(), 2 * facesData.capacity())); - } - facesData = std::move(facesStd); - faces.markHostBufferUpdated(); + auto facesStd = standardizeVectorArray(newFaces); + faces.resize(facesStd.size()); + faces.setDataHost(facesStd); updateObjectSpaceBounds(); } diff --git a/include/polyscope/simple_triangle_mesh_color_quantity.h b/include/polyscope/simple_triangle_mesh_color_quantity.h index 8ba13982..669301fe 100644 --- a/include/polyscope/simple_triangle_mesh_color_quantity.h +++ b/include/polyscope/simple_triangle_mesh_color_quantity.h @@ -29,10 +29,9 @@ class SimpleTriangleMeshColorQuantity : public SimpleTriangleMeshQuantity, // For same-size updates, prefer ColorQuantity::updateData() which validates the size. template void updateData(const V& newColors) { - std::vector newData = standardizeVectorArray(newColors); - colors.resize(newData.size()); - colors.data.assign(newData.begin(), newData.end()); - colors.markHostBufferUpdated(); + auto newData = standardizeVectorArray(newColors); + colors.resize(newData.size()); // amortized doubling handled by ManagedBuffer + colors.setDataHost(newData); } const std::string definedOn; diff --git a/include/polyscope/simple_triangle_mesh_scalar_quantity.h b/include/polyscope/simple_triangle_mesh_scalar_quantity.h index ab42b461..63f70f17 100644 --- a/include/polyscope/simple_triangle_mesh_scalar_quantity.h +++ b/include/polyscope/simple_triangle_mesh_scalar_quantity.h @@ -30,10 +30,9 @@ class SimpleTriangleMeshScalarQuantity : public SimpleTriangleMeshQuantity, // For same-size updates, prefer ScalarQuantity::updateData() which validates the size. template void updateData(const V& newValues) { - std::vector newData = standardizeArray(newValues); - values.resize(newData.size()); - values.data.assign(newData.begin(), newData.end()); - values.markHostBufferUpdated(); + auto newData = standardizeArray(newValues); + values.resize(newData.size()); // amortized doubling handled by ManagedBuffer + values.setDataHost(newData); } const std::string definedOn; diff --git a/src/simple_triangle_mesh.cpp b/src/simple_triangle_mesh.cpp index 8e09d3f4..37fafad0 100644 --- a/src/simple_triangle_mesh.cpp +++ b/src/simple_triangle_mesh.cpp @@ -310,8 +310,8 @@ void SimpleTriangleMesh::updateObjectSpaceBounds() { } void SimpleTriangleMesh::reserve(size_t nVerts, size_t nFaces) { - verticesData.reserve(nVerts); - facesData.reserve(nFaces); + vertices.setCapacity(nVerts); + faces.setCapacity(nFaces); } SimpleTriangleMeshPickResult SimpleTriangleMesh::interpretPickResult(const PickResult& rawResult) { diff --git a/src/simple_triangle_mesh_color_quantity.cpp b/src/simple_triangle_mesh_color_quantity.cpp index fb2b4d34..64baf8c2 100644 --- a/src/simple_triangle_mesh_color_quantity.cpp +++ b/src/simple_triangle_mesh_color_quantity.cpp @@ -112,6 +112,7 @@ SimpleTriangleMeshFaceColorQuantity::SimpleTriangleMeshFaceColorQuantity(std::st const std::vector& colors_, SimpleTriangleMesh& mesh_) : SimpleTriangleMeshColorQuantity(name, colors_, "face", mesh_) { + colors.setAsType(DeviceBufferType::Texture1d); colors.setTextureSize(parent.nFaces()); } diff --git a/src/simple_triangle_mesh_scalar_quantity.cpp b/src/simple_triangle_mesh_scalar_quantity.cpp index 6492afd7..c7a06e0b 100644 --- a/src/simple_triangle_mesh_scalar_quantity.cpp +++ b/src/simple_triangle_mesh_scalar_quantity.cpp @@ -107,6 +107,7 @@ SimpleTriangleMeshFaceScalarQuantity::SimpleTriangleMeshFaceScalarQuantity(std:: SimpleTriangleMesh& mesh_, DataType dataType_) : SimpleTriangleMeshScalarQuantity(name, values_, "face", mesh_, dataType_) { + values.setAsType(DeviceBufferType::Texture1d); values.setTextureSize(parent.nFaces()); } From 60a13f4767bb89aac9083971b89955f1c1948234 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sat, 9 May 2026 19:32:45 -0700 Subject: [PATCH 21/31] add forced update option for object bounds --- include/polyscope/polyscope.h | 1 + include/polyscope/structure.h | 3 ++- src/polyscope.cpp | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/polyscope/polyscope.h b/include/polyscope/polyscope.h index d228c316..6450229b 100644 --- a/include/polyscope/polyscope.h +++ b/include/polyscope/polyscope.h @@ -209,6 +209,7 @@ void drawStructuresDelayed(); // need to call this directly. void processLazyProperties(); void processLazyPropertiesOutsideOfImGui(); +void forceRecomputeAllStructureBounds(); } // namespace polyscope diff --git a/include/polyscope/structure.h b/include/polyscope/structure.h index 10d39ed5..475151a8 100644 --- a/include/polyscope/structure.h +++ b/include/polyscope/structure.h @@ -118,6 +118,8 @@ class Structure : public render::ManagedBufferRegistry, public virtual WeakRefer std::tuple boundingBox(); // get axis-aligned bounding box float lengthScale(); // get characteristic length virtual bool hasExtents(); // bounding box and length scale are only meaningful if true + virtual void updateObjectSpaceBounds() = 0; // force an update of bbox and length scale, only rarely needs to be + // called if certain kinds of updates are performed // ==================================================================== // ==== Enabling, Selection, and Groups =============================== @@ -281,7 +283,6 @@ class Structure : public render::ManagedBufferRegistry, public virtual WeakRefer // The STRUCTURE is responsible for making sure updateObjectSpaceBounds() gets called any time the geometry changes std::tuple objectSpaceBoundingBox; float objectSpaceLengthScale; - virtual void updateObjectSpaceBounds() = 0; }; diff --git a/src/polyscope.cpp b/src/polyscope.cpp index 0cd27c2f..3bf6cfb6 100644 --- a/src/polyscope.cpp +++ b/src/polyscope.cpp @@ -760,6 +760,8 @@ void buildPolyscopeGui() { ImGui::Begin("Polyscope ", nullptr); if (ImGui::Button(ICON_LC_HOUSE " Reset View")) { + forceRecomputeAllStructureBounds(); + updateStructureExtents(); view::flyToHomeView(); } ImGui::SameLine(); @@ -1607,6 +1609,14 @@ void updateStructureExtents() { requestRedraw(); } +void forceRecomputeAllStructureBounds() { + for (auto& cat : state::structures) { + for (auto& x : cat.second) { + x.second->updateObjectSpaceBounds(); + } + } +} + namespace state { glm::vec3 center() { return 0.5f * (std::get<0>(state::boundingBox) + std::get<1>(state::boundingBox)); } } // namespace state From 66a103eea863abe8c1f01e23693cd33a3decb781 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sat, 9 May 2026 19:34:34 -0700 Subject: [PATCH 22/31] remove redundant updaters --- include/polyscope/simple_triangle_mesh.h | 8 -------- include/polyscope/simple_triangle_mesh.ipp | 20 -------------------- 2 files changed, 28 deletions(-) diff --git a/include/polyscope/simple_triangle_mesh.h b/include/polyscope/simple_triangle_mesh.h index c246b820..fe8582e5 100644 --- a/include/polyscope/simple_triangle_mesh.h +++ b/include/polyscope/simple_triangle_mesh.h @@ -86,14 +86,6 @@ class SimpleTriangleMesh : public Structure { // === Mutate - template - void updateVertices(const V& newPositions); - - template - void update(const V& newVertices, const F& newFaces); - - // CPU-side update functions. - // update only positions (vertex count must stay the same). template void updateVertexPositions(const V& newPositions); diff --git a/include/polyscope/simple_triangle_mesh.ipp b/include/polyscope/simple_triangle_mesh.ipp index 51132795..14b0aa93 100644 --- a/include/polyscope/simple_triangle_mesh.ipp +++ b/include/polyscope/simple_triangle_mesh.ipp @@ -23,28 +23,10 @@ SimpleTriangleMesh* registerSimpleTriangleMesh(std::string name, const V& vertex } -template -void SimpleTriangleMesh::updateVertices(const V& newPositions) { - validateSize(newPositions, vertices.size(), "newPositions"); - vertices.setDataHost(standardizeVectorArray(newPositions)); -} - -template -void SimpleTriangleMesh::update(const V& newPositions, const F& newFaces) { - auto vertsStd = standardizeVectorArray(newPositions); - vertices.resize(vertsStd.size()); // ManagedBuffer internally does amortized doubling to make this efficient - vertices.setDataHost(vertsStd); - - auto facesStd = standardizeVectorArray(newFaces); - faces.resize(facesStd.size()); - faces.setDataHost(facesStd); -} - template void SimpleTriangleMesh::updateVertexPositions(const V& newPositions) { validateSize(newPositions, nVertices(), "newPositions"); vertices.setDataHost(standardizeVectorArray(newPositions)); - updateObjectSpaceBounds(); } template @@ -56,8 +38,6 @@ void SimpleTriangleMesh::updateMesh(const V& newVerts, const F& newFaces) { auto facesStd = standardizeVectorArray(newFaces); faces.resize(facesStd.size()); faces.setDataHost(facesStd); - - updateObjectSpaceBounds(); } // ===================================================== From 073701aa406738878fe246343624df9748c0181d Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Sat, 9 May 2026 22:48:13 -0700 Subject: [PATCH 23/31] naming fixes --- include/polyscope/simple_triangle_mesh.h | 2 +- src/simple_triangle_mesh.cpp | 2 +- test/src/surface_mesh_test.cpp | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/polyscope/simple_triangle_mesh.h b/include/polyscope/simple_triangle_mesh.h index fe8582e5..b17e3787 100644 --- a/include/polyscope/simple_triangle_mesh.h +++ b/include/polyscope/simple_triangle_mesh.h @@ -95,7 +95,7 @@ class SimpleTriangleMesh : public Structure { void updateMesh(const V& newVertices, const F& newFaces); // optionally pre-allocates capacity to avoid reallocations on future updateMesh() calls. - void reserve(size_t nVerts, size_t nFaces); + void reserveMeshCapacity(size_t nVerts, size_t nFaces); // Misc data static const std::string structureTypeName; diff --git a/src/simple_triangle_mesh.cpp b/src/simple_triangle_mesh.cpp index 37fafad0..9ae2b180 100644 --- a/src/simple_triangle_mesh.cpp +++ b/src/simple_triangle_mesh.cpp @@ -309,7 +309,7 @@ void SimpleTriangleMesh::updateObjectSpaceBounds() { objectSpaceLengthScale = 2 * std::sqrt(lengthScale); } -void SimpleTriangleMesh::reserve(size_t nVerts, size_t nFaces) { +void SimpleTriangleMesh::reserveMeshCapacity(size_t nVerts, size_t nFaces) { vertices.setCapacity(nVerts); faces.setCapacity(nFaces); } diff --git a/test/src/surface_mesh_test.cpp b/test/src/surface_mesh_test.cpp index 48eba571..f5cfbb31 100644 --- a/test/src/surface_mesh_test.cpp +++ b/test/src/surface_mesh_test.cpp @@ -643,24 +643,24 @@ TEST_F(PolyscopeTest, SimpleTriangleMesUpdate) { polyscope::show(3); // make sure everything is populated // update just the locations - psMesh->updateVertices(std::vector(4)); + psMesh->updateVertexPositions(std::vector(4)); polyscope::show(3); // update the locations and faces - psMesh->update(std::vector(4), std::vector(4, glm::uvec3(0, 1, 2))); + psMesh->updateMesh(std::vector(4), std::vector(4, glm::uvec3(0, 1, 2))); polyscope::show(3); // do a bunch of resizing - psMesh->update(std::vector(12), std::vector(4, glm::uvec3(0, 1, 2))); + psMesh->updateMesh(std::vector(12), std::vector(4, glm::uvec3(0, 1, 2))); polyscope::show(3); - psMesh->update(std::vector(3), std::vector(4, glm::uvec3(0, 1, 2))); + psMesh->updateMesh(std::vector(3), std::vector(4, glm::uvec3(0, 1, 2))); polyscope::show(3); - psMesh->update(std::vector(3), std::vector(14, glm::uvec3(0, 1, 2))); + psMesh->updateMesh(std::vector(3), std::vector(14, glm::uvec3(0, 1, 2))); polyscope::show(3); - psMesh->update(std::vector(3), std::vector(1, glm::uvec3(0, 1, 2))); + psMesh->updateMesh(std::vector(3), std::vector(1, glm::uvec3(0, 1, 2))); polyscope::show(3); polyscope::removeAllStructures(); From 4a799c4d340e88a55310d633162434be2002539f Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Mon, 23 Nov 2026 00:53:20 -0800 Subject: [PATCH 24/31] update SimpleTriangleMesh for new buffer conventions --- include/polyscope/simple_triangle_mesh.h | 1 + src/simple_triangle_mesh.cpp | 28 ++++++++++---------- src/simple_triangle_mesh_color_quantity.cpp | 4 +-- src/simple_triangle_mesh_scalar_quantity.cpp | 4 +-- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/include/polyscope/simple_triangle_mesh.h b/include/polyscope/simple_triangle_mesh.h index b17e3787..3eb1e15a 100644 --- a/include/polyscope/simple_triangle_mesh.h +++ b/include/polyscope/simple_triangle_mesh.h @@ -163,6 +163,7 @@ class SimpleTriangleMesh : public Structure { // == Picking related things size_t pickStart = 0; + size_t pickRangeSize = 0; }; diff --git a/src/simple_triangle_mesh.cpp b/src/simple_triangle_mesh.cpp index 9ae2b180..ff374f08 100644 --- a/src/simple_triangle_mesh.cpp +++ b/src/simple_triangle_mesh.cpp @@ -109,7 +109,6 @@ void SimpleTriangleMesh::draw() { setSimpleTriangleMeshUniforms(*program); render::engine->setMaterialUniforms(*program, material.get()); program->setUniform("u_baseColor", surfaceColor.get()); - program->setDrawCount(static_cast(faces.size())); program->draw(); } @@ -151,7 +150,6 @@ void SimpleTriangleMesh::drawPick() { setStructureUniforms(*pickProgram); setSimpleTriangleMeshUniforms(*pickProgram, false); setPickUniforms(*pickProgram); - pickProgram->setDrawCount(static_cast(faces.size())); pickProgram->draw(); @@ -191,7 +189,7 @@ void SimpleTriangleMesh::setSimpleTriangleMeshUniforms(render::ShaderProgram& p, } } - p.setDrawCount(faces.size() * 3); + p.setDrawCount(static_cast(faces.size())); // this is indexed draw, so no x3 needed } void SimpleTriangleMesh::ensureRenderProgramPrepared() { @@ -217,19 +215,21 @@ void SimpleTriangleMesh::ensureRenderProgramPrepared() { void SimpleTriangleMesh::ensurePickProgramPrepared() { - // If already prepared, do nothing - if (pickProgram) return; - - // clang-format off - pickProgram = render::engine->requestShader("SIMPLE_MESH", - addSimpleTriangleMeshRules({"SIMPLE_MESH_PROPAGATE_FACE_PICK"}, false), - render::ShaderReplacementDefaults::Pick); - // clang-format on + if (!pickProgram) { + // clang-format off + pickProgram = render::engine->requestShader("SIMPLE_MESH", + addSimpleTriangleMeshRules({"SIMPLE_MESH_PROPAGATE_FACE_PICK"}, false), + render::ShaderReplacementDefaults::Pick); + // clang-format on - setSimpleTriangleMeshProgramGeometryAttributes(*pickProgram); + setSimpleTriangleMeshProgramGeometryAttributes(*pickProgram); + } - // Allocate one pick index per face - pickStart = pick::requestPickBufferRange(this, nFaces()); + // Ensure pick index range covers all faces (allocate for capacity so amortized doubling avoids frequent reallocs) + if (nFaces() > pickRangeSize) { + pickRangeSize = faces.capacity(); + pickStart = pick::requestPickBufferRange(this, pickRangeSize); + } } void SimpleTriangleMesh::setPickUniforms(render::ShaderProgram& p) { diff --git a/src/simple_triangle_mesh_color_quantity.cpp b/src/simple_triangle_mesh_color_quantity.cpp index 64baf8c2..9395f5ae 100644 --- a/src/simple_triangle_mesh_color_quantity.cpp +++ b/src/simple_triangle_mesh_color_quantity.cpp @@ -99,7 +99,7 @@ void SimpleTriangleMeshVertexColorQuantity::createProgram() { // clang-format on parent.setSimpleTriangleMeshProgramGeometryAttributes(*program); - program->setAttribute("a_color", colors.getRenderAttributeBuffer()); + program->setAttribute("a_color", colors); render::engine->setMaterial(*program, parent.getMaterial()); } @@ -140,7 +140,7 @@ void SimpleTriangleMeshFaceColorQuantity::createProgram() { // clang-format on parent.setSimpleTriangleMeshProgramGeometryAttributes(*program); - program->setTextureFromBuffer("t_faceColors", colors.getRenderTextureBuffer().get()); + program->setTextureFromBuffer("t_faceColors", colors); render::engine->setMaterial(*program, parent.getMaterial()); } diff --git a/src/simple_triangle_mesh_scalar_quantity.cpp b/src/simple_triangle_mesh_scalar_quantity.cpp index c7a06e0b..491d22f0 100644 --- a/src/simple_triangle_mesh_scalar_quantity.cpp +++ b/src/simple_triangle_mesh_scalar_quantity.cpp @@ -92,7 +92,7 @@ void SimpleTriangleMeshVertexScalarQuantity::createProgram() { // clang-format on parent.setSimpleTriangleMeshProgramGeometryAttributes(*program); - program->setAttribute("a_value", values.getRenderAttributeBuffer()); + program->setAttribute("a_value", values); program->setTextureFromColormap("t_colormap", cMap.get()); render::engine->setMaterial(*program, parent.getMaterial()); } @@ -132,7 +132,7 @@ void SimpleTriangleMeshFaceScalarQuantity::createProgram() { // clang-format on parent.setSimpleTriangleMeshProgramGeometryAttributes(*program); - program->setTextureFromBuffer("t_faceValues", values.getRenderTextureBuffer().get()); + program->setTextureFromBuffer("t_faceValues", values); program->setTextureFromColormap("t_colormap", cMap.get()); render::engine->setMaterial(*program, parent.getMaterial()); } From 0b9c058695f5394d9acf5bf3d810232ac5263ecc Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Mon, 23 Nov 2026 01:08:31 -0800 Subject: [PATCH 25/31] add better warnings around 1d texture size --- include/polyscope/render/managed_buffer.h | 4 ++- src/render/opengl/gl_engine.cpp | 38 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/include/polyscope/render/managed_buffer.h b/include/polyscope/render/managed_buffer.h index 08cb940d..e6ae31bb 100644 --- a/include/polyscope/render/managed_buffer.h +++ b/include/polyscope/render/managed_buffer.h @@ -139,7 +139,9 @@ class ManagedBuffer : public ManagedBufferBase, public virtual WeakReferrable { void setCapacity(size_t newCapacity); // Set the device buffer type. Call once at construction time before setTextureSize(). - // All buffers are Attribute by default, and can be switch to texture exactly once. + // All buffers are Attribute by default, and can be switched to texture exactly once. + // NOTE: Texture1d is limited to OpenGL's GL_MAX_TEXTURE_SIZE (typically 16k-32k elements); + // for large 1D arrays, keep the default Attribute type (perhaps binding as a buffer texture if needed). void setAsType(DeviceBufferType type); // Is it an attribute, texture1d, texture2d, etc? diff --git a/src/render/opengl/gl_engine.cpp b/src/render/opengl/gl_engine.cpp index bb1ed452..c2008475 100644 --- a/src/render/opengl/gl_engine.cpp +++ b/src/render/opengl/gl_engine.cpp @@ -506,10 +506,40 @@ uint32_t GLAttributeBuffer::getNativeBufferID() { return static_cast(V // ============================================================= +namespace { +void checkTextureSize1D(unsigned int sizeX) { + GLint maxTexSize = 0; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize); + if (maxTexSize > 0 && sizeX > static_cast(maxTexSize)) { + exception("OpenGL error: 1D texture size (" + std::to_string(sizeX) + ") exceeds GL_MAX_TEXTURE_SIZE (" + + std::to_string(maxTexSize) + + "). 1D textures cannot hold large arrays; use an AttributeBuffer / buffer texture instead."); + } +} +void checkTextureSize2D(unsigned int sizeX, unsigned int sizeY) { + GLint maxTexSize = 0; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize); + if (maxTexSize > 0 && (sizeX > static_cast(maxTexSize) || sizeY > static_cast(maxTexSize))) { + exception("OpenGL error: 2D texture size (" + std::to_string(sizeX) + " x " + std::to_string(sizeY) + + ") exceeds GL_MAX_TEXTURE_SIZE (" + std::to_string(maxTexSize) + ")."); + } +} +void checkTextureSize3D(unsigned int sizeX, unsigned int sizeY, unsigned int sizeZ) { + GLint maxTexSize = 0; + glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &maxTexSize); + if (maxTexSize > 0 && (sizeX > static_cast(maxTexSize) || sizeY > static_cast(maxTexSize) || + sizeZ > static_cast(maxTexSize))) { + exception("OpenGL error: 3D texture size (" + std::to_string(sizeX) + " x " + std::to_string(sizeY) + " x " + + std::to_string(sizeZ) + ") exceeds GL_MAX_3D_TEXTURE_SIZE (" + std::to_string(maxTexSize) + ")."); + } +} +} // namespace + // create a 1D texture from data GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int size1D, const unsigned char* data) : TextureBuffer(1, format_, size1D) { + checkTextureSize1D(size1D); glEnable(GL_TEXTURE_1D); glGenTextures(1, &handle); @@ -523,6 +553,7 @@ GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int size1D, con GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int size1D, const float* data) : TextureBuffer(1, format_, size1D) { + checkTextureSize1D(size1D); glGenTextures(1, &handle); glBindTexture(GL_TEXTURE_1D, handle); glTexImage1D(GL_TEXTURE_1D, 0, internalFormat(format), size1D, 0, formatF(format), GL_FLOAT, data); @@ -536,6 +567,7 @@ GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int sizeX_, uns const unsigned char* data) : TextureBuffer(2, format_, sizeX_, sizeY_) { + checkTextureSize2D(sizeX, sizeY); glGenTextures(1, &handle); glBindTexture(GL_TEXTURE_2D, handle); glTexImage2D(GL_TEXTURE_2D, 0, internalFormat(format), sizeX, sizeY, 0, formatF(format), GL_UNSIGNED_BYTE, data); @@ -547,6 +579,7 @@ GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int sizeX_, uns GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int sizeX_, unsigned int sizeY_, const float* data) : TextureBuffer(2, format_, sizeX_, sizeY_) { + checkTextureSize2D(sizeX, sizeY); glGenTextures(1, &handle); glBindTexture(GL_TEXTURE_2D, handle); glTexImage2D(GL_TEXTURE_2D, 0, internalFormat(format), sizeX, sizeY, 0, formatF(format), GL_FLOAT, data); @@ -560,6 +593,7 @@ GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int sizeX_, uns const unsigned char* data) : TextureBuffer(3, format_, sizeX_, sizeY_, sizeZ_) { + checkTextureSize3D(sizeX, sizeY, sizeZ); glGenTextures(1, &handle); glBindTexture(GL_TEXTURE_3D, handle); glTexImage3D(GL_TEXTURE_3D, 0, internalFormat(format), sizeX, sizeY, sizeZ, 0, formatF(format), GL_UNSIGNED_BYTE, @@ -573,6 +607,7 @@ GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int sizeX_, uns const float* data) : TextureBuffer(3, format_, sizeX_, sizeY_, sizeZ_) { + checkTextureSize3D(sizeX, sizeY, sizeZ); glGenTextures(1, &handle); glBindTexture(GL_TEXTURE_3D, handle); glTexImage3D(GL_TEXTURE_3D, 0, internalFormat(format), sizeX, sizeY, sizeZ, 0, formatF(format), GL_FLOAT, data); @@ -589,6 +624,7 @@ void GLTextureBuffer::resize(unsigned int newLen) { bind(); if (dim == 1) { + checkTextureSize1D(sizeX); glTexImage1D(GL_TEXTURE_1D, 0, internalFormat(format), sizeX, 0, formatF(format), type(format), nullptr); } else { exception("OpenGL error: called 1D resize on not-1D texture"); @@ -602,6 +638,7 @@ void GLTextureBuffer::resize(unsigned int newX, unsigned int newY) { bind(); if (dim == 2) { + checkTextureSize2D(sizeX, sizeY); glTexImage2D(GL_TEXTURE_2D, 0, internalFormat(format), sizeX, sizeY, 0, formatF(format), type(format), nullptr); } else { exception("OpenGL error: called 2D resize on not-2D texture"); @@ -615,6 +652,7 @@ void GLTextureBuffer::resize(unsigned int newX, unsigned int newY, unsigned int bind(); if (dim == 3) { + checkTextureSize3D(sizeX, sizeY, sizeZ); glTexImage3D(GL_TEXTURE_3D, 0, internalFormat(format), sizeX, sizeY, sizeZ, 0, formatF(format), type(format), nullptr); } else { From c8a6a2cb9993d7d3a73868d650a489fdfbff7c83 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Tue, 24 Nov 2026 14:20:08 -0800 Subject: [PATCH 26/31] add support for GLBufferTextures from attributes, generalize texture types to enum --- include/polyscope/render/engine.h | 13 +- include/polyscope/render/managed_buffer.h | 5 + .../render/mock_opengl/mock_gl_engine.h | 9 +- include/polyscope/render/opengl/gl_engine.h | 15 ++- src/render/engine.cpp | 14 +++ src/render/mock_opengl/mock_gl_engine.cpp | 66 +++++++--- src/render/opengl/gl_engine.cpp | 114 ++++++++++++++++-- src/render/opengl/shaders/grid_shaders.cpp | 4 +- .../opengl/shaders/ground_plane_shaders.cpp | 8 +- .../opengl/shaders/lighting_shaders.cpp | 6 +- src/render/opengl/shaders/rules.cpp | 16 +-- .../opengl/shaders/surface_mesh_shaders.cpp | 22 ++-- .../opengl/shaders/texture_draw_shaders.cpp | 32 ++--- src/render/shader_builder.cpp | 4 +- src/simple_triangle_mesh_color_quantity.cpp | 7 +- src/simple_triangle_mesh_scalar_quantity.cpp | 7 +- test/src/surface_mesh_test.cpp | 80 ++++++++++++ 17 files changed, 333 insertions(+), 89 deletions(-) diff --git a/include/polyscope/render/engine.h b/include/polyscope/render/engine.h index b199e0cc..de20d714 100644 --- a/include/polyscope/render/engine.h +++ b/include/polyscope/render/engine.h @@ -63,6 +63,7 @@ enum class RenderDataType { }; enum class DeviceBufferType { Attribute, Texture1d, Texture2d, Texture3d }; +enum class ShaderTextureType { Texture1D, Texture2D, Texture3D, TextureBuffer }; int dimension(const TextureFormat& x); int sizeInBytes(const TextureFormat& f); @@ -72,6 +73,7 @@ int sizeInBytes(const RenderDataType& r); int renderDataTypeCountCompatbility(const RenderDataType r1, const RenderDataType r2); std::string getImageOriginRule(ImageOrigin imageOrigin); std::string deviceBufferTypeName(const DeviceBufferType& d); +std::string shaderTextureTypeName(const ShaderTextureType& t); namespace render { @@ -354,7 +356,7 @@ struct ShaderSpecAttribute { }; struct ShaderSpecTexture { const std::string name; - const int dim; + const ShaderTextureType type; }; @@ -454,12 +456,21 @@ class ShaderProgram { virtual void setTextureFromColormap(std::string name, const std::string& colorMap, bool allowUpdate = false) = 0; // TODO make this one take a shared pointer and have the same semantics as the attribute version virtual void setTextureFromBuffer(std::string name, TextureBuffer* textureBuffer, ManagedBufferBase* source = nullptr) = 0; + // Bind an AttributeBuffer (VBO) as a buffer texture (samplerBuffer / GL_TEXTURE_BUFFER, ShaderTextureType::TextureBuffer). + // Note: the buffer is always exposed to shaders as a flattened 1-component scalar stream (GL_R32F / GL_R32I / GL_R32UI), + // even for vector/matrix types (e.g., fetch a vec3 at index i via texelFetch at 3*i + {0, 1, 2}). This is done because + // OpenGL 3.3 core does not support 3-component buffer texture formats (GL_RGB32F requires GL 4.0). + virtual void setTextureFromAttributeBuffer(std::string name, std::shared_ptr externalBuffer, + ManagedBufferBase* source = nullptr) = 0; // Convenience overloads for ManagedBuffer — set the buffer AND record the source for lazy sync. // Defined in managed_buffer.h after ManagedBuffer is fully declared. template void setAttribute(std::string name, ManagedBuffer& buf); + // Binds a texture-backed ManagedBuffer (DeviceBufferType::Texture1d/2d/3d) to a sampler1D/2D/3D. template void setTextureFromBuffer(std::string name, ManagedBuffer& buf); + // Binds an attribute-backed ManagedBuffer (DeviceBufferType::Attribute) as a buffer texture (samplerBuffer / TextureBuffer). + template void setTextureFromAttributeBuffer(std::string name, ManagedBuffer& buf); template void setIndex(ManagedBuffer& buf); // Indices diff --git a/include/polyscope/render/managed_buffer.h b/include/polyscope/render/managed_buffer.h index e6ae31bb..e94bdc85 100644 --- a/include/polyscope/render/managed_buffer.h +++ b/include/polyscope/render/managed_buffer.h @@ -429,6 +429,11 @@ void ShaderProgram::setTextureFromBuffer(std::string name, ManagedBuffer& buf setTextureFromBuffer(name, buf.getRenderTextureBuffer().get(), &buf); } +template +void ShaderProgram::setTextureFromAttributeBuffer(std::string name, ManagedBuffer& buf) { + setTextureFromAttributeBuffer(name, buf.getRenderAttributeBuffer(), &buf); +} + template void ShaderProgram::setIndex(ManagedBuffer& buf) { indexSourceManagedBuffer = &buf; diff --git a/include/polyscope/render/mock_opengl/mock_gl_engine.h b/include/polyscope/render/mock_opengl/mock_gl_engine.h index 7bda3c0a..58b545a4 100644 --- a/include/polyscope/render/mock_opengl/mock_gl_engine.h +++ b/include/polyscope/render/mock_opengl/mock_gl_engine.h @@ -249,12 +249,15 @@ struct GLShaderAttribute { struct GLShaderTexture { std::string name; - int dim; + ShaderTextureType type; uint32_t index; bool isSet; - GLTextureBuffer* textureBuffer; + GLTextureBuffer* textureBuffer; // NOTE: very confusing name, unrelated to the OpenGL "TextureBuffer" concept as used below std::shared_ptr textureBufferOwned; // might be empty, if texture isn't owned ManagedBufferBase* sourceManagedBuffer; // might be empty if not from a managed buffer. for indexed views, it's the original data source + + // used only for ShaderTextureType::TextureBuffer + std::shared_ptr attrBuffer; }; // A thin wrapper around a program handle. @@ -345,6 +348,8 @@ class GLShaderProgram : public ShaderProgram { bool withAlpha = true, bool useMipMap = false, bool repeat = false) override; void setTextureFromColormap(std::string name, const std::string& colorMap, bool allowUpdate = false) override; void setTextureFromBuffer(std::string name, TextureBuffer* textureBuffer, ManagedBufferBase* source = nullptr) override; + void setTextureFromAttributeBuffer(std::string name, std::shared_ptr externalBuffer, + ManagedBufferBase* source = nullptr) override; // ManagedBuffer source registration diff --git a/include/polyscope/render/opengl/gl_engine.h b/include/polyscope/render/opengl/gl_engine.h index 008344c7..2ac6b46d 100644 --- a/include/polyscope/render/opengl/gl_engine.h +++ b/include/polyscope/render/opengl/gl_engine.h @@ -140,6 +140,9 @@ class GLAttributeBuffer : public AttributeBuffer { }; class GLTextureBuffer : public TextureBuffer { +// NOTE: very confusing name, this is unrelated to the openGL "TextureBuffer" concept as used in ShaderTextureType::TextureBuffer +// this is just a texture that happens to hold buffer data + public: // create a 1D texture from data GLTextureBuffer(TextureFormat format, unsigned int size1D, const unsigned char* data = nullptr); @@ -289,13 +292,19 @@ struct GLShaderAttribute { struct GLShaderTexture { std::string name; - int dim; + ShaderTextureType type; uint32_t index; bool isSet; - GLTextureBuffer* textureBuffer; + GLTextureBuffer* textureBuffer; // NOTE: very confusing name, unrelated to the openGL "TextureBuffer" concept as used below std::shared_ptr textureBufferOwned; // might be empty, if texture isn't owned TextureLocation location; // -1 means "no location", usually because it was optimized out ManagedBufferBase* sourceManagedBuffer; // might be empty if not from a managed buffer. for indexed views, it's the original data source + + // these are used only for ShaderTextureType::TextureBuffer + std::shared_ptr attrBuffer; + TextureBufferHandle bufferTexHandle; // GL_TEXTURE_BUFFER handle owned by GLShaderProgram + GLenum bufferTexFormat; // GL_R32F / GL_R32I / GL_R32UI + uint64_t bufferTexBoundSize; // tracks attrBuffer->getBufferSize() across reallocations }; // A thin wrapper around a program handle. @@ -389,6 +398,8 @@ class GLShaderProgram : public ShaderProgram { bool withAlpha = true, bool useMipMap = false, bool repeat = false) override; void setTextureFromColormap(std::string name, const std::string& colorMap, bool allowUpdate = false) override; void setTextureFromBuffer(std::string name, TextureBuffer* textureBuffer, ManagedBufferBase* source = nullptr) override; + void setTextureFromAttributeBuffer(std::string name, std::shared_ptr externalBuffer, + ManagedBufferBase* source = nullptr) override; // ManagedBuffer source registration diff --git a/src/render/engine.cpp b/src/render/engine.cpp index aa64f978..79ff0ac8 100644 --- a/src/render/engine.cpp +++ b/src/render/engine.cpp @@ -49,6 +49,20 @@ int sizeInBytes(const TextureFormat& f) { return -1; } +std::string shaderTextureTypeName(const ShaderTextureType& t) { + switch (t) { + case ShaderTextureType::Texture1D: + return "Texture1D"; + case ShaderTextureType::Texture2D: + return "Texture2D"; + case ShaderTextureType::Texture3D: + return "Texture3D"; + case ShaderTextureType::TextureBuffer: + return "TextureBuffer"; + } + return ""; +} + std::string renderDataTypeName(const RenderDataType& r) { switch (r) { case RenderDataType::Vector2Float: diff --git a/src/render/mock_opengl/mock_gl_engine.cpp b/src/render/mock_opengl/mock_gl_engine.cpp index 28d00fde..5d4da881 100644 --- a/src/render/mock_opengl/mock_gl_engine.cpp +++ b/src/render/mock_opengl/mock_gl_engine.cpp @@ -821,13 +821,13 @@ void GLCompiledProgram::addUniqueTexture(ShaderSpecTexture newTexture) { if (t.name == newTexture.name) { // if it occurs twice, confirm that the occurences match - if (t.dim != newTexture.dim) - exception("texture " + t.name + " appears twice in program with different dimensions"); + if (t.type != newTexture.type) + exception("texture " + t.name + " appears twice in program with different types"); return; } } - textures.push_back(GLShaderTexture{newTexture.name, newTexture.dim, 777, false, nullptr, nullptr, nullptr}); + textures.push_back(GLShaderTexture{newTexture.name, newTexture.type, 777, false, nullptr, nullptr, nullptr, nullptr}); } @@ -1419,8 +1419,9 @@ void GLShaderProgram::setTexture1D(std::string name, unsigned char* texData, uns throw std::invalid_argument("Attempted to set texture twice"); } - if (t.dim != 1) { - throw std::invalid_argument("Tried to use texture with mismatched dimension " + std::to_string(t.dim)); + if (t.type != ShaderTextureType::Texture1D) { + throw std::invalid_argument("Tried to use texture " + name + " with mismatched type " + + shaderTextureTypeName(t.type) + " (expected Texture1D)"); } // Create a new texture object @@ -1452,8 +1453,9 @@ void GLShaderProgram::setTexture2D(std::string name, unsigned char* texData, uns throw std::invalid_argument("Attempted to set texture twice"); } - if (t.dim != 2) { - throw std::invalid_argument("Tried to use texture with mismatched dimension " + std::to_string(t.dim)); + if (t.type != ShaderTextureType::Texture2D) { + throw std::invalid_argument("Tried to use texture " + name + " with mismatched type " + + shaderTextureTypeName(t.type) + " (expected Texture2D)"); } if (withAlpha) { @@ -1487,8 +1489,13 @@ void GLShaderProgram::setTextureFromBuffer(std::string name, TextureBuffer* text for (GLShaderTexture& t : textures) { if (t.name != name) continue; - if (t.dim != (int)textureBuffer->getDimension()) { - throw std::invalid_argument("Tried to use texture with mismatched dimension " + std::to_string(t.dim)); + unsigned int bufDim = textureBuffer->getDimension(); + bool typeMatches = (bufDim == 1 && t.type == ShaderTextureType::Texture1D) || + (bufDim == 2 && t.type == ShaderTextureType::Texture2D) || + (bufDim == 3 && t.type == ShaderTextureType::Texture3D); + if (!typeMatches) { + throw std::invalid_argument("Tried to use texture " + name + " with mismatched type " + + shaderTextureTypeName(t.type) + " for " + std::to_string(bufDim) + "D texture buffer"); } t.textureBuffer = dynamic_cast(textureBuffer); @@ -1504,6 +1511,30 @@ void GLShaderProgram::setTextureFromBuffer(std::string name, TextureBuffer* text throw std::invalid_argument("No texture with name " + name); } +void GLShaderProgram::setTextureFromAttributeBuffer(std::string name, std::shared_ptr externalBuffer, + ManagedBufferBase* source) { + std::shared_ptr engineExtBuff = std::dynamic_pointer_cast(externalBuffer); + if (!engineExtBuff) { + throw std::invalid_argument("Bad attribute buffer in setTextureFromAttributeBuffer()"); + } + + for (GLShaderTexture& t : textures) { + if (t.name != name) continue; + + if (t.type != ShaderTextureType::TextureBuffer) { + throw std::invalid_argument("Tried to bind attribute buffer to texture " + name + " of type " + + shaderTextureTypeName(t.type) + " (expected TextureBuffer)"); + } + + t.attrBuffer = engineExtBuff; + t.sourceManagedBuffer = source; + t.isSet = true; + return; + } + + throw std::invalid_argument("No texture with name " + name); +} + void GLShaderProgram::setTextureFromColormap(std::string name, const std::string& colormapName, bool allowUpdate) { const ValueColorMap& colormap = render::engine->getColorMap(colormapName); @@ -1515,8 +1546,9 @@ void GLShaderProgram::setTextureFromColormap(std::string name, const std::string throw std::invalid_argument("Attempted to set texture twice"); } - if (t.dim != 1) { - throw std::invalid_argument("Tried to use texture with mismatched dimension " + std::to_string(t.dim)); + if (t.type != ShaderTextureType::Texture1D) { + throw std::invalid_argument("Tried to use texture " + name + " with mismatched type " + + shaderTextureTypeName(t.type) + " (expected Texture1D)"); } // Fill a buffer with the data @@ -1620,6 +1652,9 @@ void GLShaderProgram::validateData() { if (!t.isSet) { throw std::invalid_argument("Texture " + t.name + " has not been set"); } + if (t.type == ShaderTextureType::TextureBuffer && (!t.attrBuffer || !t.attrBuffer->isSet())) { + throw std::invalid_argument("Buffer texture " + t.name + " has no data set"); + } } // Check index (if applicable) @@ -1667,14 +1702,9 @@ void GLShaderProgram::activateTextures() { // Point the uniform at this texture // Bind to the texture buffer - switch (t.dim) { - case 1: - break; - case 2: - break; + if (t.type != ShaderTextureType::TextureBuffer) { + t.textureBuffer->bind(); } - - t.textureBuffer->bind(); } } diff --git a/src/render/opengl/gl_engine.cpp b/src/render/opengl/gl_engine.cpp index c2008475..c4e7f4d9 100644 --- a/src/render/opengl/gl_engine.cpp +++ b/src/render/opengl/gl_engine.cpp @@ -1309,13 +1309,14 @@ void GLCompiledProgram::addUniqueTexture(ShaderSpecTexture newTexture) { if (t.name == newTexture.name) { // if it occurs twice, confirm that the occurences match - if (t.dim != newTexture.dim) - exception("texture " + t.name + " appears twice in program with different dimensions"); + if (t.type != newTexture.type) + exception("texture " + t.name + " appears twice in program with different types"); return; } } - textures.push_back(GLShaderTexture{newTexture.name, newTexture.dim, 777, false, nullptr, nullptr, 777, nullptr}); + textures.push_back( + GLShaderTexture{newTexture.name, newTexture.type, 777, false, nullptr, nullptr, 777, nullptr, nullptr, 0, 0, 0}); } @@ -1332,7 +1333,14 @@ GLShaderProgram::GLShaderProgram(const std::shared_ptr& compi checkGLError(); } -GLShaderProgram::~GLShaderProgram() { glDeleteVertexArrays(1, &vaoHandle); } +GLShaderProgram::~GLShaderProgram() { + for (GLShaderTexture& t : textures) { + if (t.bufferTexHandle != 0) { + glDeleteTextures(1, &t.bufferTexHandle); + } + } + glDeleteVertexArrays(1, &vaoHandle); +} void GLShaderProgram::bindVAO() { glBindVertexArray(vaoHandle); } @@ -2012,8 +2020,9 @@ void GLShaderProgram::setTexture1D(std::string name, unsigned char* texData, uns throw std::invalid_argument("Attempted to set texture twice"); } - if (t.dim != 1) { - throw std::invalid_argument("Tried to use texture with mismatched dimension " + std::to_string(t.dim)); + if (t.type != ShaderTextureType::Texture1D) { + throw std::invalid_argument("Tried to use texture " + name + " with mismatched type " + + shaderTextureTypeName(t.type) + " (expected Texture1D)"); } // Create a new texture object @@ -2043,8 +2052,9 @@ void GLShaderProgram::setTexture2D(std::string name, unsigned char* texData, uns throw std::invalid_argument("Attempted to set texture twice"); } - if (t.dim != 2) { - throw std::invalid_argument("Tried to use texture with mismatched dimension " + std::to_string(t.dim)); + if (t.type != ShaderTextureType::Texture2D) { + throw std::invalid_argument("Tried to use texture " + name + " with mismatched type " + + shaderTextureTypeName(t.type) + " (expected Texture2D)"); } if (withAlpha) { @@ -2087,8 +2097,13 @@ void GLShaderProgram::setTextureFromBuffer(std::string name, TextureBuffer* text for (GLShaderTexture& t : textures) { if (t.name != name || t.location == -1) continue; - if (t.dim != (int)textureBuffer->getDimension()) { - throw std::invalid_argument("Tried to use texture with mismatched dimension " + std::to_string(t.dim)); + unsigned int bufDim = textureBuffer->getDimension(); + bool typeMatches = (bufDim == 1 && t.type == ShaderTextureType::Texture1D) || + (bufDim == 2 && t.type == ShaderTextureType::Texture2D) || + (bufDim == 3 && t.type == ShaderTextureType::Texture3D); + if (!typeMatches) { + throw std::invalid_argument("Tried to use texture " + name + " with mismatched type " + + shaderTextureTypeName(t.type) + " for " + std::to_string(bufDim) + "D texture buffer"); } t.textureBuffer = dynamic_cast(textureBuffer); @@ -2104,6 +2119,65 @@ void GLShaderProgram::setTextureFromBuffer(std::string name, TextureBuffer* text throw std::invalid_argument("No texture with name " + name); } +void GLShaderProgram::setTextureFromAttributeBuffer(std::string name, std::shared_ptr externalBuffer, + ManagedBufferBase* source) { + glUseProgram(compiledProgram->getHandle()); + + std::shared_ptr engineExtBuff = std::dynamic_pointer_cast(externalBuffer); + if (!engineExtBuff) { + throw std::invalid_argument("Bad attribute buffer in setTextureFromAttributeBuffer()"); + } + + // Use 1-component 32-bit formats (strictly OpenGL 3.3 core everywhere, GL_RGB32F TBOs require GL 4.0). + GLenum texFormat = GL_R32F; + switch (engineExtBuff->getType()) { + case RenderDataType::Float: + case RenderDataType::Vector2Float: + case RenderDataType::Vector3Float: + case RenderDataType::Vector4Float: + case RenderDataType::Matrix44Float: + texFormat = GL_R32F; + break; + case RenderDataType::Int: + case RenderDataType::Vector2Int: + case RenderDataType::Vector3Int: + case RenderDataType::Vector4Int: + texFormat = GL_R32I; + break; + case RenderDataType::UInt: + case RenderDataType::Vector2UInt: + case RenderDataType::Vector3UInt: + case RenderDataType::Vector4UInt: + texFormat = GL_R32UI; + break; + } + + for (GLShaderTexture& t : textures) { + if (t.name != name || t.location == -1) continue; + + if (t.type != ShaderTextureType::TextureBuffer) { + throw std::invalid_argument("Tried to bind attribute buffer to texture " + name + " of type " + + shaderTextureTypeName(t.type) + " (expected TextureBuffer)"); + } + + if (t.bufferTexHandle == 0) { + glGenTextures(1, &t.bufferTexHandle); + } + glBindTexture(GL_TEXTURE_BUFFER, t.bufferTexHandle); + glTexBuffer(GL_TEXTURE_BUFFER, texFormat, engineExtBuff->getHandle()); + checkGLError(); + + t.attrBuffer = engineExtBuff; + t.bufferTexFormat = texFormat; + t.bufferTexBoundSize = engineExtBuff->getBufferSize(); + t.sourceManagedBuffer = source; + t.isSet = true; + return; + } + + throw std::invalid_argument("No texture with name " + name); +} + void GLShaderProgram::setTextureFromColormap(std::string name, const std::string& colormapName, bool allowUpdate) { const ValueColorMap& colormap = render::engine->getColorMap(colormapName); @@ -2117,8 +2191,9 @@ void GLShaderProgram::setTextureFromColormap(std::string name, const std::string throw std::invalid_argument("Attempted to set texture twice"); } - if (t.dim != 1) { - throw std::invalid_argument("Tried to use texture with mismatched dimension " + std::to_string(t.dim)); + if (t.type != ShaderTextureType::Texture1D) { + throw std::invalid_argument("Tried to use texture " + name + " with mismatched type " + + shaderTextureTypeName(t.type) + " (expected Texture1D)"); } // Fill a buffer with the data @@ -2229,6 +2304,9 @@ void GLShaderProgram::validateData() { if (!t.isSet) { throw std::invalid_argument("Texture " + t.name + " has not been set"); } + if (t.type == ShaderTextureType::TextureBuffer && (!t.attrBuffer || !t.attrBuffer->isSet())) { + throw std::invalid_argument("Buffer texture " + t.name + " has no data set"); + } } // Check index (if applicable) @@ -2277,7 +2355,17 @@ void GLShaderProgram::activateTextures() { if (t.location == -1) continue; glActiveTexture(GL_TEXTURE0 + t.index); - t.textureBuffer->bind(); + if (t.type == ShaderTextureType::TextureBuffer) { + glBindTexture(GL_TEXTURE_BUFFER, t.bufferTexHandle); + // Re-attach if the underlying VBO was reallocated (guards against driver quirks where + // glTexBuffer caches the old backing storage across glBufferData). + if (t.attrBuffer->getBufferSize() != t.bufferTexBoundSize) { + glTexBuffer(GL_TEXTURE_BUFFER, t.bufferTexFormat, t.attrBuffer->getHandle()); + t.bufferTexBoundSize = t.attrBuffer->getBufferSize(); + } + } else { + t.textureBuffer->bind(); + } glUniform1i(t.location, t.index); } } diff --git a/src/render/opengl/shaders/grid_shaders.cpp b/src/render/opengl/shaders/grid_shaders.cpp index 0cc479ad..c7923d79 100644 --- a/src/render/opengl/shaders/grid_shaders.cpp +++ b/src/render/opengl/shaders/grid_shaders.cpp @@ -400,7 +400,7 @@ const ShaderReplacementRule GRIDCUBE_PROPAGATE_NODE_VALUE ( /* uniforms */ {}, /* attributes */ { }, /* textures */ { - {"t_value", 3}, + {"t_value", ShaderTextureType::Texture3D}, } ); @@ -417,7 +417,7 @@ const ShaderReplacementRule GRIDCUBE_PROPAGATE_CELL_VALUE ( /* uniforms */ {}, /* attributes */ { }, /* textures */ { - {"t_value", 3}, + {"t_value", ShaderTextureType::Texture3D}, } ); diff --git a/src/render/opengl/shaders/ground_plane_shaders.cpp b/src/render/opengl/shaders/ground_plane_shaders.cpp index d0782028..38c4346b 100644 --- a/src/render/opengl/shaders/ground_plane_shaders.cpp +++ b/src/render/opengl/shaders/ground_plane_shaders.cpp @@ -70,7 +70,7 @@ const ShaderStageSpecification GROUND_PLANE_TILE_FRAG_SHADER= { // textures { - {"t_ground", 2}, + {"t_ground", ShaderTextureType::Texture2D}, }, // source @@ -166,8 +166,8 @@ const ShaderStageSpecification GROUND_PLANE_TILE_REFLECT_FRAG_SHADER = { // textures { - {"t_ground", 2}, - {"t_mirrorImage", 2}, + {"t_ground", ShaderTextureType::Texture2D}, + {"t_mirrorImage", ShaderTextureType::Texture2D}, }, // source @@ -273,7 +273,7 @@ const ShaderStageSpecification GROUND_PLANE_SHADOW_FRAG_SHADER = { // textures { - {"t_shadow", 2}, + {"t_shadow", ShaderTextureType::Texture2D}, }, // source diff --git a/src/render/opengl/shaders/lighting_shaders.cpp b/src/render/opengl/shaders/lighting_shaders.cpp index 5eb8e91b..91a355f8 100644 --- a/src/render/opengl/shaders/lighting_shaders.cpp +++ b/src/render/opengl/shaders/lighting_shaders.cpp @@ -30,7 +30,7 @@ const ShaderStageSpecification MAP_LIGHT_FRAG_SHADER = { // textures { - {"t_image", 2}, + {"t_image", ShaderTextureType::Texture2D}, }, // source @@ -279,7 +279,7 @@ const ShaderReplacementRule TRANSPARENCY_PEEL_STRUCTURE ( }, /* attributes */ {}, /* textures */ { - {"t_minDepth", 2}, + {"t_minDepth", ShaderTextureType::Texture2D}, } ); @@ -302,7 +302,7 @@ const ShaderReplacementRule TRANSPARENCY_PEEL_GROUND ( /* uniforms */ {}, /* attributes */ {}, /* textures */ { - {"t_minDepth", 2}, + {"t_minDepth", ShaderTextureType::Texture2D}, } ); diff --git a/src/render/opengl/shaders/rules.cpp b/src/render/opengl/shaders/rules.cpp index c7a19719..5bc7fcbd 100644 --- a/src/render/opengl/shaders/rules.cpp +++ b/src/render/opengl/shaders/rules.cpp @@ -47,10 +47,10 @@ const ShaderReplacementRule LIGHT_MATCAP ( /* uniforms */ {}, /* attributes */ {}, /* textures */ { - {"t_mat_r", 2}, - {"t_mat_g", 2}, - {"t_mat_b", 2}, - {"t_mat_k", 2}, + {"t_mat_r", ShaderTextureType::Texture2D}, + {"t_mat_g", ShaderTextureType::Texture2D}, + {"t_mat_b", ShaderTextureType::Texture2D}, + {"t_mat_k", ShaderTextureType::Texture2D}, } ); @@ -135,7 +135,7 @@ const ShaderReplacementRule SHADE_COLORMAP_VALUE( }, /* attributes */ {}, /* textures */ { - {"t_colormap", 1} + {"t_colormap", ShaderTextureType::Texture1D} } ); @@ -162,7 +162,7 @@ const ShaderReplacementRule SHADE_CATEGORICAL_COLORMAP( }, /* attributes */ {}, /* textures */ { - {"t_colormap", 1} + {"t_colormap", ShaderTextureType::Texture1D} } ); @@ -187,7 +187,7 @@ const ShaderReplacementRule SHADE_COLORMAP_ANGULAR2( }, /* attributes */ {}, /* textures */ { - {"t_colormap", 1} + {"t_colormap", ShaderTextureType::Texture1D} } ); @@ -288,7 +288,7 @@ const ShaderReplacementRule SHADE_CHECKER_CATEGORY( }, /* attributes */ {}, /* textures */ { - {"t_colormap", 1} + {"t_colormap", ShaderTextureType::Texture1D} } ); diff --git a/src/render/opengl/shaders/surface_mesh_shaders.cpp b/src/render/opengl/shaders/surface_mesh_shaders.cpp index 6b842c6a..d278a8f3 100644 --- a/src/render/opengl/shaders/surface_mesh_shaders.cpp +++ b/src/render/opengl/shaders/surface_mesh_shaders.cpp @@ -731,38 +731,44 @@ const ShaderReplacementRule MESH_PROPAGATE_PICK_SIMPLE ( // this one does faces ); -// Uses gl_PrimitiveID to look up per-face scalar data from a 1D texture. +// Uses gl_PrimitiveID to look up per-face scalar data from a buffer texture (GL_TEXTURE_BUFFER / GL_R32F). // Requires indexed triangle drawing (DrawMode::IndexedTriangles) where primitive index == face index. const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_VALUE( /* rule name */ "SIMPLE_MESH_PROPAGATE_FACE_VALUE", { /* replacement sources */ {"FRAG_DECLARATIONS", R"( - uniform sampler1D t_faceValues; + uniform samplerBuffer t_faceValues; )"}, {"GENERATE_SHADE_VALUE", R"( - float shadeValue = texelFetch(t_faceValues, gl_PrimitiveID, 0).r; + float shadeValue = texelFetch(t_faceValues, gl_PrimitiveID).r; )"}, }, /* uniforms */ {}, /* attributes */ {}, - /* textures */ {{"t_faceValues", 1}} + /* textures */ {{"t_faceValues", ShaderTextureType::TextureBuffer}} ); -// Uses gl_PrimitiveID to look up per-face color data from a 1D texture. +// Uses gl_PrimitiveID to look up per-face color data from a buffer texture (GL_TEXTURE_BUFFER / GL_R32F). +// Fetches 3 floats per face so this stays valid in strict OpenGL 3.3 core (GL_RGB32F TBOs require GL 4.0). // Requires indexed triangle drawing (DrawMode::IndexedTriangles) where primitive index == face index. const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_COLOR( /* rule name */ "SIMPLE_MESH_PROPAGATE_FACE_COLOR", { /* replacement sources */ {"FRAG_DECLARATIONS", R"( - uniform sampler1D t_faceColors; + uniform samplerBuffer t_faceColors; )"}, {"GENERATE_SHADE_VALUE", R"( - vec3 shadeColor = texelFetch(t_faceColors, gl_PrimitiveID, 0).rgb; + int faceColorBase = 3 * gl_PrimitiveID; + vec3 shadeColor = vec3( + texelFetch(t_faceColors, faceColorBase).r, + texelFetch(t_faceColors, faceColorBase + 1).r, + texelFetch(t_faceColors, faceColorBase + 2).r + ); )"}, }, /* uniforms */ {}, /* attributes */ {}, - /* textures */ {{"t_faceColors", 1}} + /* textures */ {{"t_faceColors", ShaderTextureType::TextureBuffer}} ); // Encodes the pick color for each face directly from gl_PrimitiveID + pickStart, with no texture lookup. diff --git a/src/render/opengl/shaders/texture_draw_shaders.cpp b/src/render/opengl/shaders/texture_draw_shaders.cpp index 507a5fe2..09655245 100644 --- a/src/render/opengl/shaders/texture_draw_shaders.cpp +++ b/src/render/opengl/shaders/texture_draw_shaders.cpp @@ -100,7 +100,7 @@ const ShaderStageSpecification PLAIN_TEXTURE_DRAW_FRAG_SHADER = { { }, // textures - { {"t_image", 2} }, + { {"t_image", ShaderTextureType::Texture2D} }, // source R"( @@ -141,7 +141,7 @@ const ShaderStageSpecification PLAIN_RENDERIMAGE_TEXTURE_DRAW_FRAG_SHADER = { // textures { - {"t_depth", 2}, + {"t_depth", ShaderTextureType::Texture2D}, }, // source @@ -225,7 +225,7 @@ const ShaderStageSpecification PLAIN_RAW_RENDERIMAGE_TEXTURE_DRAW_FRAG_SHADER = // textures { - {"t_depth", 2}, + {"t_depth", ShaderTextureType::Texture2D}, }, // source @@ -304,7 +304,7 @@ const ShaderStageSpecification DOT3_TEXTURE_DRAW_FRAG_SHADER = { { }, // textures - { {"t_image", 2} }, + { {"t_image", ShaderTextureType::Texture2D} }, // source R"( @@ -338,7 +338,7 @@ const ShaderStageSpecification MAP3_TEXTURE_DRAW_FRAG_SHADER = { { }, // textures - { {"t_image", 2} }, + { {"t_image", ShaderTextureType::Texture2D} }, // source R"( @@ -371,7 +371,7 @@ const ShaderStageSpecification SPHEREBG_DRAW_FRAG_SHADER = { { }, // textures - { {"t_image", 2} }, + { {"t_image", ShaderTextureType::Texture2D} }, // source R"( @@ -407,7 +407,7 @@ const ShaderStageSpecification COMPOSITE_PEEL = { { }, // textures - { {"t_image", 2} }, + { {"t_image", ShaderTextureType::Texture2D} }, // source R"( @@ -437,7 +437,7 @@ const ShaderStageSpecification DEPTH_COPY = { { }, // textures - { {"t_depth", 2} }, + { {"t_depth", ShaderTextureType::Texture2D} }, // source R"( @@ -469,7 +469,7 @@ const ShaderStageSpecification DEPTH_TO_MASK = { { }, // textures - { {"t_depth", 2} }, + { {"t_depth", ShaderTextureType::Texture2D} }, // source R"( @@ -504,7 +504,7 @@ const ShaderStageSpecification SCALAR_TEXTURE_COLORMAP = { // textures { - {"t_scalar", 2}, + {"t_scalar", ShaderTextureType::Texture2D}, }, // source @@ -549,7 +549,7 @@ const ShaderStageSpecification BLUR_RGB = { // textures { - {"t_image", 2}, + {"t_image", ShaderTextureType::Texture2D}, }, // source @@ -670,7 +670,7 @@ const ShaderReplacementRule TEXTURE_SHADE_COLOR( /* uniforms */ {}, /* attributes */ {}, /* textures */ { - {"t_color", 2}, + {"t_color", ShaderTextureType::Texture2D}, } ); @@ -693,7 +693,7 @@ const ShaderReplacementRule TEXTURE_SHADE_COLORALPHA( /* uniforms */ {}, /* attributes */ {}, /* textures */ { - {"t_color", 2}, + {"t_color", ShaderTextureType::Texture2D}, } ); @@ -712,7 +712,7 @@ const ShaderReplacementRule TEXTURE_PROPAGATE_VALUE( /* uniforms */ {}, /* attributes */ {}, /* textures */ { - {"t_scalar", 2}, + {"t_scalar", ShaderTextureType::Texture2D}, } ); @@ -731,7 +731,7 @@ const ShaderReplacementRule TEXTURE_PROPAGATE_COLOR( /* uniforms */ {}, /* attributes */ {}, /* textures */ { - {"t_color", 2}, + {"t_color", ShaderTextureType::Texture2D}, } ); @@ -774,7 +774,7 @@ const ShaderReplacementRule SHADE_NORMAL_FROM_TEXTURE ( /* uniforms */ {}, /* attributes */ {}, /* textures */ { - {"t_normal", 2}, + {"t_normal", ShaderTextureType::Texture2D}, } ); diff --git a/src/render/shader_builder.cpp b/src/render/shader_builder.cpp index b4fdb4ec..b64211bb 100644 --- a/src/render/shader_builder.cpp +++ b/src/render/shader_builder.cpp @@ -139,9 +139,9 @@ applyShaderReplacements(const std::vector& stages, for (ShaderSpecTexture existingT : replacedTextures) { if (existingT.name == newT.name) { // check for conflics - if (existingT.dim != newT.dim) { + if (existingT.type != newT.type) { throw std::runtime_error("ShaderBuilder: rule texture [" + newT.name + - "] conflicts with existing texture of different dim"); + "] conflicts with existing texture of different type"); } existingFound = true; break; // no need to continue, it already exists diff --git a/src/simple_triangle_mesh_color_quantity.cpp b/src/simple_triangle_mesh_color_quantity.cpp index 9395f5ae..28ed0e38 100644 --- a/src/simple_triangle_mesh_color_quantity.cpp +++ b/src/simple_triangle_mesh_color_quantity.cpp @@ -111,10 +111,7 @@ void SimpleTriangleMeshVertexColorQuantity::createProgram() { SimpleTriangleMeshFaceColorQuantity::SimpleTriangleMeshFaceColorQuantity(std::string name, const std::vector& colors_, SimpleTriangleMesh& mesh_) - : SimpleTriangleMeshColorQuantity(name, colors_, "face", mesh_) { - colors.setAsType(DeviceBufferType::Texture1d); - colors.setTextureSize(parent.nFaces()); -} + : SimpleTriangleMeshColorQuantity(name, colors_, "face", mesh_) {} void SimpleTriangleMeshFaceColorQuantity::buildFaceInfoGUI(size_t fInd) { ImGui::TextUnformatted(name.c_str()); @@ -140,7 +137,7 @@ void SimpleTriangleMeshFaceColorQuantity::createProgram() { // clang-format on parent.setSimpleTriangleMeshProgramGeometryAttributes(*program); - program->setTextureFromBuffer("t_faceColors", colors); + program->setTextureFromAttributeBuffer("t_faceColors", colors); render::engine->setMaterial(*program, parent.getMaterial()); } diff --git a/src/simple_triangle_mesh_scalar_quantity.cpp b/src/simple_triangle_mesh_scalar_quantity.cpp index 491d22f0..f0044db3 100644 --- a/src/simple_triangle_mesh_scalar_quantity.cpp +++ b/src/simple_triangle_mesh_scalar_quantity.cpp @@ -106,10 +106,7 @@ SimpleTriangleMeshFaceScalarQuantity::SimpleTriangleMeshFaceScalarQuantity(std:: const std::vector& values_, SimpleTriangleMesh& mesh_, DataType dataType_) - : SimpleTriangleMeshScalarQuantity(name, values_, "face", mesh_, dataType_) { - values.setAsType(DeviceBufferType::Texture1d); - values.setTextureSize(parent.nFaces()); -} + : SimpleTriangleMeshScalarQuantity(name, values_, "face", mesh_, dataType_) {} void SimpleTriangleMeshFaceScalarQuantity::buildFaceInfoGUI(size_t fInd) { ImGui::TextUnformatted(name.c_str()); @@ -132,7 +129,7 @@ void SimpleTriangleMeshFaceScalarQuantity::createProgram() { // clang-format on parent.setSimpleTriangleMeshProgramGeometryAttributes(*program); - program->setTextureFromBuffer("t_faceValues", values); + program->setTextureFromAttributeBuffer("t_faceValues", values); program->setTextureFromColormap("t_colormap", cMap.get()); render::engine->setMaterial(*program, parent.getMaterial()); } diff --git a/test/src/surface_mesh_test.cpp b/test/src/surface_mesh_test.cpp index f5cfbb31..c78df9eb 100644 --- a/test/src/surface_mesh_test.cpp +++ b/test/src/surface_mesh_test.cpp @@ -789,3 +789,83 @@ TEST_F(PolyscopeTest, SimpleTriangleMeshQuantityUpdateAfterResize) { polyscope::removeAllStructures(); } + +TEST_F(PolyscopeTest, SimpleTriangleMeshLargeFaceQuantities) { + + // This primarily serves to test the GL Texture Buffers and using attributes to set texture buffers within the ManagedBuffer and Engine classes. + // Currently, SimpleTriangleMesh face quantities are the only users of this, and there's a fair bit of special-purpose code just to support that. + + // Exceeds typical 16k/32k GL_MAX_TEXTURE_SIZE for 1D textures; verifies TBO-backed face quantities + // render the expected data at high gl_PrimitiveID across initial upload, updateData(), and growth via updateMesh(). + std::vector verts = {{-2.f, -2.f, 0.f}, {2.f, -2.f, 0.f}, {0.f, 2.f, 0.f}}; + // Only the final face is non-degenerate, so center-pixel rendering directly tests gl_PrimitiveID == nFaces - 1. + std::vector faces(40000, glm::uvec3{0, 0, 0}); + faces.back() = glm::uvec3{0, 1, 2}; + auto psMesh = polyscope::registerSimpleTriangleMesh("large_stm", verts, faces); + psMesh->setMaterial("flat"); + polyscope::view::lookAt(glm::vec3{0.f, 0.f, 2.f}, glm::vec3{0.f, 0.f, 0.f}); + + auto sampleCenterPixel = []() -> std::array { + std::vector buf = polyscope::screenshotToBuffer(false); + size_t cx = static_cast(polyscope::view::bufferWidth / 2); + size_t cy = static_cast(polyscope::view::bufferHeight / 2); + size_t idx = 4 * (cy * static_cast(polyscope::view::bufferWidth) + cx); + return {buf[idx], buf[idx + 1], buf[idx + 2]}; + }; + + std::vector scalarInit(faces.size(), 0.f); + scalarInit.back() = 0.f; + std::vector colorInit(faces.size(), glm::vec3{0.f, 0.f, 1.f}); + colorInit.back() = glm::vec3{1.f, 0.f, 0.f}; + + auto qFS = psMesh->addFaceScalarQuantity("fScalar", scalarInit); + qFS->setMapRange({0., 1.}); + auto qFC = psMesh->addFaceColorQuantity("fColor", colorInit); + + qFS->setEnabled(true); + polyscope::show(3); + auto scalarLowPx = sampleCenterPixel(); + + qFC->setEnabled(true); + polyscope::show(3); + auto colorInitPx = sampleCenterPixel(); + if (testBackend != "openGL_mock") { + EXPECT_GT(colorInitPx[0], 200); + EXPECT_LT(colorInitPx[1], 30); + EXPECT_LT(colorInitPx[2], 30); + } + + // Grow beyond initial capacity so VBO reallocates in-place and TBO re-attaches + std::vector biggerFaces(50000, glm::uvec3{0, 0, 0}); + biggerFaces.back() = glm::uvec3{0, 1, 2}; + psMesh->updateMesh(verts, biggerFaces); + + std::vector scalarGrown(biggerFaces.size(), 0.f); + scalarGrown.back() = 1.f; + std::vector colorGrown(biggerFaces.size(), glm::vec3{0.f, 0.f, 1.f}); + colorGrown.back() = glm::vec3{0.f, 1.f, 0.f}; + qFS->updateData(scalarGrown); + qFC->updateData(colorGrown); + + qFS->setEnabled(true); + polyscope::show(3); + auto scalarHighPx = sampleCenterPixel(); + if (testBackend != "openGL_mock") { + int diff = std::abs(int(scalarHighPx[0]) - int(scalarLowPx[0])) + + std::abs(int(scalarHighPx[1]) - int(scalarLowPx[1])) + + std::abs(int(scalarHighPx[2]) - int(scalarLowPx[2])); + EXPECT_GT(diff, 150); + } + + qFC->setEnabled(true); + polyscope::show(3); + auto colorGrownPx = sampleCenterPixel(); + if (testBackend != "openGL_mock") { + EXPECT_LT(colorGrownPx[0], 30); + EXPECT_GT(colorGrownPx[1], 200); + EXPECT_LT(colorGrownPx[2], 30); + } + + polyscope::removeAllStructures(); + polyscope::view::resetCameraToHomeView(); +} From 60bb35ddfb449e5e739cffe897acfc781adbe9d5 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Tue, 24 Nov 2026 15:57:34 -0800 Subject: [PATCH 27/31] add edge wireframe support to SimpleTriangleMesh --- .../opengl/shaders/surface_mesh_shaders.h | 1 + include/polyscope/simple_triangle_mesh.h | 11 ++++ src/render/mock_opengl/mock_gl_engine.cpp | 1 + src/render/opengl/gl_engine.cpp | 1 + .../opengl/shaders/surface_mesh_shaders.cpp | 57 +++++++++++++++++ src/simple_triangle_mesh.cpp | 61 +++++++++++++++++++ test/src/surface_mesh_test.cpp | 34 +++++++++++ 7 files changed, 166 insertions(+) diff --git a/include/polyscope/render/opengl/shaders/surface_mesh_shaders.h b/include/polyscope/render/opengl/shaders/surface_mesh_shaders.h index 9185b6c1..2a565203 100644 --- a/include/polyscope/render/opengl/shaders/surface_mesh_shaders.h +++ b/include/polyscope/render/opengl/shaders/surface_mesh_shaders.h @@ -39,6 +39,7 @@ extern const ShaderReplacementRule MESH_PROPAGATE_TYPE_AND_BASECOLOR2_SHADE; extern const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_VALUE; extern const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_COLOR; extern const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_PICK; +extern const ShaderReplacementRule SIMPLE_MESH_WIREFRAME_FROM_BUFFERS; } // namespace backend_openGL3 diff --git a/include/polyscope/simple_triangle_mesh.h b/include/polyscope/simple_triangle_mesh.h index 3eb1e15a..17350d45 100644 --- a/include/polyscope/simple_triangle_mesh.h +++ b/include/polyscope/simple_triangle_mesh.h @@ -109,10 +109,19 @@ class SimpleTriangleMesh : public Structure { SimpleTriangleMesh* setSurfaceColor(glm::vec3 newVal); glm::vec3 getSurfaceColor(); + // set the color of the edges + SimpleTriangleMesh* setEdgeColor(glm::vec3 newVal); + glm::vec3 getEdgeColor(); + // Material SimpleTriangleMesh* setMaterial(std::string name); std::string getMaterial(); + // Width of the edges. Scaled such that 1 is a reasonable weight for visible edges, but values >1 can be used for + // bigger edges. Use 0. to disable. + SimpleTriangleMesh* setEdgeWidth(double newVal); + double getEdgeWidth(); + // Backface color SimpleTriangleMesh* setBackFaceColor(glm::vec3 val); glm::vec3 getBackFaceColor(); @@ -136,7 +145,9 @@ class SimpleTriangleMesh : public Structure { private: // === Visualization parameters PersistentValue surfaceColor; + PersistentValue edgeColor; PersistentValue material; + PersistentValue edgeWidth; PersistentValue backFacePolicy; PersistentValue backFaceColor; PersistentValue selectionMode; diff --git a/src/render/mock_opengl/mock_gl_engine.cpp b/src/render/mock_opengl/mock_gl_engine.cpp index 5d4da881..2be064b8 100644 --- a/src/render/mock_opengl/mock_gl_engine.cpp +++ b/src/render/mock_opengl/mock_gl_engine.cpp @@ -2299,6 +2299,7 @@ void MockGLEngine::populateDefaultShadersAndRules() { registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_VALUE", SIMPLE_MESH_PROPAGATE_FACE_VALUE); registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_COLOR", SIMPLE_MESH_PROPAGATE_FACE_COLOR); registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_PICK", SIMPLE_MESH_PROPAGATE_FACE_PICK); + registerShaderRule("SIMPLE_MESH_WIREFRAME_FROM_BUFFERS", SIMPLE_MESH_WIREFRAME_FROM_BUFFERS); // volume gridcube things registerShaderRule("GRIDCUBE_PROPAGATE_NODE_VALUE", GRIDCUBE_PROPAGATE_NODE_VALUE); diff --git a/src/render/opengl/gl_engine.cpp b/src/render/opengl/gl_engine.cpp index c4e7f4d9..7438766b 100644 --- a/src/render/opengl/gl_engine.cpp +++ b/src/render/opengl/gl_engine.cpp @@ -2891,6 +2891,7 @@ void GLEngine::populateDefaultShadersAndRules() { registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_VALUE", SIMPLE_MESH_PROPAGATE_FACE_VALUE); registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_COLOR", SIMPLE_MESH_PROPAGATE_FACE_COLOR); registerShaderRule("SIMPLE_MESH_PROPAGATE_FACE_PICK", SIMPLE_MESH_PROPAGATE_FACE_PICK); + registerShaderRule("SIMPLE_MESH_WIREFRAME_FROM_BUFFERS", SIMPLE_MESH_WIREFRAME_FROM_BUFFERS); // volume gridcube things registerShaderRule("GRIDCUBE_PROPAGATE_NODE_VALUE", GRIDCUBE_PROPAGATE_NODE_VALUE); diff --git a/src/render/opengl/shaders/surface_mesh_shaders.cpp b/src/render/opengl/shaders/surface_mesh_shaders.cpp index d278a8f3..c687b745 100644 --- a/src/render/opengl/shaders/surface_mesh_shaders.cpp +++ b/src/render/opengl/shaders/surface_mesh_shaders.cpp @@ -182,6 +182,9 @@ R"( ${ GENERATE_SHADE_VALUE }$ ${ GENERATE_SHADE_COLOR }$ + // Handle the wireframe + ${ APPLY_WIREFRAME }$ + // Lighting ${ PERTURB_SHADE_NORMAL }$ ${ GENERATE_LIT_COLOR }$ @@ -793,6 +796,60 @@ const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_PICK( /* textures */ {} ); +// Computes per-fragment barycentric coordinates for an indexed triangle mesh by looking up the current +// triangle's 3 vertices from TBOs (t_faces / t_vertices) using gl_PrimitiveID and the interpolated +// object-space position. Feeds wireframe_UVW / wireframe_mask into MESH_WIREFRAME with no extra buffers. +const ShaderReplacementRule SIMPLE_MESH_WIREFRAME_FROM_BUFFERS( + /* rule name */ "SIMPLE_MESH_WIREFRAME_FROM_BUFFERS", + { /* replacement sources */ + {"VERT_DECLARATIONS", R"( + out vec3 a_objectPosToFrag; + )"}, + {"VERT_ASSIGNMENTS", R"( + a_objectPosToFrag = a_vertexPositions; + )"}, + {"FRAG_DECLARATIONS", R"( + in vec3 a_objectPosToFrag; + uniform samplerBuffer t_vertices; + uniform usamplerBuffer t_faces; + )"}, + {"APPLY_WIREFRAME", R"( + int wfFaceBase = 3 * gl_PrimitiveID; + int wfV0 = int(texelFetch(t_faces, wfFaceBase).r); + int wfV1 = int(texelFetch(t_faces, wfFaceBase + 1).r); + int wfV2 = int(texelFetch(t_faces, wfFaceBase + 2).r); + vec3 wfP0 = vec3(texelFetch(t_vertices, 3 * wfV0).r, + texelFetch(t_vertices, 3 * wfV0 + 1).r, + texelFetch(t_vertices, 3 * wfV0 + 2).r); + vec3 wfP1 = vec3(texelFetch(t_vertices, 3 * wfV1).r, + texelFetch(t_vertices, 3 * wfV1 + 1).r, + texelFetch(t_vertices, 3 * wfV1 + 2).r); + vec3 wfP2 = vec3(texelFetch(t_vertices, 3 * wfV2).r, + texelFetch(t_vertices, 3 * wfV2 + 1).r, + texelFetch(t_vertices, 3 * wfV2 + 2).r); + vec3 wfE0 = wfP1 - wfP0; + vec3 wfE1 = wfP2 - wfP0; + vec3 wfD = a_objectPosToFrag - wfP0; + float wfScale = max(max(length(wfE0), length(wfE1)), 1e-30); + vec3 wfE0s = wfE0 / wfScale; + vec3 wfE1s = wfE1 / wfScale; + vec3 wfDs = wfD / wfScale; + vec3 wfN = cross(wfE0s, wfE1s); + vec3 wfNOverArea2 = wfN / max(dot(wfN, wfN), 1e-30); + float wfV = dot(cross(wfDs, wfE1s), wfNOverArea2); + float wfW = dot(cross(wfE0s, wfDs), wfNOverArea2); + vec3 wireframe_UVW = vec3(1.0 - wfV - wfW, wfV, wfW); + vec3 wireframe_mask = vec3(1.0, 1.0, 1.0); + )"}, + }, + /* uniforms */ {}, + /* attributes */ {}, + /* textures */ { + {"t_vertices", ShaderTextureType::TextureBuffer}, + {"t_faces", ShaderTextureType::TextureBuffer}, + } +); + // clang-format on } // namespace backend_openGL3 diff --git a/src/simple_triangle_mesh.cpp b/src/simple_triangle_mesh.cpp index ff374f08..b8523fc9 100644 --- a/src/simple_triangle_mesh.cpp +++ b/src/simple_triangle_mesh.cpp @@ -27,7 +27,9 @@ SimpleTriangleMesh::SimpleTriangleMesh(std::string name, std::vector vertices(this, uniquePrefix() + "vertices", std::move(vertices_)), faces(this, uniquePrefix() + "faces", std::move(faces_)), surfaceColor(uniquePrefix() + "surfaceColor", getNextUniqueColor()), + edgeColor(uniquePrefix() + "edgeColor", glm::vec3{0., 0., 0.}), material(uniquePrefix() + "material", "clay"), + edgeWidth(uniquePrefix() + "edgeWidth", 0.), backFacePolicy(uniquePrefix() + "backFacePolicy", BackFacePolicy::Different), backFaceColor(uniquePrefix() + "backFaceColor", glm::vec3(1.f - surfaceColor.get().r, 1.f - surfaceColor.get().g, 1.f - surfaceColor.get().b)), selectionMode(uniquePrefix() + "selectionMode", MeshSelectionMode::Auto) @@ -49,6 +51,38 @@ void SimpleTriangleMesh::buildCustomUI() { setSurfaceColor(surfaceColor.get()); } + { // Edge options + ImGui::SameLine(); + ImGui::PushItemWidth(100 * options::uiScale); + if (edgeWidth.get() == 0.) { + bool showEdges = false; + if (ImGui::Checkbox("Edges", &showEdges)) { + setEdgeWidth(1.); + } + } else { + bool showEdges = true; + if (ImGui::Checkbox("Edges", &showEdges)) { + setEdgeWidth(0.); + } + + // Edge color + ImGui::PushItemWidth(100 * options::uiScale); + if (ImGui::ColorEdit3("Edge Color", &edgeColor.get()[0], ImGuiColorEditFlags_NoInputs)) + setEdgeColor(edgeColor.get()); + ImGui::PopItemWidth(); + + // Edge width + ImGui::SameLine(); + ImGui::PushItemWidth(75 * options::uiScale); + if (ImGui::SliderFloat("Width", &edgeWidth.get(), 0.001, 2.)) { + edgeWidth.manuallyChanged(); + requestRedraw(); + } + ImGui::PopItemWidth(); + } + ImGui::PopItemWidth(); + } + { // Backface color (only visible if policy is selected) if (backFacePolicy.get() == BackFacePolicy::Custom) { if (ImGui::ColorEdit3("Backface Color", &backFaceColor.get()[0], ImGuiColorEditFlags_NoInputs)) @@ -184,6 +218,11 @@ void SimpleTriangleMesh::setSimpleTriangleMeshUniforms(render::ShaderProgram& p, if (withSurfaceShade) { + if (getEdgeWidth() > 0) { + p.setUniform("u_edgeWidth", getEdgeWidth() * render::engine->getCurrentPixelScaling()); + p.setUniform("u_edgeColor", getEdgeColor()); + } + if (backFacePolicy.get() == BackFacePolicy::Custom) { p.setUniform("u_backfaceColor", getBackFaceColor()); } @@ -250,6 +289,10 @@ std::vector SimpleTriangleMesh::addSimpleTriangleMeshRules(std::vec if (withSurfaceShade) { // rules that only get used when we're shading the surface of the mesh + if (getEdgeWidth() > 0) { + initRules.push_back("SIMPLE_MESH_WIREFRAME_FROM_BUFFERS"); + initRules.push_back("MESH_WIREFRAME"); + } if (backFacePolicy.get() == BackFacePolicy::Different) { initRules.push_back("MESH_BACKFACE_DARKEN"); @@ -277,6 +320,8 @@ std::vector SimpleTriangleMesh::addSimpleTriangleMeshRules(std::vec void SimpleTriangleMesh::setSimpleTriangleMeshProgramGeometryAttributes(render::ShaderProgram& p) { p.setAttribute("a_vertexPositions", vertices); p.setIndex(faces); + if (p.hasTexture("t_vertices")) p.setTextureFromAttributeBuffer("t_vertices", vertices); + if (p.hasTexture("t_faces")) p.setTextureFromAttributeBuffer("t_faces", faces); } @@ -448,6 +493,22 @@ SimpleTriangleMesh* SimpleTriangleMesh::setSurfaceColor(glm::vec3 val) { } glm::vec3 SimpleTriangleMesh::getSurfaceColor() { return surfaceColor.get(); } +SimpleTriangleMesh* SimpleTriangleMesh::setEdgeColor(glm::vec3 val) { + edgeColor = val; + requestRedraw(); + return this; +} +glm::vec3 SimpleTriangleMesh::getEdgeColor() { return edgeColor.get(); } + +SimpleTriangleMesh* SimpleTriangleMesh::setEdgeWidth(double newVal) { + bool toggled = (edgeWidth.get() == 0.) != (newVal == 0.); + edgeWidth = newVal; + if (toggled) refresh(); + requestRedraw(); + return this; +} +double SimpleTriangleMesh::getEdgeWidth() { return edgeWidth.get(); } + SimpleTriangleMesh* SimpleTriangleMesh::setMaterial(std::string m) { material = m; refresh(); diff --git a/test/src/surface_mesh_test.cpp b/test/src/surface_mesh_test.cpp index c78df9eb..7a2fd2ef 100644 --- a/test/src/surface_mesh_test.cpp +++ b/test/src/surface_mesh_test.cpp @@ -869,3 +869,37 @@ TEST_F(PolyscopeTest, SimpleTriangleMeshLargeFaceQuantities) { polyscope::removeAllStructures(); polyscope::view::resetCameraToHomeView(); } + +TEST_F(PolyscopeTest, SimpleTriangleMeshWireframe) { + auto psMesh = registerSimpleTriangleMesh(); + + psMesh->setEdgeWidth(1.5); + EXPECT_DOUBLE_EQ(psMesh->getEdgeWidth(), 1.5); + psMesh->setEdgeColor(glm::vec3(1.f, 0.f, 0.f)); + EXPECT_EQ(psMesh->getEdgeColor(), glm::vec3(1.f, 0.f, 0.f)); + polyscope::show(3); + + // With vertex and face quantities enabled + auto qVS = psMesh->addVertexScalarQuantity("vScalar", std::vector(psMesh->nVertices(), 1.f)); + qVS->setEnabled(true); + polyscope::show(3); + + auto qFS = psMesh->addFaceScalarQuantity("fScalar", std::vector(psMesh->nFaces(), 1.f)); + qFS->setEnabled(true); + polyscope::show(3); + + auto qFC = psMesh->addFaceColorQuantity("fColor", std::vector(psMesh->nFaces(), glm::vec3{0.f, 1.f, 0.f})); + qFC->setEnabled(true); + polyscope::show(3); + + // After resizing the mesh while wireframe is enabled + psMesh->updateMesh(std::vector(20), std::vector(20, glm::uvec3{0, 1, 2})); + qFC->updateData(std::vector(20, glm::vec3{0.f, 1.f, 0.f})); + polyscope::show(3); + + psMesh->setEdgeWidth(0.); + EXPECT_DOUBLE_EQ(psMesh->getEdgeWidth(), 0.); + polyscope::show(3); + + polyscope::removeAllStructures(); +} From 10fa03414c957da2a49822ff24054d0dabf128eb Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Tue, 24 Nov 2026 15:58:36 -0800 Subject: [PATCH 28/31] fix premultiplied alpha --- src/render/opengl/shaders/surface_mesh_shaders.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/render/opengl/shaders/surface_mesh_shaders.cpp b/src/render/opengl/shaders/surface_mesh_shaders.cpp index c687b745..93582751 100644 --- a/src/render/opengl/shaders/surface_mesh_shaders.cpp +++ b/src/render/opengl/shaders/surface_mesh_shaders.cpp @@ -196,6 +196,7 @@ R"( ${ PERTURB_LIT_COLOR }$ // Write output + litColor *= alphaOut; // premultiplied alpha outputF = vec4(litColor, alphaOut); } )" From 3dde74b23f108c2071c03f3f5592068b909ad69b Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Tue, 24 Nov 2026 17:06:30 -0800 Subject: [PATCH 29/31] fix packages for latest github actions --- .github/workflows/linux.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 26c3fe1f..1da2ac63 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -28,7 +28,7 @@ jobs: submodules: true - name: install packages - run: sudo apt-get update && sudo apt-get install -y xorg-dev libglu1-mesa-dev xpra xserver-xorg-video-dummy freeglut3-dev + run: sudo apt-get update && sudo apt-get install -y xorg-dev libglu1-mesa-dev libegl-dev libgl1-mesa-dri - name: configure run: cd test && mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DPOLYSCOPE_BACKEND_OPENGL3_GLFW=ON -DPOLYSCOPE_BACKEND_OPENGL_MOCK=ON .. @@ -65,7 +65,7 @@ jobs: submodules: true - name: install packages - run: sudo apt-get update && sudo apt-get install -y xorg-dev libglu1-mesa-dev xpra xserver-xorg-video-dummy freeglut3-dev + run: sudo apt-get update && sudo apt-get install -y xorg-dev libglu1-mesa-dev libegl-dev libgl1-mesa-dri - name: configure run: cd test && mkdir build && cd build && cmake -DBUILD_SHARED_LIBS=TRUE -DCMAKE_BUILD_TYPE=Debug -DPOLYSCOPE_BACKEND_OPENGL3_GLFW=ON -DPOLYSCOPE_BACKEND_OPENGL_MOCK=ON -DPOLYSCOPE_BACKEND_OPENGL3_EGL=ON .. From 0992207cf9214f5c34491800e8b437335d62c08b Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Tue, 24 Nov 2026 17:10:24 -0800 Subject: [PATCH 30/31] avoid reference inside constructor lambda for MSVC --- test/src/interop_and_serialization_test.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/src/interop_and_serialization_test.cpp b/test/src/interop_and_serialization_test.cpp index e8bc65ac..926aaee7 100644 --- a/test/src/interop_and_serialization_test.cpp +++ b/test/src/interop_and_serialization_test.cpp @@ -128,11 +128,13 @@ TEST_F(PolyscopeTest, ManagedBufferDeviceSync) { TEST_F(PolyscopeTest, ManagedBufferLazyCompute) { int computeCount = 0; - polyscope::render::ManagedBuffer mb(nullptr, "lazy_test", [&]() { + std::unique_ptr> mbPtr; + mbPtr = std::make_unique>(nullptr, "lazy_test", [&]() { computeCount++; - mb.resize(3); - mb.setDataHost({static_cast(computeCount), 2.f, 3.f}); + mbPtr->resize(3); + mbPtr->setDataHost({static_cast(computeCount), 2.f, 3.f}); }); + auto& mb = *mbPtr; // Should not compute before first access, even after pre-reserving capacity mb.setCapacity(8); From 4eb69e0e86a545a52b33c01df9ed0f40710a2a12 Mon Sep 17 00:00:00 2001 From: Nicholas Sharp Date: Tue, 24 Nov 2026 17:22:09 -0800 Subject: [PATCH 31/31] limit parallel builds in CI --- .github/workflows/linux.yml | 4 ++-- .github/workflows/macos.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 1da2ac63..ee66648f 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -34,7 +34,7 @@ jobs: run: cd test && mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DPOLYSCOPE_BACKEND_OPENGL3_GLFW=ON -DPOLYSCOPE_BACKEND_OPENGL_MOCK=ON .. - name: build - run: cd test/build && cmake --build "." --parallel + run: cd test/build && cmake --build "." --parallel 4 - name: run test mock backend run: cd test/build && ./bin/polyscope-test --gtest_catch_exceptions=0 backend=openGL_mock @@ -71,7 +71,7 @@ jobs: run: cd test && mkdir build && cd build && cmake -DBUILD_SHARED_LIBS=TRUE -DCMAKE_BUILD_TYPE=Debug -DPOLYSCOPE_BACKEND_OPENGL3_GLFW=ON -DPOLYSCOPE_BACKEND_OPENGL_MOCK=ON -DPOLYSCOPE_BACKEND_OPENGL3_EGL=ON .. - name: build - run: cd test/build && cmake --build "." --parallel + run: cd test/build && cmake --build "." --parallel 4 - name: run test mock backend run: cd test/build && ./bin/polyscope-test --gtest_catch_exceptions=0 backend=openGL_mock diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 085c0fc2..a123a6f6 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -30,7 +30,7 @@ jobs: run: cd test && mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DPOLYSCOPE_BACKEND_OPENGL3_GLFW=ON -DPOLYSCOPE_BACKEND_OPENGL_MOCK=ON .. - name: build - run: cd test/build && cmake --build "." --parallel + run: cd test/build && cmake --build "." --parallel 4 - name: run test run: cd test/build && ./bin/polyscope-test --gtest_catch_exceptions=0 backend=openGL_mock @@ -58,7 +58,7 @@ jobs: run: cd test && mkdir build && cd build && cmake -DBUILD_SHARED_LIBS=TRUE -DCMAKE_BUILD_TYPE=Debug -DPOLYSCOPE_BACKEND_OPENGL3_GLFW=ON -DPOLYSCOPE_BACKEND_OPENGL_MOCK=ON .. - name: build - run: cd test/build && cmake --build "." --parallel + run: cd test/build && cmake --build "." --parallel 4 - name: run test run: cd test/build && ./bin/polyscope-test --gtest_catch_exceptions=0 backend=openGL_mock