added geometry_pipeline hpp and cpp files.

This commit is contained in:
rachytski 2012-12-19 20:39:20 +03:00 committed by Alex Zolotarev
parent 2cbd81e6d8
commit ad5ff0d271
2 changed files with 378 additions and 0 deletions

View file

@ -0,0 +1,222 @@
#include "geometry_pipeline.hpp"
#include "resource_manager.hpp"
#include "resource_cache.hpp"
#include "opengl/buffer_object.hpp"
namespace graphics
{
GeometryPipeline::GeometryPipeline(shared_ptr<ResourceCache> const & cache,
EStorageType storageType,
shared_ptr<ResourceManager> const & rm,
VertexDecl const * decl)
: m_cache(cache),
m_currentVx(0),
m_currentIdx(0),
m_maxVx(0),
m_maxIdx(0),
m_rm(rm),
m_storageType(storageType),
m_decl(decl)
{}
GeometryPipeline::GeometryPipeline(ETextureType textureType,
EStorageType storageType,
shared_ptr<ResourceManager> const & rm,
VertexDecl const * decl,
uint8_t pipelineID)
: m_cache(new ResourceCache(rm, textureType, pipelineID)),
m_currentVx(0),
m_currentIdx(0),
m_maxVx(0),
m_maxIdx(0),
m_rm(rm),
m_storageType(storageType),
m_decl(decl)
{}
bool GeometryPipeline::hasStorage() const
{
return m_storage.isValid();
}
bool GeometryPipeline::hasTexture() const
{
return m_cache->texture() != 0;
}
void GeometryPipeline::checkStorage() const
{
if (!m_storage.isValid())
{
resetStorage();
m_storage = m_rm->storagePool(m_storageType)->Reserve();
if (m_storage.isValid())
{
m_maxVx = m_storage.m_vertices->size() / m_decl->elemSize();
m_maxIdx = m_storage.m_indices->size() / sizeof(unsigned short);
}
}
}
void GeometryPipeline::checkTexture() const
{
m_cache->checkTexture();
}
void GeometryPipeline::resetTexture()
{
m_cache->resetTexture();
}
void GeometryPipeline::resetStorage() const
{
m_storage = gl::Storage();
m_currentVx = 0;
m_currentIdx = 0;
m_maxIdx = 0;
m_maxVx = 0;
}
bool GeometryPipeline::hasRoom(unsigned verticesCount, unsigned indicesCount) const
{
checkStorage();
if (!hasStorage())
return false;
return ((m_currentVx + verticesCount <= m_maxVx)
&& (m_currentIdx + indicesCount <= m_maxIdx));
}
void GeometryPipeline::setVertexDecl(VertexDecl * decl)
{
m_decl = decl;
}
VertexDecl const * GeometryPipeline::vertexDecl() const
{
return m_decl;
}
shared_ptr<gl::Program> const & GeometryPipeline::program() const
{
return m_program;
}
void GeometryPipeline::setProgram(shared_ptr<gl::Program> const & prg)
{
m_program = prg;
}
uint8_t GeometryPipeline::pipelineID() const
{
return m_cache->pipelineID();
}
shared_ptr<gl::BaseTexture> const & GeometryPipeline::texture() const
{
return m_cache->texture();
}
gl::Storage const & GeometryPipeline::storage() const
{
return m_storage;
}
TTexturePool * GeometryPipeline::texturePool() const
{
return m_cache->texturePool();
}
TStoragePool * GeometryPipeline::storagePool() const
{
if (m_storageType != EInvalidStorage)
return m_rm->storagePool(m_storageType);
else
{
LOG(LERROR, ("no storagePool for such storageType", m_storageType));
return 0;
}
}
bool GeometryPipeline::hasGeometry() const
{
return m_storage.isValid() && (m_currentIdx != 0);
}
ResourceCache::TUploadQueue const & GeometryPipeline::uploadQueue() const
{
return m_cache->uploadQueue();
}
void GeometryPipeline::clearUploadQueue()
{
return m_cache->clearUploadQueue();
}
bool GeometryPipeline::hasUploadData() const
{
return m_cache->hasData();
}
unsigned GeometryPipeline::vxLeft() const
{
checkStorage();
if (!m_storage.isValid())
return (unsigned)-1;
return m_maxVx - m_currentVx;
}
unsigned GeometryPipeline::currentVx() const
{
return m_currentVx;
}
void GeometryPipeline::advanceVx(unsigned elemCnt)
{
m_currentVx += elemCnt;
}
void * GeometryPipeline::vxData()
{
return m_storage.m_vertices->data();
}
unsigned GeometryPipeline::idxLeft() const
{
checkStorage();
if (!m_storage.isValid())
return (unsigned)-1;
return m_maxIdx - m_currentIdx;
}
unsigned GeometryPipeline::currentIdx() const
{
return m_currentIdx;
}
void GeometryPipeline::advanceIdx(unsigned elemCnt)
{
m_currentIdx += elemCnt;
}
void * GeometryPipeline::idxData()
{
return m_storage.m_indices->data();
}
void GeometryPipeline::addHandlesOverflowFn(ResourceCache::handlesOverflowFn const & fn,
int priority)
{
m_cache->addHandlesOverflowFn(fn, priority);
}
shared_ptr<ResourceCache> const & GeometryPipeline::cache() const
{
return m_cache;
}
}

