Skip to content

Commit

Permalink
[hdEmbree] ies.h / ies.cpp: patch, and add pxr-IES.patch and README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
pmolodo committed Jun 6, 2024
1 parent 7e89a6f commit 3e962c6
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 18 deletions.
31 changes: 31 additions & 0 deletions pxr/imaging/plugin/hdEmbree/pxrIES/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# IES utilities

Utilities for reading and using .ies files (IESNA LM-63 Format), which are used
to describe lights.

The files `ies.h` and `ies.cpp` are originally from
[Cycles](https://docs.blender.org/manual/en/latest/render/cycles/index.html),
which is part of the larger
[Blender](https://projects.blender.org/blender/blender/) project, though with
a the less restrictive Apache 2.0 license - see:

- https://www.blender.org/about/license/

## Version

v4.1.1 ( 53c49589f311bd3c9e3ea8f62b3fa8fe8e5d2c8c )

## Setup

When updating IES, the following steps should be followed:

1. Copy `intern/cycles/util/ies.h` and `intern/cycles/util/ies.cpp` over the
copies in `pxr/imaging/plugin/hdEmbree/pxrIES`.
2. Apply `pxr-IES.patch` to update the source files with modifications for USD,
ie, from the USD repo root folder:

```sh
patch -p1 -i pxr/imaging/plugin/hdEmbree/pxrIES/pxr-IES.patch
```
3. Commit your changes, noting the exact version of blender that the new ies
files were copied from.
49 changes: 35 additions & 14 deletions pxr/imaging/plugin/hdEmbree/pxrIES/ies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
*
* SPDX-License-Identifier: Apache-2.0 */

#include "ies.h"
#include <algorithm>
#include <cstring>

#include "util/foreach.h"
#include "util/ies.h"
#include "util/math.h"
#include "util/string.h"
#define _USE_MATH_DEFINES
#include <cmath>

CCL_NAMESPACE_BEGIN
#if !defined(M_PI)
#define M_PI 3.14159265358979323846
#endif

// NOTE: For some reason gcc-7.2 does not instantiate this version of the
// allocator here (used in IESTextParser). Works fine for gcc-6, gcc-7.3 and gcc-8.
//
// TODO(sergey): Get to the root of this issue, or confirm this is a compiler
// issue.
template class GuardedAllocator<char>;
#define M_PI_F M_PI

PXR_NAMESPACE_OPEN_SCOPE

namespace pxr_ccl {

bool IESFile::load(const string &ies)
{
Expand All @@ -43,11 +44,28 @@ int IESFile::packed_size()
return 0;
}

template <class To, class From>
std::enable_if_t<sizeof(To) == sizeof(From) &&
std::is_trivially_copyable_v<From> &&
std::is_trivially_copyable_v<To>,
To>
// constexpr support needs compiler magic
bit_cast(const From &src) noexcept
{
static_assert(std::is_trivially_constructible_v<To>,
"This implementation additionally requires "
"destination type to be trivially constructible");

To dst;
std::memcpy(&dst, &src, sizeof(To));
return dst;
}

void IESFile::pack(float *data)
{
if (v_angles.size() && h_angles.size()) {
*(data++) = __int_as_float(h_angles.size());
*(data++) = __int_as_float(v_angles.size());
*(data++) = bit_cast<float>(static_cast<int>(h_angles.size()));
*(data++) = bit_cast<float>(static_cast<int>(v_angles.size()));

memcpy(data, &h_angles[0], h_angles.size() * sizeof(float));
data += h_angles.size();
Expand Down Expand Up @@ -407,4 +425,7 @@ IESFile::~IESFile()
clear();
}

CCL_NAMESPACE_END
} // namespace pxr_ccl

PXR_NAMESPACE_CLOSE_SCOPE

23 changes: 19 additions & 4 deletions pxr/imaging/plugin/hdEmbree/pxrIES/ies.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
#ifndef __UTIL_IES_H__
#define __UTIL_IES_H__

#include "util/string.h"
#include "util/vector.h"
#include <string>
#include <vector>

using std::string;
using std::vector;

#include "pxr/pxr.h"

PXR_NAMESPACE_OPEN_SCOPE

namespace pxr_ccl {

CCL_NAMESPACE_BEGIN

class IESFile {
public:
Expand All @@ -19,11 +27,16 @@ class IESFile {
void pack(float *data);

bool load(const string &ies);

virtual
void clear();

protected:
bool parse(const string &ies);

virtual
bool process();

void process_type_a();
void process_type_b();
void process_type_c();
Expand All @@ -41,6 +54,8 @@ class IESFile {
enum IESType { TYPE_A = 3, TYPE_B = 2, TYPE_C = 1 } type;
};

CCL_NAMESPACE_END
} // namespace pxr_ccl

PXR_NAMESPACE_CLOSE_SCOPE

#endif /* __UTIL_IES_H__ */
131 changes: 131 additions & 0 deletions pxr/imaging/plugin/hdEmbree/pxrIES/pxr-IES.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
diff --git a/pxr/imaging/plugin/hdEmbree/pxrIES/ies.cpp b/pxr/imaging/plugin/hdEmbree/pxrIES/ies.cpp
index a6725cc04..7a6917303 100644
--- a/pxr/imaging/plugin/hdEmbree/pxrIES/ies.cpp
+++ b/pxr/imaging/plugin/hdEmbree/pxrIES/ies.cpp
@@ -2,21 +2,22 @@
*
* SPDX-License-Identifier: Apache-2.0 */

+#include "ies.h"
#include <algorithm>
+#include <cstring>

-#include "util/foreach.h"
-#include "util/ies.h"
-#include "util/math.h"
-#include "util/string.h"
+#define _USE_MATH_DEFINES
+#include <cmath>

-CCL_NAMESPACE_BEGIN
+#if !defined(M_PI)
+#define M_PI 3.14159265358979323846
+#endif

-// NOTE: For some reason gcc-7.2 does not instantiate this version of the
-// allocator here (used in IESTextParser). Works fine for gcc-6, gcc-7.3 and gcc-8.
-//
-// TODO(sergey): Get to the root of this issue, or confirm this is a compiler
-// issue.
-template class GuardedAllocator<char>;
+#define M_PI_F M_PI
+
+PXR_NAMESPACE_OPEN_SCOPE
+
+namespace pxr_ccl {

bool IESFile::load(const string &ies)
{
@@ -43,11 +44,28 @@ int IESFile::packed_size()
return 0;
}

+template <class To, class From>
+std::enable_if_t<sizeof(To) == sizeof(From) &&
+ std::is_trivially_copyable_v<From> &&
+ std::is_trivially_copyable_v<To>,
+ To>
+// constexpr support needs compiler magic
+bit_cast(const From &src) noexcept
+{
+ static_assert(std::is_trivially_constructible_v<To>,
+ "This implementation additionally requires "
+ "destination type to be trivially constructible");
+
+ To dst;
+ std::memcpy(&dst, &src, sizeof(To));
+ return dst;
+}
+
void IESFile::pack(float *data)
{
if (v_angles.size() && h_angles.size()) {
- *(data++) = __int_as_float(h_angles.size());
- *(data++) = __int_as_float(v_angles.size());
+ *(data++) = bit_cast<float>(static_cast<int>(h_angles.size()));
+ *(data++) = bit_cast<float>(static_cast<int>(v_angles.size()));

memcpy(data, &h_angles[0], h_angles.size() * sizeof(float));
data += h_angles.size();
@@ -407,4 +425,7 @@ IESFile::~IESFile()
clear();
}

-CCL_NAMESPACE_END
+} // namespace pxr_ccl
+
+PXR_NAMESPACE_CLOSE_SCOPE
+
diff --git a/pxr/imaging/plugin/hdEmbree/pxrIES/ies.h b/pxr/imaging/plugin/hdEmbree/pxrIES/ies.h
index 8c506befd..051c23f6c 100644
--- a/pxr/imaging/plugin/hdEmbree/pxrIES/ies.h
+++ b/pxr/imaging/plugin/hdEmbree/pxrIES/ies.h
@@ -5,10 +5,18 @@
#ifndef __UTIL_IES_H__
#define __UTIL_IES_H__

-#include "util/string.h"
-#include "util/vector.h"
+#include <string>
+#include <vector>
+
+using std::string;
+using std::vector;
+
+#include "pxr/pxr.h"
+
+PXR_NAMESPACE_OPEN_SCOPE
+
+namespace pxr_ccl {

-CCL_NAMESPACE_BEGIN

class IESFile {
public:
@@ -19,11 +27,16 @@ class IESFile {
void pack(float *data);

bool load(const string &ies);
+
+ virtual
void clear();

protected:
bool parse(const string &ies);
+
+ virtual
bool process();
+
void process_type_a();
void process_type_b();
void process_type_c();
@@ -41,6 +54,8 @@ class IESFile {
enum IESType { TYPE_A = 3, TYPE_B = 2, TYPE_C = 1 } type;
};

-CCL_NAMESPACE_END
+} // namespace pxr_ccl
+
+PXR_NAMESPACE_CLOSE_SCOPE

#endif /* __UTIL_IES_H__ */

0 comments on commit 3e962c6

Please sign in to comment.