bool Buffer::Create(unsigned int size, UInt32 storage, BufferUsage usage) { Destroy(); // Notre buffer est-il supporté ? if (!IsStorageSupported(storage)) { NazaraError("Buffer storage not supported"); return false; } std::unique_ptr<AbstractBuffer> impl(s_bufferFactories[storage](this, m_type)); if (!impl->Create(size, usage)) { NazaraError("Failed to create buffer"); return false; } m_impl = impl.release(); m_size = size; m_storage = storage; m_usage = usage; return true; // Si on arrive ici c'est que tout s'est bien passé. }
bool Buffer::SetStorage(UInt32 storage) { #if NAZARA_UTILITY_SAFE if (!m_impl) { NazaraError("Buffer not valid"); return false; } #endif if (m_storage == storage) return true; if (!IsStorageSupported(storage)) { NazaraError("Storage not supported"); return false; } void* ptr = m_impl->Map(BufferAccess_ReadOnly, 0, m_size); if (!ptr) { NazaraError("Failed to map buffer"); return false; } CallOnExit unmapMyImpl([this]() { m_impl->Unmap(); }); std::unique_ptr<AbstractBuffer> impl(s_bufferFactories[storage](this, m_type)); if (!impl->Create(m_size, m_usage)) { NazaraError("Failed to create buffer"); return false; } CallOnExit destroyImpl([&impl]() { impl->Destroy(); }); if (!impl->Fill(ptr, 0, m_size)) { NazaraError("Failed to fill buffer"); return false; } destroyImpl.Reset(); unmapMyImpl.CallAndReset(); m_impl->Destroy(); delete m_impl; m_impl = impl.release(); m_storage = storage; return true; }
bool Buffer::SetStorage(DataStorage storage) { NazaraAssert(m_impl, "Invalid buffer"); if (HasStorage(storage)) return true; if (!IsStorageSupported(storage)) { NazaraError("Storage not supported"); return false; } void* ptr = m_impl->Map(BufferAccess_ReadOnly, 0, m_size); if (!ptr) { NazaraError("Failed to map buffer"); return false; } CallOnExit unmapMyImpl([this]() { m_impl->Unmap(); }); std::unique_ptr<AbstractBuffer> impl(s_bufferFactories[storage](this, m_type)); if (!impl->Initialize(m_size, m_usage)) { NazaraError("Failed to create buffer"); return false; } if (!impl->Fill(ptr, 0, m_size)) { NazaraError("Failed to fill buffer"); return false; } unmapMyImpl.CallAndReset(); m_impl = std::move(impl); return true; }