Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid updating textures more often than needed #2365

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/mvTextureItems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ PyObject* mvDynamicTexture::getPyValue()
void mvDynamicTexture::setPyValue(PyObject* value)
{
*_value = ToFloatVect(value);
_updateNeeded = true;
}

void mvDynamicTexture::setDataSource(mvUUID dataSource)
Expand All @@ -135,6 +136,7 @@ void mvDynamicTexture::setDataSource(mvUUID dataSource)
return;
}
_value = *static_cast<std::shared_ptr<std::vector<float>>*>(item->getValue());
_updateNeeded = true;
}

void mvDynamicTexture::draw(ImDrawList* drawlist, float x, float y)
Expand All @@ -151,8 +153,11 @@ void mvDynamicTexture::draw(ImDrawList* drawlist, float x, float y)
return;
}

UpdateTexture(_texture, _permWidth, _permHeight, *_value);
if (!_updateNeeded)
return;

UpdateTexture(_texture, _permWidth, _permHeight, *_value);
_updateNeeded = false;
}

void mvDynamicTexture::handleSpecificRequiredArgs(PyObject* dict)
Expand All @@ -165,6 +170,7 @@ void mvDynamicTexture::handleSpecificRequiredArgs(PyObject* dict)
_permHeight = ToInt(PyTuple_GetItem(dict, 1));
config.height = _permHeight;
*_value = ToFloatVect(PyTuple_GetItem(dict, 2));
_updateNeeded = true;
}

void mvDynamicTexture::handleSpecificKeywordArgs(PyObject* dict)
Expand Down Expand Up @@ -203,6 +209,7 @@ void mvRawTexture::setPyValue(PyObject* value)
{
mvThrowPythonError(mvErrorCode::mvTextureNotFound, GetEntityCommand(type), "Texture data not valid", this);
}
_updateNeeded = true;
}
PyBuffer_Release(&buffer_info);
if (_buffer)
Expand Down Expand Up @@ -238,9 +245,12 @@ void mvRawTexture::draw(ImDrawList* drawlist, float x, float y)
return;
}

if (!_updateNeeded)
return;

if (_componentType == ComponentType::MV_FLOAT_COMPONENT)
UpdateRawTexture(_texture, _permWidth, _permHeight, (float*)_value, _components);

_updateNeeded = false;
}

void mvRawTexture::handleSpecificRequiredArgs(PyObject* dict)
Expand Down
2 changes: 2 additions & 0 deletions src/mvTextureItems.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class mvRawTexture : public mvAppItem
void* _value = nullptr;
void* _texture = nullptr;
bool _dirty = true;
bool _updateNeeded = true;
ComponentType _componentType = ComponentType::MV_FLOAT_COMPONENT;
int _components = 4;
int _permWidth = 0;
Expand Down Expand Up @@ -106,6 +107,7 @@ class mvDynamicTexture : public mvAppItem
std::shared_ptr<std::vector<float>> _value = std::make_shared<std::vector<float>>(std::vector<float>{0.0f});
void* _texture = nullptr;
bool _dirty = true;
bool _updateNeeded = true;
int _permWidth = 0;
int _permHeight = 0;

Expand Down
52 changes: 26 additions & 26 deletions src/mvUtilities_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,6 @@ UpdateTexture(void* texture, unsigned width, unsigned height, std::vector<float>
{
auto textureId = (GLuint)(size_t)texture;

// start to copy from PBO to texture object ///////

// bind the texture and PBO
glBindTexture(GL_TEXTURE_2D, textureId);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, PBO_ids[textureId]);

// copy pixels from PBO to texture object
// Use offset instead of ponter.
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_FLOAT, 0);

///////////////////////////////////////////////////

// start to modify pixel values ///////////////////

// bind PBO to update pixel values
Expand All @@ -256,16 +244,6 @@ UpdateTexture(void* texture, unsigned width, unsigned height, std::vector<float>

///////////////////////////////////////////////////

// it is good idea to release PBOs with ID 0 after use.
// Once bound with 0, all pixel operations behave normal ways.
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
}

void
UpdateRawTexture(void* texture, unsigned width, unsigned height, float* data, int components)
{
auto textureId = (GLuint)(size_t)texture;

// start to copy from PBO to texture object ///////

// bind the texture and PBO
Expand All @@ -274,13 +252,20 @@ UpdateRawTexture(void* texture, unsigned width, unsigned height, float* data, in

// copy pixels from PBO to texture object
// Use offset instead of ponter.
if(components == 4)
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_FLOAT, 0);
else
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_FLOAT, 0);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_FLOAT, 0);

///////////////////////////////////////////////////

// it is good idea to release PBOs with ID 0 after use.
// Once bound with 0, all pixel operations behave normal ways.
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
}

void
UpdateRawTexture(void* texture, unsigned width, unsigned height, float* data, int components)
{
auto textureId = (GLuint)(size_t)texture;

// start to modify pixel values ///////////////////

// bind PBO to update pixel values
Expand All @@ -306,6 +291,21 @@ UpdateRawTexture(void* texture, unsigned width, unsigned height, float* data, in

///////////////////////////////////////////////////

// start to copy from PBO to texture object ///////

// bind the texture and PBO
glBindTexture(GL_TEXTURE_2D, textureId);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, PBO_ids[textureId]);

// copy pixels from PBO to texture object
// Use offset instead of ponter.
if(components == 4)
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_FLOAT, 0);
else
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_FLOAT, 0);

///////////////////////////////////////////////////

// it is good idea to release PBOs with ID 0 after use.
// Once bound with 0, all pixel operations behave normal ways.
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
Expand Down