View file

@ -0,0 +1,156 @@
#pragma once
#include "../std/shared_ptr.hpp"
#include "opengl/storage.hpp"
#include "opengl/program.hpp"
#include "defines.hpp"
#include "vertex_decl.hpp"
#include "resource_cache.hpp"
#include "resource_manager.hpp"
namespace graphics
{
class ResourceCache;
class ResourceManager;
/// Single pipeline for batchable geometry.
class GeometryPipeline
{
private:
/// VertexDeclaration, which sets how the
/// vertex is interpreted into m_storage.m_vertexBuffer
VertexDecl const * m_decl;
/// Program, which is used for this pipeline.
/// Different pipelines could use different programs.
shared_ptr<gl::Program> m_program;
/// ResourceCache, which collects Resource's which are used
/// by objects while batching them into this pipeline
shared_ptr<ResourceCache> m_cache;
/// Storage, which contains vertex and index data
mutable gl::Storage m_storage;
/// current rendering position
/// @{
mutable size_t m_currentVx;
mutable size_t m_currentIdx;
mutable size_t m_maxVx;
mutable size_t m_maxIdx;
/// @}
/// resource manager to interact with pool's
shared_ptr<ResourceManager> m_rm;
/// type of storage pools
EStorageType m_storageType;
public:
typedef ResourceCache::handlesOverflowFn handlesOverflowFn;
/// Constructor of static GeometryPipeline from predefined
/// ResourceCache(possibly loaded from file)
GeometryPipeline(shared_ptr<ResourceCache> const & cache,
EStorageType storageType,
shared_ptr<ResourceManager> const & rm,
VertexDecl const * decl);
/// Constructor of dynamic GeometryPipeline from texture
/// and storage pool types and resourceManager
GeometryPipeline(ETextureType textureType,
EStorageType storageType,
shared_ptr<ResourceManager> const & rm,
VertexDecl const * decl,
uint8_t pipelineID);
/// Setting VertexDeclaration on this pipeline, which will define
/// how to interpret data into m_storage.m_vertexBuffer
void setVertexDecl(VertexDecl * decl);
VertexDecl const * vertexDecl() const;
/// Does this pipeline holds enough free room for the
/// specified amount of vertices and indices.
bool hasRoom(unsigned verticesCount,
unsigned indicesCount) const;
/// Get ResourceCache, associated with this pipeline
shared_ptr<ResourceCache> const & cache() const;
void setProgram(shared_ptr<gl::Program> const & prg);
shared_ptr<gl::Program> const & program() const;
/// ID of this pipeline
uint8_t pipelineID() const;
/// Working with texture and ResourceCache
/// @{
/// Checking, whether this pipeline
/// has texture associated with it.
bool hasTexture() const;
/// Reserve texture if there is no texture at this pipeline.
void checkTexture() const;
/// Get the texture associated with this pipeline.
shared_ptr<gl::BaseTexture> const & texture() const;
/// Get the texture pool this pipeline get it's texture from.
TTexturePool * texturePool() const;
/// Reset the texture, associated with this pipeline.
void resetTexture();
/// Does this pipeline has something to upload before render?
bool hasUploadData() const;
/// Get the queue of resources, which are waiting for upload
ResourceCache::TUploadQueue const & uploadQueue() const;
/// Clear upload queue
void clearUploadQueue();
/// @}
/// Working with Storage
/// @{
/// Checking, whether we should allocate the storage for this pipeline.
void checkStorage() const;
/// Checking, whether we have a valid storage
/// associated with this pipeline.
bool hasStorage() const;
/// getting storage associated with this pipeline
gl::Storage const & storage() const;
/// Get the storage pool this pipeline get it's storages from
TStoragePool * storagePool() const;
/// Reset storage and rendering info,
/// associated with this pipeline.
void resetStorage() const;
/// Does this pipeline has something to render?
bool hasGeometry() const;
/// How much more vertices with the current VertexDecl
/// could be fitted into this pipeline
unsigned vxLeft() const;
/// Current vertex number
unsigned currentVx() const;
/// Advance current vertex number
void advanceVx(unsigned elemCnt);
/// Get direct pointer to vertex data
void * vxData();
/// How much more indices could be fitted into this pipeline
unsigned idxLeft() const;
/// Current index number
unsigned currentIdx() const;
/// Advance current index number
void advanceIdx(unsigned elemCnt);
/// Get direct pointer to index data
void * idxData();
/// @}
/// Add function, which will be called upon handles overflow.
void addHandlesOverflowFn(ResourceCache::handlesOverflowFn const & fn,
int priority);
};
}