Add initial uniform buffer code

--HG--
branch : opengl3
hg/feature/opengl3
kaetemi 10 years ago
parent 660e72e756
commit 6ea037e48d

@ -0,0 +1,115 @@
/*
Copyright (C) 2015 Jan Boon <jan.boon@kaetemi.be>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef NL_UNIFORM_BUFFER_H
#define NL_UNIFORM_BUFFER_H
#include <nel/misc/types_nl.h>
#include <nel/misc/smart_ptr.h>
#include <nel/misc/vector.h>
#include <nel/misc/vector_2d.h>
#include <nel/misc/vector_h.h>
#include <nel/misc/matrix.h>
#include <nel/3d/uniform_buffer_format.h>
#define NL3D_UNIFORM_BUFFER_DEBUG 1
namespace NLMISC {
class CMatrix;
}
namespace NL3D {
class CUniformBuffer;
class IUBDrvInfos;
typedef std::list<IUBDrvInfos*> TUBDrvInfoPtrList;
typedef TUBDrvInfoPtrList::iterator ItUBDrvInfoPtrList;
/*
**** IMPORTANT ********************
**** IF YOU MODIFY THE STRUCTURE OF THIS CLASS, PLEASE INCREMENT IDriver::InterfaceVersion TO INVALIDATE OLD DRIVER DLL
***********************************
*/
// Uniform buffer
class CUniformBuffer : public NLMISC::CRefCount
{
public:
CUniformBuffer();
~CUniformBuffer();
void *lock();
void unlock();
inline void set(sint offset, float f) { reinterpret_cast<float &>(m_HostMemory[offset]) = f; }
inline void set(sint offset, float f0, float f1) { float *f = reinterpret_cast<float *>(&m_HostMemory[offset]); f[0] = f0; f[1] = f1; }
inline void set(sint offset, float f0, float f1, float f2) { float *f = reinterpret_cast<float *>(&m_HostMemory[offset]); f[0] = f0; f[1] = f1; f[2] = f2; }
inline void set(sint offset, float f0, float f1, float f2, float f3) { float *f = reinterpret_cast<float *>(&m_HostMemory[offset]); f[0] = f0; f[1] = f1; f[2] = f2; f[3] = f3; }
inline void set(sint offset, NLMISC::CVector2d vec2) { reinterpret_cast<NLMISC::CVector2d &>(m_HostMemory[offset]) = vec2; }
inline void set(sint offset, NLMISC::CVector vec3) { reinterpret_cast<NLMISC::CVector &>(m_HostMemory[offset]) = vec3; }
inline void set(sint offset, NLMISC::CVectorH vec4) { reinterpret_cast<NLMISC::CVectorH &>(m_HostMemory[offset]) = vec4; }
inline void set(sint offset, NLMISC::CMatrix mat4) { float *f = reinterpret_cast<float *>(&m_HostMemory[offset]); mat4.get(f); }
private:
std::vector<char> m_HostMemory;
public:
CUniformBufferFormat Format;
public: // Driver-only
NLMISC::CRefPtr<IUBDrvInfos> DrvInfos;
bool Touched;
#if NL3D_UNIFORM_BUFFER_DEBUG
sint Locked;
#endif
};
// TODO: Investigate if more efficient or not to do like vertex_buffer and call into driver for locking (what are best practices for updating uniform buffers?)
class IUBDrvInfos : public NLMISC::CRefCount
{
protected:
IDriver *_Driver;
private:
ItUBDrvInfoPtrList _DriverIterator;
public:
NLMISC::CRefPtr<CUniformBuffer> UniformBufferPtr;
IUBDrvInfos(IDriver *drv, ItUBDrvInfoPtrList it, CUniformBuffer *ub) { _Driver = drv; _DriverIterator = it; UniformBufferPtr = ub; }
virtual ~IUBDrvInfos();
};
} /* namespace NL3D */
#endif /* #ifndef NL_UNIFORM_BUFFER_H */
/* end of file */

