VideoCommon: rewrite DataReader

This commit is contained in:
degasus 2014-11-24 22:22:42 +01:00
parent 1ad8d4629c
commit 27f984c02e
4 changed files with 71 additions and 41 deletions

View File

@ -9,9 +9,63 @@
extern u8* g_video_buffer_read_ptr;
#if _M_SSE >= 0x301 && !(defined __GNUC__ && !defined __SSSE3__)
#include <tmmintrin.h>
#endif
class DataReader
{
public:
__forceinline DataReader()
: buffer(nullptr), end(nullptr) {}
__forceinline DataReader(u8* src, u8* _end)
: buffer(src), end(_end) {}
__forceinline void WritePointer(u8** src)
{
*src = buffer;
}
__forceinline u8* operator=(u8* src)
{
buffer = src;
return src;
}
__forceinline size_t size()
{
return end - buffer;
}
template <typename T, bool swapped = true> __forceinline T Peek(int offset = 0)
{
T data = *(T*)(buffer + offset);
if (swapped)
data = Common::FromBigEndian(data);
return data;
}
template <typename T, bool swapped = true> __forceinline T Read()
{
const T result = Peek<T, swapped>();
buffer += sizeof(T);
return result;
}
template <typename T, bool swapped = false> __forceinline void Write(T data)
{
if (swapped)
data = Common::FromBigEndian(data);
*(T*)(buffer) = data;
buffer += sizeof(T);
}
template <typename T = u8> __forceinline void Skip(size_t data = 1)
{
buffer += sizeof(T) * data;
}
private:
u8* __restrict buffer;
u8* end;
};
__forceinline void DataSkip(u32 skip)
{
@ -56,22 +110,6 @@ __forceinline T DataRead(u8** bufp = &g_video_buffer_read_ptr)
return result;
}
class DataReader
{
public:
inline DataReader() : buffer(g_video_buffer_read_ptr), offset(0) {}
inline ~DataReader() { g_video_buffer_read_ptr += offset; }
template <typename T> 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()
{
@ -111,18 +149,3 @@ __forceinline void DataWrite(T 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 <typename T> inline void Write(T data)
{
*(T*)(buffer+offset) = data;
offset += sizeof(T);
}
private:
u8 *buffer;
int offset;
};

View File

@ -43,13 +43,14 @@ template <typename T, int N>
__forceinline void ReadIndirect(const T* data)
{
static_assert(3 == N || 9 == N, "N is only sane as 3 or 9!");
DataWriter dst;
DataReader dst(VertexManager::s_pCurBufferPointer, nullptr);
for (int i = 0; i != N; ++i)
{
dst.Write(FracAdjust(Common::FromBigEndian(data[i])));
}
dst.WritePointer(&VertexManager::s_pCurBufferPointer);
LOG_NORM();
}

View File

@ -75,12 +75,14 @@ void LOADERDECL Pos_ReadDirect()
{
static_assert(N <= 3, "N > 3 is not sane!");
auto const scale = posScale[0];
DataWriter dst;
DataReader src;
DataReader dst(VertexManager::s_pCurBufferPointer, nullptr);
DataReader src(g_video_buffer_read_ptr, nullptr);
for (int i = 0; i < 3; ++i)
dst.Write(i<N ? PosScale(src.Read<T>(), scale) : 0.f);
dst.WritePointer(&VertexManager::s_pCurBufferPointer);
src.WritePointer(&g_video_buffer_read_ptr);
LOG_VTX();
}
@ -93,11 +95,12 @@ void LOADERDECL Pos_ReadIndex()
auto const index = DataRead<I>();
auto const data = reinterpret_cast<const T*>(cached_arraybases[ARRAY_POSITION] + (index * g_main_cp_state.array_strides[ARRAY_POSITION]));
auto const scale = posScale[0];
DataWriter dst;
DataReader dst(VertexManager::s_pCurBufferPointer, nullptr);
for (int i = 0; i < 3; ++i)
dst.Write(i<N ? PosScale(Common::FromBigEndian(data[i]), scale) : 0.f);
dst.WritePointer(&VertexManager::s_pCurBufferPointer);
LOG_VTX();
}

View File

@ -50,12 +50,14 @@ template <typename T, int N>
void LOADERDECL TexCoord_ReadDirect()
{
auto const scale = tcScale[tcIndex][0];
DataWriter dst;
DataReader src;
DataReader dst(VertexManager::s_pCurBufferPointer, nullptr);
DataReader src(g_video_buffer_read_ptr, nullptr);
for (int i = 0; i != N; ++i)
dst.Write(TCScale(src.Read<T>(), scale));
dst.WritePointer(&VertexManager::s_pCurBufferPointer);
src.WritePointer(&g_video_buffer_read_ptr);
LOG_TEX<N>();
++tcIndex;
@ -70,11 +72,12 @@ void LOADERDECL TexCoord_ReadIndex()
auto const data = reinterpret_cast<const T*>(cached_arraybases[ARRAY_TEXCOORD0 + tcIndex]
+ (index * g_main_cp_state.array_strides[ARRAY_TEXCOORD0 + tcIndex]));
auto const scale = tcScale[tcIndex][0];
DataWriter dst;
DataReader dst(VertexManager::s_pCurBufferPointer, nullptr);
for (int i = 0; i != N; ++i)
dst.Write(TCScale(Common::FromBigEndian(data[i]), scale));
dst.WritePointer(&VertexManager::s_pCurBufferPointer);
LOG_TEX<N>();
++tcIndex;
}