Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 58 additions & 16 deletions lib/rendering/pbr/core/Cryptomatte.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

#include "Cryptomatte.h"

#include <cstring> // for size_t
#include <moonray/rendering/shading/AttributeKey.h>
#include <moonray/rendering/shading/Intersection.h>
#include <moonray/rendering/shading/State.h>

namespace moonray {
namespace pbr {
Expand Down Expand Up @@ -50,19 +52,59 @@ void CryptomatteBuffer::clear()
mFinalized = false;
}

void CryptomatteBuffer::addSampleScalar(unsigned x, unsigned y, float sampleId, float weight,
const scene_rdl2::math::Vec3f& position,
const scene_rdl2::math::Vec3f& p0,
const scene_rdl2::math::Vec3f& normal,
const scene_rdl2::math::Color4& beauty,
const scene_rdl2::math::Vec3f refP,
const scene_rdl2::math::Vec3f refN,
const scene_rdl2::math::Vec2f uv,

void computeCryptomatteResults(CryptomatteResults &results, int cryptoIdAttrIdx, int cryptoUVAttrIdx,
moonray::shading::Intersection &isect)
{
results.mHit = true;
results.mPosition = isect.getP();
results.mNormal = isect.getN();

results.mId = 0.0f;
if (cryptoIdAttrIdx >= 0) {
moonray::shading::TypedAttributeKey<float> cryptoIdAttrKey(cryptoIdAttrIdx);
if (isect.isProvided(cryptoIdAttrKey)) {
results.mId = isect.getAttribute(cryptoIdAttrKey);
}
}

results.mUV = isect.getSt();
if (cryptoUVAttrIdx >= 0) {
moonray::shading::TypedAttributeKey<scene_rdl2::rdl2::Vec2f> cryptoUVAttrKey(cryptoUVAttrIdx);
if (isect.isProvided(cryptoUVAttrKey)) {
results.mUV = isect.getAttribute(cryptoUVAttrKey);
}
}

results.mP0 = scene_rdl2::math::zero;
if (isect.isProvided(shading::StandardAttributes::sP0)) {
results.mP0 = scene_rdl2::math::transformPoint(isect.getGeometryObject()->getSceneClass().getSceneContext()->getRender2World()->inverse(),
isect.getAttribute(shading::StandardAttributes::sP0));
}

shading::State sstate(&isect);
sstate.getRefP(results.mRefP);
sstate.getRefN(results.mRefN);
}


void CryptomatteBuffer::addSampleScalar(unsigned x, unsigned y, float weight,
const CryptomatteResults &cryptomatteResults,
const scene_rdl2::math::Color &beauty,
unsigned presenceDepth,
int cryptoType)
{
PixelEntry &pixelEntry = mPixelEntries[cryptoType][y * mWidth + x];

const float sampleId = cryptomatteResults.mId;
const scene_rdl2::math::Vec3f &position = cryptomatteResults.mPosition;
const scene_rdl2::math::Vec3f &p0 = cryptomatteResults.mP0;
const scene_rdl2::math::Vec3f &normal = cryptomatteResults.mNormal;
const scene_rdl2::math::Vec3f &refP = cryptomatteResults.mRefP;
const scene_rdl2::math::Vec3f &refN = cryptomatteResults.mRefN;
const scene_rdl2::math::Vec2f &uv = cryptomatteResults.mUV;
const scene_rdl2::math::Color4 beauty4 = scene_rdl2::math::Color4(beauty);

// Iterate over fragments stored at current pixel and see if we can merge the sample in to any of them
for (Fragment &fragment : pixelEntry.mFragments) {
// if multi presence is on, we treat each presence bounce as a separate cryptomatte fragment
Expand All @@ -71,19 +113,19 @@ void CryptomatteBuffer::addSampleScalar(unsigned x, unsigned y, float sampleId,
if (fragMatches) {
fragment.mCoverage += weight;
fragment.mPosition += position;
fragment.mP0 += p0;
fragment.mNormal += normal;
fragment.mBeauty += beauty;
fragment.mRefP += refP;
fragment.mRefN += refN;
fragment.mUV += uv;
fragment.mP0 += p0;
fragment.mNormal += normal;
fragment.mBeauty += beauty4;
fragment.mRefP += refP;
fragment.mRefN += refN;
fragment.mUV += uv;
fragment.mNumSamples++;
return;
}
}

// No match, so add a new fragment.
pixelEntry.mFragments.push_back(Fragment(sampleId, weight, position, p0, normal, beauty,
pixelEntry.mFragments.push_back(Fragment(sampleId, weight, position, p0, normal, beauty4,
refP, refN, uv, presenceDepth));
}

Expand Down
44 changes: 36 additions & 8 deletions lib/rendering/pbr/core/Cryptomatte.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
#include <scene_rdl2/common/fb_util/PixelBuffer.h>
#include <scene_rdl2/scene/rdl2/RenderOutput.h>

#include <cstring>
#include <list>
#include <tbb/mutex.h>
#include <vector>

namespace moonray {

namespace shading { class Intersection; }

namespace pbr {

/*
Expand Down Expand Up @@ -62,6 +66,35 @@ enum CryptomatteType {
NUM_CRYPTOMATTE_TYPES
};

enum CryptomatteFlags {
CRYPTOMATTE_FLAG_REGULAR = (1 << CRYPTOMATTE_TYPE_REGULAR),
CRYPTOMATTE_FLAG_REFLECTED = (1 << CRYPTOMATTE_TYPE_REFLECTED),
CRYPTOMATTE_FLAG_REFRACTED = (1 << CRYPTOMATTE_TYPE_REFRACTED),

CRYPTOMATTE_FLAGS_ALL = (CRYPTOMATTE_FLAG_REGULAR | CRYPTOMATTE_FLAG_REFLECTED | CRYPTOMATTE_FLAG_REFRACTED)
};

struct CryptomatteResults
{
// intersection results
bool mHit;
float mId;
scene_rdl2::math::Vec3f mPosition;
scene_rdl2::math::Vec3f mP0;
scene_rdl2::math::Vec3f mNormal;
scene_rdl2::math::Vec3f mRefP;
scene_rdl2::math::Vec3f mRefN;
scene_rdl2::math::Vec2f mUV;

void init() {
memset(this, 0, sizeof(CryptomatteResults));
}
};


void computeCryptomatteResults(CryptomatteResults &results, int cryptoIdAttrIdx, int cryptoUVAttrIdx,
moonray::shading::Intersection &isect);


class CryptomatteBuffer
{
Expand Down Expand Up @@ -129,14 +162,9 @@ class CryptomatteBuffer

// -----------------------------------------------------------------------------------------------------------------

void addSampleScalar(unsigned x, unsigned y, float id, float weight,
const scene_rdl2::math::Vec3f& position,
const scene_rdl2::math::Vec3f& p0,
const scene_rdl2::math::Vec3f& normal,
const scene_rdl2::math::Color4& beauty,
const scene_rdl2::math::Vec3f refP,
const scene_rdl2::math::Vec3f refN,
const scene_rdl2::math::Vec2f uv,
void addSampleScalar(unsigned x, unsigned y, float weight,
const CryptomatteResults &cryptomatteResults,
const scene_rdl2::math::Color &beauty,
unsigned presenceDepth,
int cryptoType);

Expand Down
8 changes: 6 additions & 2 deletions lib/rendering/pbr/core/RayState.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
HVD_MEMBER(float, mSampleClampingValue); \
HVD_MEMBER(float, mPrimaryRayDiffScale); \
HVD_MEMBER(float, mTextureDiffScale); \
HVD_PTR(void *, mCryptomatteBuffer); \
HVD_PTR(void *, mCryptomatteResultsArray); \
HVD_PTR(DeferredNode *, mDeferredNodesHead); \
HVD_PTR(DeferredNode **, mDeferredNodesTailPtr); \
HVD_MEMBER(int, mNumDeferredNodes); \
Expand All @@ -36,6 +38,8 @@
HVD_VALIDATE(Subpixel, mSampleClampingValue); \
HVD_VALIDATE(Subpixel, mPrimaryRayDiffScale); \
HVD_VALIDATE(Subpixel, mTextureDiffScale); \
HVD_VALIDATE(Subpixel, mCryptomatteBuffer); \
HVD_VALIDATE(Subpixel, mCryptomatteResultsArray); \
HVD_VALIDATE(Subpixel, mDeferredNodesHead); \
HVD_VALIDATE(Subpixel, mDeferredNodesTailPtr); \
HVD_VALIDATE(Subpixel, mNumDeferredNodes); \
Expand Down Expand Up @@ -116,9 +120,9 @@
//----------------------------------------------------------------------------

#if CACHE_LINE_SIZE == 128
#define RAY_STATE_MEMBERS_PAD 92
#define RAY_STATE_MEMBERS_PAD 76
#else
#define RAY_STATE_MEMBERS_PAD 44
#define RAY_STATE_MEMBERS_PAD 28
#endif

#define RAY_STATE_MEMBERS \
Expand Down
Loading