PR feedback

This commit is contained in:
Brad Davis 2016-05-12 21:37:13 -07:00
parent eb84459f03
commit 9509e32928
8 changed files with 129 additions and 175 deletions

View file

@ -267,7 +267,7 @@ void RenderableParticleEffectEntityItem::updateRenderItem() {
if (numBytes == 0) {
return;
}
memcpy(particleBuffer->editData(), particlePrimitives->data(), numBytes);
particleBuffer->setData(numBytes, (const gpu::Byte*)particlePrimitives->data());
// Update transform and bounds
payload.setModelTransform(transform);

View file

@ -69,10 +69,10 @@ public:
const GLuint _size;
const Stamp _stamp;
GLBuffer(const Buffer& buffer);
GLBuffer(const Buffer& buffer, GLBuffer* original = nullptr);
~GLBuffer();
void transfer(bool forceAll = false);
void transfer();
private:
bool getNextTransferBlock(GLintptr& outOffset, GLsizeiptr& outSize, size_t& currentPage) const;

View file

@ -12,43 +12,42 @@
using namespace gpu;
static std::once_flag check_dsa;
static bool DSA_SUPPORTED { false };
GLuint allocateSingleBuffer() {
std::call_once(check_dsa, [&] {
DSA_SUPPORTED = (GLEW_VERSION_4_5 || GLEW_ARB_direct_state_access);
});
GLuint result;
if (DSA_SUPPORTED) {
glCreateBuffers(1, &result);
} else {
glGenBuffers(1, &result);
}
glGenBuffers(1, &result);
(void)CHECK_GL_ERROR();
return result;
}
GLBackend::GLBuffer::GLBuffer(const Buffer& buffer) :
_buffer(allocateSingleBuffer()),
_size(buffer._sysmem.getSize()),
_stamp(buffer._sysmem.getStamp()),
GLBackend::GLBuffer::GLBuffer(const Buffer& buffer, GLBuffer* original) :
_buffer(allocateSingleBuffer()),
_size((GLuint)buffer._sysmem.getSize()),
_stamp(buffer._sysmem.getStamp()),
_gpuBuffer(buffer) {
(void)CHECK_GL_ERROR();
Backend::setGPUObject(buffer, this);
if (DSA_SUPPORTED) {
glNamedBufferStorage(_buffer, _size, nullptr, GL_DYNAMIC_STORAGE_BIT);
glBindBuffer(GL_ARRAY_BUFFER, _buffer);
if (GLEW_VERSION_4_4 || GLEW_ARB_buffer_storage) {
glBufferStorage(GL_ARRAY_BUFFER, _size, nullptr, GL_DYNAMIC_STORAGE_BIT);
(void)CHECK_GL_ERROR();
} else {
glBindBuffer(GL_ARRAY_BUFFER, _buffer);
if (GLEW_VERSION_4_4 || GLEW_ARB_buffer_storage) {
glBufferStorage(GL_ARRAY_BUFFER, _size, nullptr, GL_DYNAMIC_STORAGE_BIT);
} else {
glBufferData(GL_ARRAY_BUFFER, buffer.getSysmem().getSize(), buffer.getSysmem().readData(), GL_DYNAMIC_DRAW);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBufferData(GL_ARRAY_BUFFER, _size, nullptr, GL_DYNAMIC_DRAW);
(void)CHECK_GL_ERROR();
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
if (original) {
glBindBuffer(GL_COPY_WRITE_BUFFER, _buffer);
glBindBuffer(GL_COPY_READ_BUFFER, original->_buffer);
glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, original->_size);
glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
glBindBuffer(GL_COPY_READ_BUFFER, 0);
(void)CHECK_GL_ERROR();
}
Backend::setGPUObject(buffer, this);
Backend::incrementBufferGPUCount();
}
GLBackend::GLBuffer::~GLBuffer() {
if (_buffer != 0) {
glDeleteBuffers(1, &_buffer);
@ -57,62 +56,20 @@ GLBackend::GLBuffer::~GLBuffer() {
Backend::decrementBufferGPUCount();
}
void GLBackend::GLBuffer::transfer(bool forceAll) {
const auto& pageFlags = _gpuBuffer._pages;
if (!forceAll) {
size_t transitions = 0;
if (pageFlags.size()) {
bool lastDirty = (0 != (pageFlags[0] & Buffer::DIRTY));
for (size_t i = 1; i < pageFlags.size(); ++i) {
bool newDirty = (0 != (pageFlags[0] & Buffer::DIRTY));
if (newDirty != lastDirty) {
++transitions;
lastDirty = newDirty;
}
}
}
// If there are no transitions (implying the whole buffer is dirty)
// or more than 20 transitions, then just transfer the whole buffer
if (transitions == 0 || transitions > 20) {
forceAll = true;
}
}
// Are we transferring the whole buffer?
if (forceAll) {
if (DSA_SUPPORTED) {
glNamedBufferSubData(_buffer, 0, _size, _gpuBuffer.getSysmem().readData());
} else {
// Now let's update the content of the bo with the sysmem version
// TODO: in the future, be smarter about when to actually upload the glBO version based on the data that did change
//if () {
glBindBuffer(GL_ARRAY_BUFFER, _buffer);
glBufferData(GL_ARRAY_BUFFER, _gpuBuffer.getSysmem().getSize(), _gpuBuffer.getSysmem().readData(), GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
} else {
if (!DSA_SUPPORTED) {
glBindBuffer(GL_ARRAY_BUFFER, _buffer);
}
GLintptr offset;
GLsizeiptr size;
size_t currentPage { 0 };
auto data = _gpuBuffer.getSysmem().readData();
while (getNextTransferBlock(offset, size, currentPage)) {
if (DSA_SUPPORTED) {
glNamedBufferSubData(_buffer, offset, size, data + offset);
} else {
glBufferSubData(GL_ARRAY_BUFFER, offset, size, data + offset);
}
}
if (!DSA_SUPPORTED) {
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}
_gpuBuffer._flags &= ~Buffer::DIRTY;
void GLBackend::GLBuffer::transfer() {
glBindBuffer(GL_ARRAY_BUFFER, _buffer);
(void)CHECK_GL_ERROR();
GLintptr offset;
GLsizeiptr size;
size_t currentPage { 0 };
auto data = _gpuBuffer.getSysmem().readData();
while (getNextTransferBlock(offset, size, currentPage)) {
glBufferSubData(GL_ARRAY_BUFFER, offset, size, data + offset);
(void)CHECK_GL_ERROR();
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
(void)CHECK_GL_ERROR();
_gpuBuffer._flags &= ~Buffer::DIRTY;
}
bool GLBackend::GLBuffer::getNextTransferBlock(GLintptr& outOffset, GLsizeiptr& outSize, size_t& currentPage) const {
@ -130,6 +87,7 @@ bool GLBackend::GLBuffer::getNextTransferBlock(GLintptr& outOffset, GLsizeiptr&
// Advance to the next clean page
outOffset = static_cast<GLintptr>(currentPage * _gpuBuffer._pageSize);
while (currentPage < pageCount && (0 != (Buffer::DIRTY & _gpuBuffer._pages[currentPage]))) {
_gpuBuffer._pages[currentPage] &= ~Buffer::DIRTY;
++currentPage;
}
outSize = static_cast<GLsizeiptr>((currentPage * _gpuBuffer._pageSize) - outOffset);
@ -139,15 +97,13 @@ bool GLBackend::GLBuffer::getNextTransferBlock(GLintptr& outOffset, GLsizeiptr&
GLBackend::GLBuffer* GLBackend::syncGPUObject(const Buffer& buffer) {
GLBuffer* object = Backend::getGPUObject<GLBackend::GLBuffer>(buffer);
bool forceTransferAll = false;
// Has the storage size changed?
if (!object || object->_stamp != buffer.getSysmem().getStamp()) {
object = new GLBuffer(buffer);
forceTransferAll = true;
object = new GLBuffer(buffer, object);
}
if (forceTransferAll || (0 != (buffer._flags & Buffer::DIRTY))) {
object->transfer(forceTransferAll);
if (0 != (buffer._flags & Buffer::DIRTY)) {
object->transfer();
}
return object;

View file

@ -270,7 +270,7 @@ Buffer::Size Buffer::resize(Size size) {
if (prevSize < size) {
auto newPages = getRequiredPageCount();
auto newSize = newPages * _pageSize;
editSysmem().resize(size);
editSysmem().resize(newSize);
// All new pages start off as clean, because they haven't been populated by data
_pages.resize(newPages, 0);
Buffer::updateBufferCPUMemoryUsage(prevSize, newSize);
@ -278,7 +278,7 @@ Buffer::Size Buffer::resize(Size size) {
return _end;
}
void Buffer::dirtyPages(Size offset, Size bytes) {
void Buffer::markDirty(Size offset, Size bytes) {
if (!bytes) {
return;
}
@ -316,7 +316,7 @@ Buffer::Size Buffer::setData(Size size, const Byte* data) {
Buffer::Size Buffer::setSubData(Size offset, Size size, const Byte* data) {
auto changedBytes = editSysmem().setSubData(offset, size, data);
if (changedBytes) {
dirtyPages(offset, changedBytes);
markDirty(offset, changedBytes);
}
return changedBytes;
}

View file

@ -136,8 +136,7 @@ public:
// The size in bytes of data stored in the buffer
Size getSize() const;
const Byte* getData() const { return getSysmem().readData(); }
Byte* editData() { return editSysmem().editData(); }
// Resize the buffer
// Keep previous data [0 to min(pSize, mSize)]
Size resize(Size pSize);
@ -159,6 +158,9 @@ public:
template <typename T>
Size setSubData(Size index, const std::vector<T>& t) {
if (t.empty()) {
return 0;
}
Size offset = index * sizeof(T);
Size size = t.size() * sizeof(T);
return setSubData(offset, size, reinterpret_cast<const Byte*>(&t[0]));
@ -176,18 +178,28 @@ public:
template <typename T>
Size append(const std::vector<T>& t) {
if (t.empty()) {
return _end;
}
return append(sizeof(T) * t.size(), reinterpret_cast<const Byte*>(&t[0]));
}
const GPUObjectPointer gpuObject {};
protected:
void markDirty(Size offset, Size bytes);
template <typename T>
void markDirty(Size index, Size count = 1) {
markDirty(sizeof(T) * index, sizeof(T) * count);
}
// Access the sysmem object, limited to ourselves and GPUObject derived classes
const Sysmem& getSysmem() const { return _sysmem; }
Sysmem& editSysmem() { return _sysmem; }
Byte* editData() { return editSysmem().editData(); }
Size getRequiredPageCount() const;
void dirtyPages(Size offset, Size bytes);
Size _end { 0 };
mutable uint8_t _flags;
@ -197,6 +209,7 @@ protected:
// FIXME find a more generic way to do this.
friend class GLBackend;
friend class BufferView;
};
typedef std::shared_ptr<Buffer> BufferPointer;
@ -320,8 +333,14 @@ public:
int _stride;
};
#if 0
// Direct memory access to the buffer contents is incompatible with the paging memory scheme
template <typename T> Iterator<T> begin() { return Iterator<T>(&edit<T>(0), _stride); }
template <typename T> Iterator<T> end() { return Iterator<T>(&edit<T>(getNum<T>()), _stride); }
#else
template <typename T> Iterator<const T> begin() const { return Iterator<const T>(&get<T>(), _stride); }
template <typename T> Iterator<const T> end() const { return Iterator<const T>(&get<T>(getNum<T>()), _stride); }
#endif
template <typename T> Iterator<const T> cbegin() const { return Iterator<const T>(&get<T>(), _stride); }
template <typename T> Iterator<const T> cend() const { return Iterator<const T>(&get<T>(getNum<T>()), _stride); }
@ -358,6 +377,7 @@ public:
qDebug() << "Accessing buffer outside the BufferView range, element size = " << sizeof(T) << " when bufferView size = " << _size;
}
#endif
_buffer->markDirty(_offset, sizeof(T));
T* t = (reinterpret_cast<T*> (_buffer->editData() + _offset));
return *(t);
}
@ -391,6 +411,7 @@ public:
qDebug() << "Accessing buffer outside the BufferView range, index = " << index << " number elements = " << getNum<T>();
}
#endif
_buffer->markDirty(elementOffset, sizeof(T));
return *(reinterpret_cast<T*> (_buffer->editData() + elementOffset));
}
};

View file

@ -118,29 +118,18 @@ AnimDebugDraw::AnimDebugDraw() :
// HACK: add red, green and blue axis at (1,1,1)
_animDebugDrawData->_vertexBuffer->resize(sizeof(Vertex) * 6);
Vertex* data = (Vertex*)_animDebugDrawData->_vertexBuffer->editData();
data[0].pos = glm::vec3(1.0, 1.0f, 1.0f);
data[0].rgba = toRGBA(255, 0, 0, 255);
data[1].pos = glm::vec3(2.0, 1.0f, 1.0f);
data[1].rgba = toRGBA(255, 0, 0, 255);
data[2].pos = glm::vec3(1.0, 1.0f, 1.0f);
data[2].rgba = toRGBA(0, 255, 0, 255);
data[3].pos = glm::vec3(1.0, 2.0f, 1.0f);
data[3].rgba = toRGBA(0, 255, 0, 255);
data[4].pos = glm::vec3(1.0, 1.0f, 1.0f);
data[4].rgba = toRGBA(0, 0, 255, 255);
data[5].pos = glm::vec3(1.0, 1.0f, 2.0f);
data[5].rgba = toRGBA(0, 0, 255, 255);
_animDebugDrawData->_indexBuffer->resize(sizeof(uint16_t) * 6);
uint16_t* indices = (uint16_t*)_animDebugDrawData->_indexBuffer->editData();
for (int i = 0; i < 6; i++) {
indices[i] = i;
}
static std::vector<Vertex> vertices({
Vertex { glm::vec3(1.0, 1.0f, 1.0f), toRGBA(255, 0, 0, 255) },
Vertex { glm::vec3(2.0, 1.0f, 1.0f), toRGBA(255, 0, 0, 255) },
Vertex { glm::vec3(1.0, 1.0f, 1.0f), toRGBA(0, 255, 0, 255) },
Vertex { glm::vec3(1.0, 2.0f, 1.0f), toRGBA(0, 255, 0, 255) },
Vertex { glm::vec3(1.0, 1.0f, 1.0f), toRGBA(0, 0, 255, 255) },
Vertex { glm::vec3(1.0, 1.0f, 2.0f), toRGBA(0, 0, 255, 255) },
});
static std::vector<uint16_t> indices({ 0, 1, 2, 3, 4, 5 });
_animDebugDrawData->_vertexBuffer->setSubData<Vertex>(0, vertices);
_animDebugDrawData->_indexBuffer->setSubData<uint16_t>(0, indices);
}
AnimDebugDraw::~AnimDebugDraw() {
@ -356,9 +345,13 @@ void AnimDebugDraw::update() {
numVerts += (int)DebugDraw::getInstance().getRays().size() * VERTICES_PER_RAY;
// allocate verts!
data._vertexBuffer->resize(sizeof(Vertex) * numVerts);
Vertex* verts = (Vertex*)data._vertexBuffer->editData();
Vertex* v = verts;
std::vector<Vertex> vertices;
vertices.resize(numVerts);
//Vertex* verts = (Vertex*)data._vertexBuffer->editData();
Vertex* v = nullptr;
if (numVerts) {
v = &vertices[0];
}
// draw absolute poses
for (auto& iter : _absolutePoses) {
@ -381,6 +374,8 @@ void AnimDebugDraw::update() {
}
}
}
data._vertexBuffer->resize(sizeof(Vertex) * numVerts);
data._vertexBuffer->setSubData<Vertex>(0, vertices);
// draw markers from shared DebugDraw singleton
for (auto& iter : markerMap) {
@ -408,20 +403,19 @@ void AnimDebugDraw::update() {
}
DebugDraw::getInstance().clearRays();
assert(numVerts == (v - verts));
assert((!numVerts && !v) || (numVerts == (v - &vertices[0])));
render::Item::Bound theBound;
for (int i = 0; i < numVerts; i++) {
theBound += verts[i].pos;
theBound += vertices[i].pos;
}
data._bound = theBound;
data._isVisible = (numVerts > 0);
data._indexBuffer->resize(sizeof(uint16_t) * numVerts);
uint16_t* indices = (uint16_t*)data._indexBuffer->editData();
for (int i = 0; i < numVerts; i++) {
indices[i] = i;
data._indexBuffer->setSubData<uint16_t>(i, (uint16_t)i);;
}
});
scene->enqueuePendingChanges(pendingChanges);

View file

@ -116,35 +116,25 @@ void DrawStatus::run(const SceneContextPointer& sceneContext,
// FIrst thing, we collect the bound and the status for all the items we want to render
int nbItems = 0;
{
if (!_itemBounds) {
_itemBounds = std::make_shared<gpu::Buffer>();
}
if (!_itemStatus) {
_itemStatus = std::make_shared<gpu::Buffer>();;
}
if (!_itemCells) {
_itemCells = std::make_shared<gpu::Buffer>();;
}
_itemBounds.resize(inItems.size());
_itemStatus.resize(inItems.size());
_itemCells.resize(inItems.size());
_itemBounds->resize((inItems.size() * sizeof(AABox)));
_itemStatus->resize((inItems.size() * NUM_STATUS_VEC4_PER_ITEM * sizeof(glm::vec4)));
_itemCells->resize((inItems.size() * sizeof(Octree::Location)));
AABox* itemAABox = reinterpret_cast<AABox*> (_itemBounds->editData());
glm::ivec4* itemStatus = reinterpret_cast<glm::ivec4*> (_itemStatus->editData());
Octree::Location* itemCell = reinterpret_cast<Octree::Location*> (_itemCells->editData());
for (auto& item : inItems) {
// AABox* itemAABox = reinterpret_cast<AABox*> (_itemBounds->editData());
// glm::ivec4* itemStatus = reinterpret_cast<glm::ivec4*> (_itemStatus->editData());
// Octree::Location* itemCell = reinterpret_cast<Octree::Location*> (_itemCells->editData());
for (size_t i = 0; i < inItems.size(); ++i) {
const auto& item = inItems[i];
if (!item.bound.isInvalid()) {
if (!item.bound.isNull()) {
(*itemAABox) = item.bound;
_itemBounds[i] = item.bound;
} else {
(*itemAABox).setBox(item.bound.getCorner(), 0.1f);
_itemBounds[i].setBox(item.bound.getCorner(), 0.1f);
}
auto& itemScene = scene->getItem(item.id);
(*itemCell) = scene->getSpatialTree().getCellLocation(itemScene.getCell());
_itemCells[i] = scene->getSpatialTree().getCellLocation(itemScene.getCell());
auto itemStatusPointer = itemScene.getStatus();
if (itemStatusPointer) {
@ -152,25 +142,19 @@ void DrawStatus::run(const SceneContextPointer& sceneContext,
auto&& currentStatusValues = itemStatusPointer->getCurrentValues();
int valueNum = 0;
for (int vec4Num = 0; vec4Num < NUM_STATUS_VEC4_PER_ITEM; vec4Num++) {
(*itemStatus) = glm::ivec4(Item::Status::Value::INVALID.getPackedData());
auto& value = (vec4Num ? _itemStatus[i].first : _itemStatus[i].second);
value = glm::ivec4(Item::Status::Value::INVALID.getPackedData());
for (int component = 0; component < VEC4_LENGTH; component++) {
valueNum = vec4Num * VEC4_LENGTH + component;
if (valueNum < (int)currentStatusValues.size()) {
(*itemStatus)[component] = currentStatusValues[valueNum].getPackedData();
value[component] = currentStatusValues[valueNum].getPackedData();
}
}
itemStatus++;
}
} else {
(*itemStatus) = glm::ivec4(Item::Status::Value::INVALID.getPackedData());
itemStatus++;
(*itemStatus) = glm::ivec4(Item::Status::Value::INVALID.getPackedData());
itemStatus++;
_itemStatus[i].first = _itemStatus[i].second = glm::ivec4(Item::Status::Value::INVALID.getPackedData());
}
nbItems++;
itemAABox++;
itemCell++;
}
}
}
@ -194,25 +178,20 @@ void DrawStatus::run(const SceneContextPointer& sceneContext,
// bind the one gpu::Pipeline we need
batch.setPipeline(getDrawItemBoundsPipeline());
AABox* itemAABox = reinterpret_cast<AABox*> (_itemBounds->editData());
glm::ivec4* itemStatus = reinterpret_cast<glm::ivec4*> (_itemStatus->editData());
Octree::Location* itemCell = reinterpret_cast<Octree::Location*> (_itemCells->editData());
//AABox* itemAABox = reinterpret_cast<AABox*> (_itemBounds->editData());
//glm::ivec4* itemStatus = reinterpret_cast<glm::ivec4*> (_itemStatus->editData());
//Octree::Location* itemCell = reinterpret_cast<Octree::Location*> (_itemCells->editData());
const unsigned int VEC3_ADRESS_OFFSET = 3;
if (_showDisplay) {
for (int i = 0; i < nbItems; i++) {
batch._glUniform3fv(_drawItemBoundPosLoc, 1, (const float*) (itemAABox + i));
batch._glUniform3fv(_drawItemBoundDimLoc, 1, ((const float*) (itemAABox + i)) + VEC3_ADRESS_OFFSET);
glm::ivec4 cellLocation(itemCell->pos.x, itemCell->pos.y, itemCell->pos.z, itemCell->depth);
batch._glUniform3fv(_drawItemBoundPosLoc, 1, (const float*)&(_itemBounds[i]));
batch._glUniform3fv(_drawItemBoundDimLoc, 1, ((const float*)&(_itemBounds[i])) + VEC3_ADRESS_OFFSET);
glm::ivec4 cellLocation(_itemCells[i].pos, _itemCells[i].depth);
batch._glUniform4iv(_drawItemCellLocLoc, 1, ((const int*)(&cellLocation)));
batch.draw(gpu::LINES, 24, 0);
itemCell++;
}
}
@ -222,10 +201,10 @@ void DrawStatus::run(const SceneContextPointer& sceneContext,
if (_showNetwork) {
for (int i = 0; i < nbItems; i++) {
batch._glUniform3fv(_drawItemStatusPosLoc, 1, (const float*) (itemAABox + i));
batch._glUniform3fv(_drawItemStatusDimLoc, 1, ((const float*) (itemAABox + i)) + VEC3_ADRESS_OFFSET);
batch._glUniform4iv(_drawItemStatusValue0Loc, 1, (const int*)(itemStatus + NUM_STATUS_VEC4_PER_ITEM * i));
batch._glUniform4iv(_drawItemStatusValue1Loc, 1, (const int*)(itemStatus + NUM_STATUS_VEC4_PER_ITEM * i + 1));
batch._glUniform3fv(_drawItemStatusPosLoc, 1, (const float*)&(_itemBounds[i]));
batch._glUniform3fv(_drawItemStatusDimLoc, 1, ((const float*)&(_itemBounds[i])) + VEC3_ADRESS_OFFSET);
batch._glUniform4iv(_drawItemStatusValue0Loc, 1, (const int*)&(_itemStatus[i].first));
batch._glUniform4iv(_drawItemStatusValue1Loc, 1, (const int*)&(_itemStatus[i].second));
batch.draw(gpu::TRIANGLES, 24 * NUM_STATUS_VEC4_PER_ITEM, 0);
}
}

View file

@ -68,9 +68,13 @@ namespace render {
gpu::Stream::FormatPointer _drawItemFormat;
gpu::PipelinePointer _drawItemBoundsPipeline;
gpu::PipelinePointer _drawItemStatusPipeline;
gpu::BufferPointer _itemBounds;
gpu::BufferPointer _itemCells;
gpu::BufferPointer _itemStatus;
std::vector<AABox> _itemBounds;
std::vector<std::pair<glm::ivec4, glm::ivec4>> _itemStatus;
std::vector<Octree::Location> _itemCells;
//gpu::BufferPointer _itemBounds;
//gpu::BufferPointer _itemCells;
//gpu::BufferPointer _itemStatus;
gpu::TexturePointer _statusIconMap;
};
}