| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- //
- // Copyright (c) 2008-2017 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "../../Precompiled.h"
- #include "../../Graphics/Graphics.h"
- #include "../../Graphics/GraphicsImpl.h"
- #include "../../Graphics/VertexBuffer.h"
- #include "../../IO/Log.h"
- #include "../../DebugNew.h"
- namespace Urho3D
- {
- void VertexBuffer::OnDeviceLost()
- {
- GPUObject::OnDeviceLost();
- }
- void VertexBuffer::OnDeviceReset()
- {
- if (!object_.name_)
- {
- Create();
- dataLost_ = !UpdateToGPU();
- }
- else if (dataPending_)
- dataLost_ = !UpdateToGPU();
- dataPending_ = false;
- }
- void VertexBuffer::Release()
- {
- Unlock();
- if (object_.name_)
- {
- if (!graphics_)
- return;
- if (!graphics_->IsDeviceLost())
- {
- for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
- {
- if (graphics_->GetVertexBuffer(i) == this)
- graphics_->SetVertexBuffer(nullptr);
- }
- graphics_->SetVBO(0);
- glDeleteBuffers(1, &object_.name_);
- }
- object_.name_ = 0;
- }
- }
- bool VertexBuffer::SetData(const void* data)
- {
- if (!data)
- {
- URHO3D_LOGERROR("Null pointer for vertex buffer data");
- return false;
- }
- if (!vertexSize_)
- {
- URHO3D_LOGERROR("Vertex elements not defined, can not set vertex buffer data");
- return false;
- }
- if (shadowData_ && data != shadowData_.Get())
- memcpy(shadowData_.Get(), data, vertexCount_ * vertexSize_);
- if (object_.name_)
- {
- if (!graphics_->IsDeviceLost())
- {
- graphics_->SetVBO(object_.name_);
- glBufferData(GL_ARRAY_BUFFER, vertexCount_ * vertexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
- }
- else
- {
- URHO3D_LOGWARNING("Vertex buffer data assignment while device is lost");
- dataPending_ = true;
- }
- }
- dataLost_ = false;
- return true;
- }
- bool VertexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
- {
- if (start == 0 && count == vertexCount_)
- return SetData(data);
- if (!data)
- {
- URHO3D_LOGERROR("Null pointer for vertex buffer data");
- return false;
- }
- if (!vertexSize_)
- {
- URHO3D_LOGERROR("Vertex elements not defined, can not set vertex buffer data");
- return false;
- }
- if (start + count > vertexCount_)
- {
- URHO3D_LOGERROR("Illegal range for setting new vertex buffer data");
- return false;
- }
- if (!count)
- return true;
- if (shadowData_ && shadowData_.Get() + start * vertexSize_ != data)
- memcpy(shadowData_.Get() + start * vertexSize_, data, count * vertexSize_);
- if (object_.name_)
- {
- if (!graphics_->IsDeviceLost())
- {
- graphics_->SetVBO(object_.name_);
- if (!discard || start != 0)
- glBufferSubData(GL_ARRAY_BUFFER, start * vertexSize_, count * vertexSize_, data);
- else
- glBufferData(GL_ARRAY_BUFFER, count * vertexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
- }
- else
- {
- URHO3D_LOGWARNING("Vertex buffer data assignment while device is lost");
- dataPending_ = true;
- }
- }
- return true;
- }
- void* VertexBuffer::Lock(unsigned start, unsigned count, bool discard)
- {
- if (lockState_ != LOCK_NONE)
- {
- URHO3D_LOGERROR("Vertex buffer already locked");
- return nullptr;
- }
- if (!vertexSize_)
- {
- URHO3D_LOGERROR("Vertex elements not defined, can not lock vertex buffer");
- return nullptr;
- }
- if (start + count > vertexCount_)
- {
- URHO3D_LOGERROR("Illegal range for locking vertex buffer");
- return nullptr;
- }
- if (!count)
- return nullptr;
- lockStart_ = start;
- lockCount_ = count;
- discardLock_ = discard;
- if (shadowData_)
- {
- lockState_ = LOCK_SHADOW;
- return shadowData_.Get() + start * vertexSize_;
- }
- else if (graphics_)
- {
- lockState_ = LOCK_SCRATCH;
- lockScratchData_ = graphics_->ReserveScratchBuffer(count * vertexSize_);
- return lockScratchData_;
- }
- else
- return nullptr;
- }
- void VertexBuffer::Unlock()
- {
- switch (lockState_)
- {
- case LOCK_SHADOW:
- SetDataRange(shadowData_.Get() + lockStart_ * vertexSize_, lockStart_, lockCount_, discardLock_);
- lockState_ = LOCK_NONE;
- break;
- case LOCK_SCRATCH:
- SetDataRange(lockScratchData_, lockStart_, lockCount_, discardLock_);
- if (graphics_)
- graphics_->FreeScratchBuffer(lockScratchData_);
- lockScratchData_ = nullptr;
- lockState_ = LOCK_NONE;
- break;
- default:
- break;
- }
- }
- bool VertexBuffer::Create()
- {
- if (!vertexCount_ || !elementMask_)
- {
- Release();
- return true;
- }
- if (graphics_)
- {
- if (graphics_->IsDeviceLost())
- {
- URHO3D_LOGWARNING("Vertex buffer creation while device is lost");
- return true;
- }
- if (!object_.name_)
- glGenBuffers(1, &object_.name_);
- if (!object_.name_)
- {
- URHO3D_LOGERROR("Failed to create vertex buffer");
- return false;
- }
- graphics_->SetVBO(object_.name_);
- glBufferData(GL_ARRAY_BUFFER, vertexCount_ * vertexSize_, nullptr, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
- }
- return true;
- }
- bool VertexBuffer::UpdateToGPU()
- {
- if (object_.name_ && shadowData_)
- return SetData(shadowData_.Get());
- else
- return false;
- }
- void* VertexBuffer::MapBuffer(unsigned start, unsigned count, bool discard)
- {
- // Never called on OpenGL
- return nullptr;
- }
- void VertexBuffer::UnmapBuffer()
- {
- // Never called on OpenGL
- }
- }
|