Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/renderer/VertexArray.cpp
/* Copyright (C) 2015 Wildfire Games. | /* Copyright (C) 2022 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
Show All 21 Lines | |||||
VertexArray::VertexArray(GLenum usage, GLenum target) | VertexArray::VertexArray(GLenum usage, GLenum target) | ||||
{ | { | ||||
m_Usage = usage; | m_Usage = usage; | ||||
m_Target = target; | m_Target = target; | ||||
m_NumVertices = 0; | m_NumVertices = 0; | ||||
m_VB = 0; | |||||
m_BackingStore = 0; | m_BackingStore = 0; | ||||
m_Stride = 0; | m_Stride = 0; | ||||
} | } | ||||
VertexArray::~VertexArray() | VertexArray::~VertexArray() | ||||
{ | { | ||||
Free(); | Free(); | ||||
} | } | ||||
// Free all resources on destruction or when a layout parameter changes | // Free all resources on destruction or when a layout parameter changes | ||||
void VertexArray::Free() | void VertexArray::Free() | ||||
{ | { | ||||
rtl_FreeAligned(m_BackingStore); | rtl_FreeAligned(m_BackingStore); | ||||
m_BackingStore = 0; | m_BackingStore = 0; | ||||
if (m_VB) | m_VB.Reset(); | ||||
{ | |||||
g_VBMan.Release(m_VB); | |||||
m_VB = 0; | |||||
} | |||||
} | } | ||||
// Set the number of vertices stored in the array | // Set the number of vertices stored in the array | ||||
void VertexArray::SetNumVertices(size_t num) | void VertexArray::SetNumVertices(size_t num) | ||||
{ | { | ||||
if (num == m_NumVertices) | if (num == m_NumVertices) | ||||
return; | return; | ||||
Free(); | Free(); | ||||
m_NumVertices = num; | m_NumVertices = num; | ||||
} | } | ||||
// Add vertex attributes like Position, Normal, UV | // Add vertex attributes like Position, Normal, UV | ||||
void VertexArray::AddAttribute(Attribute* attr) | void VertexArray::AddAttribute(Attribute* attr) | ||||
{ | { | ||||
ENSURE( | ENSURE( | ||||
(attr->type == GL_FLOAT || attr->type == GL_SHORT || attr->type == GL_UNSIGNED_SHORT || attr->type == GL_UNSIGNED_BYTE) | (attr->type == GL_FLOAT || attr->type == GL_SHORT || attr->type == GL_UNSIGNED_SHORT || attr->type == GL_UNSIGNED_BYTE) | ||||
&& "Unsupported attribute type" | && "Unsupported attribute type" | ||||
); | ); | ||||
ENSURE(attr->elems >= 1 && attr->elems <= 4); | ENSURE(attr->elems >= 1 && attr->elems <= 4); | ||||
attr->vertexArray = this; | attr->vertexArray = this; | ||||
m_Attributes.push_back(attr); | m_Attributes.push_back(attr); | ||||
Free(); | Free(); | ||||
} | } | ||||
// Template specialization for GetIterator(). | // Template specialization for GetIterator(). | ||||
// We can put this into the source file because only a fixed set of types | // We can put this into the source file because only a fixed set of types | ||||
// is supported for type safety. | // is supported for type safety. | ||||
template<> | template<> | ||||
VertexArrayIterator<CVector3D> VertexArray::Attribute::GetIterator<CVector3D>() const | VertexArrayIterator<CVector3D> VertexArray::Attribute::GetIterator<CVector3D>() const | ||||
{ | { | ||||
ENSURE(vertexArray); | ENSURE(vertexArray); | ||||
ENSURE(type == GL_FLOAT); | ENSURE(type == GL_FLOAT); | ||||
▲ Show 20 Lines • Show All 172 Lines • ▼ Show 20 Lines | void VertexArray::Layout() | ||||
//debug_printf("Stride: %u\n", m_Stride); | //debug_printf("Stride: %u\n", m_Stride); | ||||
if (m_Stride) | if (m_Stride) | ||||
m_BackingStore = (char*)rtl_AllocateAligned(m_Stride * m_NumVertices, 16); | m_BackingStore = (char*)rtl_AllocateAligned(m_Stride * m_NumVertices, 16); | ||||
} | } | ||||
void VertexArray::PrepareForRendering() | void VertexArray::PrepareForRendering() | ||||
{ | { | ||||
m_VB->m_Owner->PrepareForRendering(m_VB); | m_VB->m_Owner->PrepareForRendering(m_VB.Get()); | ||||
} | } | ||||
// (Re-)Upload the attributes. | // (Re-)Upload the attributes. | ||||
// Create the VBO if necessary. | // Create the VBO if necessary. | ||||
void VertexArray::Upload() | void VertexArray::Upload() | ||||
{ | { | ||||
ENSURE(m_BackingStore); | ENSURE(m_BackingStore); | ||||
if (!m_VB) | if (!m_VB) | ||||
m_VB = g_VBMan.Allocate(m_Stride, m_NumVertices, m_Usage, m_Target, m_BackingStore); | m_VB = g_VBMan.AllocateChunk(m_Stride, m_NumVertices, m_Usage, m_Target, m_BackingStore); | ||||
if (!m_VB) | if (!m_VB) | ||||
{ | { | ||||
LOGERROR("Failed to allocate VBO for vertex array"); | LOGERROR("Failed to allocate VBO for vertex array"); | ||||
return; | return; | ||||
} | } | ||||
m_VB->m_Owner->UpdateChunkVertices(m_VB, m_BackingStore); | m_VB->m_Owner->UpdateChunkVertices(m_VB.Get(), m_BackingStore); | ||||
} | } | ||||
// Bind this array, returns the base address for calls to glVertexPointer etc. | // Bind this array, returns the base address for calls to glVertexPointer etc. | ||||
u8* VertexArray::Bind() | u8* VertexArray::Bind() | ||||
{ | { | ||||
if (!m_VB) | if (!m_VB) | ||||
return NULL; | return NULL; | ||||
Show All 31 Lines |
Wildfire Games · Phabricator