1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/* Copyright (C) 2024 Wildfire Games.
* This file is part of 0 A.D.
*
* 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
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "lib/alignment.h"
#include "lib/bits.h"
#include "lib/sysdep/rtl.h"
#include "maths/Vector3D.h"
#include "maths/Vector4D.h"
#include "ps/CLogger.h"
#include "graphics/Color.h"
#include "graphics/SColor.h"
#include "renderer/Renderer.h"
#include "renderer/VertexArray.h"
#include "renderer/VertexBuffer.h"
#include "renderer/VertexBufferManager.h"
namespace
{
uint32_t GetAttributeSize(const Renderer::Backend::Format format)
{
switch (format)
{
case Renderer::Backend::Format::R8G8B8A8_UNORM: FALLTHROUGH;
case Renderer::Backend::Format::R8G8B8A8_UINT:
return sizeof(u8) * 4;
case Renderer::Backend::Format::A8_UNORM:
return sizeof(u8);
case Renderer::Backend::Format::R16_UNORM: FALLTHROUGH;
case Renderer::Backend::Format::R16_UINT: FALLTHROUGH;
case Renderer::Backend::Format::R16_SINT: FALLTHROUGH;
case Renderer::Backend::Format::R16_SFLOAT:
return sizeof(u16);
case Renderer::Backend::Format::R16G16_UNORM: FALLTHROUGH;
case Renderer::Backend::Format::R16G16_UINT: FALLTHROUGH;
case Renderer::Backend::Format::R16G16_SINT: FALLTHROUGH;
case Renderer::Backend::Format::R16G16_SFLOAT:
return sizeof(u16) * 2;
case Renderer::Backend::Format::R16G16B16_SFLOAT:
return sizeof(u16) * 3;
case Renderer::Backend::Format::R16G16B16A16_SFLOAT:
return sizeof(u16) * 4;
case Renderer::Backend::Format::R32_SFLOAT:
return sizeof(float);
case Renderer::Backend::Format::R32G32_SFLOAT:
return sizeof(float) * 2;
case Renderer::Backend::Format::R32G32B32_SFLOAT:
return sizeof(float) * 3;
case Renderer::Backend::Format::R32G32B32A32_SFLOAT:
return sizeof(float) * 4;
default:
break;
};
return 0;
}
} // anonymous namespace
VertexArray::VertexArray(
const Renderer::Backend::IBuffer::Type type, const uint32_t usage)
: m_Type(type), m_Usage(usage)
{
}
VertexArray::~VertexArray()
{
Free();
}
// Free all resources on destruction or when a layout parameter changes
void VertexArray::Free()
{
rtl_FreeAligned(m_BackingStore);
m_BackingStore = 0;
m_VB.Reset();
}
// Set the number of vertices stored in the array
void VertexArray::SetNumberOfVertices(const size_t numberOfVertices)
{
if (numberOfVertices == m_NumberOfVertices)
return;
Free();
m_NumberOfVertices = numberOfVertices;
}
void VertexArray::SetMinimumAttributeAlignment(const uint32_t minimumAttributeAlignment)
{
ENSURE(minimumAttributeAlignment >= 4 || is_pow2(minimumAttributeAlignment));
if (minimumAttributeAlignment == m_MinimumAttributeAlignment)
return;
Free();
m_MinimumAttributeAlignment = minimumAttributeAlignment;
}
// Add vertex attributes like Position, Normal, UV
void VertexArray::AddAttribute(Attribute* attr)
{
// Attribute is supported is its size is greater than zero.
ENSURE(GetAttributeSize(attr->format) > 0 && "Unsupported attribute.");
attr->vertexArray = this;
m_Attributes.push_back(attr);
Free();
}
// Template specialization for GetIterator().
// We can put this into the source file because only a fixed set of types
// is supported for type safety.
template<>
VertexArrayIterator<CVector3D> VertexArray::Attribute::GetIterator<CVector3D>() const
{
ENSURE(vertexArray);
ENSURE(
format == Renderer::Backend::Format::R32G32B32_SFLOAT ||
format == Renderer::Backend::Format::R32G32B32A32_SFLOAT);
return vertexArray->MakeIterator<CVector3D>(this);
}
template<>
VertexArrayIterator<CVector4D> VertexArray::Attribute::GetIterator<CVector4D>() const
{
ENSURE(vertexArray);
ENSURE(format == Renderer::Backend::Format::R32G32B32A32_SFLOAT);
return vertexArray->MakeIterator<CVector4D>(this);
}
template<>
VertexArrayIterator<float[2]> VertexArray::Attribute::GetIterator<float[2]>() const
{
ENSURE(vertexArray);
ENSURE(format == Renderer::Backend::Format::R32G32_SFLOAT);
return vertexArray->MakeIterator<float[2]>(this);
}
template<>
VertexArrayIterator<SColor4ub> VertexArray::Attribute::GetIterator<SColor4ub>() const
{
ENSURE(vertexArray);
ENSURE(
format == Renderer::Backend::Format::R8G8B8A8_UNORM ||
format == Renderer::Backend::Format::R8G8B8A8_UINT);
return vertexArray->MakeIterator<SColor4ub>(this);
}
template<>
VertexArrayIterator<u16> VertexArray::Attribute::GetIterator<u16>() const
{
ENSURE(vertexArray);
ENSURE(format == Renderer::Backend::Format::R16_UINT);
return vertexArray->MakeIterator<u16>(this);
}
template<>
VertexArrayIterator<u16[2]> VertexArray::Attribute::GetIterator<u16[2]>() const
{
ENSURE(vertexArray);
ENSURE(format == Renderer::Backend::Format::R16G16_UINT);
return vertexArray->MakeIterator<u16[2]>(this);
}
template<>
VertexArrayIterator<u8> VertexArray::Attribute::GetIterator<u8>() const
{
ENSURE(vertexArray);
ENSURE(format == Renderer::Backend::Format::A8_UNORM);
return vertexArray->MakeIterator<u8>(this);
}
template<>
VertexArrayIterator<u8[4]> VertexArray::Attribute::GetIterator<u8[4]>() const
{
ENSURE(vertexArray);
ENSURE(
format == Renderer::Backend::Format::R8G8B8A8_UNORM ||
format == Renderer::Backend::Format::R8G8B8A8_UINT);
return vertexArray->MakeIterator<u8[4]>(this);
}
template<>
VertexArrayIterator<short> VertexArray::Attribute::GetIterator<short>() const
{
ENSURE(vertexArray);
ENSURE(format == Renderer::Backend::Format::R16_SINT);
return vertexArray->MakeIterator<short>(this);
}
template<>
VertexArrayIterator<short[2]> VertexArray::Attribute::GetIterator<short[2]>() const
{
ENSURE(vertexArray);
ENSURE(format == Renderer::Backend::Format::R16G16_SINT);
return vertexArray->MakeIterator<short[2]>(this);
}
static uint32_t RoundStride(uint32_t stride)
{
if (stride <= 0)
return 0;
if (stride <= 4)
return 4;
if (stride <= 8)
return 8;
if (stride <= 16)
return 16;
return Align<32>(stride);
}
// Re-layout by assigning offsets on a first-come first-serve basis,
// then round up to a reasonable stride.
// Backing store is also created here, backend buffers are created on upload.
void VertexArray::Layout()
{
Free();
m_Stride = 0;
for (ssize_t idx = m_Attributes.size()-1; idx >= 0; --idx)
{
Attribute* attr = m_Attributes[idx];
if (attr->format == Renderer::Backend::Format::UNDEFINED)
continue;
const uint32_t attrSize = GetAttributeSize(attr->format);
ENSURE(attrSize > 0);
attr->offset = m_Stride;
m_Stride += attrSize;
if (m_Type == Renderer::Backend::IBuffer::Type::VERTEX)
m_Stride = Align<4>(m_Stride);
if (m_MinimumAttributeAlignment > 0)
m_Stride = (m_Stride + m_MinimumAttributeAlignment - 1) & ~(m_MinimumAttributeAlignment - 1);
}
if (m_Type == Renderer::Backend::IBuffer::Type::VERTEX)
m_Stride = RoundStride(m_Stride);
if (m_Stride)
m_BackingStore = (char*)rtl_AllocateAligned(m_Stride * m_NumberOfVertices, 16);
}
void VertexArray::PrepareForRendering()
{
m_VB->m_Owner->PrepareForRendering(m_VB.Get());
}
// (Re-)Upload the attributes.
// Create the backend buffer if necessary.
void VertexArray::Upload()
{
ENSURE(m_BackingStore);
if (!m_VB)
{
m_VB = g_Renderer.GetVertexBufferManager().AllocateChunk(
m_Stride, m_NumberOfVertices, m_Type, m_Usage, m_BackingStore);
}
if (!m_VB)
{
LOGERROR("Failed to allocate backend buffer for vertex array");
return;
}
m_VB->m_Owner->UpdateChunkVertices(m_VB.Get(), m_BackingStore);
}
void VertexArray::UploadIfNeeded(
Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
{
m_VB->m_Owner->UploadIfNeeded(deviceCommandContext);
}
// Free the backing store to save some memory
void VertexArray::FreeBackingStore()
{
// In streaming modes, the backing store must be retained
ENSURE(!CVertexBuffer::UseStreaming(m_Usage));
rtl_FreeAligned(m_BackingStore);
m_BackingStore = 0;
}
VertexIndexArray::VertexIndexArray(const uint32_t usage) :
VertexArray(Renderer::Backend::IBuffer::Type::INDEX, usage)
{
m_Attr.format = Renderer::Backend::Format::R16_UINT;
AddAttribute(&m_Attr);
}
VertexArrayIterator<u16> VertexIndexArray::GetIterator() const
{
return m_Attr.GetIterator<u16>();
}
|