From d8897e798e9ffd76dc2d899a44febcd0e77d67b6 Mon Sep 17 00:00:00 2001 From: dibenedetto Date: Mon, 7 May 2012 13:18:05 +0000 Subject: [PATCH] main internal changes to glw and some public interface changes that affect naming. CHANGES ARE NOT BACKWARD COMPATIBLE. --- wrap/glw/bookkeeping.h | 606 +++++++++++----------------------- wrap/glw/buffer.h | 675 +++++++++++++++++++++++++++++++++----- wrap/glw/common.h | 12 +- wrap/glw/context.h | 473 ++++++++++++++++---------- wrap/glw/fragmentshader.h | 133 ++++++-- wrap/glw/framebuffer.h | 456 +++++++++++++++++++++---- wrap/glw/geometryshader.h | 133 ++++++-- wrap/glw/object.h | 247 +++++++++++--- wrap/glw/objectdeleter.h | 28 ++ wrap/glw/program.h | 253 ++++++++++---- wrap/glw/renderable.h | 149 ++++++++- wrap/glw/renderbuffer.h | 245 +++++++++----- wrap/glw/shader.h | 205 +++++++++--- wrap/glw/texture.h | 167 +++++++--- wrap/glw/texture2d.h | 230 ++++++++----- wrap/glw/utility.h | 2 +- wrap/glw/vertexshader.h | 131 ++++++-- 17 files changed, 2930 insertions(+), 1215 deletions(-) create mode 100644 wrap/glw/objectdeleter.h diff --git a/wrap/glw/bookkeeping.h b/wrap/glw/bookkeeping.h index 04a87b38..833f7167 100644 --- a/wrap/glw/bookkeeping.h +++ b/wrap/glw/bookkeeping.h @@ -6,29 +6,85 @@ namespace glw { -class Context; - namespace detail { -template -class SharedObjectBase -{ - friend class Context; + struct NoBase { }; +template struct DefaultDeleter { void operator () (T * t) { delete t; } }; +template struct BaseOf { typedef NoBase Type; }; + +template struct RootOfType { typedef typename RootOfType::Type>::Type Type; }; +template struct RootOfType { typedef T Type; }; +template struct RootOf { typedef typename RootOfType::Type>::Type Type; }; + +template struct DeleterOfType { typedef typename DeleterOfType::Type>::Type Type; }; +template struct DeleterOfType { typedef DefaultDeleter Type; }; +template struct DeleterOf { typedef typename DeleterOfType::Type>::Type Type; }; + +template +class RefCountedObject : public RefCountedObject::Type> +{ public: - typedef void BaseType; - typedef SharedObjectBase ThisType; - typedef TObject ObjectType; + typedef RefCountedObject::Type> BaseType; + typedef RefCountedObject ThisType; + typedef TObject ObjectType; + typedef TDeleter DeleterType; + typedef TBaseObject BaseObjectType; + + RefCountedObject(ObjectType * object, const DeleterType & deleter) + : BaseType(object, deleter) + { + ; + } + + const ObjectType * object(void) const + { + return static_cast(BaseType::object()); + } + + ObjectType * object(void) + { + return static_cast(BaseType::object()); + } +}; + +template +class RefCountedObject +{ + public: + + typedef void BaseType; + typedef RefCountedObject ThisType; + typedef TObject ObjectType; + typedef TDeleter DeleterType; + typedef NoBase BaseObjectType; + + RefCountedObject(ObjectType * object, const DeleterType & deleter) + : m_object (object) + , m_refCount (0) + , m_deleter (deleter) + { + GLW_ASSERT(this->m_object != 0); + } + + ~RefCountedObject(void) + { + this->destroyObject(); + } bool isNull(void) const { return (this->m_object == 0); } - void setNull(void) + void setNull(bool deleteObject) { + if (deleteObject) + { + this->destroyObject(); + } this->m_object = 0; } @@ -42,6 +98,16 @@ class SharedObjectBase return this->m_object; } + const DeleterType & deleter(void) const + { + return this->m_deleter; + } + + DeleterType & deleter(void) + { + return this->m_deleter; + } + void ref(void) { this->m_refCount++; @@ -53,400 +119,86 @@ class SharedObjectBase this->m_refCount--; if (this->m_refCount == 0) { - if (this->m_object != 0) - { - this->signalDestruction(); - } delete this; } } - protected: + int refCount(void) const + { + return this->m_refCount; + } + + private: - Context * m_context; ObjectType * m_object; int m_refCount; + DeleterType m_deleter; - SharedObjectBase(Context * Context, ObjectType * Object) - : m_context (Context) - , m_object (Object) - , m_refCount (0) - { - GLW_ASSERT(this->m_context != 0); - GLW_ASSERT(this->m_object != 0); - } - - private: - - SharedObjectBase(const ThisType & other); + RefCountedObject(const ThisType & other); ThisType & operator = (const ThisType & other); - inline void signalDestruction(void); -}; - -template -class SharedObject; - -template -class SharedObjectTraits -{ - public: - - typedef void BaseType; - typedef SharedObjectTraits ThisType; - typedef TObject ObjectType; - typedef TObjectBase ObjectBaseType; - typedef SharedObject SharedObjectBaseType; -}; - -template -class SharedObjectTraits -{ - public: - - typedef void BaseType; - typedef SharedObjectTraits ThisType; - typedef TObject ObjectType; - typedef void ObjectBaseType; - typedef SharedObjectBase SharedObjectBaseType; -}; - -template -class SharedObject : public SharedObjectTraits::SharedObjectBaseType -{ - friend class Context; - - public: - - typedef typename SharedObjectTraits::SharedObjectBaseType BaseType; - typedef SharedObject ThisType; - typedef TObject ObjectType; - - const ObjectType * object(void) const + void destroyObject(void) { - return static_cast(BaseType::object()); - } - - ObjectType * object(void) - { - return static_cast(BaseType::object()); - } - - protected: - - SharedObject(Context * Context, ObjectType * Object) - : BaseType(Context, Object) - { - ; - } - - private: - - SharedObject(const ThisType & other); - ThisType & operator = (const ThisType & other); -}; - -template -class SharedObjectBinding -{ - friend class Context; - - public: - - typedef void BaseType; - typedef SharedObjectBinding ThisType; - typedef TObject ObjectType; - typedef ObjectType UnsafeType; - - ~SharedObjectBinding(void) - { - this->detach(); - } - - bool isNull(void) const - { - if (this->m_shared == 0) return true; - return this->m_shared->isNull(); - } - - void setNull(void) - { - this->m_shared = 0; - } - - const ObjectType * object(void) const - { - GLW_ASSERT(!this->isNull()); - const ObjectType * obj = this->m_shared->object(); - obj->setBinding(this->m_target, this->m_unit); - return obj; - } - - ObjectType * object(void) - { - GLW_ASSERT(!this->isNull()); - ObjectType * obj = this->m_shared->object(); - obj->setBinding(this->m_target, this->m_unit); - return obj; - } - - GLenum target(void) const - { - return this->m_target; - } - - GLint unit(void) const - { - return this->m_unit; - } - - void ref(void) - { - this->m_refCount++; - } - - void unref(void) - { - GLW_ASSERT(this->m_refCount > 0); - this->m_refCount--; - if (this->m_refCount == 0) - { - delete this; - } - } - - protected: - - typedef SharedObject SharedObjectType; - - SharedObjectBinding(SharedObjectType * shared, GLenum target, GLint unit) - : m_shared (0) - , m_refCount (0) - , m_target (target) - , m_unit (unit) - { - this->attach(shared); - } - - private: - - SharedObjectType * m_shared; - int m_refCount; - GLenum m_target; - GLint m_unit; - - SharedObjectBinding(const ThisType & other); - ThisType & operator = (const ThisType & other); - - void attach(SharedObjectType * shared) - { - this->detach(); - this->m_shared = shared; - if (this->m_shared != 0) - { - this->m_shared->ref(); - } - } - - void detach(void) - { - if (this->m_shared == 0) return; - this->m_shared->unref(); - this->m_shared = 0; + if (this->m_object == 0) return; + this->m_deleter(this->m_object); + this->m_object = 0; } }; -template -class SafeHandleBase -{ - friend class Context; +template struct RefCountedObjectTraits { typedef RefCountedObject::Type, typename BaseOf::Type> Type; }; - public: - - typedef void BaseType; - typedef SafeHandleBase ThisType; - typedef TObject ObjectType; - typedef typename ObjectType::SafeType SafeType; - - SafeHandleBase(void) - : m_shared(0) - { - ; - } - - SafeHandleBase(const ThisType & other) - : m_shared(0) - { - this->attach(other.shared()); - } - - ~SafeHandleBase(void) - { - this->detach(); - } - - bool isNull(void) const - { - if (this->m_shared == 0) return true; - return this->m_shared->isNull(); - } - - void setNull(void) - { - this->detach(); - } - - const SafeType * operator -> (void) const - { - GLW_ASSERT(!this->isNull()); - return this->m_shared->object(); - } - - SafeType * operator -> (void) - { - GLW_ASSERT(!this->isNull()); - return this->m_shared->object(); - } - - ThisType & operator = (const ThisType & other) - { - this->attach(other.shared()); - return (*this); - } - - protected: - - typedef SharedObject SharedObjectType; - - SafeHandleBase(SharedObjectType * shared) - : m_shared(0) - { - this->attach(shared); - } - - const ObjectType * object(void) const - { - if (this->m_shared == 0) return 0; - return this->m_shared->object(); - } - - ObjectType * object(void) - { - if (this->m_shared == 0) return 0; - return this->m_shared->object(); - } - - SharedObjectType * shared(void) const - { - return this->m_shared; - } - - private: - - SharedObjectType * m_shared; - - void attach(SharedObjectType * shared) - { - this->detach(); - this->m_shared = shared; - if (this->m_shared != 0) - { - this->m_shared->ref(); - } - } - - void detach(void) - { - if (this->m_shared == 0) return; - this->m_shared->unref(); - this->m_shared = 0; - } -}; - -template -class SafeHandle; - -template -class SafeHandleTraits +template +class ObjectSharedPointer : public ObjectSharedPointer::Type> { public: - typedef void BaseType; - typedef SafeHandleTraits ThisType; - typedef TObject ObjectType; - typedef TObjectBase ObjectBaseType; - typedef SafeHandle SafeHandleBaseType; -}; + typedef ObjectSharedPointer::Type> BaseType; + typedef ObjectSharedPointer ThisType; + typedef TObject ObjectType; + typedef TDeleter DeleterType; + typedef TBaseObject BaseObjectType; + typedef RefCountedObject RefCountedObjectType; -template -class SafeHandleTraits -{ - public: - - typedef void BaseType; - typedef SafeHandleTraits ThisType; - typedef TObject ObjectType; - typedef void ObjectBaseType; - typedef SafeHandleBase SafeHandleBaseType; -}; - -template -class SafeHandle : public SafeHandleTraits::SafeHandleBaseType -{ - friend class Context; - - public: - - typedef typename SafeHandleTraits::SafeHandleBaseType BaseType; - typedef SafeHandle ThisType; - typedef TObject ObjectType; - typedef typename ObjectType::SafeType SafeType; - - SafeHandle(void) + ObjectSharedPointer(void) : BaseType() { ; } - SafeHandle(const ThisType & other) + ObjectSharedPointer(const ThisType & other) : BaseType(other) { ; } - const SafeType * operator -> (void) const - { - return dynamic_cast(BaseType:: operator ->()); - } - - SafeType * operator -> (void) - { - return dynamic_cast(BaseType:: operator ->()); - } - - /* - ThisType & operator = (const ThisType & other) - { - this->attach(other.shared()); - return (*this); - } - */ - - operator bool (void) const - { - return !this->isNull(); - } - - protected: - - typedef SharedObject SharedObjectType; - - SafeHandle(SharedObjectType * shared) - : BaseType(shared) + ObjectSharedPointer(RefCountedObjectType * refObject) + : BaseType(refObject) { ; } + const ObjectType & operator * (void) const + { + return (*(this->object())); + } + + ObjectType & operator * (void) + { + return (*(this->object())); + } + + const ObjectType * operator -> (void) const + { + return this->object(); + } + + ObjectType * operator -> (void) + { + return this->object(); + } + + protected: + const ObjectType * object(void) const { return static_cast(BaseType::object()); @@ -457,45 +209,51 @@ class SafeHandle : public SafeHandleTraits: return static_cast(BaseType::object()); } - SharedObjectType * shared(void) const + RefCountedObjectType * refObject(void) const { - return static_cast(BaseType::shared()); + return static_cast(BaseType::refObject()); } }; -template -class UnsafeHandle +template +class ObjectSharedPointer { - friend class Context; - public: - typedef void BaseType; - typedef UnsafeHandle ThisType; - typedef TObject ObjectType; - typedef ObjectType UnsafeType; + typedef void BaseType; + typedef ObjectSharedPointer ThisType; + typedef TObject ObjectType; + typedef TDeleter DeleterType; + typedef NoBase BaseObjectType; + typedef RefCountedObject RefCountedObjectType; - UnsafeHandle(void) - : m_shared(0) + ObjectSharedPointer(void) + : m_refObject(0) { ; } - UnsafeHandle(const ThisType & other) - : m_shared(0) + ObjectSharedPointer(const ThisType & other) + : m_refObject(0) { - this->attach(other.shared()); + this->attach(other.refObject()); } - ~UnsafeHandle(void) + ObjectSharedPointer(RefCountedObjectType * refObject) + : m_refObject(0) + { + this->attach(refObject); + } + + ~ObjectSharedPointer(void) { this->detach(); } bool isNull(void) const { - if (this->m_shared == 0) return true; - return this->m_shared->isNull(); + if (this->m_refObject == 0) return true; + return this->m_refObject->isNull(); } void setNull(void) @@ -503,78 +261,80 @@ class UnsafeHandle this->detach(); } - const UnsafeType * operator -> (void) const + const ObjectType & operator * (void) const { - GLW_ASSERT(!this->isNull()); - return this->m_shared->object(); + return (*(this->object())); } - UnsafeType * operator -> (void) + ObjectType & operator * (void) { - GLW_ASSERT(!this->isNull()); - return this->m_shared->object(); + return (*(this->object())); } - ThisType & operator = (const ThisType & other) + const ObjectType * operator -> (void) const { - this->attach(other.shared()); - return (*this); + return this->object(); + } + + ObjectType * operator -> (void) + { + return this->object(); } operator bool (void) const { - return !this->isNull(); + return (!this->isNull()); + } + + ThisType & operator = (const ThisType & other) + { + this->attach(other.refObject()); + return (*this); } protected: - typedef SharedObjectBinding SharedObjectBindingType; - - UnsafeHandle(SharedObjectBindingType * shared) - : m_shared(0) + const ObjectType * object(void) const { - this->attach(shared); + GLW_ASSERT(!this->isNull()); + return this->m_refObject->object(); } - const ObjectType * Object(void) const + ObjectType * object(void) { - if (this->m_shared == 0) return true; - return this->m_shared->object(); + GLW_ASSERT(!this->isNull()); + return this->m_refObject->object(); } - ObjectType * Object(void) + RefCountedObjectType * refObject(void) const { - if (this->m_shared == 0) return true; - return this->m_shared->object(); + return this->m_refObject; } private: - SharedObjectBindingType * m_shared; + RefCountedObjectType * m_refObject; - void attach(SharedObjectBindingType * shared) + void attach(RefCountedObjectType * reObject) { this->detach(); - this->m_shared = shared; - if (this->m_shared != 0) + this->m_refObject = reObject; + if (this->m_refObject != 0) { - this->m_shared->ref(); + this->m_refObject->ref(); } } void detach(void) { - if (this->m_shared == 0) return; - this->m_shared->unref(); - this->m_shared = 0; - } - - SharedObjectBindingType * shared(void) const - { - return this->m_shared; + if (this->m_refObject == 0) return; + this->m_refObject->unref(); + this->m_refObject = 0; } }; +template struct ObjectSharedPointerTraits { typedef ObjectSharedPointer::Type>::Type, typename BaseOf::Type> Type; }; + }; }; diff --git a/wrap/glw/buffer.h b/wrap/glw/buffer.h index 6d06bd14..fce656df 100644 --- a/wrap/glw/buffer.h +++ b/wrap/glw/buffer.h @@ -18,27 +18,52 @@ class BufferArguments : public ObjectArguments const void * data; BufferArguments(void) + : BaseType () + , size (0) + , usage (GL_STATIC_DRAW) + , data (0) { this->clear(); } + BufferArguments(GLsizeiptr aSize, GLenum aUsage = GL_STATIC_DRAW, const void * aData = 0) + : BaseType () + , size (aSize) + , usage (aUsage) + , data (aData) + { + ; + } + void clear(void) { BaseType::clear(); this->size = 0; - this->usage = GL_NONE; + this->usage = GL_STATIC_DRAW; this->data = 0; } }; -class SafeBuffer : public virtual SafeObject +class Buffer : public Object { + friend class Context; + public: - typedef SafeObject BaseType; - typedef SafeBuffer ThisType; + typedef Object BaseType; + typedef Buffer ThisType; - GLsizei size(void) const + virtual ~Buffer(void) + { + this->destroy(); + } + + virtual Type type(void) const + { + return BufferType; + } + + GLsizeiptr size(void) const { return this->m_size; } @@ -48,163 +73,631 @@ class SafeBuffer : public virtual SafeObject return this->m_usage; } - protected: - - GLsizeiptr m_size; - GLenum m_usage; - - SafeBuffer(Context * ctx) - : BaseType (ctx) - , m_size (0) - , m_usage (GL_NONE) - { - ; - } -}; - -class Buffer : public Object, public SafeBuffer -{ - friend class Context; - friend class detail::SharedObjectBinding; - - public: - - typedef Object BaseType; - typedef SafeBuffer SafeType; - typedef Buffer ThisType; - - virtual Type type(void) const - { - return BufferType; - } - - void setData(const GLsizeiptr size, GLenum usage, const void * data) + void setData(GLenum target, GLint unit, const GLsizeiptr size, GLenum usage, const GLvoid * data) { + (void)unit; GLW_ASSERT(this->isValid()); - glBufferData(this->m_target, size, data, usage); + glBufferData(target, size, data, usage); this->m_size = size; this->m_usage = usage; } - void setSubData(GLintptr offset, GLsizeiptr size, const void * data) + void setSubData(GLenum target, GLint unit, GLintptr offset, GLsizeiptr size, const GLvoid * data) { + (void)unit; GLW_ASSERT(this->isValid()); - glBufferSubData(this->m_target, offset, size, data); + glBufferSubData(target, offset, size, data); } - void getSubData(GLintptr offset, GLsizeiptr size, void * data) + void getSubData(GLenum target, GLint unit, GLintptr offset, GLsizeiptr size, GLvoid * data) { + (void)unit; GLW_ASSERT(this->isValid()); - glGetBufferSubData(this->m_target, offset, size, data); + glGetBufferSubData(target, offset, size, data); } - void * map(GLenum access) + void * map(GLenum target, GLint unit, GLenum access) { + (void)unit; GLW_ASSERT(this->isValid()); - GLW_ASSERT(!this->isMapped()); - void * ptr = glMapBuffer(this->m_target, access); + GLW_ASSERT(!this->isMapped(target, unit)); + void * ptr = glMapBuffer(target, access); if (ptr == 0) return 0; - this->m_mapAccess = access; - this->m_mapPtr = ptr; + this->m_mapAccess = access; + this->m_mapPointer = ptr; return ptr; } - void unmap(void) + void unmap(GLenum target, GLint unit) { + (void)unit; GLW_ASSERT(this->isValid()); - GLW_ASSERT(this->isMapped()); - glUnmapBuffer(this->m_target); - this->m_mapAccess = GL_NONE; - this->m_mapPtr = 0; + GLW_ASSERT(this->isMapped(target, unit)); + glUnmapBuffer(target); + this->m_mapAccess = GL_NONE; + this->m_mapPointer = 0; } - GLenum mapAccess(void) const + GLenum mapAccess(GLenum target, GLint unit) const { + (void)target; + (void)unit; return this->m_mapAccess; } - bool isMapped(void) const + bool isMapped(GLenum target, GLint unit) const { + (void)target; + (void)unit; return (this->m_mapAccess != GL_NONE); } - void * mapPointer(void) const + void * mapPointer(GLenum target, GLint unit) const { - return this->m_mapPtr; + (void)target; + (void)unit; + return this->m_mapPointer; } - void vertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid * offset) + void vertexAttribPointer(GLenum target, GLint unit, GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid * offset) { + (void)target; + (void)unit; GLW_ASSERT(this->isValid()); - GLW_ASSERT(this->m_target == GL_ARRAY_BUFFER); glVertexAttribPointer(index, size, type, normalized, stride, offset); } + void drawElements(GLenum target, GLint unit, GLenum mode, GLsizei count, GLenum type, const GLvoid * indices) + { + (void)target; + (void)unit; + GLW_ASSERT(this->isValid()); + glDrawElements(mode, count, type, indices); + } + protected: + GLsizeiptr m_size; + GLenum m_usage; + GLenum m_mapAccess; + void * m_mapPointer; + Buffer(Context * ctx) - : SafeObject (ctx) - , BaseType (ctx) - , SafeType (ctx) - , m_mapAccess (GL_NONE) - , m_mapPtr (0) + : BaseType (ctx) + , m_size (0) + , m_usage (GL_NONE) + , m_mapAccess (GL_NONE) + , m_mapPointer (0) { ; } - virtual ~Buffer(void) - { - this->destroy(); - } - bool create(const BufferArguments & args) { this->destroy(); - GLint boundName = 0; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &boundName); - glGenBuffers(1, &(this->m_name)); - this->setBinding(GL_ARRAY_BUFFER, 0); - this->bind(); - this->setData(args.size, args.usage, args.data); - + glBindBuffer(GL_ARRAY_BUFFER, this->m_name); + glBufferData(GL_ARRAY_BUFFER, args.size, args.data, args.usage); glBindBuffer(GL_ARRAY_BUFFER, boundName); - + this->m_size = args.size; + this->m_usage = args.usage; return true; } - virtual void doDestroy(Context * ctx, GLuint name) + virtual void doDestroy(void) { - (void)ctx; - if (name == 0) return; - if (this->isMapped()) this->unmap(); - this->m_size = 0; - this->m_usage = GL_NONE; - this->m_mapAccess = GL_NONE; - this->m_mapPtr = 0; - glDeleteBuffers(1, &name); + glDeleteBuffers(1, &(this->m_name)); + this->m_size = 0; + this->m_usage = GL_NONE; + this->m_mapAccess = GL_NONE; + this->m_mapPointer = 0; } - virtual void doBind(void) + virtual bool doIsValid(void) const { - glBindBuffer(this->m_target, this->m_name); + return (this->m_size > 0); + } +}; + +namespace detail { template <> struct BaseOf { typedef Object Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BufferPtr; + +class SafeBuffer : public SafeObject +{ + friend class Context; + friend class BoundBuffer; + + public: + + typedef SafeObject BaseType; + typedef SafeBuffer ThisType; + + SafeBuffer(void) + : BaseType() + { + ; } - virtual void doUnbind(void) + GLsizeiptr size(void) const + { + return this->object()->size(); + } + + GLenum usage(void) const + { + return this->object()->usage(); + } + + protected: + + SafeBuffer(const BufferPtr & buffer) + : BaseType(buffer) + { + ; + } + + const BufferPtr & object(void) const + { + return static_cast(BaseType::object()); + } + + BufferPtr & object(void) + { + return static_cast(BaseType::object()); + } +}; + +namespace detail { template <> struct BaseOf { typedef SafeObject Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Buffer Type; }; }; +namespace detail { template <> struct ObjectSafe { typedef SafeBuffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BufferHandle; + +class BufferBindingParams : public ObjectBindingParams +{ + public: + + typedef ObjectBindingParams BaseType; + typedef BufferBindingParams ThisType; + + BufferBindingParams(void) + : BaseType() + { + ; + } + + BufferBindingParams(GLenum aTarget, GLenum aUnit) + : BaseType(aTarget, aUnit) + { + ; + } +}; + +class BoundBuffer : public BoundObject +{ + friend class Context; + + public: + + typedef BoundObject BaseType; + typedef BoundBuffer ThisType; + + BoundBuffer(void) + : BaseType() + { + ; + } + + const BufferHandle & handle(void) const + { + return static_cast(BaseType::handle()); + } + + BufferHandle & handle(void) + { + return static_cast(BaseType::handle()); + } + + void setData(const GLsizeiptr size, GLenum usage, const GLvoid * data) + { + this->object()->setData(this->m_target, this->m_unit, size, usage, data); + } + + void setSubData(GLintptr offset, GLsizeiptr size, const GLvoid * data) + { + this->object()->setSubData(this->m_target, this->m_unit, offset, size, data); + } + + void getSubData(GLintptr offset, GLsizeiptr size, GLvoid * data) + { + this->object()->getSubData(this->m_target, this->m_unit, offset, size, data); + } + + void * map(GLenum access) + { + return this->object()->map(this->m_target, this->m_unit, access); + } + + void unmap(void) + { + this->object()->unmap(this->m_target, this->m_unit); + } + + GLenum mapAccess(void) const + { + return this->object()->mapAccess(this->m_target, this->m_unit); + } + + bool isMapped(void) const + { + return this->object()->isMapped(this->m_target, this->m_unit); + } + + void * mapPointer(void) const + { + return this->object()->mapPointer(this->m_target, this->m_unit); + } + + protected: + + BoundBuffer(const BufferHandle & handle, const BufferBindingParams & params) + : BaseType(handle, params) + { + ; + } + + const BufferPtr & object(void) const + { + return this->handle()->object(); + } + + BufferPtr & object(void) + { + return this->handle()->object(); + } + + virtual void bind(void) + { + glBindBuffer(this->m_target, this->object()->name()); + } + + virtual void unbind(void) { glBindBuffer(this->m_target, 0); } +}; + +namespace detail { template <> struct ParamsOf { typedef BufferBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundObject Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Buffer Type; }; }; +namespace detail { template <> struct ObjectBound { typedef BoundBuffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundBufferHandle; + +class VertexBufferBindingParams : public BufferBindingParams +{ + public: + + typedef BufferBindingParams BaseType; + typedef VertexBufferBindingParams ThisType; + + VertexBufferBindingParams(void) + : BaseType(GL_ARRAY_BUFFER, 0) + { + ; + } +}; + +class BoundVertexBuffer : public BoundBuffer +{ + friend class Context; + + public: + + typedef BoundBuffer BaseType; + typedef BoundVertexBuffer ThisType; + + BoundVertexBuffer(void) + : BaseType() + { + ; + } + + void vertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid * offset) + { + this->object()->vertexAttribPointer(this->m_target, this->m_unit, index, size, type, normalized, stride, offset); + } + + protected: + + BoundVertexBuffer(const BufferHandle & handle, const VertexBufferBindingParams & params) + : BaseType(handle, params) + { + ; + } +}; + +namespace detail { template <> struct ParamsOf { typedef VertexBufferBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundBuffer Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Buffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundVertexBufferHandle; + +class IndexBufferBindingParams : public BufferBindingParams +{ + public: + + typedef BufferBindingParams BaseType; + typedef IndexBufferBindingParams ThisType; + + IndexBufferBindingParams(void) + : BaseType(GL_ELEMENT_ARRAY_BUFFER, 0) + { + ; + } +}; + +class BoundIndexBuffer : public BoundBuffer +{ + friend class Context; + + public: + + typedef BoundBuffer BaseType; + typedef BoundIndexBuffer ThisType; + + BoundIndexBuffer(void) + : BaseType() + { + ; + } + + void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid * indices) + { + this->object()->drawElements(this->m_target, this->m_unit, mode, count, type, indices); + } + + protected: + + BoundIndexBuffer(const BufferHandle & handle, const IndexBufferBindingParams & params) + : BaseType(handle, params) + { + ; + } +}; + +namespace detail { template <> struct ParamsOf { typedef IndexBufferBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundBuffer Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Buffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundIndexBufferHandle; + +class PixelPackBufferBindingParams : public BufferBindingParams +{ + public: + + typedef BufferBindingParams BaseType; + typedef PixelPackBufferBindingParams ThisType; + + PixelPackBufferBindingParams(void) + : BaseType(GL_PIXEL_PACK_BUFFER, 0) + { + ; + } +}; + +class BoundPixelPackBuffer : public BoundBuffer +{ + friend class Context; + + public: + + typedef BoundBuffer BaseType; + typedef BoundPixelPackBuffer ThisType; + + BoundPixelPackBuffer(void) + : BaseType() + { + ; + } + + protected: + + BoundPixelPackBuffer(const BufferHandle & handle, const PixelPackBufferBindingParams & params) + : BaseType(handle, params) + { + ; + } +}; + +namespace detail { template <> struct ParamsOf { typedef PixelPackBufferBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundBuffer Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Buffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundPixelPackBufferHandle; + +class PixelUnpackBufferBindingParams : public BufferBindingParams +{ + public: + + typedef BufferBindingParams BaseType; + typedef PixelUnpackBufferBindingParams ThisType; + + PixelUnpackBufferBindingParams(void) + : BaseType(GL_PIXEL_UNPACK_BUFFER, 0) + { + ; + } +}; + +class BoundPixelUnpackBuffer : public BoundBuffer +{ + friend class Context; + + public: + + typedef BoundBuffer BaseType; + typedef BoundPixelUnpackBuffer ThisType; + + BoundPixelUnpackBuffer(void) + : BaseType() + { + ; + } + + protected: + + BoundPixelUnpackBuffer(const BufferHandle & handle, const PixelUnpackBufferBindingParams & params) + : BaseType(handle, params) + { + ; + } +}; + +namespace detail { template <> struct ParamsOf { typedef PixelUnpackBufferBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundBuffer Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Buffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundPixelUnpackBufferHandle; + +class UniformBufferBindingParams : public BufferBindingParams +{ + public: + + typedef BufferBindingParams BaseType; + typedef UniformBufferBindingParams ThisType; + + GLintptr offset; + GLsizeiptr size; + + UniformBufferBindingParams(void) + : BaseType (GL_UNIFORM_BUFFER, 0) + , offset (0) + , size (0) + { + ; + } + + UniformBufferBindingParams(GLuint aIndex, GLintptr aOffset, GLsizeiptr aSize) + : BaseType (GL_UNIFORM_BUFFER, GLint(aIndex)) + , offset (aOffset) + , size (aSize) + { + ; + } +}; + +class BoundUniformBuffer : public BoundBuffer +{ + friend class Context; + + public: + + typedef BoundBuffer BaseType; + typedef BoundUniformBuffer ThisType; + + BoundUniformBuffer(void) + : BaseType() + { + ; + } + + protected: + + BoundUniformBuffer(const BufferHandle & handle, const UniformBufferBindingParams & params) + : BaseType(handle, params) + { + ; + } + + virtual void bind(void) + { + glBindBufferRange(this->m_target, GLuint(this->m_unit), this->object()->name(), this->m_offset, this->m_size); + } + + virtual void unbind(void) + { + glBindBufferRange(this->m_target, GLuint(this->m_unit), 0, 0, 0); + } private: - GLenum m_mapAccess; - void * m_mapPtr; + GLintptr m_offset; + GLsizeiptr m_size; }; -typedef detail::SafeHandle BufferHandle; -typedef detail::UnsafeHandle BoundBuffer; +namespace detail { template <> struct ParamsOf { typedef UniformBufferBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundBuffer Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Buffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundUniformBufferHandle; -} // end namespace glw +class FeedbackBufferBindingParams : public BufferBindingParams +{ + public: + + typedef BufferBindingParams BaseType; + typedef FeedbackBufferBindingParams ThisType; + + GLintptr offset; + GLsizeiptr size; + + FeedbackBufferBindingParams(void) + : BaseType (GL_TRANSFORM_FEEDBACK_BUFFER, 0) + , offset (0) + , size (0) + { + ; + } + + FeedbackBufferBindingParams(GLuint aIndex, GLintptr aOffset, GLsizeiptr aSize) + : BaseType (GL_TRANSFORM_FEEDBACK_BUFFER, GLint(aIndex)) + , offset (aOffset) + , size (aSize) + { + ; + } +}; + +class BoundFeedbackBuffer : public BoundBuffer +{ + friend class Context; + + public: + + typedef BoundBuffer BaseType; + typedef BoundFeedbackBuffer ThisType; + + BoundFeedbackBuffer(void) + : BaseType() + { + ; + } + + protected: + + BoundFeedbackBuffer(const BufferHandle & handle, const FeedbackBufferBindingParams & params) + : BaseType(handle, params) + { + ; + } + + virtual void bind(void) + { + glBindBufferRange(this->m_target, GLuint(this->m_unit), this->object()->name(), this->m_offset, this->m_size); + } + + virtual void unbind(void) + { + glBindBufferRange(this->m_target, GLuint(this->m_unit), 0, 0, 0); + } + + private: + + GLintptr m_offset; + GLsizeiptr m_size; +}; + +namespace detail { template <> struct ParamsOf { typedef FeedbackBufferBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundBuffer Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Buffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundFeedbackBufferHandle; + +}; #endif // GLW_BUFFER_H diff --git a/wrap/glw/common.h b/wrap/glw/common.h index c84c27e5..1914db66 100644 --- a/wrap/glw/common.h +++ b/wrap/glw/common.h @@ -5,13 +5,13 @@ #include "./config.h" -#define GLW_DONT_CARE (0xFFFFFFFF) -#define GLW_CARE_OF(X) ((X) != GLW_DONT_CARE) +#define GLW_DONT_CARE (0xFFFFFFFF) +#define GLW_CARE_OF(X) ((X) != GLW_DONT_CARE) -#define GLW_CHECK_GL_ERROR GLW_ASSERT(glGetError() == GL_NO_ERROR) -#define GLW_CHECK_GL_READ_FRAMEBUFFER_STATUS GLW_ASSERT(glCheckFramebufferStatus(GL_READ_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) -#define GLW_CHECK_GL_DRAW_FRAMEBUFFER_STATUS GLW_ASSERT(glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) -#define GLW_CHECK_GL_FRAMEBUFFER_STATUS GLW_ASSERT(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) +#define GLW_CHECK_GL_ERROR GLW_ASSERT(glGetError() == GL_NO_ERROR) +#define GLW_CHECK_GL_READ_FRAMEBUFFER_STATUS GLW_ASSERT(glCheckFramebufferStatus(GL_READ_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) +#define GLW_CHECK_GL_DRAW_FRAMEBUFFER_STATUS GLW_ASSERT(glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) +#define GLW_CHECK_GL_READ_DRAW_FRAMEBUFFER_STATUS GLW_ASSERT(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) namespace glw { diff --git a/wrap/glw/context.h b/wrap/glw/context.h index ef56fb0c..dbdf65c9 100644 --- a/wrap/glw/context.h +++ b/wrap/glw/context.h @@ -2,12 +2,13 @@ #define GLW_CONTEXT_H #include "./noncopyable.h" +#include "./objectdeleter.h" #include "./buffer.h" -#include "./renderbuffer.h" #include "./vertexshader.h" #include "./geometryshader.h" #include "./fragmentshader.h" #include "./program.h" +#include "./renderbuffer.h" #include "./texture2d.h" #include "./framebuffer.h" @@ -20,7 +21,7 @@ namespace glw class Context : public detail::NonCopyable { - friend class detail::SharedObjectBase; + friend class detail::ObjectDeleter; public: @@ -52,7 +53,7 @@ class Context : public detail::NonCopyable if (!this->isAcquired()) return; this->m_acquired = false; this->terminateTargets(); - this->destroyAllObjects(); + this->invalidateReferencesToAllObjects(); } bool isAcquired(void) const @@ -68,13 +69,13 @@ class Context : public detail::NonCopyable BufferHandle createBuffer(const BufferArguments & args) { BufferHandle handle = this->createHandle(); - handle.object()->create(args); + handle->object()->create(args); return handle; } - BoundBuffer bindVertexBuffer(BufferHandle & handle) + BoundVertexBufferHandle bindVertexBuffer(BufferHandle & handle) { - return this->bind(BindingTarget(GL_ARRAY_BUFFER, 0), handle); + return this->bind(handle); } void unbindVertexBuffer(void) @@ -83,9 +84,9 @@ class Context : public detail::NonCopyable this->bindVertexBuffer(nullHandle); } - BoundBuffer bindIndexBuffer(BufferHandle & handle) + BoundIndexBufferHandle bindIndexBuffer(BufferHandle & handle) { - return this->bind(BindingTarget(GL_ELEMENT_ARRAY_BUFFER, 0), handle); + return this->bind(handle); } void unbindIndexBuffer(void) @@ -94,16 +95,60 @@ class Context : public detail::NonCopyable this->bindIndexBuffer(nullHandle); } + BoundPixelPackBufferHandle bindPixelPackBuffer(BufferHandle & handle) + { + return this->bind(handle); + } + + void unbindPixelPackBuffer(void) + { + BufferHandle nullHandle; + this->bindPixelPackBuffer(nullHandle); + } + + BoundPixelUnpackBufferHandle bindPixelUnpackBuffer(BufferHandle & handle) + { + return this->bind(handle); + } + + void unbindPixelUnpackBuffer(void) + { + BufferHandle nullHandle; + this->bindPixelUnpackBuffer(nullHandle); + } + + BoundUniformBufferHandle bindUniformBuffer(BufferHandle & handle, GLuint index, GLintptr offset, GLsizeiptr size) + { + return this->bind(handle, UniformBufferBindingParams(index, offset, size)); + } + + void unbindUniformBuffer(GLuint index) + { + BufferHandle nullHandle; + this->bindUniformBuffer(nullHandle, index, 0, 0); + } + + BoundFeedbackBufferHandle bindFeedbackBuffer(BufferHandle & handle, GLuint index, GLintptr offset, GLsizeiptr size) + { + return this->bind(handle, FeedbackBufferBindingParams(index, offset, size)); + } + + void unbindFeedbackBuffer(GLuint index) + { + BufferHandle nullHandle; + this->bindFeedbackBuffer(nullHandle, index, 0, 0); + } + RenderbufferHandle createRenderbuffer(const RenderbufferArguments & args) { RenderbufferHandle handle = this->createHandle(); - handle.object()->create(args); + handle->object()->create(args); return handle; } - BoundRenderbuffer bindRenderbuffer(RenderbufferHandle & handle) + BoundRenderbufferHandle bindRenderbuffer(RenderbufferHandle & handle) { - return this->bind(BindingTarget(GL_RENDERBUFFER, 0), handle); + return this->bind(handle); } void unbindRenderbuffer(void) @@ -115,13 +160,13 @@ class Context : public detail::NonCopyable VertexShaderHandle createVertexShader(const VertexShaderArguments & args) { VertexShaderHandle handle = this->createHandle(); - handle.object()->create(args); + handle->object()->create(args); return handle; } - BoundVertexShader bindVertexShader(VertexShaderHandle & handle) + BoundVertexShaderHandle bindVertexShader(VertexShaderHandle & handle) { - return this->bind(BindingTarget(GL_VERTEX_SHADER, 0), handle); + return this->bind(handle, VertexShaderBindingParams()); } void unbindVertexShader(void) @@ -133,13 +178,13 @@ class Context : public detail::NonCopyable GeometryShaderHandle createGeometryShader(const GeometryShaderArguments & args) { GeometryShaderHandle handle = this->createHandle(); - handle.object()->create(args); + handle->object()->create(args); return handle; } - BoundGeometryShader bindGeometryShader(GeometryShaderHandle & handle) + BoundGeometryShaderHandle bindGeometryShader(GeometryShaderHandle & handle) { - return this->bind(BindingTarget(GL_GEOMETRY_SHADER, 0), handle); + return this->bind(handle, GeometryShaderBindingParams()); } void unbindGeometryShader(void) @@ -151,13 +196,13 @@ class Context : public detail::NonCopyable FragmentShaderHandle createFragmentShader(const FragmentShaderArguments & args) { FragmentShaderHandle handle = this->createHandle(); - handle.object()->create(args); + handle->object()->create(args); return handle; } - BoundFragmentShader bindFragmentShader(FragmentShaderHandle & handle) + BoundFragmentShaderHandle bindFragmentShader(FragmentShaderHandle & handle) { - return this->bind(BindingTarget(GL_FRAGMENT_SHADER, 0), handle); + return this->bind(handle, FragmentShaderBindingParams()); } void unbindFragmentShader(void) @@ -169,13 +214,13 @@ class Context : public detail::NonCopyable ProgramHandle createProgram(const ProgramArguments & args) { ProgramHandle handle = this->createHandle(); - handle.object()->create(args); + handle->object()->create(args); return handle; } - BoundProgram bindProgram(ProgramHandle & handle) + BoundProgramHandle bindProgram(ProgramHandle & handle) { - return this->bind(BindingTarget(GL_CURRENT_PROGRAM, 0), handle); + return this->bind(handle, ProgramBindingParams()); } void unbindProgram(void) @@ -187,34 +232,33 @@ class Context : public detail::NonCopyable Texture2DHandle createTexture2D(const Texture2DArguments & args) { Texture2DHandle handle = this->createHandle(); - handle.object()->create(args); + handle->object()->create(args); return handle; } - BoundTexture2D bindTexture2D(GLint unit, Texture2DHandle & handle) + BoundTexture2DHandle bindTexture2D(Texture2DHandle & handle, GLint unit) { - glActiveTexture(GL_TEXTURE0 + unit); - return this->bind(BindingTarget(GL_TEXTURE_2D, unit), handle); + return this->bind(handle, Texture2DBindingParams(unit)); } void unbindTexture2D(GLint unit) { Texture2DHandle nullHandle; - this->bindTexture2D(unit, nullHandle); + this->bindTexture2D(nullHandle, unit); } FramebufferHandle createFramebuffer(const FramebufferArguments & args) { FramebufferHandle handle = this->createHandle(); - handle.object()->create(args); + handle->object()->create(args); return handle; } - BoundFramebuffer bindReadFramebuffer(FramebufferHandle & handle) + BoundReadFramebufferHandle bindReadFramebuffer(FramebufferHandle & handle) { FramebufferHandle nullHandle; - this->bind(BindingTarget(GL_FRAMEBUFFER, 0), nullHandle); - return this->bind(BindingTarget(GL_READ_FRAMEBUFFER, 0), handle); + this->bind(nullHandle, ReadDrawFramebufferBindingParams()); + return this->bind(handle, ReadFramebufferBindingParams()); } void unbindReadFramebuffer(void) @@ -223,11 +267,11 @@ class Context : public detail::NonCopyable this->bindReadFramebuffer(nullHandle); } - BoundFramebuffer bindDrawFramebuffer(FramebufferHandle & handle) + BoundDrawFramebufferHandle bindDrawFramebuffer(FramebufferHandle & handle) { FramebufferHandle nullHandle; - this->bind(BindingTarget(GL_FRAMEBUFFER, 0), nullHandle); - return this->bind(BindingTarget(GL_DRAW_FRAMEBUFFER, 0), handle); + this->bind(nullHandle, ReadDrawFramebufferBindingParams()); + return this->bind(handle, DrawFramebufferBindingParams()); } void unbindDrawFramebuffer(void) @@ -236,199 +280,296 @@ class Context : public detail::NonCopyable this->bindDrawFramebuffer(nullHandle); } - BoundFramebuffer bindFramebuffer(FramebufferHandle & handle) + BoundReadDrawFramebufferHandle bindReadDrawFramebuffer(FramebufferHandle & handle) { FramebufferHandle nullHandle; - this->bind(BindingTarget(GL_READ_FRAMEBUFFER, 0), nullHandle); - this->bind(BindingTarget(GL_DRAW_FRAMEBUFFER, 0), nullHandle); - return this->bind(BindingTarget(GL_FRAMEBUFFER, 0), handle); + this->bind(nullHandle, ReadFramebufferBindingParams()); + this->bind(nullHandle, DrawFramebufferBindingParams()); + return this->bind(handle, ReadDrawFramebufferBindingParams()); } - void unbindFramebuffer(void) + void unbindReadDrawFramebuffer(void) { FramebufferHandle nullHandle; - this->bindFramebuffer(nullHandle); + this->bindReadDrawFramebuffer(nullHandle); } private: - typedef detail::SharedObjectBase SharedObjectType; - - typedef void * GenericPtr; - typedef std::pair BindingTarget; - - typedef std::set ShaderObjecPtrSet; - typedef ShaderObjecPtrSet::const_iterator ShaderObjecPtrSetConstIterator; - typedef ShaderObjecPtrSet::iterator ShaderObjecPtrSetIterator; - - typedef std::map SharedObjectBindingPtrMap; - typedef SharedObjectBindingPtrMap::const_iterator SharedObjectBindingPtrConstIterator; - typedef SharedObjectBindingPtrMap::iterator SharedObjectBindingPtrIterator; - typedef SharedObjectBindingPtrMap::value_type SharedObjectBindingPtrValue; - - bool m_acquired; - int m_textureUnits; - ShaderObjecPtrSet m_shareds; - SharedObjectBindingPtrMap m_bindings; + template + struct ObjectSafeFromObject + { + typedef typename detail::ObjectSafe::Type Type; + }; template - void initializeTarget(BindingTarget bt) + struct ObjectBoundFromObject { - typedef TObject ObjectType; - typedef detail::UnsafeHandle UnsafeObjectType; + typedef typename detail::ObjectBound::Type Type; + }; - UnsafeObjectType * unsafeObject = 0; - this->m_bindings.insert(SharedObjectBindingPtrValue(bt, unsafeObject)); + template + struct ObjectFromBinding + { + typedef typename detail::ObjectBase::Type Type; + }; + + template + struct RefCountedPtrFromObject + { + typedef detail::RefCountedObject::Type>::Type, typename detail::BaseOf::Type> Type; + }; + + template + struct RefCountedSafeHandleFromObject + { + typedef typename ObjectSafeFromObject::Type ObjectSafeType; + typedef detail::RefCountedObject::Type>::Type, typename detail::BaseOf::Type> Type; + }; + + template + struct RefCountedBindingHandleFromObject + { + typedef typename ObjectBoundFromObject::Type ObjectBoundType; + typedef detail::RefCountedObject::Type>::Type, typename detail::BaseOf::Type> Type; + }; + + template + struct RefCountedBindingHandleFromBinding + { + typedef detail::RefCountedObject::Type>::Type, typename detail::BaseOf::Type> Type; + }; + + template + struct PtrFromObject + { + typedef detail::ObjectSharedPointer::Type>::Type, typename detail::BaseOf::Type> Type; + }; + + template + struct SafeHandleFromObject + { + typedef typename ObjectSafeFromObject::Type ObjectSafeType; + typedef detail::ObjectSharedPointer::Type>::Type, typename detail::BaseOf::Type> Type; + }; + + template + struct BindingHandleFromObject + { + typedef typename ObjectBoundFromObject::Type ObjectBoundType; + typedef detail::ObjectSharedPointer::Type>::Type, typename detail::BaseOf::Type> Type; + }; + + template + struct SafeHandleFromBinding + { + typedef typename SafeHandleFromObject::Type>::Type Type; + }; + + template + struct BindingHandleFromBinding + { + typedef detail::ObjectSharedPointer::Type>::Type, typename detail::BaseOf::Type> Type; + }; + + typedef Object ObjectType; + typedef RefCountedPtrFromObject::Type RefCountedPtrType; + typedef std::map RefCountedPtrPtrMap; + typedef RefCountedPtrPtrMap::const_iterator RefCountedPtrPtrMapConstIterator; + typedef RefCountedPtrPtrMap::iterator RefCountedPtrPtrMapIterator; + typedef RefCountedPtrPtrMap::value_type RefCountedPtrPtrMapValue; + + typedef std::pair BindingTarget; + typedef BoundObjectHandle::RefCountedObjectType RefCountedBindingType; + typedef std::map RefCountedBindingPtrMap; + typedef RefCountedBindingPtrMap::const_iterator RefCountedBindingPtrMapConstIterator; + typedef RefCountedBindingPtrMap::iterator RefCountedBindingPtrMapIterator; + typedef RefCountedBindingPtrMap::value_type RefCountedBindingPtrMapValue; + + bool m_acquired; + int m_textureUnits; + RefCountedPtrPtrMap m_objects; + RefCountedBindingPtrMap m_bindings; + + template + void initializeTarget(const TBindingParams & params) + { + typedef TBinding BindingType; + typedef typename RefCountedBindingHandleFromBinding::Type RefCountedBindingHandleType; + + const BindingTarget bt = BindingTarget(params.target, params.unit); + RefCountedBindingHandleType * binding = 0; + this->m_bindings.insert(RefCountedBindingPtrMapValue(bt, binding)); } - template - void terminateTarget(BindingTarget bt) + template + void terminateTarget(const TBindingParams & params) { - typedef TObject ObjectType; - typedef detail::SafeHandle SafeObjectType; + typedef TBinding BindingType; + typedef typename SafeHandleFromBinding::Type SafeHandleType; - SafeObjectType nullHandle; - this->bind(bt, nullHandle); + SafeHandleType nullHandle; + this->bind(nullHandle, params); } void initializeTargets(void) { + this->initializeTarget(VertexBufferBindingParams () ); + this->initializeTarget(IndexBufferBindingParams () ); + this->initializeTarget(PixelPackBufferBindingParams () ); + this->initializeTarget(PixelUnpackBufferBindingParams () ); + this->initializeTarget(UniformBufferBindingParams (0, 0, 0)); + this->initializeTarget(FeedbackBufferBindingParams (0, 0, 0)); + this->initializeTarget(RenderbufferBindingParams () ); + this->initializeTarget(VertexShaderBindingParams () ); + this->initializeTarget(GeometryShaderBindingParams () ); + this->initializeTarget(FragmentShaderBindingParams () ); + this->initializeTarget(ProgramBindingParams () ); + this->initializeTarget(ReadFramebufferBindingParams () ); + this->initializeTarget(DrawFramebufferBindingParams () ); + this->initializeTarget(ReadDrawFramebufferBindingParams () ); + { GLint texUnits = 0; glGetIntegerv(GL_MAX_TEXTURE_UNITS, &texUnits); this->m_textureUnits = int(texUnits); } - - this->initializeTarget(BindingTarget(GL_ARRAY_BUFFER, 0)); - this->initializeTarget(BindingTarget(GL_ELEMENT_ARRAY_BUFFER, 0)); - this->initializeTarget(BindingTarget(GL_RENDERBUFFER, 0)); - this->initializeTarget(BindingTarget(GL_VERTEX_SHADER, 0)); - this->initializeTarget(BindingTarget(GL_GEOMETRY_SHADER, 0)); - this->initializeTarget(BindingTarget(GL_FRAGMENT_SHADER, 0)); - this->initializeTarget(BindingTarget(GL_CURRENT_PROGRAM, 0)); + for (int i=0; im_textureUnits; ++i) { - this->initializeTarget(BindingTarget(GL_TEXTURE_2D, GLint(i))); + this->initializeTarget(Texture2DBindingParams(GLint(i))); } - this->initializeTarget(BindingTarget(GL_READ_FRAMEBUFFER, 0)); - this->initializeTarget(BindingTarget(GL_DRAW_FRAMEBUFFER, 0)); - this->initializeTarget(BindingTarget(GL_FRAMEBUFFER, 0)); } void terminateTargets(void) { - this->terminateTarget(BindingTarget(GL_ARRAY_BUFFER, 0)); - this->terminateTarget(BindingTarget(GL_ELEMENT_ARRAY_BUFFER, 0)); - this->terminateTarget(BindingTarget(GL_RENDERBUFFER, 0)); - this->terminateTarget(BindingTarget(GL_VERTEX_SHADER, 0)); - this->terminateTarget(BindingTarget(GL_GEOMETRY_SHADER, 0)); - this->terminateTarget(BindingTarget(GL_FRAGMENT_SHADER, 0)); - this->terminateTarget(BindingTarget(GL_CURRENT_PROGRAM, 0)); + this->terminateTarget(VertexBufferBindingParams () ); + this->terminateTarget(IndexBufferBindingParams () ); + this->terminateTarget(PixelPackBufferBindingParams () ); + this->terminateTarget(PixelUnpackBufferBindingParams () ); + this->terminateTarget(UniformBufferBindingParams (0, 0, 0)); + this->terminateTarget(FeedbackBufferBindingParams (0, 0, 0)); + this->terminateTarget(RenderbufferBindingParams () ); + this->terminateTarget(VertexShaderBindingParams () ); + this->terminateTarget(GeometryShaderBindingParams () ); + this->terminateTarget(FragmentShaderBindingParams () ); + this->terminateTarget(ProgramBindingParams () ); + this->terminateTarget(ReadFramebufferBindingParams () ); + this->terminateTarget(DrawFramebufferBindingParams () ); + this->terminateTarget(ReadDrawFramebufferBindingParams () ); + for (int i=0; im_textureUnits; ++i) { - this->terminateTarget(BindingTarget(GL_TEXTURE_2D, GLint(i))); + this->terminateTarget(Texture2DBindingParams(GLint(i))); } - this->terminateTarget(BindingTarget(GL_READ_FRAMEBUFFER, 0)); - this->terminateTarget(BindingTarget(GL_DRAW_FRAMEBUFFER, 0)); - this->terminateTarget(BindingTarget(GL_FRAMEBUFFER, 0)); this->m_textureUnits = 0; } - template - detail::UnsafeHandle bind(BindingTarget bt, detail::SafeHandle & h) - { - typedef TObject ObjectType; - typedef detail::SharedObjectBinding SharedObjectBindingType; - typedef detail::UnsafeHandle UnsafeObjectType; - - SharedObjectBindingPtrIterator it = this->m_bindings.find(bt); - GLW_ASSERT(it != this->m_bindings.end()); - - ObjectType * Object = h.object(); - - SharedObjectBindingType * currentBinding = static_cast(it->second); - if (currentBinding != 0) - { - GLW_ASSERT(!currentBinding->isNull()); - // WARNING: as state could have been changed outside GLW, uncommenting the following line may prevent correct binding. - //if (currentBinding->object() == Object) return UnsafeObjectType(currentBinding); - if (h.isNull()) currentBinding->object()->unbind(); - currentBinding->setNull(); - currentBinding->unref(); - currentBinding = 0; - it->second = 0; - } - - if (h.isNull()) return UnsafeObjectType(); - - SharedObjectBindingType * newBinding = new SharedObjectBindingType(h.shared(), bt.first, bt.second); - newBinding->ref(); - it->second = newBinding; - - Object->setBinding(bt.first, bt.second); - Object->bind(); - - return UnsafeObjectType(newBinding); - } - template TObject * createObject(void) { typedef TObject ObjectType; - ObjectType * Object = new ObjectType(this); - return Object; + ObjectType * object = new ObjectType(this); + return object; } - void destroyObject(Object * Object) + void destroyObject(Object * object) { - GLW_ASSERT(Object != 0); - Object->destroy(); - delete Object; - } - - void removeShared(SharedObjectType * shared) - { - GLW_ASSERT(shared != 0); - GLW_ASSERT(this->m_shareds.count(shared) > 0); - this->m_shareds.erase(shared); - this->destroyObject(shared->object()); - } - - void destroyAllObjects(void) - { - for (ShaderObjecPtrSetIterator it=this->m_shareds.begin(); it!=this->m_shareds.end(); ++it) - { - SharedObjectType * shared = *it; - Object * Object = shared->object(); - shared->setNull(); - this->destroyObject(Object); - } + GLW_ASSERT(object != 0); + object->destroy(); + delete object; } template - detail::SafeHandle createHandle(void) + typename SafeHandleFromObject::Type createHandle(void) { - typedef TObject ObjectType; - typedef detail::SharedObject SharedObjectType; - typedef detail::SafeHandle SafeType; + typedef TObject ObjectType; + typedef typename RefCountedPtrFromObject::Type RefCountedPtrType; + typedef typename PtrFromObject::Type PtrType; - ObjectType * Object = new ObjectType(this); - SharedObjectType * shared = new SharedObjectType(this, Object); + typedef typename ObjectSafeFromObject::Type ObjectSafeType; + typedef typename RefCountedSafeHandleFromObject::Type RefCountedSafeHandleType; + typedef typename SafeHandleFromObject::Type SafeHandleType; - this->m_shareds.insert(shared); + ObjectType * object = this->createObject(); + RefCountedPtrType * refCountedPtr = new RefCountedPtrType(object, typename detail::DeleterOf::Type()); + PtrType ptr = PtrType(refCountedPtr); - return SafeType(shared); + ObjectSafeType * objecSafe = new ObjectSafeType(ptr); + RefCountedSafeHandleType * refCountedHandle = new RefCountedSafeHandleType(objecSafe, typename detail::DeleterOf::Type()); + SafeHandleType handle = SafeHandleType(refCountedHandle); + + this->m_objects.insert(RefCountedPtrPtrMapValue(object, refCountedPtr)); + + return handle; + } + + void noMoreReferencesTo(Object * object) + { + GLW_ASSERT(object != 0); + RefCountedPtrPtrMapIterator it = this->m_objects.find(object); + GLW_ASSERT(it != this->m_objects.end()); + this->m_objects.erase(it); + this->destroyObject(object); + } + + void invalidateReferencesToAllObjects(void) + { + for (RefCountedPtrPtrMapIterator it=this->m_objects.begin(); it!=this->m_objects.end(); ++it) + { + Object * object = it->first; + RefCountedPtrType * refPtr = it->second; + refPtr->setNull(false); + this->destroyObject(object); + } + } + + template + typename BindingHandleFromBinding::Type bind(typename SafeHandleFromBinding::Type & h, const typename detail::ParamsOf::Type & params = typename detail::ParamsOf::Type()) + { + typedef TBinding BindingType; + typedef typename detail::ParamsOf::Type BindingParamsType; + typedef typename BindingHandleFromBinding::Type BindingHandleType; + typedef typename RefCountedBindingHandleFromBinding::Type RefCountedBindingHandleType; + + const BindingTarget bt = BindingTarget(params.target, params.unit); + + RefCountedBindingPtrMapIterator it = this->m_bindings.find(bt); + GLW_ASSERT(it != this->m_bindings.end()); + + RefCountedBindingHandleType * currentBinding = static_cast(it->second); + if (currentBinding != 0) + { + GLW_ASSERT(!currentBinding->isNull()); + // WARNING: as state could have been changed outside GLW, uncommenting the following line may prevent correct binding. + //if (currentBinding->object() == Object) return UnsafeObjectType(currentBinding); + if (h.isNull()) currentBinding->object()->unbind(); + currentBinding->setNull(true); + currentBinding->unref(); + currentBinding = 0; + it->second = 0; + } + + if (h.isNull()) return BindingHandleType(); + + BindingType * binding = new BindingType(h, params); + RefCountedBindingHandleType * newBinding = new RefCountedBindingHandleType(binding, typename detail::DeleterOf::Type()); + newBinding->ref(); + newBinding->object()->bind(); + it->second = newBinding; + + return BindingHandleType(newBinding); } }; namespace detail { -template -inline void SharedObjectBase::signalDestruction(void) +inline void ObjectDeleter :: operator () (Object * object) const { - this->m_context->removeShared(this); + if (object == 0) return; + object->context()->noMoreReferencesTo(object); } }; diff --git a/wrap/glw/fragmentshader.h b/wrap/glw/fragmentshader.h index 3a5b3f92..3967fb14 100644 --- a/wrap/glw/fragmentshader.h +++ b/wrap/glw/fragmentshader.h @@ -10,12 +10,13 @@ class FragmentShaderArguments : public ShaderArguments { public: - typedef ShaderArguments BaseType; + typedef ShaderArguments BaseType; typedef FragmentShaderArguments ThisType; FragmentShaderArguments(void) + : BaseType() { - this->clear(); + ; } void clear(void) @@ -24,33 +25,14 @@ class FragmentShaderArguments : public ShaderArguments } }; -class SafeFragmentShader : public virtual SafeShader -{ - public: - - typedef SafeShader BaseType; - typedef SafeFragmentShader ThisType; - - protected: - - SafeFragmentShader(Context * ctx) - : SafeObject (ctx) - , BaseType (ctx) - { - ; - } -}; - -class FragmentShader : public Shader, public SafeFragmentShader +class FragmentShader : public Shader { friend class Context; - friend class detail::SharedObjectBinding; public: - typedef Shader BaseType; - typedef SafeFragmentShader SafeType; - typedef FragmentShader ThisType; + typedef Shader BaseType; + typedef FragmentShader ThisType; virtual Type type(void) const { @@ -60,10 +42,7 @@ class FragmentShader : public Shader, public SafeFragmentShader protected: FragmentShader(Context * ctx) - : SafeObject (ctx) - , SafeShader (ctx) - , BaseType (ctx) - , SafeType (ctx) + : BaseType(ctx) { ; } @@ -79,9 +58,101 @@ class FragmentShader : public Shader, public SafeFragmentShader } }; -typedef detail::SafeHandle FragmentShaderHandle; -typedef detail::UnsafeHandle BoundFragmentShader; +namespace detail { template <> struct BaseOf { typedef Shader Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type FragmentShaderPtr; -} // end namespace glw +class SafeFragmentShader : public SafeShader +{ + friend class Context; + friend class BoundFragmentShader; + + public: + + typedef SafeShader BaseType; + typedef SafeFragmentShader ThisType; + + protected: + + SafeFragmentShader(const FragmentShaderPtr & fragmentShader) + : BaseType(fragmentShader) + { + ; + } + + const FragmentShaderPtr & object(void) const + { + return static_cast(BaseType::object()); + } + + FragmentShaderPtr & object(void) + { + return static_cast(BaseType::object()); + } +}; + +namespace detail { template <> struct BaseOf { typedef SafeShader Type; }; }; +namespace detail { template <> struct ObjectBase { typedef FragmentShader Type; }; }; +namespace detail { template <> struct ObjectSafe { typedef SafeFragmentShader Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type FragmentShaderHandle; + +class FragmentShaderBindingParams : public ShaderBindingParams +{ + public: + + typedef ShaderBindingParams BaseType; + typedef FragmentShaderBindingParams ThisType; + + FragmentShaderBindingParams(void) + : BaseType(GL_FRAGMENT_SHADER, 0) + { + ; + } +}; + +class BoundFragmentShader : public BoundShader +{ + friend class Context; + + public: + + typedef BoundShader BaseType; + typedef BoundFragmentShader ThisType; + + const FragmentShaderHandle & handle(void) const + { + return static_cast(BaseType::handle()); + } + + FragmentShaderHandle & handle(void) + { + return static_cast(BaseType::handle()); + } + + protected: + + BoundFragmentShader(const FragmentShaderHandle & handle, const ShaderBindingParams & params) + : BaseType(handle, params) + { + ; + } + + const FragmentShaderPtr & object(void) const + { + return this->handle()->object(); + } + + FragmentShaderPtr & object(void) + { + return this->handle()->object(); + } +}; + +namespace detail { template <> struct ParamsOf { typedef FragmentShaderBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundShader Type; }; }; +namespace detail { template <> struct ObjectBase { typedef FragmentShader Type; }; }; +namespace detail { template <> struct ObjectBound { typedef BoundFragmentShader Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundFragmentShaderHandle; + +}; #endif // GLW_FRAGMENTSHADER_H diff --git a/wrap/glw/framebuffer.h b/wrap/glw/framebuffer.h index ac8b0fc6..aa472690 100644 --- a/wrap/glw/framebuffer.h +++ b/wrap/glw/framebuffer.h @@ -1,8 +1,11 @@ #ifndef GLW_FRAMEBUFFER_H #define GLW_FRAMEBUFFER_H -#include "./object.h" +#include "./texture2D.h" +#include "./renderbuffer.h" + #include +#include namespace glw { @@ -134,12 +137,12 @@ class FramebufferArguments : public ObjectArguments RenderTargetMapping colorTargets; RenderTarget depthTarget; RenderTarget stencilTarget; - RenderTarget depthStencilTarget; RenderTargetBinding targetInputs; FramebufferArguments(void) + : BaseType() { - this->clear(); + ; } void clear(void) @@ -148,58 +151,102 @@ class FramebufferArguments : public ObjectArguments this->colorTargets .clear(); this->depthTarget .clear(); this->stencilTarget .clear(); - this->depthStencilTarget .clear(); this->targetInputs .clear(); } }; -class SafeFramebuffer : public virtual SafeObject -{ - public: - - typedef SafeObject BaseType; - typedef SafeFramebuffer ThisType; - - protected: - - SafeFramebuffer(Context * ctx) - : BaseType(ctx) - { - ; - } -}; - -class Framebuffer : public Object, public SafeFramebuffer +class Framebuffer : public Object { friend class Context; - friend class detail::SharedObjectBinding; public: - typedef Object BaseType; - typedef SafeFramebuffer SafeType; - typedef Framebuffer ThisType; + typedef Object BaseType; + typedef Framebuffer ThisType; + + virtual ~Framebuffer(void) + { + this->destroy(); + } virtual Type type(void) const { return FramebufferType; } + const FramebufferArguments & arguments(void) const + { + return this->m_config; + } + + bool setColorTarget(GLenum target, GLint unit, GLint index, const RenderTarget & renderTarget) + { + (void)unit; + GLW_ASSERT(this->isValid()); + this->m_config.colorTargets[index].clear(); + const bool r = this->attachTarget(target, GL_COLOR_ATTACHMENT0 + index, renderTarget); + if (!r) return false; + this->m_config.colorTargets[index] = renderTarget; + return true; + } + + bool removeColorTarget(GLenum target, GLint unit, GLint index) + { + (void)unit; + GLW_ASSERT(this->isValid()); + glFramebufferRenderbuffer(target, GL_COLOR_ATTACHMENT0 + index, GL_RENDERBUFFER, 0); + this->m_config.colorTargets[index].clear(); + return true; + } + + bool setDepthTarget(GLenum target, GLint unit, const RenderTarget & renderTarget) + { + (void)unit; + GLW_ASSERT(this->isValid()); + this->m_config.depthTarget.clear(); + const bool r = this->attachTarget(target, GL_DEPTH_ATTACHMENT, renderTarget); + if (!r) return false; + this->m_config.depthTarget = renderTarget; + return true; + } + + bool removeDepthTarget(GLenum target, GLint unit) + { + (void)unit; + GLW_ASSERT(this->isValid()); + glFramebufferRenderbuffer(target, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0); + this->m_config.depthTarget.clear(); + return true; + } + + bool setStencilTarget(GLenum target, GLint unit, const RenderTarget & renderTarget) + { + (void)unit; + GLW_ASSERT(this->isValid()); + this->m_config.stencilTarget.clear(); + const bool r = this->attachTarget(target, GL_STENCIL_ATTACHMENT, renderTarget); + if (!r) return false; + this->m_config.stencilTarget = renderTarget; + return true; + } + + bool removeStencilTarget(GLenum target, GLint unit) + { + (void)unit; + GLW_ASSERT(this->isValid()); + glFramebufferRenderbuffer(target, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0); + this->m_config.stencilTarget.clear(); + return true; + } + protected: Framebuffer(Context * ctx) - : SafeObject (ctx) - , BaseType (ctx) - , SafeType (ctx) + : BaseType(ctx) { ; } - virtual ~Framebuffer(void) - { - this->destroy(); - } - bool create(const FramebufferArguments & args) { this->destroy(); @@ -213,9 +260,9 @@ class Framebuffer : public Object, public SafeFramebuffer glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundNameRead); glGenFramebuffers(1, &(this->m_name)); - this->setBinding(GL_FRAMEBUFFER, 0); - this->bind(); - this->configure(); + glBindFramebuffer(GL_FRAMEBUFFER, this->m_name); + this->configure(GL_FRAMEBUFFER); + glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundNameDraw); glBindFramebuffer(GL_READ_FRAMEBUFFER, boundNameRead); @@ -223,45 +270,30 @@ class Framebuffer : public Object, public SafeFramebuffer return true; } - virtual void doDestroy(Context * ctx, GLuint name) + virtual void doDestroy(void) { - (void)ctx; - if (name == 0) return; - glDeleteFramebuffers(1, &name); + glDeleteFramebuffers(1, &(this->m_name)); this->m_config.clear(); } - virtual void doBind(void) + virtual bool doIsValid(void) const { - glBindFramebuffer(this->m_target, this->m_name); - } - - virtual void doUnbind(void) - { - glBindFramebuffer(this->m_target, 0); + return true; } private: FramebufferArguments m_config; - void configure(void) + void configure(GLenum target) { for (RenderTargetMapping::Iterator it=this->m_config.colorTargets.bindings.begin(); it!=this->m_config.colorTargets.bindings.end(); ++it) { - this->attachTarget(GL_COLOR_ATTACHMENT0 + it->first, it->second); + this->attachTarget(target, GL_COLOR_ATTACHMENT0 + it->first, it->second); } - if (this->m_config.depthStencilTarget.target) - { - this->attachTarget(GL_DEPTH_ATTACHMENT, this->m_config.depthStencilTarget); - this->attachTarget(GL_STENCIL_ATTACHMENT, this->m_config.depthStencilTarget); - } - else - { - this->attachTarget(GL_DEPTH_ATTACHMENT, this->m_config.depthTarget ); - this->attachTarget(GL_STENCIL_ATTACHMENT, this->m_config.stencilTarget); - } + this->attachTarget(target, GL_DEPTH_ATTACHMENT, this->m_config.depthTarget ); + this->attachTarget(target, GL_STENCIL_ATTACHMENT, this->m_config.stencilTarget); if (this->m_config.colorTargets.bindings.empty()) { @@ -287,30 +319,318 @@ class Framebuffer : public Object, public SafeFramebuffer } } - bool attachTarget(GLenum attachment, RenderTarget & target) + bool attachTarget(GLenum target, GLenum attachment, const RenderTarget & renderTarget) { - RenderableHandle & handle = target.target; + const RenderableHandle & handle = renderTarget.target; if (!handle) { - glFramebufferRenderbuffer(this->m_target, attachment, GL_RENDERBUFFER, 0); + glFramebufferRenderbuffer(target, attachment, GL_RENDERBUFFER, 0); return false; } switch (handle->type()) { - case RenderbufferType : glFramebufferRenderbuffer (this->m_target, attachment, GL_RENDERBUFFER, handle->name() ); break; - case Texture2DType : glFramebufferTexture2D (this->m_target, attachment, GL_TEXTURE_2D, handle->name(), target.level); break; - default : GLW_ASSERT(0); break; + case RenderbufferType : glFramebufferRenderbuffer (target, attachment, GL_RENDERBUFFER, handle->name() ); break; + case Texture2DType : glFramebufferTexture2D (target, attachment, GL_TEXTURE_2D, handle->name(), renderTarget.level); break; + default : GLW_ASSERT(0); break; } return true; } }; -typedef detail::SafeHandle FramebufferHandle; -typedef detail::UnsafeHandle BoundFramebuffer; +namespace detail { template <> struct BaseOf { typedef Object Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type FramebufferPtr; -} // end namespace glw +class SafeFramebuffer : public SafeObject +{ + friend class Context; + friend class BoundFramebuffer; + + public: + + typedef SafeObject BaseType; + typedef SafeFramebuffer ThisType; + + const FramebufferArguments & arguments(void) const + { + return this->object()->arguments(); + } + + protected: + + SafeFramebuffer(const FramebufferPtr & program) + : BaseType(program) + { + ; + } + + const FramebufferPtr & object(void) const + { + return static_cast(BaseType::object()); + } + + FramebufferPtr & object(void) + { + return static_cast(BaseType::object()); + } +}; + +namespace detail { template <> struct BaseOf { typedef SafeObject Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Framebuffer Type; }; }; +namespace detail { template <> struct ObjectSafe { typedef SafeFramebuffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type FramebufferHandle; + +class FramebufferBindingParams : public ObjectBindingParams +{ + public: + + typedef ObjectBindingParams BaseType; + typedef FramebufferBindingParams ThisType; + + FramebufferBindingParams(void) + : BaseType() + { + ; + } + + FramebufferBindingParams(GLenum target) + : BaseType(target, 0) + { + ; + } +}; + +class BoundFramebuffer : public BoundObject +{ + friend class Context; + + public: + + typedef BoundObject BaseType; + typedef BoundFramebuffer ThisType; + + BoundFramebuffer(void) + : BaseType() + { + ; + } + + const FramebufferHandle & handle(void) const + { + return static_cast(BaseType::handle()); + } + + FramebufferHandle & handle(void) + { + return static_cast(BaseType::handle()); + } + + GLenum getStatus(void) const + { + return glCheckFramebufferStatus(this->m_target); + } + + bool isComplete(void) const + { + return (this->getStatus() == GL_FRAMEBUFFER_COMPLETE); + } + + bool setColorTarget(GLint index, const RenderTarget & renderTarget) + { + return this->object()->setColorTarget(this->m_target, this->m_unit, index, renderTarget); + } + + bool removeColorTarget(GLint index) + { + return this->object()->removeColorTarget(this->m_target, this->m_unit, index); + } + + bool setDepthTarget(const RenderTarget & renderTarget) + { + return this->object()->setDepthTarget(this->m_target, this->m_unit, renderTarget); + } + + bool removeDepthTarget(void) + { + return this->object()->removeDepthTarget(this->m_target, this->m_unit); + } + + bool setStencilTarget(const RenderTarget & renderTarget) + { + return this->object()->setStencilTarget(this->m_target, this->m_unit, renderTarget); + } + + bool removeStencilTarget(void) + { + return this->object()->removeStencilTarget(this->m_target, this->m_unit); + } + + protected: + + BoundFramebuffer(const FramebufferHandle & handle, const FramebufferBindingParams & params) + : BaseType(handle, params) + { + ; + } + + const FramebufferPtr & object(void) const + { + return this->handle()->object(); + } + + FramebufferPtr & object(void) + { + return this->handle()->object(); + } + + virtual void bind(void) + { + glBindFramebuffer(this->m_target, this->object()->name()); + } + + virtual void unbind(void) + { + glBindFramebuffer(this->m_target, 0); + } +}; + +namespace detail { template <> struct ParamsOf { typedef FramebufferBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundObject Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Framebuffer Type; }; }; +namespace detail { template <> struct ObjectBound { typedef BoundFramebuffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundFramebufferHandle; + +class ReadFramebufferBindingParams : public FramebufferBindingParams +{ + public: + + typedef FramebufferBindingParams BaseType; + typedef ReadFramebufferBindingParams ThisType; + + ReadFramebufferBindingParams(void) + : BaseType(GL_READ_FRAMEBUFFER) + { + ; + } +}; + +class BoundReadFramebuffer : public BoundFramebuffer +{ + friend class Context; + + public: + + typedef BoundFramebuffer BaseType; + typedef BoundReadFramebuffer ThisType; + + BoundReadFramebuffer(void) + : BaseType() + { + ; + } + + protected: + + BoundReadFramebuffer(const FramebufferHandle & handle, const ReadFramebufferBindingParams & params) + : BaseType(handle, params) + { + ; + } +}; + +namespace detail { template <> struct ParamsOf { typedef ReadFramebufferBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundFramebuffer Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Framebuffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundReadFramebufferHandle; + +class DrawFramebufferBindingParams : public FramebufferBindingParams +{ + public: + + typedef FramebufferBindingParams BaseType; + typedef DrawFramebufferBindingParams ThisType; + + DrawFramebufferBindingParams(void) + : BaseType(GL_DRAW_FRAMEBUFFER) + { + ; + } +}; + +class BoundDrawFramebuffer : public BoundFramebuffer +{ + friend class Context; + + public: + + typedef BoundFramebuffer BaseType; + typedef BoundDrawFramebuffer ThisType; + + BoundDrawFramebuffer(void) + : BaseType() + { + ; + } + + protected: + + BoundDrawFramebuffer(const FramebufferHandle & handle, const DrawFramebufferBindingParams & params) + : BaseType(handle, params) + { + ; + } +}; + +namespace detail { template <> struct ParamsOf { typedef DrawFramebufferBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundFramebuffer Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Framebuffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundDrawFramebufferHandle; + +class ReadDrawFramebufferBindingParams : public FramebufferBindingParams +{ + public: + + typedef FramebufferBindingParams BaseType; + typedef ReadDrawFramebufferBindingParams ThisType; + + ReadDrawFramebufferBindingParams(void) + : BaseType(GL_FRAMEBUFFER) + { + ; + } +}; + +class BoundReadDrawFramebuffer : public BoundFramebuffer +{ + friend class Context; + + public: + + typedef BoundFramebuffer BaseType; + typedef BoundReadDrawFramebuffer ThisType; + + BoundReadDrawFramebuffer(void) + : BaseType() + { + ; + } + + protected: + + BoundReadDrawFramebuffer(const FramebufferHandle & handle, const ReadDrawFramebufferBindingParams & params) + : BaseType(handle, params) + { + ; + } +}; + +namespace detail { template <> struct ParamsOf { typedef ReadDrawFramebufferBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundFramebuffer Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Framebuffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundReadDrawFramebufferHandle; + +}; #endif // GLW_FRAMEBUFFER_H diff --git a/wrap/glw/geometryshader.h b/wrap/glw/geometryshader.h index b0990aa4..31bf723a 100644 --- a/wrap/glw/geometryshader.h +++ b/wrap/glw/geometryshader.h @@ -10,12 +10,13 @@ class GeometryShaderArguments : public ShaderArguments { public: - typedef ShaderArguments BaseType; + typedef ShaderArguments BaseType; typedef GeometryShaderArguments ThisType; GeometryShaderArguments(void) + : BaseType() { - this->clear(); + ; } void clear(void) @@ -24,33 +25,14 @@ class GeometryShaderArguments : public ShaderArguments } }; -class SafeGeometryShader : public virtual SafeShader -{ - public: - - typedef SafeShader BaseType; - typedef SafeGeometryShader ThisType; - - protected: - - SafeGeometryShader(Context * ctx) - : SafeObject (ctx) - , BaseType (ctx) - { - ; - } -}; - -class GeometryShader : public Shader, public SafeGeometryShader +class GeometryShader : public Shader { friend class Context; - friend class detail::SharedObjectBinding; public: - typedef Shader BaseType; - typedef SafeGeometryShader SafeType; - typedef GeometryShader ThisType; + typedef Shader BaseType; + typedef GeometryShader ThisType; virtual Type type(void) const { @@ -60,10 +42,7 @@ class GeometryShader : public Shader, public SafeGeometryShader protected: GeometryShader(Context * ctx) - : SafeObject (ctx) - , SafeShader (ctx) - , BaseType (ctx) - , SafeType (ctx) + : BaseType(ctx) { ; } @@ -79,9 +58,101 @@ class GeometryShader : public Shader, public SafeGeometryShader } }; -typedef detail::SafeHandle GeometryShaderHandle; -typedef detail::UnsafeHandle BoundGeometryShader; +namespace detail { template <> struct BaseOf { typedef Shader Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type GeometryShaderPtr; -} // end namespace glw +class SafeGeometryShader : public SafeShader +{ + friend class Context; + friend class BoundGeometryShader; + + public: + + typedef SafeShader BaseType; + typedef SafeGeometryShader ThisType; + + protected: + + SafeGeometryShader(const GeometryShaderPtr & geometryShader) + : BaseType(geometryShader) + { + ; + } + + const GeometryShaderPtr & object(void) const + { + return static_cast(BaseType::object()); + } + + GeometryShaderPtr & object(void) + { + return static_cast(BaseType::object()); + } +}; + +namespace detail { template <> struct BaseOf { typedef SafeShader Type; }; }; +namespace detail { template <> struct ObjectBase { typedef GeometryShader Type; }; }; +namespace detail { template <> struct ObjectSafe { typedef SafeGeometryShader Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type GeometryShaderHandle; + +class GeometryShaderBindingParams : public ShaderBindingParams +{ + public: + + typedef ShaderBindingParams BaseType; + typedef GeometryShaderBindingParams ThisType; + + GeometryShaderBindingParams(void) + : BaseType(GL_GEOMETRY_SHADER, 0) + { + ; + } +}; + +class BoundGeometryShader : public BoundShader +{ + friend class Context; + + public: + + typedef BoundShader BaseType; + typedef BoundGeometryShader ThisType; + + const GeometryShaderHandle & handle(void) const + { + return static_cast(BaseType::handle()); + } + + GeometryShaderHandle & handle(void) + { + return static_cast(BaseType::handle()); + } + + protected: + + BoundGeometryShader(const GeometryShaderHandle & handle, const ShaderBindingParams & params) + : BaseType(handle, params) + { + ; + } + + const GeometryShaderPtr & object(void) const + { + return this->handle()->object(); + } + + GeometryShaderPtr & object(void) + { + return this->handle()->object(); + } +}; + +namespace detail { template <> struct ParamsOf { typedef GeometryShaderBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundShader Type; }; }; +namespace detail { template <> struct ObjectBase { typedef GeometryShader Type; }; }; +namespace detail { template <> struct ObjectBound { typedef BoundGeometryShader Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundGeometryShaderHandle; + +}; #endif // GLW_GEOMETRYSHADER_H diff --git a/wrap/glw/object.h b/wrap/glw/object.h index 8cf2cda0..492108ac 100644 --- a/wrap/glw/object.h +++ b/wrap/glw/object.h @@ -3,6 +3,7 @@ #include "./bookkeeping.h" #include "./noncopyable.h" +#include "./objectdeleter.h" #include "./type.h" #include "./glheaders.h" @@ -20,7 +21,7 @@ class ObjectArguments ObjectArguments(void) { - this->clear(); + ; } void clear(void) @@ -29,26 +30,31 @@ class ObjectArguments } }; -class SafeObject +class Object : public detail::NonCopyable { - friend class Object; + friend class Context; public: - typedef void BaseType; - typedef SafeObject ThisType; + typedef detail::NonCopyable BaseType; + typedef Object ThisType; + + virtual ~Object(void) + { + this->destroy(); + } bool isValid(void) const { - return (this->m_name != 0); + return ((this->m_name != 0) && this->doIsValid()); } - Context * context(void) + const Context * context(void) const { return this->m_context; } - const Context * context(void) const + Context * context(void) { return this->m_context; } @@ -62,12 +68,48 @@ class SafeObject protected: - Context * m_context; GLuint m_name; + Context * m_context; - SafeObject(Context * ctx) - : m_context (ctx) - , m_name (0) + Object(Context * ctx) + : m_name (0) + , m_context (ctx) + { + ; + } + + void destroy(void) + { + if (this->m_name == 0) return; + this->doDestroy(); + this->m_name = 0; + this->m_context = 0; + } + + virtual void doDestroy(void) = 0; + virtual bool doIsValid(void) const = 0; +}; + +namespace detail { template struct ObjectBase { typedef NoBase Type; }; }; +namespace detail { template struct ObjectSafe { typedef NoBase Type; }; }; +namespace detail { template struct ObjectBound { typedef NoBase Type; }; }; + +namespace detail { template <> struct BaseOf { typedef NoBase Type; }; }; +namespace detail { template <> struct DeleterOf { typedef ObjectDeleter Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type ObjectPtr; + +class SafeObject : public detail::NonCopyable +{ + friend class Context; + friend class BoundObject; + + public: + + typedef detail::NonCopyable BaseType; + typedef SafeObject ThisType; + + SafeObject(void) + : m_object(0) { ; } @@ -76,67 +118,178 @@ class SafeObject { ; } + + bool isNull(void) const + { + return this->m_object.isNull(); + } + + bool isValid(void) const + { + return this->m_object->isValid(); + } + + const Context * context(void) const + { + if (this->isNull()) return 0; + return this->m_object->context(); + } + + Context * context(void) + { + if (this->isNull()) return 0; + return this->m_object->context(); + } + + GLuint name(void) const + { + if (this->isNull()) return 0; + return this->m_object->name(); + } + + Type type(void) const + { + if (this->isNull()) return NoType; + return this->m_object->type(); + } + + protected: + + SafeObject(const ObjectPtr & object) + : m_object(object) + { + ; + } + + const ObjectPtr & object(void) const + { + return this->m_object; + } + + ObjectPtr & object(void) + { + return this->m_object; + } + + private: + + ObjectPtr m_object; }; -class Object : public detail::NonCopyable, public virtual SafeObject +namespace detail { template <> struct BaseOf { typedef NoBase Type; }; }; +namespace detail { template <> struct DeleterOf { typedef DefaultDeleter Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Object Type; }; }; +namespace detail { template <> struct ObjectSafe { typedef SafeObject Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type ObjectHandle; + +class ObjectBindingParams +{ + public: + + typedef void BaseType; + typedef ObjectBindingParams ThisType; + + GLenum target; + GLint unit; + + ObjectBindingParams(void) + : target (GL_NONE) + , unit (0) + { + ; + } + + ObjectBindingParams(GLenum aTarget, GLenum aUnit) + : target (aTarget) + , unit (aUnit) + { + ; + } +}; + +class BoundObject : public detail::NonCopyable { friend class Context; public: - typedef void BaseType; - typedef SafeObject SafeType; - typedef Object ThisType; + typedef detail::NonCopyable BaseType; + typedef BoundObject ThisType; - protected: - - GLenum m_target; - GLint m_unit; - - Object(Context * ctx) - : SafeType (ctx) - , m_target (GL_NONE) - , m_unit (0) + BoundObject(void) + : m_handle (0) + , m_target (GL_NONE) + , m_unit (0) { ; } - void destroy(void) + virtual ~BoundObject(void) { - if (!this->isValid()) return; - - this->doDestroy(this->m_context, this->m_name); - - this->m_context = 0; - this->m_name = 0; + ; } - void setBinding(GLenum target, GLint unit) const + bool isNull(void) const { - ThisType * that = const_cast(this); - that->m_target = target; - that->m_unit = unit; + return this->m_handle.isNull(); } - void bind(void) + const ObjectHandle & handle(void) const { - GLW_ASSERT(this->isValid()); - this->doBind(); + return this->m_handle; } - void unbind(void) + ObjectHandle & handle(void) { - GLW_ASSERT(this->isValid()); - this->doUnbind(); + return this->m_handle; } - virtual void doDestroy (Context * ctx, GLuint name) = 0; - virtual void doBind (void) = 0; - virtual void doUnbind (void) = 0; + GLenum target(void) const + { + return this->m_target; + } + + GLint unit(void) const + { + return this->m_unit; + } + + protected: + + ObjectHandle m_handle; + GLenum m_target; + GLint m_unit; + + BoundObject(const ObjectHandle & handle, const ObjectBindingParams & params) + : m_handle (handle) + , m_target (params.target) + , m_unit (params.unit) + { + ; + } + + const ObjectPtr & object(void) const + { + return this->handle()->object(); + } + + ObjectPtr & object(void) + { + return this->handle()->object(); + } + + virtual void bind (void) = 0; + virtual void unbind (void) = 0; }; -typedef detail::SafeHandle ObjectHandle; -typedef detail::UnsafeHandle BoundObject; +namespace detail { template struct ParamsOf { typedef NoBase Type; }; }; + +namespace detail { template <> struct ParamsOf { typedef ObjectBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef NoBase Type; }; }; +namespace detail { template <> struct DeleterOf { typedef DefaultDeleter Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Object Type; }; }; +namespace detail { template <> struct ObjectBound { typedef BoundObject Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundObjectHandle; }; diff --git a/wrap/glw/objectdeleter.h b/wrap/glw/objectdeleter.h new file mode 100644 index 00000000..dae30fba --- /dev/null +++ b/wrap/glw/objectdeleter.h @@ -0,0 +1,28 @@ +#ifndef GLW_OBJECTDELETER_H +#define GLW_OBJECTDELETER_H + +#include "./common.h" + +namespace glw +{ + +class Object; + +namespace detail +{ + +class ObjectDeleter +{ + public: + + typedef void BaseType; + typedef ObjectDeleter ThisType; + + inline void operator () (Object * object) const; +}; + +}; + +}; + +#endif // GLW_OBJECTDELETER_H diff --git a/wrap/glw/program.h b/wrap/glw/program.h index a876c80c..2e7025d4 100644 --- a/wrap/glw/program.h +++ b/wrap/glw/program.h @@ -182,8 +182,9 @@ class ProgramArguments : public ObjectArguments FragmentOutputBinding fragmentOutputs; ProgramArguments(void) + : BaseType() { - this->clear(); + ; } void clear(void) @@ -198,12 +199,24 @@ class ProgramArguments : public ObjectArguments } }; -class SafeProgram : public virtual SafeObject +class Program : public Object { + friend class Context; + public: - typedef SafeObject BaseType; - typedef SafeProgram ThisType; + typedef Object BaseType; + typedef Program ThisType; + + virtual ~Program(void) + { + this->destroy(); + } + + virtual Type type(void) const + { + return ProgramType; + } const ProgramArguments & arguments(void) const { @@ -220,36 +233,6 @@ class SafeProgram : public virtual SafeObject return this->m_linked; } - protected: - - ProgramArguments m_arguments; - std::string m_log; - bool m_linked; - - SafeProgram(Context * ctx) - : BaseType (ctx) - , m_linked (false) - { - ; - } -}; - -class Program : public Object, public SafeProgram -{ - friend class Context; - friend class detail::SharedObjectBinding; - - public: - - typedef Object BaseType; - typedef SafeProgram SafeType; - typedef Program ThisType; - - virtual Type type(void) const - { - return ProgramType; - } - GLint getUniformLocation(const std::string & name) const { GLW_ASSERT(this->m_uniforms.count(name) > 0); @@ -296,18 +279,12 @@ class Program : public Object, public SafeProgram protected: Program(Context * ctx) - : SafeObject (ctx) - , BaseType (ctx) - , SafeType (ctx) + : BaseType (ctx) + , m_linked (false) { ; } - virtual ~Program(void) - { - this->destroy(); - } - bool create(const ProgramArguments & args) { this->destroy(); @@ -385,31 +362,22 @@ class Program : public Object, public SafeProgram this->postLink(); } - this->setBinding(GL_CURRENT_PROGRAM, 0); - this->bind(); - // TODO - // ... nothing to do ... - glUseProgram(boundName); return this->m_linked; } - virtual void doDestroy(Context * ctx, GLuint name) + virtual void doDestroy() { - (void)ctx; - if (name == 0) return; - glDeleteProgram(name); + glDeleteProgram(this->m_name); + this->m_arguments.clear(); + this->m_log.clear(); + this->m_linked = false; } - virtual void doBind(void) + virtual bool doIsValid(void) const { - glUseProgram(this->m_name); - } - - virtual void doUnbind(void) - { - glUseProgram(0); + return this->m_linked; } private: @@ -440,7 +408,10 @@ class Program : public Object, public SafeProgram typedef UniformMap::iterator UniformMapIterator; typedef UniformMap::value_type UniformMapValue; - UniformMap m_uniforms; + ProgramArguments m_arguments; + UniformMap m_uniforms; + std::string m_log; + bool m_linked; static std::string getInfoLog(GLuint Program) { @@ -496,9 +467,167 @@ class Program : public Object, public SafeProgram } }; -typedef detail::SafeHandle ProgramHandle; -typedef detail::UnsafeHandle BoundProgram; +namespace detail { template <> struct BaseOf { typedef Object Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type ProgramPtr; -} // end namespace glw +class SafeProgram : public SafeObject +{ + friend class Context; + friend class BoundProgram; + + public: + + typedef SafeObject BaseType; + typedef SafeProgram ThisType; + + const ProgramArguments & arguments(void) const + { + return this->object()->arguments(); + } + + const std::string & log(void) const + { + return this->object()->log(); + } + + bool isLinked(void) const + { + return this->object()->isLinked(); + } + + protected: + + SafeProgram(const ProgramPtr & program) + : BaseType(program) + { + ; + } + + const ProgramPtr & object(void) const + { + return static_cast(BaseType::object()); + } + + ProgramPtr & object(void) + { + return static_cast(BaseType::object()); + } +}; + +namespace detail { template <> struct BaseOf { typedef SafeObject Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Program Type; }; }; +namespace detail { template <> struct ObjectSafe { typedef SafeProgram Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type ProgramHandle; + +class ProgramBindingParams : public ObjectBindingParams +{ + public: + + typedef ObjectBindingParams BaseType; + typedef ProgramBindingParams ThisType; + + ProgramBindingParams(void) + : BaseType(GL_CURRENT_PROGRAM, 0) + { + ; + } +}; + +class BoundProgram : public BoundObject +{ + friend class Context; + + public: + + typedef BoundObject BaseType; + typedef BoundProgram ThisType; + + BoundProgram(void) + : BaseType() + { + ; + } + + const ProgramHandle & handle(void) const + { + return static_cast(BaseType::handle()); + } + + ProgramHandle & handle(void) + { + return static_cast(BaseType::handle()); + } + +#define _GLW_FORWARD_SCALAR_UNIFORM_(TYPE) \ + void setUniform (const std::string & name, TYPE x ) { this->object()->setUniform(name, x ); } \ + void setUniform (const std::string & name, TYPE x, TYPE y ) { this->object()->setUniform(name, x, y ); } \ + void setUniform (const std::string & name, TYPE x, TYPE y, TYPE z ) { this->object()->setUniform(name, x, y, z ); } \ + void setUniform (const std::string & name, TYPE x, TYPE y, TYPE z, TYPE w ) { this->object()->setUniform(name, x, y, z, w); } + +#define _GLW_FORWARD_VECTOR_UNIFORM_(TYPE) \ + void setUniform1 (const std::string & name, const TYPE * v, int count = 1) { this->object()->setUniform1(name, v, count); } \ + void setUniform2 (const std::string & name, const TYPE * v, int count = 1) { this->object()->setUniform2(name, v, count); } \ + void setUniform3 (const std::string & name, const TYPE * v, int count = 1) { this->object()->setUniform3(name, v, count); } \ + void setUniform4 (const std::string & name, const TYPE * v, int count = 1) { this->object()->setUniform4(name, v, count); } + +#define _GLW_FORWARD_MATRIX_UNIFORM_(TYPE) \ + void setUniform2x2 (const std::string & name, const TYPE * m, bool transpose, int count = 1) { this->object()->setUniform2x2(name, m, transpose, count); } \ + void setUniform2x3 (const std::string & name, const TYPE * m, bool transpose, int count = 1) { this->object()->setUniform2x3(name, m, transpose, count); } \ + void setUniform2x4 (const std::string & name, const TYPE * m, bool transpose, int count = 1) { this->object()->setUniform2x4(name, m, transpose, count); } \ + void setUniform3x2 (const std::string & name, const TYPE * m, bool transpose, int count = 1) { this->object()->setUniform3x2(name, m, transpose, count); } \ + void setUniform3x3 (const std::string & name, const TYPE * m, bool transpose, int count = 1) { this->object()->setUniform3x3(name, m, transpose, count); } \ + void setUniform3x4 (const std::string & name, const TYPE * m, bool transpose, int count = 1) { this->object()->setUniform3x4(name, m, transpose, count); } \ + void setUniform4x2 (const std::string & name, const TYPE * m, bool transpose, int count = 1) { this->object()->setUniform4x2(name, m, transpose, count); } \ + void setUniform4x3 (const std::string & name, const TYPE * m, bool transpose, int count = 1) { this->object()->setUniform4x3(name, m, transpose, count); } \ + void setUniform4x4 (const std::string & name, const TYPE * m, bool transpose, int count = 1) { this->object()->setUniform4x4(name, m, transpose, count); } + + _GLW_FORWARD_SCALAR_UNIFORM_(int) + _GLW_FORWARD_SCALAR_UNIFORM_(unsigned int) + _GLW_FORWARD_SCALAR_UNIFORM_(float) + _GLW_FORWARD_VECTOR_UNIFORM_(int) + _GLW_FORWARD_VECTOR_UNIFORM_(unsigned int) + _GLW_FORWARD_VECTOR_UNIFORM_(float) + _GLW_FORWARD_MATRIX_UNIFORM_(float) + +#undef _GLW_FORWARD_SCALAR_UNIFORM_ +#undef _GLW_FORWARD_VECTOR_UNIFORM_ +#undef _GLW_FORWARD_MATRIX_UNIFORM_ + + protected: + + BoundProgram(const ProgramHandle & handle, const ProgramBindingParams & params) + : BaseType(handle, params) + { + ; + } + + const ProgramPtr & object(void) const + { + return this->handle()->object(); + } + + ProgramPtr & object(void) + { + return this->handle()->object(); + } + + virtual void bind(void) + { + glUseProgram(this->object()->name()); + } + + virtual void unbind(void) + { + glUseProgram(0); + } +}; + +namespace detail { template <> struct ParamsOf { typedef ProgramBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundObject Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Program Type; }; }; +namespace detail { template <> struct ObjectBound { typedef BoundProgram Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundProgramHandle; + +}; #endif // GLW_PROGRAM_H diff --git a/wrap/glw/renderable.h b/wrap/glw/renderable.h index 129a859a..b84a2e56 100644 --- a/wrap/glw/renderable.h +++ b/wrap/glw/renderable.h @@ -18,23 +18,34 @@ class RenderableArguments : public ObjectArguments GLenum format; RenderableArguments(void) + : BaseType () + , format (GL_RGBA8) { - this->clear(); + ; + } + + RenderableArguments(GLenum aFormat) + : BaseType () + , format (aFormat) + { + ; } void clear(void) { BaseType::clear(); - this->format = GL_NONE; + this->format = GL_RGBA8; } }; -class SafeRenderable : public virtual SafeObject +class Renderable : public Object { + friend class Context; + public: - typedef SafeObject BaseType; - typedef SafeRenderable ThisType; + typedef Object BaseType; + typedef Renderable ThisType; GLenum format(void) const { @@ -48,7 +59,7 @@ class SafeRenderable : public virtual SafeObject GLenum m_format; - SafeRenderable(Context * ctx) + Renderable(Context * ctx) : BaseType (ctx) , m_format (GL_NONE) { @@ -56,27 +67,133 @@ class SafeRenderable : public virtual SafeObject } }; -class Renderable : public Object, public virtual SafeRenderable +namespace detail { template <> struct BaseOf { typedef Object Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type RenderablePtr; + +class SafeRenderable : public SafeObject { + friend class Context; + friend class BoundRenderable; + public: - typedef Object BaseType; - typedef SafeRenderable SafeType; - typedef Renderable ThisType; + typedef SafeObject BaseType; + typedef SafeRenderable ThisType; + + SafeRenderable(void) + : BaseType() + { + ; + } + + GLenum format(void) const + { + return this->object()->format(); + } + + int imageDimensions(void) const + { + return this->object()->imageDimensions(); + } + + bool isArray(void) const + { + return this->object()->isArray(); + } protected: - Renderable(Context * ctx) - : SafeObject (ctx) - , SafeType (ctx) - , BaseType (ctx) + SafeRenderable(const RenderablePtr & renderable) + : BaseType(renderable) + { + ; + } + + const RenderablePtr & object(void) const + { + return static_cast(BaseType::object()); + } + + RenderablePtr & object(void) + { + return static_cast(BaseType::object()); + } +}; + +namespace detail { template <> struct BaseOf { typedef SafeObject Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Renderable Type; }; }; +namespace detail { template <> struct ObjectSafe { typedef SafeRenderable Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type RenderableHandle; + +class RenderableBindingParams : public ObjectBindingParams +{ + public: + + typedef ObjectBindingParams BaseType; + typedef RenderableBindingParams ThisType; + + RenderableBindingParams(void) + : BaseType() + { + ; + } + + RenderableBindingParams(GLenum aTarget, GLenum aUnit) + : BaseType(aTarget, aUnit) { ; } }; -typedef detail::SafeHandle RenderableHandle; -typedef detail::UnsafeHandle BoundRenderable; +class BoundRenderable : public BoundObject +{ + friend class Context; + + public: + + typedef BoundObject BaseType; + typedef BoundRenderable ThisType; + + BoundRenderable(void) + : BaseType() + { + ; + } + + const RenderableHandle & handle(void) const + { + return static_cast(BaseType::handle()); + } + + RenderableHandle & handle(void) + { + return static_cast(BaseType::handle()); + } + + protected: + + BoundRenderable(const RenderableHandle & handle, const RenderableBindingParams & params) + : BaseType(handle, params) + { + ; + } + + const RenderablePtr & object(void) const + { + return this->handle()->object(); + } + + RenderablePtr & object(void) + { + return this->handle()->object(); + } +}; + +namespace detail { template <> struct ParamsOf { typedef RenderableBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundObject Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Renderable Type; }; }; +namespace detail { template <> struct ObjectBound { typedef BoundRenderable Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundRenderableHandle; }; diff --git a/wrap/glw/renderbuffer.h b/wrap/glw/renderbuffer.h index 03060740..14ac3261 100644 --- a/wrap/glw/renderbuffer.h +++ b/wrap/glw/renderbuffer.h @@ -17,8 +17,18 @@ class RenderbufferArguments : public RenderableArguments GLsizei height; RenderbufferArguments(void) + : BaseType () + , width (0) + , height (0) { - this->clear(); + } + + RenderbufferArguments(GLenum aFormat, GLsizei aWidth, GLsizei aHeight) + : BaseType (aFormat) + , width (aWidth) + , height (aHeight) + { + ; } void clear(void) @@ -29,48 +39,19 @@ class RenderbufferArguments : public RenderableArguments } }; -class SafeRenderbuffer : public virtual SafeRenderable -{ - public: - - typedef SafeRenderable BaseType; - typedef SafeRenderbuffer ThisType; - - GLsizei width(void) const - { - return this->m_width; - } - - GLsizei height(void) const - { - return this->m_height; - } - - protected: - - GLsizei m_width; - GLsizei m_height; - - SafeRenderbuffer(Context * ctx) - : SafeObject (ctx) - , BaseType (ctx) - , m_width (0) - , m_height (0) - { - ; - } -}; - -class Renderbuffer : public Renderable, public SafeRenderbuffer +class Renderbuffer : public Renderable { friend class Context; - friend class detail::SharedObjectBinding; public: - typedef Renderable BaseType; - typedef SafeRenderbuffer SafeType; - typedef Renderbuffer ThisType; + typedef Renderable BaseType; + typedef Renderbuffer ThisType; + + virtual ~Renderbuffer(void) + { + this->destroy(); + } virtual Type type(void) const { @@ -87,72 +68,180 @@ class Renderbuffer : public Renderable, public SafeRenderbuffer return false; } - protected: - - Renderbuffer(Context * ctx) - : SafeObject (ctx) - , SafeRenderable (ctx) - , BaseType (ctx) - , SafeType (ctx) + void setStorage(GLenum target, GLint unit, GLenum format, GLsizei width, GLsizei height) { - ; + (void)unit; + GLW_ASSERT(this->isValid()); + glRenderbufferStorage(target, format, width, height); + this->m_format = format; + this->m_width = width; + this->m_height = height; } - virtual ~Renderbuffer(void) + protected: + + GLsizei m_width; + GLsizei m_height; + + Renderbuffer(Context * ctx) + : BaseType (ctx) + , m_width (0) + , m_height (0) { - this->destroy(); + ; } bool create(const RenderbufferArguments & args) { this->destroy(); - GLint boundName = 0; glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundName); - glGenRenderbuffers(1, &(this->m_name)); - this->setBinding(GL_RENDERBUFFER, 0); - this->bind(); - this->allocate(args.format, args.width, args.height); - + glBindRenderbuffer(GL_RENDERBUFFER, this->m_name); + glRenderbufferStorage(GL_RENDERBUFFER, args.format, args.width, args.height); glBindRenderbuffer(GL_RENDERBUFFER, boundName); - + this->m_format = args.format; + this->m_width = args.width; + this->m_height = args.height; return true; } - virtual void doDestroy(Context * ctx, GLuint name) + virtual void doDestroy(void) { - (void)ctx; - if (name == 0) return; + glDeleteRenderbuffers(1, &(this->m_name)); this->m_format = GL_NONE; this->m_width = 0; this->m_height = 0; - glDeleteRenderbuffers(1, &name); } - virtual void doBind(void) + virtual bool doIsValid(void) const { - glBindRenderbuffer(this->m_target, this->m_name); - } - - virtual void doUnbind(void) - { - glBindRenderbuffer(this->m_target, 0); - } - - void allocate(GLenum format, GLsizei width, GLsizei height) - { - GLW_ASSERT(this->isValid()); - glRenderbufferStorage(this->m_target, format, width, height); - this->m_format = format; - this->m_width = width; - this->m_height = height; + return ((this->m_format != GL_NONE) && (this->m_width > 0) && (this->m_height > 0)); } }; -typedef detail::SafeHandle RenderbufferHandle; -typedef detail::UnsafeHandle BoundRenderbuffer; +namespace detail { template <> struct BaseOf { typedef Renderable Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type RenderbufferPtr; -} // end namespace glw +class SafeRenderbuffer : public SafeRenderable +{ + friend class Context; + friend class BoundRenderbuffer; + + public: + + typedef SafeRenderable BaseType; + typedef SafeRenderbuffer ThisType; + + SafeRenderbuffer(void) + : BaseType() + { + ; + } + + protected: + + SafeRenderbuffer(const RenderbufferPtr & renderbuffer) + : BaseType(renderbuffer) + { + ; + } + + const RenderbufferPtr & object(void) const + { + return static_cast(BaseType::object()); + } + + RenderbufferPtr & object(void) + { + return static_cast(BaseType::object()); + } +}; + +namespace detail { template <> struct BaseOf { typedef SafeRenderable Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Renderbuffer Type; }; }; +namespace detail { template <> struct ObjectSafe { typedef SafeRenderbuffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type RenderbufferHandle; + +class RenderbufferBindingParams : public RenderableBindingParams +{ + public: + + typedef RenderableBindingParams BaseType; + typedef RenderbufferBindingParams ThisType; + + RenderbufferBindingParams(void) + : BaseType(GL_RENDERBUFFER, 0) + { + ; + } +}; + +class BoundRenderbuffer : public BoundRenderable +{ + friend class Context; + + public: + + typedef BoundRenderable BaseType; + typedef BoundRenderbuffer ThisType; + + BoundRenderbuffer(void) + : BaseType() + { + ; + } + + const RenderbufferHandle & handle(void) const + { + return static_cast(BaseType::handle()); + } + + RenderbufferHandle & handle(void) + { + return static_cast(BaseType::handle()); + } + + void setStorage(GLenum format, GLsizei width, GLsizei height) + { + this->object()->setStorage(this->m_target, this->m_unit, format, width, height); + } + + protected: + + BoundRenderbuffer(const RenderbufferHandle & handle, const RenderbufferBindingParams & params) + : BaseType(handle, params) + { + ; + } + + const RenderbufferPtr & object(void) const + { + return this->handle()->object(); + } + + RenderbufferPtr & object(void) + { + return this->handle()->object(); + } + + virtual void bind(void) + { + glBindRenderbuffer(this->m_target, this->object()->name()); + } + + virtual void unbind(void) + { + glBindRenderbuffer(this->m_target, 0); + } +}; + +namespace detail { template <> struct ParamsOf { typedef RenderbufferBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundObject Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Renderbuffer Type; }; }; +namespace detail { template <> struct ObjectBound { typedef BoundRenderbuffer Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundRenderbufferHandle; + +}; #endif // GLW_RENDERBUFFER_H diff --git a/wrap/glw/shader.h b/wrap/glw/shader.h index 3ced40d7..c5b59a78 100644 --- a/wrap/glw/shader.h +++ b/wrap/glw/shader.h @@ -18,8 +18,9 @@ class ShaderArguments : public ObjectArguments std::string source; ShaderArguments(void) + : BaseType() { - this->clear(); + ; } void clear(void) @@ -29,12 +30,19 @@ class ShaderArguments : public ObjectArguments } }; -class SafeShader : public virtual SafeObject +class Shader : public Object { + friend class Context; + public: - typedef SafeObject BaseType; - typedef SafeShader ThisType; + typedef Object BaseType; + typedef Shader ThisType; + + virtual ~Shader(void) + { + this->destroy(); + } const std::string & source(void) const { @@ -57,77 +65,39 @@ class SafeShader : public virtual SafeObject std::string m_log; bool m_compiled; - SafeShader(Context * ctx) + Shader(Context * ctx) : BaseType (ctx) , m_compiled (false) { ; } -}; - -class Shader : public Object, public virtual SafeShader -{ - public: - - typedef Object BaseType; - typedef SafeShader SafeType; - typedef Shader ThisType; - - protected: - - Shader(Context * ctx) - : SafeObject (ctx) - , SafeType (ctx) - , BaseType (ctx) - { - ; - } - - virtual ~Shader(void) - { - this->destroy(); - } virtual GLenum shaderType(void) const = 0; bool create(const ShaderArguments & args) { this->destroy(); - const GLenum shType = this->shaderType(); - this->m_name = glCreateShader(shType); - this->setBinding(shType, 0); - this->bind(); this->compile(args.source); - return this->m_compiled; } - virtual void doDestroy(Context * ctx, GLuint name) + virtual void doDestroy(void) { - (void)ctx; - if (name == 0) return; + glDeleteShader(this->m_name); this->m_source.clear(); this->m_log.clear(); this->m_compiled = false; - glDeleteShader(name); } - virtual void doBind(void) + virtual bool doIsValid(void) const { - ; - } - - virtual void doUnbind(void) - { - ; + return this->m_compiled; } void compile(const std::string & source) { - GLW_ASSERT(this->isValid()); - const char * src = source.c_str(); glShaderSource(this->m_name, 1, &src, 0); glCompileShader(this->m_name); @@ -166,9 +136,144 @@ class Shader : public Object, public virtual SafeShader } }; -typedef detail::SafeHandle ShaderHandle; -typedef detail::UnsafeHandle BoundShader; +namespace detail { template <> struct BaseOf { typedef Object Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type ShaderPtr; -} // end namespace glw +class SafeShader : public SafeObject +{ + friend class Context; + friend class BoundShader; + + public: + + typedef SafeObject BaseType; + typedef SafeShader ThisType; + + SafeShader(void) + : BaseType() + { + ; + } + + const std::string & source(void) const + { + return this->object()->source(); + } + + const std::string & log(void) const + { + return this->object()->log(); + } + + bool isCompiled(void) const + { + return this->object()->isCompiled(); + } + + protected: + + SafeShader(const ShaderPtr & shader) + : BaseType(shader) + { + ; + } + + const ShaderPtr & object(void) const + { + return static_cast(BaseType::object()); + } + + ShaderPtr & object(void) + { + return static_cast(BaseType::object()); + } +}; + +namespace detail { template <> struct BaseOf { typedef SafeObject Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Shader Type; }; }; +namespace detail { template <> struct ObjectSafe { typedef SafeShader Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type ShaderHandle; + +class ShaderBindingParams : public ObjectBindingParams +{ + public: + + typedef ObjectBindingParams BaseType; + typedef ShaderBindingParams ThisType; + + ShaderBindingParams(void) + : BaseType() + { + ; + } + + ShaderBindingParams(GLenum aTarget, GLenum aUnit) + : BaseType(aTarget, aUnit) + { + ; + } +}; + +class BoundShader : public BoundObject +{ + friend class Context; + + public: + + typedef BoundObject BaseType; + typedef BoundShader ThisType; + + BoundShader(void) + : BaseType() + { + ; + } + + const ShaderHandle & handle(void) const + { + return static_cast(BaseType::handle()); + } + + ShaderHandle & handle(void) + { + return static_cast(BaseType::handle()); + } + + protected: + + BoundShader(const ShaderHandle & handle, const ShaderBindingParams & params) + : BaseType(handle, params) + { + ; + } + + const ShaderPtr & object(void) const + { + return this->handle()->object(); + } + + ShaderPtr & object(void) + { + return this->handle()->object(); + } + + virtual void bind(void) + { + ; + } + + virtual void unbind(void) + { + ; + } +}; + +namespace detail { template <> struct ParamsOf { typedef ShaderBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundObject Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Shader Type; }; }; +namespace detail { template <> struct ObjectBound { typedef BoundShader Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundShaderHandle; + +}; #endif // GLW_SHADER_H diff --git a/wrap/glw/texture.h b/wrap/glw/texture.h index a5e6b871..01546fe4 100644 --- a/wrap/glw/texture.h +++ b/wrap/glw/texture.h @@ -44,7 +44,7 @@ class TextureSampleMode } }; -inline TextureSampleMode texSampleMode(GLenum minFilter = GLW_DONT_CARE, GLenum magFilter = GLW_DONT_CARE, GLenum wrapS = GLW_DONT_CARE, GLenum wrapT = GLW_DONT_CARE, GLenum wrapR = GLW_DONT_CARE) +inline TextureSampleMode textureSampleMode(GLenum minFilter = GLW_DONT_CARE, GLenum magFilter = GLW_DONT_CARE, GLenum wrapS = GLW_DONT_CARE, GLenum wrapT = GLW_DONT_CARE, GLenum wrapR = GLW_DONT_CARE) { return TextureSampleMode(minFilter, magFilter, wrapS, wrapT, wrapR); } @@ -67,69 +67,158 @@ class TextureArguments : public RenderableArguments } }; -class SafeTexture : public virtual SafeRenderable +class Texture : public Renderable { - public: + friend class Context; - typedef SafeRenderable BaseType; - typedef SafeTexture ThisType; - - protected: - - SafeTexture(Context * ctx) - : SafeObject (ctx) - , BaseType (ctx) - { - ; - } -}; - -class Texture : public Renderable, public virtual SafeTexture -{ public: typedef Renderable BaseType; - typedef SafeTexture SafeType; typedef Texture ThisType; - protected: - - Texture(Context * ctx) - : SafeObject (ctx) - , SafeRenderable (ctx) - , SafeType (ctx) - , BaseType (ctx) - { - ; - } - virtual ~Texture(void) { this->destroy(); } - virtual void doDestroy(Context * ctx, GLuint name) + protected: + + Texture(Context * ctx) + : BaseType(ctx) { - (void)ctx; - if (name == 0) return; - glDeleteTextures(1, &name); + ; } - virtual void doBind(void) + virtual void doDestroy(void) + { + glDeleteTextures(1, &(this->m_name)); + } +}; + +namespace detail { template <> struct BaseOf { typedef Renderable Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type TexturePtr; + +class SafeTexture : public SafeRenderable +{ + friend class Context; + friend class BoundTexture; + + public: + + typedef SafeRenderable BaseType; + typedef SafeTexture ThisType; + + SafeTexture(void) + : BaseType() + { + ; + } + + protected: + + SafeTexture(const TexturePtr & texture) + : BaseType(texture) + { + ; + } + + const TexturePtr & object(void) const + { + return static_cast(BaseType::object()); + } + + TexturePtr & object(void) + { + return static_cast(BaseType::object()); + } +}; + +namespace detail { template <> struct BaseOf { typedef SafeRenderable Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Texture Type; }; }; +namespace detail { template <> struct ObjectSafe { typedef SafeTexture Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type TextureHandle; + +class TextureBindingParams : public RenderableBindingParams +{ + public: + + typedef RenderableBindingParams BaseType; + typedef TextureBindingParams ThisType; + + TextureBindingParams(void) + : BaseType() + { + ; + } + + TextureBindingParams(GLenum aTarget, GLenum aUnit) + : BaseType(aTarget, aUnit) + { + ; + } +}; + +class BoundTexture : public BoundRenderable +{ + friend class Context; + + public: + + typedef BoundRenderable BaseType; + typedef BoundTexture ThisType; + + BoundTexture(void) + : BaseType() + { + ; + } + + const TextureHandle & handle(void) const + { + return static_cast(BaseType::handle()); + } + + TextureHandle & handle(void) + { + return static_cast(BaseType::handle()); + } + + protected: + + BoundTexture(const TextureHandle & handle, const TextureBindingParams & params) + : BaseType(handle, params) + { + ; + } + + const TexturePtr & object(void) const + { + return this->handle()->object(); + } + + TexturePtr & object(void) + { + return this->handle()->object(); + } + + virtual void bind(void) { glActiveTexture(GL_TEXTURE0 + this->m_unit); - glBindTexture(this->m_target, this->m_name); + glBindTexture(this->m_target, this->object()->name()); } - virtual void doUnbind(void) + virtual void unbind(void) { glActiveTexture(GL_TEXTURE0 + this->m_unit); glBindTexture(this->m_target, 0); } }; -typedef detail::SafeHandle TextureHandle; -typedef detail::UnsafeHandle BoundTexture; +namespace detail { template <> struct ParamsOf { typedef TextureBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundObject Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Texture Type; }; }; +namespace detail { template <> struct ObjectBound { typedef BoundTexture Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundTextureHandle; }; diff --git a/wrap/glw/texture2d.h b/wrap/glw/texture2d.h index 4d622755..7fe571c7 100644 --- a/wrap/glw/texture2d.h +++ b/wrap/glw/texture2d.h @@ -37,49 +37,14 @@ class Texture2DArguments : public TextureArguments } }; -class SafeTexture2D : public virtual SafeTexture -{ - public: - - typedef SafeTexture BaseType; - typedef SafeTexture2D ThisType; - - GLsizei width(void) const - { - return this->m_width; - } - - GLsizei height(void) const - { - return this->m_height; - } - - protected: - - GLsizei m_width; - GLsizei m_height; - - SafeTexture2D(Context * ctx) - : SafeObject (ctx) - , SafeRenderable (ctx) - , BaseType (ctx) - , m_width (0) - , m_height (0) - { - ; - } -}; - -class Texture2D : public Texture, public SafeTexture2D +class Texture2D : public Texture { friend class Context; - friend class detail::SharedObjectBinding; public: - typedef Texture BaseType; - typedef SafeTexture2D SafeType; - typedef Texture ThisType; + typedef Texture BaseType; + typedef Texture2D ThisType; virtual Type type(void) const { @@ -96,41 +61,46 @@ class Texture2D : public Texture, public SafeTexture2D return false; } - void allocateLevel(GLint level, GLsizei width, GLsizei height, GLenum dataFormat, GLenum dataType, const void * data) + void setImage(GLenum target, GLint unit, GLint level, GLsizei width, GLsizei height, GLenum dataFormat, GLenum dataType, const void * data) { + (void)unit; GLW_ASSERT(this->isValid()); - glTexImage2D(this->m_target, level, this->m_format, width, height, 0, dataFormat, dataType, data); + glTexImage2D(target, level, this->m_format, width, height, 0, dataFormat, dataType, data); } - void setSubImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum dataFormat, GLenum dataType, const void * data) + void setSubImage(GLenum target, GLint unit, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum dataFormat, GLenum dataType, const void * data) { + (void)unit; GLW_ASSERT(this->isValid()); - glTexSubImage2D(this->m_target, level, xoffset, yoffset, width, height, dataFormat, dataType, data); + glTexSubImage2D(target, level, xoffset, yoffset, width, height, dataFormat, dataType, data); } - void generateMipmap(void) + void generateMipmap(GLenum target, GLint unit) { + (void)unit; GLW_ASSERT(this->isValid()); - glGenerateMipmap(this->m_target); + glGenerateMipmap(target); } - void setSampleMode(const TextureSampleMode & sampler) + void setSampleMode(GLenum target, GLint unit, const TextureSampleMode & sampler) { + (void)unit; GLW_ASSERT(this->isValid()); - if (GLW_CARE_OF(sampler.minFilter)) glTexParameteri(this->m_target, GL_TEXTURE_MIN_FILTER, sampler.minFilter); - if (GLW_CARE_OF(sampler.magFilter)) glTexParameteri(this->m_target, GL_TEXTURE_MAG_FILTER, sampler.magFilter); - if (GLW_CARE_OF(sampler.wrapS )) glTexParameteri(this->m_target, GL_TEXTURE_WRAP_S, sampler.wrapS ); - if (GLW_CARE_OF(sampler.wrapT )) glTexParameteri(this->m_target, GL_TEXTURE_WRAP_T, sampler.wrapT ); + if (GLW_CARE_OF(sampler.minFilter)) glTexParameteri(target, GL_TEXTURE_MIN_FILTER, sampler.minFilter); + if (GLW_CARE_OF(sampler.magFilter)) glTexParameteri(target, GL_TEXTURE_MAG_FILTER, sampler.magFilter); + if (GLW_CARE_OF(sampler.wrapS )) glTexParameteri(target, GL_TEXTURE_WRAP_S, sampler.wrapS ); + if (GLW_CARE_OF(sampler.wrapT )) glTexParameteri(target, GL_TEXTURE_WRAP_T, sampler.wrapT ); } protected: + GLsizei m_width; + GLsizei m_height; + Texture2D(Context * ctx) - : SafeObject (ctx) - , SafeRenderable (ctx) - , SafeTexture (ctx) - , BaseType (ctx) - , SafeType (ctx) + : BaseType (ctx) + , m_width (0) + , m_height (0) { ; } @@ -138,38 +108,146 @@ class Texture2D : public Texture, public SafeTexture2D bool create(const Texture2DArguments & args) { this->destroy(); - - GLint activeUnit = 0; - glGetIntegerv(GL_ACTIVE_TEXTURE, &activeUnit); - GLint boundName = 0; glGetIntegerv(GL_TEXTURE_BINDING_2D, &boundName); - - glGenRenderbuffers(1, &(this->m_name)); - this->setBinding(GL_TEXTURE_2D, 0); - this->bind(); - this->allocate(args.format, args.width, args.height, args.dataFormat, args.dataType, args.data); - this->setSampleMode(args.sampler); - - glActiveTexture(activeUnit); + glGenTextures(1, &(this->m_name)); + glBindTexture(GL_TEXTURE_2D, this->m_name); + glTexImage2D(GL_TEXTURE_2D, 0, args.format, args.width, args.height, 0, args.dataFormat, args.dataType, args.data); + this->m_format = args.format; + this->m_width = args.width; + this->m_height = args.height; + this->setSampleMode(GL_TEXTURE_2D, 0, args.sampler); glBindTexture(GL_TEXTURE_2D, boundName); - return true; } - void allocate(GLenum format, GLsizei width, GLsizei height, GLenum dataFormat, GLenum dataType, const void * data) + virtual void doDestroy(void) { - GLW_ASSERT(this->isValid()); - glTexImage2D(this->m_target, 0, format, width, height, 0, dataFormat, dataType, data); - this->m_format = format; - this->m_width = width; - this->m_height = height; + BaseType::doDestroy(); + this->m_format = GL_NONE; + this->m_width = 0; + this->m_height = 0; + } + + virtual bool doIsValid(void) const + { + return ((this->m_format != GL_NONE) && (this->m_width > 0) && (this->m_height > 0)); } }; -typedef detail::SafeHandle Texture2DHandle; -typedef detail::UnsafeHandle BoundTexture2D; +namespace detail { template <> struct BaseOf { typedef Texture Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type Texture2DPtr; -} // end namespace glw +class SafeTexture2D : public SafeTexture +{ + friend class Context; + friend class BoundTexture2D; + + public: + + typedef SafeTexture BaseType; + typedef SafeTexture2D ThisType; + + SafeTexture2D(void) + : BaseType() + { + ; + } + + protected: + + SafeTexture2D(const Texture2DPtr & texture2D) + : BaseType(texture2D) + { + ; + } + + const Texture2DPtr & object(void) const + { + return static_cast(BaseType::object()); + } + + Texture2DPtr & object(void) + { + return static_cast(BaseType::object()); + } +}; + +namespace detail { template <> struct BaseOf { typedef SafeTexture Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Texture2D Type; }; }; +namespace detail { template <> struct ObjectSafe { typedef SafeTexture2D Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type Texture2DHandle; + +class Texture2DBindingParams : public TextureBindingParams +{ + public: + + typedef TextureBindingParams BaseType; + typedef Texture2DBindingParams ThisType; + + Texture2DBindingParams(void) + : BaseType() + { + ; + } + + Texture2DBindingParams(GLenum aUnit) + : BaseType(GL_TEXTURE_2D, aUnit) + { + ; + } +}; + +class BoundTexture2D : public BoundTexture +{ + friend class Context; + + public: + + typedef BoundTexture BaseType; + typedef BoundTexture2D ThisType; + + BoundTexture2D(void) + : BaseType() + { + ; + } + + const Texture2DHandle & handle(void) const + { + return static_cast(BaseType::handle()); + } + + Texture2DHandle & handle(void) + { + return static_cast(BaseType::handle()); + } + + protected: + + BoundTexture2D(const Texture2DHandle & handle, const Texture2DBindingParams & params) + : BaseType(handle, params) + { + ; + } + + const Texture2DPtr & object(void) const + { + return this->handle()->object(); + } + + Texture2DPtr & object(void) + { + return this->handle()->object(); + } +}; + +namespace detail { template <> struct ParamsOf { typedef Texture2DBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundObject Type; }; }; +namespace detail { template <> struct ObjectBase { typedef Texture2D Type; }; }; +namespace detail { template <> struct ObjectBound { typedef BoundTexture2D Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundTexture2DHandle; + +}; #endif // GLW_TEXTURE2D_H diff --git a/wrap/glw/utility.h b/wrap/glw/utility.h index bce16844..ee18f67e 100644 --- a/wrap/glw/utility.h +++ b/wrap/glw/utility.h @@ -283,6 +283,6 @@ inline ProgramHandle loadProgram(Context & ctx, const std::string & srcPrefix, c return loadProgram(ctx, srcPrefix, vertexFile, "", fragmentFile.c_str(), args); } -} // end namespace glw +}; #endif // GLW_UTILITY_H diff --git a/wrap/glw/vertexshader.h b/wrap/glw/vertexshader.h index 2c723b36..225f3eb5 100644 --- a/wrap/glw/vertexshader.h +++ b/wrap/glw/vertexshader.h @@ -14,8 +14,9 @@ class VertexShaderArguments : public ShaderArguments typedef VertexShaderArguments ThisType; VertexShaderArguments(void) + : BaseType() { - this->clear(); + ; } void clear(void) @@ -24,33 +25,14 @@ class VertexShaderArguments : public ShaderArguments } }; -class SafeVertexShader : public virtual SafeShader -{ - public: - - typedef SafeShader BaseType; - typedef SafeVertexShader ThisType; - - protected: - - SafeVertexShader(Context * ctx) - : SafeObject (ctx) - , BaseType (ctx) - { - ; - } -}; - -class VertexShader : public Shader, public SafeVertexShader +class VertexShader : public Shader { friend class Context; - friend class detail::SharedObjectBinding; public: - typedef Shader BaseType; - typedef SafeVertexShader SafeType; - typedef VertexShader ThisType; + typedef Shader BaseType; + typedef VertexShader ThisType; virtual Type type(void) const { @@ -60,10 +42,7 @@ class VertexShader : public Shader, public SafeVertexShader protected: VertexShader(Context * ctx) - : SafeObject (ctx) - , SafeShader (ctx) - , BaseType (ctx) - , SafeType (ctx) + : BaseType(ctx) { ; } @@ -79,9 +58,101 @@ class VertexShader : public Shader, public SafeVertexShader } }; -typedef detail::SafeHandle VertexShaderHandle; -typedef detail::UnsafeHandle BoundVertexShader; +namespace detail { template <> struct BaseOf { typedef Shader Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type VertexShaderPtr; -} // end namespace glw +class SafeVertexShader : public SafeShader +{ + friend class Context; + friend class BoundVertexShader; + + public: + + typedef SafeShader BaseType; + typedef SafeVertexShader ThisType; + + protected: + + SafeVertexShader(const VertexShaderPtr & vertexShader) + : BaseType(vertexShader) + { + ; + } + + const VertexShaderPtr & object(void) const + { + return static_cast(BaseType::object()); + } + + VertexShaderPtr & object(void) + { + return static_cast(BaseType::object()); + } +}; + +namespace detail { template <> struct BaseOf { typedef SafeShader Type; }; }; +namespace detail { template <> struct ObjectBase { typedef VertexShader Type; }; }; +namespace detail { template <> struct ObjectSafe { typedef SafeVertexShader Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type VertexShaderHandle; + +class VertexShaderBindingParams : public ShaderBindingParams +{ + public: + + typedef ShaderBindingParams BaseType; + typedef VertexShaderBindingParams ThisType; + + VertexShaderBindingParams(void) + : BaseType(GL_VERTEX_SHADER, 0) + { + ; + } +}; + +class BoundVertexShader : public BoundShader +{ + friend class Context; + + public: + + typedef BoundShader BaseType; + typedef BoundVertexShader ThisType; + + const VertexShaderHandle & handle(void) const + { + return static_cast(BaseType::handle()); + } + + VertexShaderHandle & handle(void) + { + return static_cast(BaseType::handle()); + } + + protected: + + BoundVertexShader(const VertexShaderHandle & handle, const ShaderBindingParams & params) + : BaseType(handle, params) + { + ; + } + + const VertexShaderPtr & object(void) const + { + return this->handle()->object(); + } + + VertexShaderPtr & object(void) + { + return this->handle()->object(); + } +}; + +namespace detail { template <> struct ParamsOf { typedef VertexShaderBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundShader Type; }; }; +namespace detail { template <> struct ObjectBase { typedef VertexShader Type; }; }; +namespace detail { template <> struct ObjectBound { typedef BoundVertexShader Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundVertexShaderHandle; + +}; #endif // GLW_VERTEXSHADER_H