| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- // Copyright (c) 2008-2023 the Urho3D project
- // License: MIT
- #include "../../Precompiled.h"
- #include "../../Core/Context.h"
- #include "../../Graphics/Graphics.h"
- #include "../../GraphicsAPI/GraphicsImpl.h"
- #include "../../GraphicsAPI/IndexBuffer.h"
- #include "../../IO/Log.h"
- #include "../../DebugNew.h"
- namespace Urho3D
- {
- void IndexBuffer::OnDeviceLost_OGL()
- {
- if (object_.name_ && !graphics_->IsDeviceLost())
- glDeleteBuffers(1, &object_.name_);
- GPUObject::OnDeviceLost();
- }
- void IndexBuffer::OnDeviceReset_OGL()
- {
- if (!object_.name_)
- {
- Create_OGL();
- dataLost_ = !UpdateToGPU_OGL();
- }
- else if (dataPending_)
- dataLost_ = !UpdateToGPU_OGL();
- dataPending_ = false;
- }
- void IndexBuffer::Release_OGL()
- {
- Unlock_OGL();
- if (object_.name_)
- {
- if (!graphics_)
- return;
- if (!graphics_->IsDeviceLost())
- {
- if (graphics_->GetIndexBuffer() == this)
- graphics_->SetIndexBuffer(nullptr);
- glDeleteBuffers(1, &object_.name_);
- }
- object_.name_ = 0;
- }
- }
- bool IndexBuffer::SetData_OGL(const void* data)
- {
- if (!data)
- {
- URHO3D_LOGERROR("Null pointer for index buffer data");
- return false;
- }
- if (!indexSize_)
- {
- URHO3D_LOGERROR("Index size not defined, can not set index buffer data");
- return false;
- }
- if (shadowData_ && data != shadowData_.Get())
- memcpy(shadowData_.Get(), data, (size_t)indexCount_ * indexSize_);
- if (object_.name_)
- {
- if (!graphics_->IsDeviceLost())
- {
- graphics_->SetIndexBuffer(this);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)indexCount_ * indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
- }
- else
- {
- URHO3D_LOGWARNING("Index buffer data assignment while device is lost");
- dataPending_ = true;
- }
- }
- dataLost_ = false;
- return true;
- }
- bool IndexBuffer::SetDataRange_OGL(const void* data, i32 start, i32 count, bool discard)
- {
- assert(start >= 0 && count >= 0);
- if (start == 0 && count == indexCount_)
- return SetData_OGL(data);
- if (!data)
- {
- URHO3D_LOGERROR("Null pointer for index buffer data");
- return false;
- }
- if (!indexSize_)
- {
- URHO3D_LOGERROR("Index size not defined, can not set index buffer data");
- return false;
- }
- if (start + count > indexCount_)
- {
- URHO3D_LOGERROR("Illegal range for setting new index buffer data");
- return false;
- }
- if (!count)
- return true;
- byte* dst = shadowData_.Get() + (intptr_t)start * indexSize_;
- if (shadowData_ && dst != data)
- memcpy(dst, data, (size_t)count * indexSize_);
- if (object_.name_)
- {
- if (!graphics_->IsDeviceLost())
- {
- graphics_->SetIndexBuffer(this);
- if (!discard || start != 0)
- glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, (GLintptr)start * indexSize_, (GLsizeiptr)count * indexSize_, data);
- else
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)count * indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
- }
- else
- {
- URHO3D_LOGWARNING("Index buffer data assignment while device is lost");
- dataPending_ = true;
- }
- }
- return true;
- }
- void* IndexBuffer::Lock_OGL(i32 start, i32 count, bool discard)
- {
- assert(start >= 0 && count >= 0);
- if (lockState_ != LOCK_NONE)
- {
- URHO3D_LOGERROR("Index buffer already locked");
- return nullptr;
- }
- if (!indexSize_)
- {
- URHO3D_LOGERROR("Index size not defined, can not lock index buffer");
- return nullptr;
- }
- if (start + count > indexCount_)
- {
- URHO3D_LOGERROR("Illegal range for locking index buffer");
- return nullptr;
- }
- if (!count)
- return nullptr;
- lockStart_ = start;
- lockCount_ = count;
- discardLock_ = discard;
- if (shadowData_)
- {
- lockState_ = LOCK_SHADOW;
- return shadowData_.Get() + (intptr_t)start * indexSize_;
- }
- else if (graphics_)
- {
- lockState_ = LOCK_SCRATCH;
- lockScratchData_ = graphics_->ReserveScratchBuffer(count * indexSize_);
- return lockScratchData_;
- }
- else
- return nullptr;
- }
- void IndexBuffer::Unlock_OGL()
- {
- switch (lockState_)
- {
- case LOCK_SHADOW:
- SetDataRange_OGL(shadowData_.Get() + (intptr_t)lockStart_ * indexSize_, lockStart_, lockCount_, discardLock_);
- lockState_ = LOCK_NONE;
- break;
- case LOCK_SCRATCH:
- SetDataRange_OGL(lockScratchData_, lockStart_, lockCount_, discardLock_);
- if (graphics_)
- graphics_->FreeScratchBuffer(lockScratchData_);
- lockScratchData_ = nullptr;
- lockState_ = LOCK_NONE;
- break;
- default:
- break;
- }
- }
- bool IndexBuffer::Create_OGL()
- {
- if (!indexCount_)
- {
- Release_OGL();
- return true;
- }
- if (graphics_)
- {
- if (graphics_->IsDeviceLost())
- {
- URHO3D_LOGWARNING("Index buffer creation while device is lost");
- return true;
- }
- if (!object_.name_)
- glGenBuffers(1, &object_.name_);
- if (!object_.name_)
- {
- URHO3D_LOGERROR("Failed to create index buffer");
- return false;
- }
- graphics_->SetIndexBuffer(this);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)indexCount_ * indexSize_, nullptr, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
- }
- return true;
- }
- bool IndexBuffer::UpdateToGPU_OGL()
- {
- if (object_.name_ && shadowData_)
- return SetData_OGL(shadowData_.Get());
- else
- return false;
- }
- void* IndexBuffer::MapBuffer_OGL(i32 start, i32 count, bool discard)
- {
- // Never called on OpenGL
- return nullptr;
- }
- void IndexBuffer::UnmapBuffer_OGL()
- {
- // Never called on OpenGL
- }
- } // namespace Urho3D
|