Skip to content

Commit 6e651d9

Browse files
joye-ramonexrSimpodin
authored andcommitted
CROS_impl::update и CROS_impl::update_smooth могут довольно активно вызываться с разных потоков
1 parent d8ebca5 commit 6e651d9

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

ogsr_engine/Layers/xrRender/LightTrack.cpp

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
CROS_impl::CROS_impl()
1616
{
1717
approximate.set(0, 0, 0);
18-
dwFrame = u32(-1);
19-
shadow_recv_frame = u32(-1);
18+
19+
dwFrame = static_cast<u32>(-1);
20+
dwFrameSmooth = static_cast<u32>(-1);
21+
22+
shadow_recv_frame = static_cast<u32>(-1);
2023
shadow_recv_slot = -1;
2124

2225
result_count = 0;
2326
result_iterator = 0;
24-
result_frame = u32(-1);
27+
result_frame = static_cast<u32>(-1);
2528
result_sun = 0;
2629
hemi_value = 0.5f;
2730
hemi_smooth = 0.5f;
@@ -132,7 +135,7 @@ const float hdir[lt_hemisamples][3] = {
132135
// return (dir.z > 0) ? CUBE_FACE_POS_Z : CUBE_FACE_NEG_Z;
133136
// }
134137

135-
inline void CROS_impl::accum_hemi(float* hemi_cube, Fvector3& dir, float scale)
138+
inline void CROS_impl::accum_hemi(float* hemi_cube, const Fvector3& dir, const float scale)
136139
{
137140
if (dir.x > 0)
138141
hemi_cube[CUBE_FACE_POS_X] += dir.x * scale;
@@ -155,16 +158,19 @@ void CROS_impl::update(IRenderable* O)
155158
{
156159
ZoneScoped;
157160

158-
// clip & verify
159-
if (dwFrame == Device.dwFrame)
161+
u32 expected_frame = dwFrame.load();
162+
163+
if (!dwFrame.compare_exchange_strong(expected_frame, Device.dwFrame) || expected_frame == Device.dwFrame)
160164
return;
161-
dwFrame = Device.dwFrame;
165+
166+
// clip & verify
167+
162168
if (nullptr == O)
163169
return;
164170
if (nullptr == O->renderable.visual)
165171
return;
172+
166173
VERIFY(dynamic_cast<CROS_impl*>(O->renderable_ROS()));
167-
// float dt = Device.fTimeDelta;
168174

169175
CObject* _object = dynamic_cast<CObject*>(O);
170176

@@ -193,8 +199,6 @@ void CROS_impl::update(IRenderable* O)
193199
position.y += .3f * vis.sphere.R;
194200
Fvector direction;
195201
direction.random_dir();
196-
//. position.mad(direction,0.25f*radius);
197-
//. position.mad(direction,0.025f*radius);
198202

199203
// function call order is important at least for r1
200204
for (float& i : hemi_cube)
@@ -211,19 +215,23 @@ void CROS_impl::update(IRenderable* O)
211215
// Process ambient lighting and approximate average lighting
212216
// Process our lights to find average luminescences
213217
const CEnvDescriptor& desc = *g_pGamePersistent->Environment().CurrentEnv;
218+
214219
Fvector accum = {desc.ambient.x, desc.ambient.y, desc.ambient.z};
215220
Fvector hemi = {desc.hemi_color.x, desc.hemi_color.y, desc.hemi_color.z};
216221
Fvector sun_ = {desc.sun_color.x, desc.sun_color.y, desc.sun_color.z};
222+
217223
if (MODE & IRender_ObjectSpecific::TRACE_HEMI)
218224
hemi.mul(hemi_smooth);
219225
else
220226
hemi.mul(.2f);
221227
accum.add(hemi);
228+
222229
if (MODE & IRender_ObjectSpecific::TRACE_SUN)
223230
sun_.mul(sun_smooth);
224231
else
225232
sun_.mul(.2f);
226233
accum.add(sun_);
234+
227235
if (MODE & IRender_ObjectSpecific::TRACE_LIGHTS)
228236
{
229237
Fvector lacc = {0, 0, 0};
@@ -265,11 +273,6 @@ void CROS_impl::update(IRenderable* O)
265273
hemi_cube[i] = std::max(hemi_cube[i], minHemiValue);
266274
}
267275

268-
269-
// lacc.x *= desc.lmap_color.x;
270-
// lacc.y *= desc.lmap_color.y;
271-
// lacc.z *= desc.lmap_color.z;
272-
// Msg ("- rgb[%f,%f,%f]",lacc.x,lacc.y,lacc.z);
273276
accum.add(lacc);
274277
}
275278
else
@@ -296,9 +299,7 @@ constexpr s32 s_iUTIdleMax = 2000;
296299

297300
void CROS_impl::smart_update(IRenderable* O)
298301
{
299-
if (!O)
300-
return;
301-
if (nullptr == O->renderable.visual)
302+
if (!O || !O->renderable.visual)
302303
return;
303304

304305
--ticks_to_update;
@@ -323,9 +324,10 @@ void CROS_impl::smart_update(IRenderable* O)
323324
}
324325
else
325326
{
326-
if (!last_position.similar(position, 0.15))
327+
if (!last_position.similar(position, 0.15f))
327328
{
328329
sky_rays_uptodate = 0;
330+
329331
update(O);
330332
last_position = position;
331333

@@ -343,10 +345,10 @@ extern float ps_r2_lt_smooth;
343345
// hemi & sun: update and smooth
344346
void CROS_impl::update_smooth(IRenderable* O)
345347
{
346-
if (dwFrameSmooth == Device.dwFrame)
347-
return;
348+
u32 expected_frame = dwFrameSmooth.load();
348349

349-
dwFrameSmooth = Device.dwFrame;
350+
if (!dwFrameSmooth.compare_exchange_strong(expected_frame, Device.dwFrame) || expected_frame == Device.dwFrame)
351+
return;
350352

351353
smart_update(O);
352354

@@ -355,15 +357,16 @@ void CROS_impl::update_smooth(IRenderable* O)
355357
const float l_i = 1.f - l_f;
356358
hemi_smooth = hemi_value * l_f + hemi_smooth * l_i;
357359
sun_smooth = sun_value * l_f + sun_smooth * l_i;
360+
358361
for (size_t i = 0; i < NUM_FACES; ++i)
359362
{
360363
hemi_cube_smooth[i] = hemi_cube[i] * l_f + hemi_cube_smooth[i] * l_i;
361364
}
362365
}
363366

364-
void CROS_impl::calc_sun_value(Fvector& position, CObject* _object)
367+
void CROS_impl::calc_sun_value(const Fvector& position, const CObject* _object)
365368
{
366-
const light* sun = (light*)RImplementation.Lights.sun_adapted._get();
369+
const light* sun = dynamic_cast<light*>(RImplementation.Lights.sun_adapted._get());
367370

368371
if (MODE & IRender_ObjectSpecific::TRACE_SUN)
369372
{
@@ -377,15 +380,15 @@ void CROS_impl::calc_sun_value(Fvector& position, CObject* _object)
377380
}
378381
}
379382

380-
void CROS_impl::calc_sky_hemi_value(Fvector& position, CObject* _object)
383+
void CROS_impl::calc_sky_hemi_value(const Fvector& position, const CObject* _object)
381384
{
382385
// hemi-tracing
383386
if (MODE & IRender_ObjectSpecific::TRACE_HEMI)
384387
{
385388
sky_rays_uptodate += ps_r2_dhemi_count;
386389
sky_rays_uptodate = _min(sky_rays_uptodate, lt_hemisamples);
387390

388-
for (u32 it = 0; it < (u32)ps_r2_dhemi_count; it++)
391+
for (u32 it = 0; it < static_cast<u32>(ps_r2_dhemi_count); it++)
389392
{ // five samples per one frame
390393
u32 sample = 0;
391394
if (result_count < lt_hemisamples)
@@ -414,7 +417,7 @@ void CROS_impl::calc_sky_hemi_value(Fvector& position, CObject* _object)
414417
for (int it = 0; it < result_count; it++)
415418
if (result[it])
416419
_pass++;
417-
hemi_value = float(_pass) / float(result_count ? result_count : 1);
420+
hemi_value = static_cast<float>(_pass) / static_cast<float>(result_count ? result_count : 1);
418421
hemi_value *= ps_r2_dhemi_sky_scale;
419422

420423
for (int it = 0; it < result_count; it++)
@@ -426,7 +429,7 @@ void CROS_impl::calc_sky_hemi_value(Fvector& position, CObject* _object)
426429
}
427430
}
428431

429-
void CROS_impl::prepare_lights(Fvector& position, IRenderable* O)
432+
void CROS_impl::prepare_lights(const Fvector& position, IRenderable* O)
430433
{
431434
const CObject* _object = dynamic_cast<CObject*>(O);
432435
const float dt = Device.fTimeDelta;
@@ -449,7 +452,7 @@ void CROS_impl::prepare_lights(Fvector& position, IRenderable* O)
449452

450453
for (const auto& spatial : lstSpatial)
451454
{
452-
light* source = (light*)(spatial->dcast_Light());
455+
light* source = dynamic_cast<light*>(spatial->dcast_Light());
453456
VERIFY(source); // sanity check
454457
const float R = radius + source->range;
455458
if (position.distance_to(source->position) < R && source->flags.bStatic)
@@ -459,7 +462,7 @@ void CROS_impl::prepare_lights(Fvector& position, IRenderable* O)
459462
// Trace visibility
460463
lights.clear();
461464

462-
for (s32 id = 0; id < s32(track.size()); id++)
465+
for (s32 id = 0; id < static_cast<s32>(track.size()); id++)
463466
{
464467
// remove untouched lights
465468
const xr_vector<CROS_impl::Item>::iterator I = track.begin() + id;

ogsr_engine/Layers/xrRender/LightTrack.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class CROS_impl : public IRender_ObjectSpecific
4040
public:
4141
// general
4242
u32 MODE;
43-
u32 dwFrame;
44-
u32 dwFrameSmooth{};
43+
std::atomic<u32> dwFrame;
44+
std::atomic<u32> dwFrameSmooth;
4545
bool skip{};
4646

4747
//
@@ -124,18 +124,17 @@ class CROS_impl : public IRender_ObjectSpecific
124124
// static inline CubeFaces get_cube_face(Fvector3& dir);
125125

126126
// Accumulates light from direction for corresponding faces
127-
static inline void accum_hemi(float* hemi_cube, Fvector3& dir, float scale);
127+
static inline void accum_hemi(float* hemi_cube, const Fvector3& dir, const float scale);
128128

129129
// Calculates sun part of ambient occlusion
130-
void calc_sun_value(Fvector& position, CObject* _object);
130+
void calc_sun_value(const Fvector& position, const CObject* _object);
131131

132132
// Calculates sky part of ambient occlusion
133-
void calc_sky_hemi_value(Fvector& position, CObject* _object);
133+
void calc_sky_hemi_value(const Fvector& position, const CObject* _object);
134134

135135
// prepares static or hemisphere lights for ambient occlusion calculations
136-
void prepare_lights(Fvector& position, IRenderable* O);
136+
void prepare_lights(const Fvector& position, IRenderable* O);
137137

138138
// Updates only if makes a desizion that update is necessary
139139
void smart_update(IRenderable* O);
140-
141140
};

0 commit comments

Comments
 (0)