Skip to content

Commit

Permalink
hdPrman: Moving code to compute crop window to camera context.
Browse files Browse the repository at this point in the history
(Internal change: 2193544)
  • Loading branch information
unhyperbolic authored and pixar-oss committed Nov 5, 2021
1 parent e7bd168 commit d956f50
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 63 deletions.
64 changes: 64 additions & 0 deletions third_party/renderman-24/plugin/hdPrman/cameraContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,70 @@ HdPrmanCameraContext::SetCameraAndCameraNodeParams(
}


// The crop window for RenderMan.
//
// Computed from data window and render buffer size.
//
// Recall from the RenderMan API:
// Only the pixels within the crop window are rendered. Has no
// affect on how pixels in the image map into the filmback plane.
// The crop window is relative to the render buffer size, e.g.,
// the crop window of (0,0,1,1) corresponds to the entire render
// buffer. The coordinates of the crop window are y-down.
// Format is (xmin, xmax, ymin, ymax).
//
// The limits for the integer locations corresponding to the above crop
// window are:
//
// rxmin = clamp(ceil( renderbufferwidth*xmin ), 0, renderbufferwidth - 1)
// rxmax = clamp(ceil( renderbufferwidth*xmax - 1), 0, renderbufferwidth - 1)
// similar for y
//
static
float
_DivRoundDown(const int a, const int b)
{
// Note that if the division (performed here)
// float(a) / b
// rounds up, then the result (by RenderMan) of
// ceil(b * (float(a) / b))
// might be a+1 instead of a.
//
// We add a slight negative bias to a to avoid this (we could also
// set the floating point rounding mode but: how to do this in a
// portable way - and on x86 switching the rounding is slow).

return GfClamp((a - 0.0078125f) / b, 0.0f, 1.0f);
}

static
GfVec4f
_ComputeCropWindow(
const GfRect2i &dataWindow,
const GfVec2i &renderBufferSize)
{
return GfVec4f(
_DivRoundDown(dataWindow.GetMinX(), renderBufferSize[0]),
_DivRoundDown(dataWindow.GetMaxX() + 1, renderBufferSize[0]),
_DivRoundDown(dataWindow.GetMinY(), renderBufferSize[1]),
_DivRoundDown(dataWindow.GetMaxY() + 1, renderBufferSize[1]));
}

void
HdPrmanCameraContext::SetRileyOptions(
RtParamList * const options,
const GfVec2i &renderBufferSize) const
{
const GfVec4f cropWindow =
_framing.IsValid()
? _ComputeCropWindow(_framing.dataWindow, renderBufferSize)
: GfVec4f(0,1,0,1);

options->SetFloatArray(
RixStr.k_Ri_CropWindow,
cropWindow.data(), 4);
}

void
HdPrmanCameraContext::MarkValid()
{
Expand Down
10 changes: 9 additions & 1 deletion third_party/renderman-24/plugin/hdPrman/cameraContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class HdPrmanCameraContext final
/// and the riley camera or options need to be updated.
bool IsInvalid() const;

/// Update the riley camera parameters and the parameters for the
/// Update the given riley camera parameters and the parameters for the
/// projection shader node for the camera.
///
/// Sets fStop, focalLength, focusDistance, clippingRange.
Expand All @@ -77,6 +77,14 @@ class HdPrmanCameraContext final
RtParamList * camNodeParams,
const GfVec2i &renderBufferSize) const;

/// Update the given riley options.
///
/// Sets the crop window - if framing is invalid, crop window will be set
/// to fill the entire render buffer.
void SetRileyOptions(
RtParamList * const options,
const GfVec2i &renderBufferSize) const;

/// Mark that riley camera and options are up to date.
void MarkValid();

Expand Down
65 changes: 3 additions & 62 deletions third_party/renderman-24/plugin/hdPrman/interactiveRenderPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,61 +85,6 @@ _DiffTimeToNow(std::chrono::steady_clock::time_point const& then)
return(diff.count());
}

// The crop window for RenderMan.
//
// Computed from data window and render buffer size.
//
// Recall from the RenderMan API:
// Only the pixels within the crop window are rendered. Has no
// affect on how pixels in the image map into the filmback plane.
// The crop window is relative to the render buffer size, e.g.,
// the crop window of (0,0,1,1) corresponds to the entire render
// buffer. The coordinates of the crop window are y-down.
// Format is (xmin, xmax, ymin, ymax).
//
// The limits for the integer locations corresponding to the above crop
// window are:
//
// rxmin = clamp(ceil( renderbufferwidth*xmin ), 0, renderbufferwidth - 1)
// rxmax = clamp(ceil( renderbufferwidth*xmax - 1), 0, renderbufferwidth - 1)
// similar for y
//
static
float
_DivRoundDown(const int a, const int b)
{
// Note that if the division (performed here)
// float(a) / b
// rounds up, then the result (by RenderMan) of
// ceil(b * (float(a) / b))
// might be a+1 instead of a.
//
// We add a slight negative bias to a to avoid this (we could also
// set the floating point rounding mode but: how to do this in a
// portable way - and on x86 switching the rounding is slow).

return GfClamp((a - 0.0078125f) / b, 0.0f, 1.0f);
}

static
GfVec4f
_GetCropWindow(
HdRenderPassStateSharedPtr const& renderPassState,
const int32_t width, const int32_t height)
{
const CameraUtilFraming &framing = renderPassState->GetFraming();
if (!framing.IsValid()) {
return GfVec4f(0,1,0,1);
}

const GfRect2i &w = framing.dataWindow;
return GfVec4f(
_DivRoundDown(w.GetMinX(), width),
_DivRoundDown(w.GetMaxX() + 1, width),
_DivRoundDown(w.GetMinY(), height),
_DivRoundDown(w.GetMaxY() + 1, height));
}

static
const HdRenderBuffer *
_GetRenderBuffer(const HdRenderPassAovBinding& aov,
Expand Down Expand Up @@ -387,13 +332,9 @@ HdPrman_InteractiveRenderPass::_Execute(
}
}

const GfVec4f cropWindow =
_GetCropWindow(
renderPassState, renderBufferWidth, renderBufferHeight);

_interactiveContext->GetOptions().SetFloatArray(
RixStr.k_Ri_CropWindow,
cropWindow.data(), 4);
cameraContext.SetRileyOptions(
&(_interactiveContext->GetOptions()),
GfVec2i(renderBufferWidth, renderBufferHeight));

riley->SetOptions(
_interactiveContext->_GetDeprecatedOptionsPrunedList());
Expand Down

0 comments on commit d956f50

Please sign in to comment.