Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arnold Material Creation Improvements #58

Merged
merged 28 commits into from
Jan 10, 2020

Conversation

mistafunk
Copy link
Collaborator

note: some of the commits are a bit evolutionary/overlapping

@mistafunk mistafunk self-assigned this Jan 6, 2020
Copy link
Contributor

@jonathan-meier jonathan-meier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please avoid mixing renaming changes with changes to the logic (last commit aed5e0e)

Apart from the minor things mentioned in the inline commits, this is looking very good. I did a small functional test and the deduplication worked.

src/serlio/prtMaterial/MaterialInfo.cpp Show resolved Hide resolved
src/serlio/prtMaterial/MaterialInfo.h Outdated Show resolved Hide resolved
src/serlio/prtMaterial/MaterialNameSource.cpp Outdated Show resolved Hide resolved
src/serlio/prtMaterial/MaterialUtils.cpp Outdated Show resolved Hide resolved
src/serlio/prtMaterial/MaterialUtils.cpp Outdated Show resolved Hide resolved
src/serlio/prtMaterial/ArnoldMaterialNode.cpp Outdated Show resolved Hide resolved
src/serlio/CMakeLists.txt Show resolved Hide resolved
src/serlio/prtMaterial/ArnoldMaterialNode.cpp Show resolved Hide resolved
src/serlio/prtMaterial/MaterialNameSource.h Outdated Show resolved Hide resolved
src/serlio/prtMaterial/PRTMaterialNode.cpp Show resolved Hide resolved
@mistafunk mistafunk force-pushed the arnold-material-deduplication branch from bfc29b3 to 20a4ed5 Compare January 10, 2020 12:56
Copy link
Contributor

@jonathan-meier jonathan-meier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only some small stuff left, can be merged directly afterwards

I especially like the changes for letting Maya uniqueify node names 👍

continue;

adsk::Data::Stream* matStream = matChannel->findDataStream(gPRTMatStream);
if ((matStream == nullptr) || (matStream->elementCount() == 0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be matStream->elementCount() != 1?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


const std::wstring MATERIAL_BASE_NAME(L"serlioGeneratedArnoldMaterial");

void synchronouslyCreateShadingEngine(std::wstring& shadingEngineName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider returning the definite shading engine name separately instead of using an in-out parameter for better readability

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


class MaterialColor;
class PRTContext;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be struct PRTContext;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

auto getCachedValue(M& cache, const typename M::key_type& key, F valueFunc, ARGS&&... valueFuncArgs) {
auto p = cache.find(key);
if (p == cache.end()) {
auto value = valueFunc(std::forward<ARGS&&...>(valueFuncArgs)...);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be std::forward<ARGS>(valueFuncArgs)..., ARGS should be expanded with the outer expansion, since std::forward only takes a single template parameter. This only compiled because getCachedValue is so far only used with a single value for valueFuncArgs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oopsie, obviously wrong... fixed.

Comment on lines 251 to 259
template <typename M, typename F, typename... ARGS>
auto getCachedValue(M& cache, const typename M::key_type& key, F valueFunc, ARGS&&... valueFuncArgs) {
auto p = cache.find(key);
if (p == cache.end()) {
auto value = valueFunc(std::forward<ARGS&&...>(valueFuncArgs)...);
p = cache.emplace(key, value).first;
}
return p->second;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking a bit further about this function, two more optimization opportunities come to mind:

  • perfect forwarding of the value returned by valueFunc:
    auto value = valueFunc(std::forward<ARGS>(valueFuncArgs)...);
    p = cache.emplace(key, value).first;
    
    the two lines above should simply be the more readable version of:
    p = cache.emplace(key, valueFunc(std::forward<ARGS>(valueFuncArgs)...)).first;
    
    but unfortunately, they're not, because they make an lvalue out of an rvalue. The equivalent would be:
    auto&& value = valueFunc(std::forward<ARGS>(valueFuncArgs)...);
    p = cache.emplace(key, std::forward<decltype(value)>(value)).first;
    
  • perfect forwarding of key:
    Unfortunately, we need to rely on SFINAE for the type constraint on key to be able to use a forwarding reference.

Full code:

template <typename M, typename K, typename F, typename... ARGS,
		typename = typename std::enable_if_t<std::is_convertible<typename std::decay_t<K>, typename M::key_type>::value>>
auto getCachedValue(M& cache, K&& key, F valueFunc, ARGS&&... valueFuncArgs) {
	auto p = cache.find(key);
	if (p == cache.end()) {
		auto&& value = valueFunc(std::forward<ARGS>(valueFuncArgs)...);
		p = cache.emplace(std::forward<K>(key), std::forward<decltype(value)>(value)).first;
	}
	return p->second;
}

you just have to admire the beauty of generic C++ code 😃

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good stuff, done

@mistafunk mistafunk merged commit efac535 into master Jan 10, 2020
@mistafunk mistafunk deleted the arnold-material-deduplication branch January 10, 2020 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants