Skip to content

Commit

Permalink
xrRender_R1: unpack vertices data during level loading stage to make …
Browse files Browse the repository at this point in the history
…FFP work correctly (#1313)
  • Loading branch information
Xottab-DUTY committed Apr 28, 2023
1 parent 79c2aef commit f1fddbd
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 10 deletions.
65 changes: 65 additions & 0 deletions src/Common/OGF_GContainer_Vertices.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ D3DVERTEXELEMENT9 r1_decl_lmap[] = // 12+4+4+4+4+4 = 32
D3DDECL_END()
};

D3DVERTEXELEMENT9 r1_decl_lmap_unpacked[] = // 12+4+8+8 = 32
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
{ 0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
D3DDECL_END()
};

D3DVERTEXELEMENT9 r1_decl_vert[] = // 12+4+4+4+4+4 = 32
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
Expand All @@ -65,6 +74,15 @@ D3DVERTEXELEMENT9 r1_decl_vert[] = // 12+4+4+4+4+4 = 32
D3DDECL_END()
};

D3DVERTEXELEMENT9 r1_decl_vert_unpacked[] = // 12+4+4+8 = 28
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 0, 16, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 0, 20, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};

D3DVERTEXELEMENT9 x_decl_vert[] = // 12
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
Expand Down Expand Up @@ -107,6 +125,31 @@ struct r1v_lmap
#endif // LEVEL_COMPILER
};

struct r1v_lmap_unpacked
{
Fvector3 P;
u32 N;
float tc0x, tc0y;
float tc1x, tc1y;

r1v_lmap_unpacked& operator=(const r1v_lmap& packed)
{
P = packed.P;
N = Fcolor(packed.N).get();

Fcolor T(packed.T);
Fcolor B(packed.B);

tc0x = (packed.tc0x + T.a) * (32.f / 32768.f);
tc0y = (packed.tc0y + B.a) * (32.f / 32768.f);
tc1x = tc0x;
tc1y = tc0y;

return *this;
}

};

struct r1v_vert
{
Fvector3 P;
Expand Down Expand Up @@ -134,4 +177,26 @@ struct r1v_vert
}
#endif // XRLC_LIGHT_EXPORTS
};

struct r1v_vert_unpacked
{
Fvector3 P;
u32 N;
u32 C;
float tc0x, tc0y;

r1v_vert_unpacked& operator=(const r1v_vert& packed)
{
P = packed.P;
N = Fcolor(packed.N).get();
C = packed.C;

Fcolor T(packed.T);
Fcolor B(packed.B);

tc0x = (packed.tc0x + T.a) * (32.f / 32768.f);
tc0y = (packed.tc0y + B.a) * (32.f / 32768.f);
return *this;
}
};
#pragma pack(pop)
62 changes: 52 additions & 10 deletions src/Layers/xrRenderPC_R1/FStaticRender_Loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Layers/xrRender/FBasicVisual.h"
#include "xrCore/FMesh.hpp"
#include "Common/LevelStructure.hpp"
#include "Common/OGF_GContainer_Vertices.hpp"
#include "xrEngine/x_ray.h"
#include "xrEngine/IGame_Persistent.h"
#include "xrCore/stream_reader.h"
Expand Down Expand Up @@ -224,23 +225,64 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative)

_DC[i].resize(dcl_len);
fs->r(_DC[i].begin(), dcl_len * sizeof(VertexElement));
//.????????? remove T&B from _DC[]

// count, size
u32 vCount = fs->r_u32();
const u32 vCount = fs->r_u32();
u32 vSize = GetDeclVertexSize(dcl, 0);
#ifndef MASTER_GOLD
Msg("* [Loading VB] %d verts, %d Kb", vCount, (vCount * vSize) / 1024);
#endif

// Create and fill
_VB[i].Create(vCount * vSize);
u8* pData = static_cast<u8*>(_VB[i].Map());
fs->r(pData, vCount * vSize);
// CopyMemory (pData,fs->pointer(),vCount*vSize); //.???? copy while skip T&B
_VB[i].Unmap(true); // upload vertex data

// fs->advance (vCount*vSize);
if (o.ffp)
{
// Replace packed data with unpacked
xr_vector<u8> temp;
temp.resize(vCount * vSize);
fs->r(temp.data(), vCount * vSize);

if (dcl_equal(dcl, r1_decl_lmap))
{
dcl_len = std::size(r1_decl_lmap_unpacked);
_DC[i].resize(dcl_len);
CopyMemory(_DC[i].begin(), r1_decl_lmap_unpacked, dcl_len * sizeof(VertexElement));

vSize = GetDeclVertexSize(r1_decl_lmap_unpacked, 0);
_VB[i].Create(vCount * vSize);
auto* data = static_cast<r1v_lmap_unpacked*>(_VB[i].Map());
const auto* packedData = (r1v_lmap*)temp.data();
for (size_t i = 0; i < vCount; ++i)
data[i] = packedData[i];
}
else if (dcl_equal(dcl, r1_decl_vert))
{
dcl_len = std::size(r1_decl_vert_unpacked);
_DC[i].resize(dcl_len);
CopyMemory(_DC[i].begin(), r1_decl_vert_unpacked, dcl_len * sizeof(VertexElement));

vSize = GetDeclVertexSize(r1_decl_vert_unpacked, 0);
_VB[i].Create(vCount * vSize);
auto* data = static_cast<r1v_vert_unpacked*>(_VB[i].Map());
const auto* packedData = (r1v_vert*)temp.data();
for (size_t i = 0; i < vCount; ++i)
data[i] = packedData[i];
}
else
{
_VB[i].Create(vCount * vSize);
u8* pData = static_cast<u8*>(_VB[i].Map());
CopyMemory(pData, temp.data(), vCount * vSize);
}

_VB[i].Unmap(true); // upload vertex data
}
else
{
// Create and fill
_VB[i].Create(vCount * vSize);
u8* pData = static_cast<u8*>(_VB[i].Map());
fs->r(pData, vCount * vSize);
_VB[i].Unmap(true); // upload vertex data
}
}
fs->close();
}
Expand Down

0 comments on commit f1fddbd

Please sign in to comment.