From c512dfbb3dcf6be8d833670e806631a75c305887 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Mon, 9 Sep 2013 01:31:15 +0200 Subject: [PATCH] Separate count and size --- code/nel/include/nel/3d/driver.h | 6 -- code/nel/include/nel/3d/gpu_program_params.h | 39 +++++----- code/nel/src/3d/gpu_program_params.cpp | 75 +++++++++----------- 3 files changed, 52 insertions(+), 68 deletions(-) diff --git a/code/nel/include/nel/3d/driver.h b/code/nel/include/nel/3d/driver.h index 2414d3df5..3e6b1e856 100644 --- a/code/nel/include/nel/3d/driver.h +++ b/code/nel/include/nel/3d/driver.h @@ -1124,9 +1124,6 @@ public: virtual void setVertexProgram3f(uint index, const NLMISC::CVector& v) = 0; virtual void setVertexProgram4f(uint index, const NLMISC::CVector& v, float f3) = 0; virtual void setVertexProgram4x4f(uint index, const NLMISC::CMatrix& m) = 0; - virtual void setVertexProgram1fv(uint index, size_t num, const float *src) = 0; - virtual void setVertexProgram2fv(uint index, size_t num, const float *src) = 0; - virtual void setVertexProgram3fv(uint index, size_t num, const float *src) = 0; virtual void setVertexProgram4fv(uint index, size_t num, const float *src) = 0; // @} @@ -1170,9 +1167,6 @@ public: virtual void setPixelProgram3f(uint index, const NLMISC::CVector& v) = 0; virtual void setPixelProgram4f(uint index, const NLMISC::CVector& v, float f3) = 0; virtual void setPixelProgram4x4f(uint index, const NLMISC::CMatrix& m) = 0; - virtual void setPixelProgram1fv(uint index, size_t num, const float *src) = 0; - virtual void setPixelProgram2fv(uint index, size_t num, const float *src) = 0; - virtual void setPixelProgram3fv(uint index, size_t num, const float *src) = 0; virtual void setPixelProgram4fv(uint index, size_t num, const float *src) = 0; // @} diff --git a/code/nel/include/nel/3d/gpu_program_params.h b/code/nel/include/nel/3d/gpu_program_params.h index 08d8010d4..ce454b2ed 100644 --- a/code/nel/include/nel/3d/gpu_program_params.h +++ b/code/nel/include/nel/3d/gpu_program_params.h @@ -56,11 +56,11 @@ namespace NL3D { class CGPUProgramParams { public: - enum TType { Float, Int }; - struct CMeta { uint Index, Count; TType Type; size_t Next, Prev; }; + enum TType { Float, Int, UInt }; + struct CMeta { uint Index, Size, Count; TType Type; size_t Next, Prev; }; // size is element size, count is nb of elements private: - union CVec { float F[4]; sint32 I[4]; }; + union CVec { float F[4]; sint32 I[4]; uint32 UI[4]; }; public: CGPUProgramParams(); @@ -81,9 +81,6 @@ public: void set3f(uint index, const NLMISC::CVector& v); void set4f(uint index, const NLMISC::CVector& v, float f3); void set4x4f(uint index, const NLMISC::CMatrix& m); - void set1fv(uint index, size_t num, const float *src); - void set2fv(uint index, size_t num, const float *src); - void set3fv(uint index, size_t num, const float *src); void set4fv(uint index, size_t num, const float *src); void unset(uint index); @@ -99,9 +96,6 @@ public: void set3f(const std::string &name, const NLMISC::CVector& v); void set4f(const std::string &name, const NLMISC::CVector& v, float f3); void set4x4f(const std::string &name, const NLMISC::CMatrix& m); - void set1fv(const std::string &name, size_t num, const float *src); - void set2fv(const std::string &name, size_t num, const float *src); - void set3fv(const std::string &name, size_t num, const float *src); void set4fv(const std::string &name, size_t num, const float *src); void unset(const std::string &name); @@ -110,8 +104,8 @@ public: // Internal /// Allocate specified number of components if necessary - size_t allocOffset(uint index, uint count, TType type); - size_t allocOffset(const std::string &name, uint count, TType type); + size_t allocOffset(uint index, uint size, uint count, TType type); + size_t allocOffset(const std::string &name, uint size, uint count, TType type); /// Return offset for specified index size_t getOffset(uint index) const; size_t getOffset(const std::string &name) const; @@ -119,20 +113,23 @@ public: void freeOffset(size_t offset); // Iteration (returns the offsets for access using getFooByOffset) - size_t getBegin() const { return m_Meta.size() ? m_First : s_End; } - size_t getNext(size_t offset) const { return m_Meta[offset].Next; } - size_t getEnd() const { return s_End; } + inline size_t getBegin() const { return m_Meta.size() ? m_First : s_End; } + inline size_t getNext(size_t offset) const { return m_Meta[offset].Next; } + inline size_t getEnd() const { return s_End; } // Data access - uint getCountByOffset(size_t offset) { return m_Meta[offset].Count; } // component count (number of floats or ints) - float *getPtrFByOffset(size_t offset) { return m_Vec[offset].F; } - int *getPtrIByOffset(size_t offset) { return m_Vec[offset].I; } - TType getTypeByOffset(size_t offset) { return m_Meta[offset].Type; } - uint getIndexByOffset(size_t offset) { return m_Meta[offset].Index; } - const std::string *getNameByOffset(size_t offset); // non-optimized for dev tools only, may return NULL if name unknown + inline uint getSizeByOffset(size_t offset) const { return m_Meta[offset].Size; } // size of element (4 for float4) + inline uint getCountByOffset(size_t offset) const { return m_Meta[offset].Count; } // number of elements (usually 1) + inline uint getNbComponentsByOffset(size_t offset) const { return m_Meta[offset].Size * m_Meta[offset].Count; } // nb of components (size * count) + inline float *getPtrFByOffset(size_t offset) { return m_Vec[offset].F; } + inline sint32 *getPtrIByOffset(size_t offset) { return m_Vec[offset].I; } + inline uint32 *getPtrUIByOffset(size_t offset) { return m_Vec[offset].UI; } + inline TType getTypeByOffset(size_t offset) const { return m_Meta[offset].Type; } + inline uint getIndexByOffset(size_t offset) const { return m_Meta[offset].Index; } + const std::string *getNameByOffset(size_t offset) const; // non-optimized for dev tools only, may return NULL if name unknown // Utility - static inline uint getNbRegistersByComponents(uint count) { return (count + 3) >> 2; } // vector register per 4 components + static inline uint getNbRegistersByComponents(uint nbComponents) { return (nbComponents + 3) >> 2; } // vector register per 4 components private: std::vector m_Vec; diff --git a/code/nel/src/3d/gpu_program_params.cpp b/code/nel/src/3d/gpu_program_params.cpp index 217a9683e..bfcc4d32c 100644 --- a/code/nel/src/3d/gpu_program_params.cpp +++ b/code/nel/src/3d/gpu_program_params.cpp @@ -53,118 +53,110 @@ CGPUProgramParams::~CGPUProgramParams() } -void CGPUProgramParams::set(uint index, float f0) +void CGPUProgramParams::set1f(uint index, float f0) { - float *f = getPtrFByOffset(allocOffset(index, 1, Float)); + float *f = getPtrFByOffset(allocOffset(index, 1, 1, Float)); f[0] = f0; } -void CGPUProgramParams::set(uint index, float f0, float f1) +void CGPUProgramParams::set2f(uint index, float f0, float f1) { - float *f = getPtrFByOffset(allocOffset(index, 2, Float)); + float *f = getPtrFByOffset(allocOffset(index, 2, 1, Float)); f[0] = f0; f[1] = f1; } -void CGPUProgramParams::set(uint index, float f0, float f1, float f2) +void CGPUProgramParams::set3f(uint index, float f0, float f1, float f2) { - float *f = getPtrFByOffset(allocOffset(index, 3, Float)); + float *f = getPtrFByOffset(allocOffset(index, 3, 1, Float)); f[0] = f0; f[1] = f1; f[2] = f2; } -void CGPUProgramParams::set(uint index, float f0, float f1, float f2, float f3) +void CGPUProgramParams::set4f(uint index, float f0, float f1, float f2, float f3) { - float *f = getPtrFByOffset(allocOffset(index, 4, Float)); + float *f = getPtrFByOffset(allocOffset(index, 4, 1, Float)); f[0] = f0; f[1] = f1; f[2] = f2; f[3] = f3; } -void CGPUProgramParams::set(uint index, int i0) +void CGPUProgramParams::set1i(uint index, int i0) { - int *i = getPtrIByOffset(allocOffset(index, 1, Int)); + int *i = getPtrIByOffset(allocOffset(index, 1, 1, Int)); i[0] = i0; } -void CGPUProgramParams::set(uint index, int i0, int i1) +void CGPUProgramParams::set2i(uint index, int i0, int i1) { - int *i = getPtrIByOffset(allocOffset(index, 2, Int)); + int *i = getPtrIByOffset(allocOffset(index, 2, 1, Int)); i[0] = i0; i[1] = i1; } -void CGPUProgramParams::set(uint index, int i0, int i1, int i2) +void CGPUProgramParams::set3i(uint index, int i0, int i1, int i2) { - int *i = getPtrIByOffset(allocOffset(index, 3, Int)); + int *i = getPtrIByOffset(allocOffset(index, 3, 1, Int)); i[0] = i0; i[1] = i1; i[2] = i2; } -void CGPUProgramParams::set(uint index, int i0, int i1, int i2, int i3) +void CGPUProgramParams::set4i(uint index, int i0, int i1, int i2, int i3) { - int *i = getPtrIByOffset(allocOffset(index, 4, Int)); + int *i = getPtrIByOffset(allocOffset(index, 4, 1, Int)); i[0] = i0; i[1] = i1; i[2] = i2; i[3] = i3; } -void CGPUProgramParams::set(uint index, const NLMISC::CVector& v) +void CGPUProgramParams::set3f(uint index, const NLMISC::CVector& v) { - float *f = getPtrFByOffset(allocOffset(index, 3, Float)); + float *f = getPtrFByOffset(allocOffset(index, 3, 1, Float)); f[0] = v.x; f[1] = v.y; f[2] = v.z; } -void CGPUProgramParams::set(uint index, const NLMISC::CMatrix& m) +void CGPUProgramParams::set4x4f(uint index, const NLMISC::CMatrix& m) { // TODO: Verify this! - float *f = getPtrFByOffset(allocOffset(index, 16, Float)); + float *f = getPtrFByOffset(allocOffset(index, 4, 4, Float)); NLMISC::CMatrix mt = m; mt.transpose(); mt.get(f); } -void CGPUProgramParams::set(uint index, const float *arr, size_t sz) +void CGPUProgramParams::set4fv(uint index, size_t num, const float *src) { - float *f = getPtrFByOffset(allocOffset(index, sz, Float)); - for (uint c = 0; c < sz; ++c) - f[c] = arr[c]; -} - -void CGPUProgramParams::set(uint index, const sint32 *arr, size_t sz) -{ - int *i = getPtrIByOffset(allocOffset(index, sz, Int)); - for (uint c = 0; c < sz; ++c) - i[c] = arr[c]; + float *f = getPtrFByOffset(allocOffset(index, 4, num, Float)); + size_t nb = 4 * num; + for (uint c = 0; c < nb; ++c) + f[c] = src[c]; } /// Allocate specified number of components if necessary -size_t CGPUProgramParams::allocOffset(uint index, uint count, TType type) +size_t CGPUProgramParams::allocOffset(uint index, uint size, uint count, TType type) { nlassert(count > 0); // this code will not properly handle 0 + nlassert(size > 0); // this code will not properly handle 0 nlassert(index < 0xFFFF); // sanity check + uint nbComponents = size * count; size_t offset = getOffset(index); if (offset != s_End) { - if (getCountByOffset(offset) == count) + if (getCountByOffset(offset) >= nbComponents) { m_Meta[offset].Type = type; + m_Meta[offset].Size = size; + m_Meta[offset].Count = count; return offset; } - if (getCountByOffset(offset) > count) - { - m_Meta[offset].Type = type; - m_Meta[offset].Count = count; // reduce count - return offset; - } - if (getCountByOffset(offset) < count) + if (getCountByOffset(offset) < nbComponents) { freeOffset(offset); } @@ -172,7 +164,7 @@ size_t CGPUProgramParams::allocOffset(uint index, uint count, TType type) // Allocate space offset = m_Meta.size(); - uint blocks = getNbRegistersByComponents(count); // per 4 components + uint blocks = getNbRegistersByComponents(nbComponents); // per 4 components m_Meta.resize(offset + blocks); m_Vec.resize(offset + blocks); @@ -183,6 +175,7 @@ size_t CGPUProgramParams::allocOffset(uint index, uint count, TType type) // Fill m_Meta[offset].Index = index; + m_Meta[offset].Size = size; m_Meta[offset].Count = count; m_Meta[offset].Type = type; m_Meta[offset].Prev = m_Last;