Skip to content

Commit 6ee4759

Browse files
Static analysis fixes
Fixes for static analysis warnings reported by PVS, including the following: - Remove a signed bitshift operation with undefined C++ behavior, replacing it with the intended constant result. - Restrict the capture scope of two lambdas, removing access to temporary variables. Also: - Add initial unit tests for the Half class, providing validation for future work.
1 parent daaf86e commit 6ee4759

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

source/MaterialXRender/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class Half
101101
static constexpr int32_t const nanN = (infC + 1) << shift; // minimum flt16 nan as a flt32
102102
static constexpr int32_t const maxC = maxN >> shift;
103103
static constexpr int32_t const minC = minN >> shift;
104-
static constexpr int32_t const signC = signN >> shiftSign; // flt16 sign bit
104+
static constexpr int32_t const signC = (int32_t) 0x00008000; // flt16 sign bit
105105

106106
static constexpr int32_t const mulN = 0x52000000; // (1 << 23) / minN
107107
static constexpr int32_t const mulC = 0x33800000; // minN / (1 << (23 - shift))

source/MaterialXTest/Render.cpp

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,57 @@
44
//
55

66
#include <MaterialXTest/Catch/catch.hpp>
7-
#include <MaterialXTest/GenShaderUtil.h>
87

9-
#include <MaterialXCore/Document.h>
10-
11-
#include <MaterialXFormat/XmlIo.h>
12-
13-
#include <MaterialXGenShader/Util.h>
14-
#include <MaterialXGenShader/HwShaderGenerator.h>
15-
#include <MaterialXGenShader/DefaultColorManagementSystem.h>
8+
#include <MaterialXTest/RenderUtil.h>
169

1710
#include <MaterialXRender/ShaderRenderer.h>
18-
#include <MaterialXRender/Util.h>
19-
20-
#include <MaterialXTest/RenderUtil.h>
11+
#include <MaterialXRender/StbImageLoader.h>
12+
#include <MaterialXRender/TinyObjLoader.h>
13+
#include <MaterialXRender/Types.h>
2114

22-
#ifdef MATERIALX_BUILD_CONTRIB
23-
#include <MaterialXContrib/Handlers/TinyEXRImageLoader.h>
24-
#endif
2515
#ifdef MATERIALX_BUILD_OIIO
2616
#include <MaterialXRender/OiioImageLoader.h>
2717
#endif
28-
#include <MaterialXRender/StbImageLoader.h>
29-
30-
#include <MaterialXRender/GeometryHandler.h>
31-
#include <MaterialXRender/TinyObjLoader.h>
18+
#ifdef MATERIALX_BUILD_CONTRIB
19+
#include <MaterialXContrib/Handlers/TinyEXRImageLoader.h>
20+
#endif
3221

3322
#include <fstream>
3423
#include <iostream>
24+
#include <limits>
3525
#include <unordered_set>
36-
#include <chrono>
37-
#include <ctime>
3826

3927
namespace mx = MaterialX;
4028

41-
#define LOG_TO_FILE
29+
TEST_CASE("Render: Half Float", "[rendercore]")
30+
{
31+
const std::vector<float> values =
32+
{
33+
0.0f, 0.25f, 0.5f, 0.75f,
34+
1.0f, 8.0f, 64.0f, 512.0f,
35+
std::numeric_limits<float>::infinity()
36+
};
37+
const std::vector<float> signs = { 1.0f, -1.0f };
38+
39+
for (float value : values)
40+
{
41+
for (float sign : signs)
42+
{
43+
float f(value * sign);
44+
mx::Half h(f);
45+
REQUIRE(h == f);
46+
REQUIRE(h + mx::Half(1.0f) == f + 1.0f);
47+
REQUIRE(h - mx::Half(1.0f) == f - 1.0f);
48+
REQUIRE(h * mx::Half(2.0f) == f * 2.0f);
49+
REQUIRE(h / mx::Half(2.0f) == f / 2.0f);
50+
REQUIRE((h += mx::Half(3.0f)) == (f += 3.0f));
51+
REQUIRE((h -= mx::Half(3.0f)) == (f -= 3.0f));
52+
REQUIRE((h *= mx::Half(4.0f)) == (f *= 4.0f));
53+
REQUIRE((h /= mx::Half(4.0f)) == (f /= 4.0f));
54+
REQUIRE(-h == -f);
55+
}
56+
}
57+
}
4258

4359
struct GeomHandlerTestOptions
4460
{

source/MaterialXView/Editor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class EditorColorPicker : public ng::ColorPicker
5151
_colorWidgets[i]->setFixedSize(ng::Vector2i(70, 20));
5252
_colorWidgets[i]->setFontSize(15);
5353
_colorWidgets[i]->setSpinnable(true);
54-
_colorWidgets[i]->setCallback([&](float)
54+
_colorWidgets[i]->setCallback([this](float)
5555
{
5656
ng::Color value(_colorWidgets[0]->value(), _colorWidgets[1]->value(), _colorWidgets[2]->value(), _colorWidgets[3]->value());
5757
mColorWheel->setColor(value);
@@ -62,7 +62,7 @@ class EditorColorPicker : public ng::ColorPicker
6262

6363
// The color wheel does not handle alpha properly, so only
6464
// overwrite RGB in the callback.
65-
mCallback = [&](const ng::Color &value) {
65+
mCallback = [this](const ng::Color &value) {
6666
_colorWidgets[0]->setValue(value[0]);
6767
_colorWidgets[1]->setValue(value[1]);
6868
_colorWidgets[2]->setValue(value[2]);

0 commit comments

Comments
 (0)