Skip to content

Commit f4a57b0

Browse files
committed
Move skeleton building and blending to engine
Also implement a render entity cache to avoid back-and-forth synchronisation.
1 parent dc586db commit f4a57b0

File tree

20 files changed

+691
-123
lines changed

20 files changed

+691
-123
lines changed

src/common/Compiler.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,15 @@ inline int CountTrailingZeroes(unsigned long long x)
6464
{
6565
unsigned long ans;
6666
#ifdef _WIN64
67-
_BitScanForward64(&ans, x); return ans;
67+
_BitScanForward64(&ans, x);
68+
return ans;
6869
#else
6970
bool nonzero = _BitScanForward(&ans, static_cast<unsigned long>(x));
70-
if (!nonzero) { _BitScanForward(&ans, x >> 32); }
71-
#endif
71+
if (!nonzero) {
72+
_BitScanForward(&ans, x >> 32);
73+
}
7274
return ans;
75+
#endif
7376
}
7477
#else
7578
inline int CountTrailingZeroes(unsigned int x)

src/engine/RefAPI.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ struct refexport_t {
113113
// Nothing is drawn until R_RenderScene is called.
114114
void ( *ClearScene )( );
115115
void ( *AddRefEntityToScene )( const refEntity_t* re );
116+
void ( *SyncRefEntities )( const std::vector<EntityUpdate>& ents );
117+
std::vector<LerpTagSync>( *SyncLerpTags )( const std::vector<LerpTagUpdate>& lerpTags );
116118

117119
void ( *AddPolyToScene )( qhandle_t hShader, int numVerts, const polyVert_t* verts );
118120
void ( *AddPolysToScene )( qhandle_t hShader, int numVerts, const polyVert_t* verts, int numPolys );
@@ -139,7 +141,6 @@ struct refexport_t {
139141
int ( *MarkFragments )( int numPoints, const vec3_t* points, const vec3_t projection,
140142
int maxPoints, vec3_t pointBuffer, int maxFragments, markFragment_t* fragmentBuffer );
141143

142-
int ( *LerpTag )( orientation_t* tag, const refEntity_t* refent, const char* tagName, int startIndex );
143144
void ( *ModelBounds )( qhandle_t model, vec3_t mins, vec3_t maxs );
144145

145146
void ( *RemapShader )( const char* oldShader, const char* newShader, const char* offsetTime );

src/engine/client/cg_msgdef.h

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,84 @@ namespace Util {
9191
}
9292
};
9393

94+
template<> struct SerializeTraits<std::vector<BoneMod>> {
95+
static void Write( Writer& stream, const std::vector<BoneMod>& boneMods ) {
96+
stream.WriteSize( boneMods.size() );
97+
stream.WriteData( boneMods.data(), boneMods.size() * sizeof( BoneMod ) );
98+
}
99+
100+
static std::vector<BoneMod> Read( Reader& stream ) {
101+
std::vector<BoneMod> boneMods;
102+
const size_t size = stream.ReadSize<BoneMod>();
103+
boneMods.resize( size );
104+
stream.ReadData( boneMods.data(), size * sizeof( BoneMod ) );
105+
return boneMods;
106+
}
107+
};
108+
94109
// Use that bone optimization for refEntity_t
95110
template<> struct SerializeTraits<refEntity_t> {
96111
static void Write(Writer& stream, const refEntity_t& ent)
97112
{
98-
stream.WriteData(&ent, offsetof(refEntity_t, skeleton));
99-
stream.Write<refSkeleton_t>(ent.skeleton);
113+
stream.WriteData(&ent, offsetof(refEntity_t, tag));
114+
stream.Write<std::string>( ent.tag );
115+
stream.Write<std::vector<BoneMod>>( ent.boneMods );
116+
// stream.Write<refSkeleton_t>(ent.skeleton);
100117
}
118+
101119
static refEntity_t Read(Reader& stream)
102120
{
103121
refEntity_t ent;
104-
stream.ReadData(&ent, offsetof(refEntity_t, skeleton));
105-
ent.skeleton = stream.Read<refSkeleton_t>();
122+
stream.ReadData(&ent, offsetof(refEntity_t, tag));
123+
ent.tag = stream.Read<std::string>();
124+
ent.boneMods = stream.Read<std::vector<BoneMod>>();
125+
// ent.skeleton = stream.Read<refSkeleton_t>();
126+
return ent;
127+
}
128+
};
129+
130+
template<> struct SerializeTraits<EntityUpdate> {
131+
static void Write( Writer& stream, const EntityUpdate& ent ) {
132+
stream.Write<refEntity_t>( ent.ent );
133+
stream.Write<uint16_t>( ent.id );
134+
}
135+
136+
static EntityUpdate Read( Reader& stream ) {
137+
EntityUpdate ent;
138+
ent.ent = stream.Read<refEntity_t>();
139+
ent.id = stream.Read<uint16_t>();
106140
return ent;
107141
}
108142
};
109143

144+
template<> struct SerializeTraits<LerpTagUpdate> {
145+
static void Write( Writer& stream, const LerpTagUpdate& tag ) {
146+
stream.Write<std::string>( tag.tag );
147+
stream.Write<uint16_t>( tag.id );
148+
}
149+
150+
static LerpTagUpdate Read( Reader& stream ) {
151+
LerpTagUpdate tag;
152+
tag.tag = stream.Read<std::string>();
153+
tag.id = stream.Read<uint16_t>();
154+
return tag;
155+
}
156+
};
157+
158+
template<> struct SerializeTraits<LerpTagSync> {
159+
static void Write( Writer& stream, const LerpTagSync& tag ) {
160+
stream.Write<orientation_t>( tag.entityOrientation );
161+
stream.Write<orientation_t>( tag.orientation );
162+
}
163+
164+
static LerpTagSync Read( Reader& stream ) {
165+
LerpTagSync tag;
166+
tag.entityOrientation = stream.Read<orientation_t>();
167+
tag.orientation = stream.Read<orientation_t>();
168+
return tag;
169+
}
170+
};
171+
110172
template<>
111173
struct SerializeTraits<Color::Color> {
112174
static void Write(Writer& stream, const Color::Color& value)
@@ -166,6 +228,8 @@ enum cgameImport_t
166228
CG_R_REGISTERFONT,
167229
CG_R_CLEARSCENE,
168230
CG_R_ADDREFENTITYTOSCENE,
231+
CG_R_SYNCREFENTITIES,
232+
CG_R_SYNCLERPTAGS,
169233
CG_R_ADDPOLYTOSCENE,
170234
CG_R_ADDPOLYSTOSCENE,
171235
CG_R_ADDLIGHTTOSCENE,
@@ -179,7 +243,6 @@ enum cgameImport_t
179243
CG_R_DRAWSTRETCHPIC,
180244
CG_R_DRAWROTATEDPIC,
181245
CG_R_MODELBOUNDS,
182-
CG_R_LERPTAG,
183246
CG_R_REMAP_SHADER,
184247
CG_R_BATCHINPVS,
185248
CG_R_REGISTERANIMATION,
@@ -318,10 +381,6 @@ namespace Render {
318381
IPC::Message<IPC::Id<VM::QVM, CG_R_MODELBOUNDS>, int>,
319382
IPC::Reply<std::array<float, 3>, std::array<float, 3>>
320383
>;
321-
using LerpTagMsg = IPC::SyncMessage<
322-
IPC::Message<IPC::Id<VM::QVM, CG_R_LERPTAG>, refEntity_t, std::string, int>,
323-
IPC::Reply<orientation_t, int>
324-
>;
325384
using RemapShaderMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_REMAP_SHADER>, std::string, std::string, std::string>;
326385
// TODO not a renderer call, handle in CM in the VM?
327386
using BatchInPVSMsg = IPC::SyncMessage<
@@ -374,6 +433,11 @@ namespace Render {
374433
using ScissorSetMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_SCISSOR_SET>, int, int, int, int>;
375434
using ClearSceneMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_CLEARSCENE>>;
376435
using AddRefEntityToSceneMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_ADDREFENTITYTOSCENE>, refEntity_t>;
436+
using SyncRefEntitiesMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_SYNCREFENTITIES>, std::vector<EntityUpdate>>;
437+
using SyncLerpTagsMsg = IPC::SyncMessage<IPC::Message<IPC::Id<VM::QVM, CG_R_SYNCLERPTAGS>,
438+
std::vector<LerpTagUpdate>>,
439+
IPC::Reply<std::vector<LerpTagSync>>
440+
>;
377441
using AddPolyToSceneMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_ADDPOLYTOSCENE>, int, std::vector<polyVert_t>>;
378442
using AddPolysToSceneMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_ADDPOLYSTOSCENE>, int, std::vector<polyVert_t>, int, int>;
379443
using AddLightToSceneMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_ADDLIGHTTOSCENE>, std::array<float, 3>, float, float, float, float, int>;

src/engine/client/cl_cgame.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,13 @@ void CGameVM::QVMSyscall(int syscallNum, Util::Reader& reader, IPC::Channel& cha
11121112
});
11131113
break;
11141114

1115+
case CG_R_SYNCLERPTAGS:
1116+
IPC::HandleMsg<Render::SyncLerpTagsMsg>( channel, std::move( reader ), [this]( const std::vector<LerpTagUpdate>& lerpTags,
1117+
std::vector<LerpTagSync>& entityOrientations ) {
1118+
entityOrientations = re.SyncLerpTags( lerpTags );
1119+
} );
1120+
break;
1121+
11151122
case CG_GETCURRENTSNAPSHOTNUMBER:
11161123
IPC::HandleMsg<GetCurrentSnapshotNumberMsg>(channel, std::move(reader), [this] (int& number, int& serverTime) {
11171124
number = cl.snap.messageNum;
@@ -1216,12 +1223,6 @@ void CGameVM::QVMSyscall(int syscallNum, Util::Reader& reader, IPC::Channel& cha
12161223
});
12171224
break;
12181225

1219-
case CG_R_LERPTAG:
1220-
IPC::HandleMsg<Render::LerpTagMsg>(channel, std::move(reader), [this] (const refEntity_t& entity, const std::string& tagName, int startIndex, orientation_t& tag, int& res) {
1221-
res = re.LerpTag(&tag, &entity, tagName.c_str(), startIndex);
1222-
});
1223-
break;
1224-
12251226
case CG_R_REMAP_SHADER:
12261227
IPC::HandleMsg<Render::RemapShaderMsg>(channel, std::move(reader), [this] (const std::string& oldShader, const std::string& newShader, const std::string& timeOffset) {
12271228
re.RemapShader(oldShader.c_str(), newShader.c_str(), timeOffset.c_str());
@@ -1592,6 +1593,12 @@ void CGameVM::CmdBuffer::HandleCommandBufferSyscall(int major, int minor, Util::
15921593
});
15931594
break;
15941595

1596+
case CG_R_SYNCREFENTITIES:
1597+
HandleMsg<Render::SyncRefEntitiesMsg>( std::move( reader ), [this]( const std::vector<EntityUpdate>& ents ) {
1598+
re.SyncRefEntities( ents );
1599+
} );
1600+
break;
1601+
15951602
case CG_R_ADDPOLYTOSCENE:
15961603
HandleMsg<Render::AddPolyToSceneMsg>(std::move(reader), [this] (int shader, const std::vector<polyVert_t>& verts) {
15971604
re.AddPolyToScene(shader, verts.size(), verts.data());

src/engine/null/null_renderer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ void RE_SetWorldVisData( const byte * ) { }
7979
void RE_EndRegistration() { }
8080
void RE_ClearScene() { }
8181
void RE_AddRefEntityToScene( const refEntity_t * ) { }
82+
void RE_SyncRefEntities( const std::vector<EntityUpdate>& ) {}
83+
std::vector<LerpTagSync> RE_SyncLerpTags( const std::vector<LerpTagUpdate>& ) {
84+
return {};
85+
}
8286
void RE_AddPolyToScene( qhandle_t, int, const polyVert_t* ) { }
8387
void RE_AddPolysToScene( qhandle_t, int, const polyVert_t*, int ) { }
8488
void RE_AddLightToScene( const vec3_t, float, float, float, float, int ) { }
@@ -203,6 +207,8 @@ refexport_t *GetRefAPI( int, refimport_t* )
203207

204208
re.ClearScene = RE_ClearScene;
205209
re.AddRefEntityToScene = RE_AddRefEntityToScene;
210+
re.SyncRefEntities = RE_SyncRefEntities;
211+
re.SyncLerpTags = RE_SyncLerpTags;
206212

207213
re.AddPolyToScene = RE_AddPolyToScene;
208214
// Ridah
@@ -224,7 +230,6 @@ refexport_t *GetRefAPI( int, refimport_t* )
224230

225231
re.MarkFragments = R_MarkFragments;
226232

227-
re.LerpTag = R_LerpTag;
228233
re.ModelBounds = R_ModelBounds;
229234

230235
re.RemapShader = R_RemapShader;

0 commit comments

Comments
 (0)