Skip to content

Commit 956d5d6

Browse files
optimize normalization states and not make them process garbage
1 parent cdfa300 commit 956d5d6

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

include/nbl/asset/filters/NormalizationStates.h

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
1+
// Copyright (C) 2018-2023 - DevSH Graphics Programming Sp. z O.O.
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
4-
54
#ifndef _NBL_ASSET_NORMALIZATION_STATES_H_INCLUDED_
65
#define _NBL_ASSET_NORMALIZATION_STATES_H_INCLUDED_
76

@@ -40,7 +39,7 @@ class CGlobalNormalizationState
4039
void prepass(Tenc* encodeBuffer, const core::vectorSIMDu32& position, uint32_t blockX, uint32_t blockY, uint8_t channels)
4140
{
4241
static_assert(std::is_floating_point_v<Tenc>, "Integer decode not supported yet!");
43-
for (uint8_t channel=0u; channel<4u; ++channel)
42+
for (uint8_t channel=0u; channel<channels; ++channel)
4443
{
4544
const auto val = encodeBuffer[channel];
4645
if constexpr (std::is_floating_point_v<Tenc>)
@@ -112,6 +111,23 @@ namespace impl
112111

113112
class CDerivativeMapNormalizationStateBase
114113
{
114+
protected:
115+
// we only care about R and G
116+
static inline constexpr uint32_t kChannels = 2;
117+
118+
template<bool isSignedFormat, typename Tenc>
119+
void impl(Tenc* encodeBuffer, const core::vectorSIMDu32& position, uint32_t blockX, uint32_t blockY, uint8_t channels) const
120+
{
121+
static_assert(std::is_floating_point_v<Tenc>, "Encode types must be double or float!");
122+
123+
if constexpr (isSignedFormat)
124+
for (uint8_t channel = 0; channel < kChannels; ++channel)
125+
encodeBuffer[channel] = encodeBuffer[channel]/maxAbsPerChannel[channel];
126+
else
127+
for (uint8_t channel = 0; channel < kChannels; ++channel)
128+
encodeBuffer[channel] = encodeBuffer[channel]*0.5f/maxAbsPerChannel[channel]+0.5f;
129+
}
130+
115131
public:
116132
inline bool validate() const {return true;}
117133

@@ -120,15 +136,15 @@ class CDerivativeMapNormalizationStateBase
120136
inline void initialize()
121137
{
122138
static_assert(std::is_floating_point_v<Tenc>, "Integer encode not supported yet!");
123-
std::fill_n(maxAbsPerChannel,4,0.f);
139+
std::fill_n(maxAbsPerChannel,kChannels,0.f);
124140
}
125141

126142
//
127143
template<typename Tenc>
128144
void prepass(Tenc* encodeBuffer, const core::vectorSIMDu32& position, uint32_t blockX, uint32_t blockY, uint8_t channels)
129145
{
130146
static_assert(std::is_floating_point_v<Tenc>, "Integer encode not supported yet!");
131-
for (uint8_t channel=0u; channel<4u; ++channel)
147+
for (uint8_t channel=0u; channel<kChannels; ++channel)
132148
core::atomic_fetch_max(maxAbsPerChannel+channel,core::abs(encodeBuffer[channel]));
133149
}
134150

@@ -144,6 +160,7 @@ class CDerivativeMapNormalizationStateBase
144160
template<typename Tenc>
145161
void operator()(E_FORMAT format, Tenc* encodeBuffer, const core::vectorSIMDu32& position, uint32_t blockX, uint32_t blockY, uint8_t channels) const
146162
{
163+
assert(channels>=kChannels);
147164
#ifdef _NBL_DEBUG
148165
bool status = isFloatingPointFormat(format)||isNormalizedFormat(format);
149166
assert(status);
@@ -155,20 +172,7 @@ class CDerivativeMapNormalizationStateBase
155172
impl<false,Tenc>(encodeBuffer,position,blockX,blockY,channels);
156173
}
157174

158-
core::atomic<float> maxAbsPerChannel[4];
159-
protected:
160-
template<bool isSignedFormat, typename Tenc>
161-
void impl(Tenc* encodeBuffer, const core::vectorSIMDu32& position, uint32_t blockX, uint32_t blockY, uint8_t channels) const
162-
{
163-
static_assert(std::is_floating_point_v<Tenc>, "Encode types must be double or float!");
164-
165-
if constexpr (isSignedFormat)
166-
for (uint8_t channel = 0; channel < channels; ++channel)
167-
encodeBuffer[channel] = encodeBuffer[channel]/maxAbsPerChannel[channel];
168-
else
169-
for (uint8_t channel = 0; channel < channels; ++channel)
170-
encodeBuffer[channel] = encodeBuffer[channel]*0.5f/maxAbsPerChannel[channel]+0.5f;
171-
}
175+
core::atomic<float> maxAbsPerChannel[kChannels];
172176
};
173177

174178
}
@@ -183,8 +187,8 @@ class CDerivativeMapNormalizationState : public impl::CDerivativeMapNormalizatio
183187
static_assert(std::is_floating_point_v<Tenc>, "Integer encode types not supported yet!");
184188
if constexpr (isotropic)
185189
{
186-
const float isotropicMax = core::max(core::max(maxAbsPerChannel[0],maxAbsPerChannel[1]),core::max(maxAbsPerChannel[2],maxAbsPerChannel[3]));
187-
for (auto i=0u; i<4u; i++)
190+
const float isotropicMax = core::max<float>(maxAbsPerChannel[0],maxAbsPerChannel[1]);
191+
for (auto i=0u; i<kChannels; i++)
188192
maxAbsPerChannel[i] = isotropicMax;
189193
}
190194
}

0 commit comments

Comments
 (0)