| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- //
- // 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 VertexBuffer code common to all graphics APIs.
- #include "../Precompiled.h"
- #include "../Graphics/Graphics.h"
- #include "../Graphics/VertexBuffer.h"
- #include "../Math/MathDefs.h"
- #include "../DebugNew.h"
- namespace Atomic
- {
- VertexBuffer::VertexBuffer(Context* context, bool forceHeadless) :
- Object(context),
- GPUObject(forceHeadless ? (Graphics*)0 : GetSubsystem<Graphics>()),
- vertexCount_(0),
- elementMask_(0),
- lockState_(LOCK_NONE),
- lockStart_(0),
- lockCount_(0),
- lockScratchData_(0),
- shadowed_(false),
- dynamic_(false),
- discardLock_(false)
- {
- UpdateOffsets();
- // Force shadowing mode if graphics subsystem does not exist
- if (!graphics_)
- shadowed_ = true;
- }
- VertexBuffer::~VertexBuffer()
- {
- Release();
- }
- void VertexBuffer::SetShadowed(bool enable)
- {
- // If no graphics subsystem, can not disable shadowing
- if (!graphics_)
- enable = true;
- if (enable != shadowed_)
- {
- if (enable && vertexSize_ && vertexCount_)
- shadowData_ = new unsigned char[vertexCount_ * vertexSize_];
- else
- shadowData_.Reset();
- shadowed_ = enable;
- }
- }
- bool VertexBuffer::SetSize(unsigned vertexCount, unsigned elementMask, bool dynamic)
- {
- return SetSize(vertexCount, GetElements(elementMask), dynamic);
- }
- bool VertexBuffer::SetSize(unsigned vertexCount, const PODVector<VertexElement>& elements, bool dynamic)
- {
- Unlock();
- vertexCount_ = vertexCount;
- elements_ = elements;
- dynamic_ = dynamic;
- UpdateOffsets();
- if (shadowed_ && vertexCount_ && vertexSize_)
- shadowData_ = new unsigned char[vertexCount_ * vertexSize_];
- else
- shadowData_.Reset();
- return Create();
- }
- void VertexBuffer::UpdateOffsets()
- {
- unsigned elementOffset = 0;
- elementHash_ = 0;
- elementMask_ = 0;
- for (PODVector<VertexElement>::Iterator i = elements_.Begin(); i != elements_.End(); ++i)
- {
- i->offset_ = elementOffset;
- elementOffset += ELEMENT_TYPESIZES[i->type_];
- elementHash_ <<= 6;
- elementHash_ += (((int)i->type_ + 1) * ((int)i->semantic_ + 1) + i->index_);
- for (unsigned j = 0; j < MAX_LEGACY_VERTEX_ELEMENTS; ++j)
- {
- const VertexElement& legacy = LEGACY_VERTEXELEMENTS[j];
- if (i->type_ == legacy.type_ && i->semantic_ == legacy.semantic_ && i->index_ == legacy.index_)
- elementMask_ |= (1 << j);
- }
- }
- vertexSize_ = elementOffset;
- }
- const VertexElement* VertexBuffer::GetElement(VertexElementSemantic semantic, unsigned char index) const
- {
- for (PODVector<VertexElement>::ConstIterator i = elements_.Begin(); i != elements_.End(); ++i)
- {
- if (i->semantic_ == semantic && i->index_ == index)
- return &(*i);
- }
- return 0;
- }
- const VertexElement* VertexBuffer::GetElement(VertexElementType type, VertexElementSemantic semantic, unsigned char index) const
- {
- for (PODVector<VertexElement>::ConstIterator i = elements_.Begin(); i != elements_.End(); ++i)
- {
- if (i->type_ == type && i->semantic_ == semantic && i->index_ == index)
- return &(*i);
- }
- return 0;
- }
- const VertexElement* VertexBuffer::GetElement(const PODVector<VertexElement>& elements, VertexElementType type, VertexElementSemantic semantic, unsigned char index)
- {
- for (PODVector<VertexElement>::ConstIterator i = elements.Begin(); i != elements.End(); ++i)
- {
- if (i->type_ == type && i->semantic_ == semantic && i->index_ == index)
- return &(*i);
- }
- return 0;
- }
- bool VertexBuffer::HasElement(const PODVector<VertexElement>& elements, VertexElementType type, VertexElementSemantic semantic, unsigned char index)
- {
- return GetElement(elements, type, semantic, index) != 0;
- }
- unsigned VertexBuffer::GetElementOffset(const PODVector<VertexElement>& elements, VertexElementType type, VertexElementSemantic semantic, unsigned char index)
- {
- const VertexElement* element = GetElement(elements, type, semantic, index);
- return element ? element->offset_ : M_MAX_UNSIGNED;
- }
- PODVector<VertexElement> VertexBuffer::GetElements(unsigned elementMask)
- {
- PODVector<VertexElement> ret;
- for (unsigned i = 0; i < MAX_LEGACY_VERTEX_ELEMENTS; ++i)
- {
- if (elementMask & (1 << i))
- ret.Push(LEGACY_VERTEXELEMENTS[i]);
- }
- return ret;
- }
- unsigned VertexBuffer::GetVertexSize(const PODVector<VertexElement>& elements)
- {
- unsigned size = 0;
- for (unsigned i = 0; i < elements.Size(); ++i)
- size += ELEMENT_TYPESIZES[elements[i].type_];
- return size;
- }
- unsigned VertexBuffer::GetVertexSize(unsigned elementMask)
- {
- unsigned size = 0;
- for (unsigned i = 0; i < MAX_LEGACY_VERTEX_ELEMENTS; ++i)
- {
- if (elementMask & (1 << i))
- size += ELEMENT_TYPESIZES[LEGACY_VERTEXELEMENTS[i].type_];
- }
- return size;
- }
- void VertexBuffer::UpdateOffsets(PODVector<VertexElement>& elements)
- {
- unsigned elementOffset = 0;
- for (PODVector<VertexElement>::Iterator i = elements.Begin(); i != elements.End(); ++i)
- {
- i->offset_ = elementOffset;
- elementOffset += ELEMENT_TYPESIZES[i->type_];
- }
- }
- }
|