cleaning up the IndexedContainer in its own file and removing warnings

This commit is contained in:
samcake 2016-09-06 16:24:03 -07:00
parent 0c45627d94
commit 1165c54f67
4 changed files with 160 additions and 145 deletions

View file

@ -177,7 +177,7 @@ protected:
public:
using Size = Resource::Size;
using Index = int;
using Index = int32_t;
BufferPointer _buffer;
Size _offset { 0 };

View file

@ -257,12 +257,12 @@ model::MeshPointer DeferredLightingEffect::getPointLightMesh() {
auto solid = geometry::icosahedron();
solid.fitDimension(1.05f); // scaled to 1.05 meters, it will be scaled by the shader accordingly to the light size
int verticesSize = solid.vertices.size() * 3 * sizeof(float);
int verticesSize = (int) (solid.vertices.size() * 3 * sizeof(float));
float* vertexData = (float*) solid.vertices.data();
_pointLightMesh->setVertexBuffer(gpu::BufferView(new gpu::Buffer(verticesSize, (gpu::Byte*) vertexData), gpu::Element::VEC3F_XYZ));
auto nbIndices = solid.faces.size() * 3;
int nbIndices = (int) solid.faces.size() * 3;
gpu::uint16* indexData = new gpu::uint16[nbIndices];
gpu::uint16* index = indexData;
@ -628,11 +628,11 @@ void RenderDeferredLocals::run(const render::SceneContextPointer& sceneContext,
if (points && !srcPointLights.empty()) {
memcpy(lightIndices.data() + (lightIndices[0] + 1), srcPointLights.data(), srcPointLights.size() * sizeof(int));
lightIndices[0] += srcPointLights.size();
lightIndices[0] += (int) srcPointLights.size();
}
if (spots && !srcSpotLights.empty()) {
memcpy(lightIndices.data() + (lightIndices[0] + 1), srcSpotLights.data(), srcSpotLights.size() * sizeof(int));
lightIndices[0] += srcSpotLights.size();
lightIndices[0] += (int) srcSpotLights.size();
}
if (lightIndices[0] > 0) {

View file

@ -14,6 +14,7 @@
#include <set>
#include <unordered_map>
#include <render/IndexedContainer.h>
#include "gpu/Framebuffer.h"
@ -21,150 +22,14 @@
class ViewFrustum;
namespace indexed_elements {
using Index = int32_t;
const Index MAXIMUM_INDEX { 1 << 30 };
const Index INVALID_INDEX { -1 };
using Indices = std::vector< Index >;
template <Index MaxNumElements = MAXIMUM_INDEX>
class Allocator {
public:
Allocator() {}
Indices _freeIndices;
Index _nextNewIndex { 0 };
bool checkIndex(Index index) const { return ((index >= 0) && (index < _nextNewIndex)); }
Index getNumIndices() const { return _nextNewIndex - (Index) _freeIndices.size(); }
Index allocateIndex() {
if (_freeIndices.empty()) {
Index index = _nextNewIndex;
if (index >= MaxNumElements) {
// abort! we are trying to go overboard with the total number of allocated elements
assert(false);
// This should never happen because Bricks are allocated along with the cells and there
// is already a cap on the cells allocation
return INVALID_INDEX;
}
_nextNewIndex++;
return index;
} else {
Index index = _freeIndices.back();
_freeIndices.pop_back();
return index;
}
}
void freeIndex(Index index) {
if (checkIndex(index)) {
_freeIndices.push_back(index);
}
}
void clear() {
_freeIndices.clear();
_nextNewIndex = 0;
}
};
template <class T, Index MaxNumElements = MAXIMUM_INDEX>
class IndexedVector {
Allocator<MaxNumElements> _allocator;
public:
using Element = T;
using Elements = std::vector<T>;
Elements _elements;
bool checkIndex(Index index) const { return _allocator.checkIndex(index); };
Index getNumElements() const { return _allocator.getNumIndices(); }
Index newElement(const Element& e) {
Index index = _allocator.allocateIndex();
if (index != INVALID_INDEX) {
if (index < _elements.size()) {
_elements.emplace(_elements.begin() + index, e);
} else {
assert(index == _elements.size());
_elements.emplace_back(e);
}
}
return index;
}
const Element& freeElement(Index index) {
_allocator.freeIndex(index);
return _elements[index];
}
const Element& get(Index index) const {
return _elements[index];
}
Element& edit(Index index) {
return _elements[index];
}
};
template <class T, Index MaxNumElements = MAXIMUM_INDEX>
class IndexedPointerVector {
Allocator<MaxNumElements> _allocator;
public:
using Data = T;
using ElementPtr = std::shared_ptr<Data>;
using Elements = std::vector<ElementPtr>;
Elements _elements;
bool checkIndex(Index index) const { return _allocator.checkIndex(index); };
Index getNumElements() const { return _allocator.getNumIndices(); }
Index newElement(const ElementPtr& e) {
Index index = _allocator.allocateIndex();
if (index != INVALID_INDEX) {
if (index < _elements.size()) {
_elements.emplace(_elements.begin() + index, e);
} else {
assert(index == _elements.size());
_elements.emplace_back(e);
}
}
return index;
}
ElementPtr freeElement(Index index) {
ElementPtr freed;
if (checkIndex(index)) {
_allocator.freeIndex(index);
freed = _elements[index];
_elements[index].reset(); // really forget it
}
return freed;
}
ElementPtr get(Index index) const {
if (checkIndex(index)) {
return _elements[index];
} else {
return ElementPtr();
}
}
};
};
// Light stage to set up light-related rendering tasks
class LightStage {
public:
using Index = indexed_elements::Index;
static const Index INVALID_INDEX { indexed_elements::INVALID_INDEX };
using Index = render::indexed_container::Index;
static const Index INVALID_INDEX { render::indexed_container::INVALID_INDEX };
using LightPointer = model::LightPointer;
using Lights = indexed_elements::IndexedPointerVector<model::Light>;
using Lights = render::indexed_container::IndexedPointerVector<model::Light>;
using LightMap = std::unordered_map<LightPointer, Index>;
class Shadow {
@ -202,7 +67,7 @@ public:
friend class Light;
};
using ShadowPointer = std::shared_ptr<Shadow>;
using Shadows = indexed_elements::IndexedPointerVector<Shadow>;
using Shadows = render::indexed_container::IndexedPointerVector<Shadow>;
struct Desc {
Index shadowId { INVALID_INDEX };

View file

@ -0,0 +1,150 @@
//
// IndexedContainer.h
// render
//
// Created by Sam Gateau on 9/6/2016.
// Copyright 2016 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_render_IndexedContainer_h
#define hifi_render_IndexedContainer_h
#include <vector>
namespace render {
namespace indexed_container {
using Index = int32_t;
const Index MAXIMUM_INDEX { 1 << 30 };
const Index INVALID_INDEX { -1 };
using Indices = std::vector< Index >;
template <Index MaxNumElements = MAXIMUM_INDEX>
class Allocator {
public:
Allocator() {}
Indices _freeIndices;
Index _nextNewIndex { 0 };
bool checkIndex(Index index) const { return ((index >= 0) && (index < _nextNewIndex)); }
Index getNumIndices() const { return _nextNewIndex - (Index) _freeIndices.size(); }
Index allocateIndex() {
if (_freeIndices.empty()) {
Index index = _nextNewIndex;
if (index >= MaxNumElements) {
// abort! we are trying to go overboard with the total number of allocated elements
assert(false);
// This should never happen because Bricks are allocated along with the cells and there
// is already a cap on the cells allocation
return INVALID_INDEX;
}
_nextNewIndex++;
return index;
} else {
Index index = _freeIndices.back();
_freeIndices.pop_back();
return index;
}
}
void freeIndex(Index index) {
if (checkIndex(index)) {
_freeIndices.push_back(index);
}
}
void clear() {
_freeIndices.clear();
_nextNewIndex = 0;
}
};
template <class T, Index MaxNumElements = MAXIMUM_INDEX>
class IndexedVector {
Allocator<MaxNumElements> _allocator;
public:
using Element = T;
using Elements = std::vector<T>;
Elements _elements;
bool checkIndex(Index index) const { return _allocator.checkIndex(index); };
Index getNumElements() const { return _allocator.getNumIndices(); }
Index newElement(const Element& e) {
Index index = _allocator.allocateIndex();
if (index != INVALID_INDEX) {
if (index < _elements.size()) {
_elements.emplace(_elements.begin() + index, e);
} else {
assert(index == _elements.size());
_elements.emplace_back(e);
}
}
return index;
}
const Element& freeElement(Index index) {
_allocator.freeIndex(index);
return _elements[index];
}
const Element& get(Index index) const {
return _elements[index];
}
Element& edit(Index index) {
return _elements[index];
}
};
template <class T, Index MaxNumElements = MAXIMUM_INDEX>
class IndexedPointerVector {
Allocator<MaxNumElements> _allocator;
public:
using Data = T;
using ElementPtr = std::shared_ptr<Data>;
using Elements = std::vector<ElementPtr>;
Elements _elements;
bool checkIndex(Index index) const { return _allocator.checkIndex(index); };
Index getNumElements() const { return _allocator.getNumIndices(); }
Index newElement(const ElementPtr& e) {
Index index = _allocator.allocateIndex();
if (index != INVALID_INDEX) {
if (index < _elements.size()) {
_elements.emplace(_elements.begin() + index, e);
} else {
assert(index == _elements.size());
_elements.emplace_back(e);
}
}
return index;
}
ElementPtr freeElement(Index index) {
ElementPtr freed;
if (checkIndex(index)) {
_allocator.freeIndex(index);
freed = _elements[index];
_elements[index].reset(); // really forget it
}
return freed;
}
ElementPtr get(Index index) const {
if (checkIndex(index)) {
return _elements[index];
} else {
return ElementPtr();
}
}
};
};
}
#endif