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
|
#include "Geometry.h"
#include "RenderResources.h"
#include "RenderStatistics.h"
#include <cstring> // for memcpy()
namespace nCine
{
Geometry::Geometry()
: primitiveType_(GL_TRIANGLES), firstVertex_(0), numVertices_(0), numElementsPerVertex_(2), firstIndex_(0), numIndices_(0),
hostVertexPointer_(nullptr), hostIndexPointer_(nullptr), vboUsageFlags_(0), sharedVboParams_(nullptr), iboUsageFlags_(0),
sharedIboParams_(nullptr), hasDirtyVertices_(true), hasDirtyIndices_(true)
{
}
Geometry::~Geometry()
{
#if defined(NCINE_PROFILING)
if (vbo_ != nullptr) {
RenderStatistics::RemoveCustomVbo(vbo_->GetSize());
}
if (ibo_ != nullptr) {
RenderStatistics::RemoveCustomIbo(ibo_->GetSize());
}
#endif
}
void Geometry::SetDrawParameters(GLenum primitiveType, GLint firstVertex, GLsizei numVertices)
{
primitiveType_ = primitiveType;
firstVertex_ = firstVertex;
numVertices_ = numVertices;
}
void Geometry::CreateCustomVbo(std::uint32_t numFloats, GLenum usage)
{
vbo_ = std::make_unique<GLBufferObject>(GL_ARRAY_BUFFER);
vbo_->BufferData(numFloats * sizeof(GLfloat), nullptr, usage);
vboUsageFlags_ = usage;
vboParams_.object = vbo_.get();
vboParams_.size = vbo_->GetSize();
vboParams_.offset = 0;
vboParams_.mapBase = nullptr;
#if defined(NCINE_PROFILING)
RenderStatistics::AddCustomVbo(vbo_->GetSize());
#endif
}
GLfloat* Geometry::AcquireVertexPointer(std::uint32_t numFloats, std::uint32_t numFloatsAlignment)
{
DEATH_ASSERT(vbo_ == nullptr);
hasDirtyVertices_ = true;
if (sharedVboParams_ != nullptr) {
vboParams_ = *sharedVboParams_;
} else {
const RenderBuffersManager::BufferTypes bufferType = RenderBuffersManager::BufferTypes::Array;
if (vboParams_.mapBase == nullptr) {
vboParams_ = RenderResources::GetBuffersManager().AcquireMemory(bufferType, numFloats * sizeof(GLfloat), numFloatsAlignment * sizeof(GLfloat));
}
}
return reinterpret_cast<GLfloat*>(vboParams_.mapBase + vboParams_.offset);
}
/*! This method can only be used when mapping of OpenGL buffers is available */
GLfloat* Geometry::AcquireVertexPointer()
{
DEATH_ASSERT(vbo_ != nullptr);
hasDirtyVertices_ = true;
if (vboParams_.mapBase == nullptr) {
const GLenum mapFlags = RenderResources::GetBuffersManager().Specs(RenderBuffersManager::BufferTypes::Array).mapFlags;
FATAL_ASSERT_MSG(mapFlags, "Mapping of OpenGL buffers is not available");
vboParams_.mapBase = static_cast<GLubyte*>(vbo_->MapBufferRange(0, vbo_->GetSize(), mapFlags));
}
return reinterpret_cast<GLfloat*>(vboParams_.mapBase);
}
void Geometry::ReleaseVertexPointer()
{
// Don't flush and unmap if the VBO is not custom
if (vbo_ != nullptr && vboParams_.mapBase != nullptr) {
vboParams_.object->FlushMappedBufferRange(vboParams_.offset, vboParams_.size);
vboParams_.object->Unmap();
}
vboParams_.mapBase = nullptr;
}
void Geometry::SetHostVertexPointer(const float* vertexPointer)
{
hasDirtyVertices_ = true;
hostVertexPointer_ = vertexPointer;
}
void Geometry::ShareVbo(const Geometry* geometry)
{
if (geometry == nullptr) {
sharedVboParams_ = nullptr;
} else if (geometry != this) {
vbo_.reset(nullptr);
sharedVboParams_ = &geometry->vboParams_;
}
}
void Geometry::CreateCustomIbo(std::uint32_t numIndices, GLenum usage)
{
ibo_ = std::make_unique<GLBufferObject>(GL_ELEMENT_ARRAY_BUFFER);
ibo_->BufferData(numIndices * sizeof(GLushort), nullptr, usage);
iboUsageFlags_ = usage;
iboParams_.object = ibo_.get();
iboParams_.size = ibo_->GetSize();
iboParams_.offset = 0;
iboParams_.mapBase = nullptr;
#if defined(NCINE_PROFILING)
RenderStatistics::AddCustomIbo(ibo_->GetSize());
#endif
}
GLushort* Geometry::AcquireIndexPointer(std::uint32_t numIndices)
{
DEATH_ASSERT(ibo_ == nullptr);
hasDirtyIndices_ = true;
if (sharedIboParams_ != nullptr) {
iboParams_ = *sharedIboParams_;
} else {
const RenderBuffersManager::BufferTypes bufferType = RenderBuffersManager::BufferTypes::ElementArray;
if (iboParams_.mapBase == nullptr) {
iboParams_ = RenderResources::GetBuffersManager().AcquireMemory(bufferType, numIndices * sizeof(GLushort));
}
}
return reinterpret_cast<GLushort*>(iboParams_.mapBase + iboParams_.offset);
}
/*! This method can only be used when mapping of OpenGL buffers is available */
GLushort* Geometry::AcquireIndexPointer()
{
DEATH_ASSERT(ibo_ != nullptr);
hasDirtyIndices_ = true;
if (iboParams_.mapBase == nullptr) {
const GLenum mapFlags = RenderResources::GetBuffersManager().Specs(RenderBuffersManager::BufferTypes::ElementArray).mapFlags;
FATAL_ASSERT_MSG(mapFlags, "Mapping of OpenGL buffers is not available");
iboParams_.mapBase = static_cast<GLubyte*>(ibo_->MapBufferRange(0, ibo_->GetSize(), mapFlags));
}
return reinterpret_cast<GLushort*>(iboParams_.mapBase);
}
void Geometry::ReleaseIndexPointer()
{
// Don't flush and unmap if the IBO is not custom
if (ibo_ != nullptr && iboParams_.mapBase != nullptr) {
iboParams_.object->FlushMappedBufferRange(iboParams_.offset, iboParams_.size);
iboParams_.object->Unmap();
}
iboParams_.mapBase = nullptr;
}
void Geometry::SetHostIndexPointer(const GLushort* indexPointer)
{
hasDirtyIndices_ = true;
hostIndexPointer_ = indexPointer;
}
void Geometry::ShareIbo(const Geometry* geometry)
{
if (geometry == nullptr) {
sharedIboParams_ = nullptr;
} else if (geometry != this) {
ibo_.reset(nullptr);
sharedIboParams_ = &geometry->iboParams_;
}
}
void Geometry::Bind()
{
if (vboParams_.object != nullptr) {
vboParams_.object->Bind();
}
}
void Geometry::Draw(GLsizei numInstances)
{
const GLint vboOffset = static_cast<GLint>(GetVboParams().offset / numElementsPerVertex_ / sizeof(GLfloat)) + firstVertex_;
void* iboOffsetPtr = nullptr;
if (numIndices_ > 0) {
iboOffsetPtr = reinterpret_cast<void*>(GetIboParams().offset + firstIndex_ * sizeof(GLushort));
}
if (numInstances == 0) {
if (numIndices_ > 0) {
#if (defined(WITH_OPENGLES) && !GL_ES_VERSION_3_2) || defined(DEATH_TARGET_EMSCRIPTEN)
glDrawElements(primitiveType_, numIndices_, GL_UNSIGNED_SHORT, iboOffsetPtr);
#else
glDrawElementsBaseVertex(primitiveType_, numIndices_, GL_UNSIGNED_SHORT, iboOffsetPtr, vboOffset);
#endif
} else {
glDrawArrays(primitiveType_, vboOffset, numVertices_);
}
} else if (numInstances > 0) {
if (numIndices_ > 0) {
#if (defined(WITH_OPENGLES) && !GL_ES_VERSION_3_2) || defined(DEATH_TARGET_EMSCRIPTEN)
glDrawElementsInstanced(primitiveType_, numIndices_, GL_UNSIGNED_SHORT, iboOffsetPtr, numInstances);
#else
glDrawElementsInstancedBaseVertex(primitiveType_, numIndices_, GL_UNSIGNED_SHORT, iboOffsetPtr, numInstances, vboOffset);
#endif
} else {
glDrawArraysInstanced(primitiveType_, vboOffset, numVertices_, numInstances);
}
}
}
void Geometry::CommitVertices()
{
if (hostVertexPointer_ != nullptr && hasDirtyVertices_) {
// Checking if the common VBO is allowed to use mapping and do the same for the custom one
const GLenum mapFlags = RenderResources::GetBuffersManager().Specs(RenderBuffersManager::BufferTypes::Array).mapFlags;
const std::uint32_t numFloats = numVertices_ * numElementsPerVertex_;
if (mapFlags == 0 && vbo_ != nullptr) {
// Using buffer orphaning + `glBufferSubData()` when having a custom VBO with no mapping available
vbo_->BufferData(vboParams_.size, nullptr, vboUsageFlags_);
vbo_->BufferSubData(vboParams_.offset, vboParams_.size, hostVertexPointer_);
} else {
GLfloat* vertices = vbo_ ? AcquireVertexPointer() : AcquireVertexPointer(numFloats, numElementsPerVertex_);
memcpy(vertices, hostVertexPointer_, numFloats * sizeof(GLfloat));
ReleaseVertexPointer();
}
// The dirty flag is only useful with a custom VBO. If the render command uses the common one, it must always copy vertices.
if (vbo_ != nullptr) {
hasDirtyVertices_ = false;
}
}
}
void Geometry::CommitIndices()
{
if (hostIndexPointer_ != nullptr && hasDirtyIndices_) {
// Checking if the common IBO is allowed to use mapping and do the same for the custom one
const GLenum mapFlags = RenderResources::GetBuffersManager().Specs(RenderBuffersManager::BufferTypes::ElementArray).mapFlags;
if (mapFlags == 0 && ibo_ != nullptr) {
// Using buffer orphaning + `glBufferSubData()` when having a custom IBO with no mapping available
ibo_->BufferData(iboParams_.size, nullptr, iboUsageFlags_);
ibo_->BufferSubData(iboParams_.offset, iboParams_.size, hostIndexPointer_);
} else {
GLushort* indices = ibo_ ? AcquireIndexPointer() : AcquireIndexPointer(numIndices_);
memcpy(indices, hostIndexPointer_, numIndices_ * sizeof(GLushort));
ReleaseIndexPointer();
}
// The dirty flag is only useful with a custom IBO. If the render command uses the common one, it must always copy indices.
if (ibo_ != nullptr) {
hasDirtyIndices_ = false;
}
}
}
}
|