Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "Buffer.h" 21 : 22 : #include "lib/config2.h" 23 : #include "lib/debug.h" 24 : #include "renderer/backend/gl/Device.h" 25 : 26 : namespace Renderer 27 : { 28 : 29 : namespace Backend 30 : { 31 : 32 : namespace GL 33 : { 34 : 35 : namespace 36 : { 37 : 38 0 : GLenum GetTargetFromBufferType(const IBuffer::Type type) 39 : { 40 0 : GLenum target{GL_ARRAY_BUFFER}; 41 0 : switch (type) 42 : { 43 : case IBuffer::Type::INDEX: 44 : target = GL_ELEMENT_ARRAY_BUFFER; 45 : break; 46 : #if !CONFIG2_GLES 47 0 : case IBuffer::Type::UNIFORM: 48 0 : target = GL_UNIFORM_BUFFER; 49 0 : break; 50 : #endif 51 0 : default: 52 0 : target = GL_ARRAY_BUFFER; 53 0 : break; 54 : } 55 0 : return target; 56 : } 57 : 58 : } // anonymous namespace 59 : 60 : // static 61 0 : std::unique_ptr<CBuffer> CBuffer::Create( 62 : CDevice* device, [[maybe_unused]] const char* name, 63 : const Type type, const uint32_t size, const uint32_t usage) 64 : { 65 0 : ENSURE(type == Type::VERTEX || type == Type::INDEX || type == Type::UNIFORM); 66 0 : std::unique_ptr<CBuffer> buffer(new CBuffer()); 67 0 : buffer->m_Device = device; 68 0 : buffer->m_Type = type; 69 0 : buffer->m_Size = size; 70 0 : buffer->m_Usage = usage; 71 0 : glGenBuffers(1, &buffer->m_Handle); 72 0 : const GLenum target{GetTargetFromBufferType(type)}; 73 0 : glBindBuffer(target, buffer->m_Handle); 74 0 : glBufferData(target, size, nullptr, (usage & IBuffer::Usage::DYNAMIC) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW); 75 : #if !CONFIG2_GLES 76 0 : if (buffer->m_Device->GetCapabilities().debugLabels) 77 : { 78 0 : glObjectLabel(GL_BUFFER, buffer->m_Handle, -1, name); 79 : } 80 : #endif 81 0 : glBindBuffer(target, 0); 82 0 : return buffer; 83 : } 84 : 85 0 : CBuffer::CBuffer() = default; 86 : 87 0 : CBuffer::~CBuffer() 88 : { 89 0 : if (m_Handle) 90 0 : glDeleteBuffers(1, &m_Handle); 91 0 : } 92 : 93 0 : IDevice* CBuffer::GetDevice() 94 : { 95 0 : return m_Device; 96 : } 97 : 98 : } // namespace GL 99 : 100 : } // namespace Backend 101 : 102 : } // namespace Renderer