@ -34,6 +34,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace NL3D {
/*
**** IMPORTANT ********************
**** IF YOU MODIFY THE STRUCTURE OF THIS CLASS, PLEASE INCREMENT IDriver::InterfaceVersion TO INVALIDATE OLD DRIVER DLL
***********************************
*/
// Uniform buffer format generation following glsl std140 rules
class CUniformBufferFormat
{
@ -47,9 +53,9 @@ public:
enum TType
{
Float, // float
FloatVec2, // CVector2D
FloatVec3,
FloatVec4, // CVector
FloatVec2, // CVector2d
FloatVec3, // CVector
FloatVec4, // CVectorH
SInt, // sint32
SIntVec2,
SIntVec3,
@ -90,6 +96,10 @@ public:
{
return stride() * Count;
}
inline sint offset(int i) const
{
return Offset + (stride() * i);
}
};
// Push a variable. Returns the byte offset in uniform buffer
@ -97,10 +107,15 @@ public:
sint push(const std::string &name, TType type, sint count = 1);
inline const CEntry &get(sint i) const { return m_Entries[i]; }
inline size_t size() const { return m_Entries.size(); }
inline size_t count() const { return m_Entries.size(); } // Return number of entries
inline void clear() { m_Entries.clear(); m_Hash = 0; }
inline sint size() const { return m_Entries.size() ? (m_Entries.back().Offset + m_Entries.back().size()) : 0; } // Return size of format in bytes
inline size_t hash() const { return m_Hash; }
// Get the offset by entry id (counted from 0 in the order of addition to the format) and index of array
inline sint offset(sint entry, sint index = 0) const { m_Entries[entry].offset(index); }
private:
static const sint s_TypeAlignment[];
static const sint s_TypeSize[];

@ -32,7 +32,7 @@ namespace NL3D
{
// ***************************************************************************
const uint32 IDriver::InterfaceVersion = 0x6e; // gpu program interface
const uint32 IDriver::InterfaceVersion = 0x6f; // uniform buffer
// ***************************************************************************
IDriver::IDriver() : _SyncTexDrvInfos( "IDriver::_SyncTexDrvInfos" )

@ -89,7 +89,7 @@ void generateUniformBufferGLSL(std::stringstream &ss, const CUniformBufferFormat
{
ss << "layout(std140, binding = " << s_UniformBufferBindDefine[binding] << ") uniform " << s_UniformBufferName[binding] << "\n";
ss << "{\n";
for (sint i = 0; i < ubf.size(); ++i)
for (sint i = 0; i < ubf.count(); ++i)
{
const CUniformBufferFormat::CEntry &entry = ubf.get(i);
ss << "\t" << s_TypeKeyword[entry.Type] << " " << NLMISC::CStringMapper::unmap(entry.Name);

@ -0,0 +1,82 @@
/*
Copyright (C) 2015 Jan Boon <jan.boon@kaetemi.be>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "std3d.h"
#include <nel/3d/uniform_buffer.h>
#include <nel/misc/debug.h>
namespace NL3D {
CUniformBuffer::CUniformBuffer()
{
// ...
}
CUniformBuffer::~CUniformBuffer()
{
/* ***********************************************
* WARNING: This Class/Method must be thread-safe (ctor/dtor/serial): no static access for instance
* It can be loaded/called through CAsyncFileManager for instance
* ***********************************************/
if (DrvInfos)
DrvInfos->UniformBufferPtr = NULL; // Tell the driver info to not restaure memory when it will die
// Must kill the drv mirror of this VB.
DrvInfos.kill();
}
void *CUniformBuffer::lock()
{
m_HostMemory.reserve(Format.size());
return &m_HostMemory[0];
#if NL3D_UNIFORM_BUFFER_DEBUG
++Locked;
#endif
}
void CUniformBuffer::unlock()
{
#if NL3D_UNIFORM_BUFFER_DEBUG
--Locked;
nlassert(Locked >= 0);
#endif
Touched = true;
}
IUBDrvInfos::~IUBDrvInfos()
{
// TODO: _Driver->removeUBDrvInfoPtr(_DriverIterator);
}
} /* namespace NL3D */
/* end of file */
Loading…
Cancel
Save