| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- // Copyright (c) 2008-2023 the Urho3D project
- // License: MIT
- #include "../../Precompiled.h"
- #include "../../Core/Context.h"
- #include "../../Graphics/Graphics.h"
- #include "../../GraphicsAPI/Direct3D11/D3D11GraphicsImpl.h"
- #include "../../GraphicsAPI/IndexBuffer.h"
- #include "../../IO/Log.h"
- #include "../../DebugNew.h"
- namespace Urho3D
- {
- void IndexBuffer::OnDeviceLost_D3D11()
- {
- // No-op on Direct3D11
- }
- void IndexBuffer::OnDeviceReset_D3D11()
- {
- // No-op on Direct3D11
- }
- void IndexBuffer::Release_D3D11()
- {
- Unlock_D3D11();
- if (graphics_ && graphics_->GetIndexBuffer() == this)
- graphics_->SetIndexBuffer(nullptr);
- URHO3D_SAFE_RELEASE(object_.ptr_);
- }
- bool IndexBuffer::SetData_D3D11(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_.ptr_)
- {
- if (dynamic_)
- {
- void* hwData = MapBuffer_D3D11(0, indexCount_, true);
- if (hwData)
- {
- memcpy(hwData, data, (size_t)indexCount_ * indexSize_);
- UnmapBuffer();
- }
- else
- return false;
- }
- else
- {
- D3D11_BOX destBox;
- destBox.left = 0;
- destBox.right = (UINT)indexCount_ * indexSize_;
- destBox.top = 0;
- destBox.bottom = 1;
- destBox.front = 0;
- destBox.back = 1;
- graphics_->GetImpl_D3D11()->GetDeviceContext()->UpdateSubresource((ID3D11Buffer*)object_.ptr_, 0, &destBox, data, 0, 0);
- }
- }
- return true;
- }
- bool IndexBuffer::SetDataRange_D3D11(const void* data, i32 start, i32 count, bool discard)
- {
- assert(start >= 0 && count >= 0);
- if (start == 0 && count == indexCount_)
- return SetData_D3D11(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_.ptr_)
- {
- if (dynamic_)
- {
- void* hwData = MapBuffer_D3D11(start, count, discard);
- if (hwData)
- {
- memcpy(hwData, data, (size_t)count * indexSize_);
- UnmapBuffer();
- }
- else
- return false;
- }
- else
- {
- D3D11_BOX destBox;
- destBox.left = (UINT)start * indexSize_;
- destBox.right = destBox.left + (UINT)count * indexSize_;
- destBox.top = 0;
- destBox.bottom = 1;
- destBox.front = 0;
- destBox.back = 1;
- graphics_->GetImpl_D3D11()->GetDeviceContext()->UpdateSubresource((ID3D11Buffer*)object_.ptr_, 0, &destBox, data, 0, 0);
- }
- }
- return true;
- }
- void* IndexBuffer::Lock_D3D11(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;
- // Because shadow data must be kept in sync, can only lock hardware buffer if not shadowed
- if (object_.ptr_ && !shadowData_ && dynamic_)
- return MapBuffer_D3D11(start, count, discard);
- else 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_D3D11()
- {
- switch (lockState_)
- {
- case LOCK_HARDWARE:
- UnmapBuffer();
- break;
- case LOCK_SHADOW:
- SetDataRange_D3D11(shadowData_.Get() + (intptr_t)lockStart_ * indexSize_, lockStart_, lockCount_);
- lockState_ = LOCK_NONE;
- break;
- case LOCK_SCRATCH:
- SetDataRange_D3D11(lockScratchData_, lockStart_, lockCount_);
- if (graphics_)
- graphics_->FreeScratchBuffer(lockScratchData_);
- lockScratchData_ = nullptr;
- lockState_ = LOCK_NONE;
- break;
- default: break;
- }
- }
- bool IndexBuffer::Create_D3D11()
- {
- Release_D3D11();
- if (!indexCount_)
- return true;
- if (graphics_)
- {
- D3D11_BUFFER_DESC bufferDesc;
- memset(&bufferDesc, 0, sizeof bufferDesc);
- bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
- bufferDesc.CPUAccessFlags = dynamic_ ? D3D11_CPU_ACCESS_WRITE : 0;
- bufferDesc.Usage = dynamic_ ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
- bufferDesc.ByteWidth = (UINT)indexCount_ * indexSize_;
- HRESULT hr = graphics_->GetImpl_D3D11()->GetDevice()->CreateBuffer(&bufferDesc, nullptr, (ID3D11Buffer**)&object_.ptr_);
- if (FAILED(hr))
- {
- URHO3D_SAFE_RELEASE(object_.ptr_);
- URHO3D_LOGD3DERROR("Failed to create index buffer", hr);
- return false;
- }
- }
- return true;
- }
- bool IndexBuffer::UpdateToGPU_D3D11()
- {
- if (object_.ptr_ && shadowData_)
- return SetData_D3D11(shadowData_.Get());
- else
- return false;
- }
- void* IndexBuffer::MapBuffer_D3D11(i32 start, i32 count, bool discard)
- {
- assert(start >= 0 && count >= 0);
- void* hwData = nullptr;
- if (object_.ptr_)
- {
- D3D11_MAPPED_SUBRESOURCE mappedData;
- mappedData.pData = nullptr;
- HRESULT hr = graphics_->GetImpl_D3D11()->GetDeviceContext()->Map((ID3D11Buffer*)object_.ptr_, 0, discard ? D3D11_MAP_WRITE_DISCARD :
- D3D11_MAP_WRITE, 0, &mappedData);
- if (FAILED(hr) || !mappedData.pData)
- URHO3D_LOGD3DERROR("Failed to map index buffer", hr);
- else
- {
- hwData = mappedData.pData;
- lockState_ = LOCK_HARDWARE;
- }
- }
- return hwData;
- }
- void IndexBuffer::UnmapBuffer_D3D11()
- {
- if (object_.ptr_ && lockState_ == LOCK_HARDWARE)
- {
- graphics_->GetImpl_D3D11()->GetDeviceContext()->Unmap((ID3D11Buffer*)object_.ptr_, 0);
- lockState_ = LOCK_NONE;
- }
- }
- } // namespace Urho3D
|