Skip to content

Commit b4c6e33

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

20 files changed

Lines changed: 689 additions & 122 deletions

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: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,82 @@ 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 );
100116
}
117+
101118
static refEntity_t Read(Reader& stream)
102119
{
103120
refEntity_t ent;
104-
stream.ReadData(&ent, offsetof(refEntity_t, skeleton));
105-
ent.skeleton = stream.Read<refSkeleton_t>();
121+
stream.ReadData(&ent, offsetof(refEntity_t, tag));
122+
ent.tag = stream.Read<std::string>();
123+
ent.boneMods = stream.Read<std::vector<BoneMod>>();
124+
return ent;
125+
}
126+
};
127+
128+
template<> struct SerializeTraits<EntityUpdate> {
129+
static void Write( Writer& stream, const EntityUpdate& ent ) {
130+
stream.Write<refEntity_t>( ent.ent );
131+
stream.Write<uint16_t>( ent.id );
132+
}
133+
134+
static EntityUpdate Read( Reader& stream ) {
135+
EntityUpdate ent;
136+
ent.ent = stream.Read<refEntity_t>();
137+
ent.id = stream.Read<uint16_t>();
106138
return ent;
107139
}
108140
};
109141

142+
template<> struct SerializeTraits<LerpTagUpdate> {
143+
static void Write( Writer& stream, const LerpTagUpdate& tag ) {
144+
stream.Write<std::string>( tag.tag );
145+
stream.Write<uint16_t>( tag.id );
146+
}
147+
148+
static LerpTagUpdate Read( Reader& stream ) {
149+
LerpTagUpdate tag;
150+
tag.tag = stream.Read<std::string>();
151+
tag.id = stream.Read<uint16_t>();
152+
return tag;
153+
}
154+
};
155+
156+
template<> struct SerializeTraits<LerpTagSync> {
157+
static void Write( Writer& stream, const LerpTagSync& tag ) {
158+
stream.Write<orientation_t>( tag.entityOrientation );
159+
stream.Write<orientation_t>( tag.orientation );
160+
}
161+
162+
static LerpTagSync Read( Reader& stream ) {
163+
LerpTagSync tag;
164+
tag.entityOrientation = stream.Read<orientation_t>();
165+
tag.orientation = stream.Read<orientation_t>();
166+
return tag;
167+
}
168+
};
169+
110170
template<>
111171
struct SerializeTraits<Color::Color> {
112172
static void Write(Writer& stream, const Color::Color& value)
@@ -168,6 +228,8 @@ enum cgameImport_t
168228
CG_R_REGISTERFONT,
169229
CG_R_CLEARSCENE,
170230
CG_R_ADDREFENTITYTOSCENE,
231+
CG_R_SYNCREFENTITIES,
232+
CG_R_SYNCLERPTAGS,
171233
CG_R_ADDPOLYTOSCENE,
172234
CG_R_ADDPOLYSTOSCENE,
173235
CG_R_ADDLIGHTTOSCENE,
@@ -181,7 +243,6 @@ enum cgameImport_t
181243
CG_R_DRAWSTRETCHPIC,
182244
CG_R_DRAWROTATEDPIC,
183245
CG_R_MODELBOUNDS,
184-
CG_R_LERPTAG,
185246
CG_R_REMAP_SHADER,
186247
CG_R_BATCHINPVS,
187248
CG_R_REGISTERANIMATION,
@@ -325,10 +386,6 @@ namespace Render {
325386
IPC::Message<IPC::Id<VM::QVM, CG_R_MODELBOUNDS>, int>,
326387
IPC::Reply<std::array<float, 3>, std::array<float, 3>>
327388
>;
328-
using LerpTagMsg = IPC::SyncMessage<
329-
IPC::Message<IPC::Id<VM::QVM, CG_R_LERPTAG>, refEntity_t, std::string, int>,
330-
IPC::Reply<orientation_t, int>
331-
>;
332389
using RemapShaderMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_REMAP_SHADER>, std::string, std::string, std::string>;
333390
// TODO not a renderer call, handle in CM in the VM?
334391
using BatchInPVSMsg = IPC::SyncMessage<
@@ -381,6 +438,11 @@ namespace Render {
381438
using ScissorSetMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_SCISSOR_SET>, int, int, int, int>;
382439
using ClearSceneMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_CLEARSCENE>>;
383440
using AddRefEntityToSceneMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_ADDREFENTITYTOSCENE>, refEntity_t>;
441+
using SyncRefEntitiesMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_SYNCREFENTITIES>, std::vector<EntityUpdate>>;
442+
using SyncLerpTagsMsg = IPC::SyncMessage<IPC::Message<IPC::Id<VM::QVM, CG_R_SYNCLERPTAGS>,
443+
std::vector<LerpTagUpdate>>,
444+
IPC::Reply<std::vector<LerpTagSync>>
445+
>;
384446
using AddPolyToSceneMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_ADDPOLYTOSCENE>, int, std::vector<polyVert_t>>;
385447
using AddPolysToSceneMsg = IPC::Message<IPC::Id<VM::QVM, CG_R_ADDPOLYSTOSCENE>, int, std::vector<polyVert_t>, int, int>;
386448
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
@@ -1141,6 +1141,13 @@ void CGameVM::QVMSyscall(int syscallNum, Util::Reader& reader, IPC::Channel& cha
11411141
});
11421142
break;
11431143

1144+
case CG_R_SYNCLERPTAGS:
1145+
IPC::HandleMsg<Render::SyncLerpTagsMsg>( channel, std::move( reader ), [this]( const std::vector<LerpTagUpdate>& lerpTags,
1146+
std::vector<LerpTagSync>& entityOrientations ) {
1147+
entityOrientations = re.SyncLerpTags( lerpTags );
1148+
} );
1149+
break;
1150+
11441151
case CG_GETCURRENTSNAPSHOTNUMBER:
11451152
IPC::HandleMsg<GetCurrentSnapshotNumberMsg>(channel, std::move(reader), [this] (int& number, int& serverTime) {
11461153
number = cl.snap.messageNum;
@@ -1257,12 +1264,6 @@ void CGameVM::QVMSyscall(int syscallNum, Util::Reader& reader, IPC::Channel& cha
12571264
});
12581265
break;
12591266

1260-
case CG_R_LERPTAG:
1261-
IPC::HandleMsg<Render::LerpTagMsg>(channel, std::move(reader), [this] (const refEntity_t& entity, const std::string& tagName, int startIndex, orientation_t& tag, int& res) {
1262-
res = re.LerpTag(&tag, &entity, tagName.c_str(), startIndex);
1263-
});
1264-
break;
1265-
12661267
case CG_R_REMAP_SHADER:
12671268
IPC::HandleMsg<Render::RemapShaderMsg>(channel, std::move(reader), [this] (const std::string& oldShader, const std::string& newShader, const std::string& timeOffset) {
12681269
re.RemapShader(oldShader.c_str(), newShader.c_str(), timeOffset.c_str());
@@ -1633,6 +1634,12 @@ void CGameVM::CmdBuffer::HandleCommandBufferSyscall(int major, int minor, Util::
16331634
});
16341635
break;
16351636

1637+
case CG_R_SYNCREFENTITIES:
1638+
HandleMsg<Render::SyncRefEntitiesMsg>( std::move( reader ), [this]( const std::vector<EntityUpdate>& ents ) {
1639+
re.SyncRefEntities( ents );
1640+
} );
1641+
break;
1642+
16361643
case CG_R_ADDPOLYTOSCENE:
16371644
HandleMsg<Render::AddPolyToSceneMsg>(std::move(reader), [this] (int shader, const std::vector<polyVert_t>& verts) {
16381645
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)