| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- //
- // 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.
- //
- // This file contains IndexBuffer code common to all graphics APIs.
- #include "../Precompiled.h"
- #include "../Graphics/Graphics.h"
- #include "../Graphics/IndexBuffer.h"
- #include "../IO/Log.h"
- #include "../DebugNew.h"
- namespace Atomic
- {
- IndexBuffer::IndexBuffer(Context* context, bool forceHeadless) :
- Object(context),
- GPUObject(forceHeadless ? (Graphics*)0 : GetSubsystem<Graphics>()),
- indexCount_(0),
- indexSize_(0),
- lockState_(LOCK_NONE),
- lockStart_(0),
- lockCount_(0),
- lockScratchData_(0),
- shadowed_(false),
- dynamic_(false),
- discardLock_(false)
- {
- // Force shadowing mode if graphics subsystem does not exist
- if (!graphics_)
- shadowed_ = true;
- }
- IndexBuffer::~IndexBuffer()
- {
- Release();
- }
- void IndexBuffer::SetShadowed(bool enable)
- {
- // If no graphics subsystem, can not disable shadowing
- if (!graphics_)
- enable = true;
- if (enable != shadowed_)
- {
- if (enable && indexCount_ && indexSize_)
- shadowData_ = new unsigned char[indexCount_ * indexSize_];
- else
- shadowData_.Reset();
- shadowed_ = enable;
- }
- }
- bool IndexBuffer::SetSize(unsigned indexCount, bool largeIndices, bool dynamic)
- {
- Unlock();
- indexCount_ = indexCount;
- indexSize_ = (unsigned)(largeIndices ? sizeof(unsigned) : sizeof(unsigned short));
- dynamic_ = dynamic;
- if (shadowed_ && indexCount_ && indexSize_)
- shadowData_ = new unsigned char[indexCount_ * indexSize_];
- else
- shadowData_.Reset();
- return Create();
- }
- bool IndexBuffer::GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
- {
- if (!shadowData_)
- {
- ATOMIC_LOGERROR("Used vertex range can only be queried from an index buffer with shadow data");
- return false;
- }
- if (start + count > indexCount_)
- {
- ATOMIC_LOGERROR("Illegal index range for querying used vertices");
- return false;
- }
- minVertex = M_MAX_UNSIGNED;
- unsigned maxVertex = 0;
- if (indexSize_ == sizeof(unsigned))
- {
- unsigned* indices = ((unsigned*)shadowData_.Get()) + start;
- for (unsigned i = 0; i < count; ++i)
- {
- if (indices[i] < minVertex)
- minVertex = indices[i];
- if (indices[i] > maxVertex)
- maxVertex = indices[i];
- }
- }
- else
- {
- unsigned short* indices = ((unsigned short*)shadowData_.Get()) + start;
- for (unsigned i = 0; i < count; ++i)
- {
- if (indices[i] < minVertex)
- minVertex = indices[i];
- if (indices[i] > maxVertex)
- maxVertex = indices[i];
- }
- }
- vertexCount = maxVertex - minVertex + 1;
- return true;
- }
- }
|