diff --git a/Source/Core/VideoCommon/DataReader.h b/Source/Core/VideoCommon/DataReader.h index b552ca34e4..3c56db9ae3 100644 --- a/Source/Core/VideoCommon/DataReader.h +++ b/Source/Core/VideoCommon/DataReader.h @@ -56,6 +56,22 @@ __forceinline T DataRead() return result; } +class DataReader +{ +public: + inline DataReader() : buffer(g_pVideoData), offset(0) {} + inline ~DataReader() { g_pVideoData += offset; } + template inline T Read() + { + const T result = Common::FromBigEndian(*(T*)(buffer + offset)); + offset += sizeof(T); + return result; + } +private: + u8 *buffer; + int offset; +}; + // TODO: kill these __forceinline u8 DataReadU8() { @@ -132,17 +148,26 @@ __forceinline u8* DataGetPosition() return g_pVideoData; } -template -__forceinline void DataWrite(u8* &dst, T data) -{ - *(T*)dst = data; - dst += sizeof(T); -} - template __forceinline void DataWrite(T data) { - DataWrite(VertexManager::s_pCurBufferPointer, data); + *(T*)VertexManager::s_pCurBufferPointer = data; + VertexManager::s_pCurBufferPointer += sizeof(T); } +class DataWriter +{ +public: + inline DataWriter() : buffer(VertexManager::s_pCurBufferPointer), offset(0) {} + inline ~DataWriter() { VertexManager::s_pCurBufferPointer += offset; } + template inline void Write(T data) + { + *(T*)(buffer+offset) = data; + offset += sizeof(T); + } +private: + u8 *buffer; + int offset; +}; + #endif diff --git a/Source/Core/VideoCommon/VertexLoader_Normal.cpp b/Source/Core/VideoCommon/VertexLoader_Normal.cpp index fc9cf9e554..e4fb2c57ce 100644 --- a/Source/Core/VideoCommon/VertexLoader_Normal.cpp +++ b/Source/Core/VideoCommon/VertexLoader_Normal.cpp @@ -48,15 +48,13 @@ template __forceinline void ReadIndirect(const T* data) { static_assert(3 == N || 9 == N, "N is only sane as 3 or 9!"); - auto dst = VertexManager::s_pCurBufferPointer; + DataWriter dst; for (int i = 0; i != N; ++i) { - DataWrite(dst, FracAdjust(Common::FromBigEndian(data[i]))); + dst.Write(FracAdjust(Common::FromBigEndian(data[i]))); } - VertexManager::s_pCurBufferPointer = dst; - LOG_NORM(); } diff --git a/Source/Core/VideoCommon/VertexLoader_Position.cpp b/Source/Core/VideoCommon/VertexLoader_Position.cpp index 6163272fb1..ee26acfbe7 100644 --- a/Source/Core/VideoCommon/VertexLoader_Position.cpp +++ b/Source/Core/VideoCommon/VertexLoader_Position.cpp @@ -77,12 +77,11 @@ void LOADERDECL Pos_ReadDirect() { static_assert(N <= 3, "N > 3 is not sane!"); auto const scale = posScale; - auto dst = VertexManager::s_pCurBufferPointer; + DataWriter dst; + DataReader src; for (int i = 0; i < 3; ++i) - DataWrite(dst, i(), scale) : 0.f); - - VertexManager::s_pCurBufferPointer = dst; + dst.Write(i(), scale) : 0.f); LOG_VTX(); } @@ -96,12 +95,10 @@ void LOADERDECL Pos_ReadIndex() auto const index = DataRead(); auto const data = reinterpret_cast(cached_arraybases[ARRAY_POSITION] + (index * arraystrides[ARRAY_POSITION])); auto const scale = posScale; - auto dst = VertexManager::s_pCurBufferPointer; + DataWriter dst; for (int i = 0; i < 3; ++i) - DataWrite(dst, i void LOADERDECL TexCoord_ReadDirect() { auto const scale = tcScale[tcIndex]; - auto dst = VertexManager::s_pCurBufferPointer; - + DataWriter dst; + DataReader src; for (int i = 0; i != N; ++i) - DataWrite(dst, TCScale(DataRead(), scale)); - - VertexManager::s_pCurBufferPointer = dst; + dst.Write(TCScale(src.Read(), scale)); LOG_TEX(); @@ -78,12 +76,10 @@ void LOADERDECL TexCoord_ReadIndex() auto const data = reinterpret_cast(cached_arraybases[ARRAY_TEXCOORD0 + tcIndex] + (index * arraystrides[ARRAY_TEXCOORD0 + tcIndex])); auto const scale = tcScale[tcIndex]; - auto dst = VertexManager::s_pCurBufferPointer; + DataWriter dst; for (int i = 0; i != N; ++i) - DataWrite(dst, TCScale(Common::FromBigEndian(data[i]), scale)); - - VertexManager::s_pCurBufferPointer = dst; + dst.Write(TCScale(Common::FromBigEndian(data[i]), scale)); LOG_TEX(); ++tcIndex;