Skip to content

Commit

Permalink
Refactor the code (PixarAnimationStudios#90)
Browse files Browse the repository at this point in the history
(cherry picked from commit 076abbae4d2bbed42ab4d465cef925b6aa06c245)
  • Loading branch information
PierreWang committed Jan 17, 2023
1 parent bcad947 commit b841b81
Show file tree
Hide file tree
Showing 16 changed files with 343 additions and 358 deletions.
3 changes: 1 addition & 2 deletions pxr/imaging/hd/simpleText.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2016 Pixar
// Copyright 2021 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
Expand All @@ -24,7 +24,6 @@
#include "pxr/pxr.h"
#include "pxr/imaging/hd/simpleText.h"
#include "pxr/imaging/hd/tokens.h"
#include "pxr/base/tf/envSetting.h"

PXR_NAMESPACE_OPEN_SCOPE

Expand Down
10 changes: 5 additions & 5 deletions pxr/imaging/hd/simpleTextTopology.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2016 Pixar
// Copyright 2021 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
Expand Down Expand Up @@ -58,14 +58,13 @@ HdSimpleTextTopology::operator==(HdSimpleTextTopology const &other) const
{
HD_TRACE_FUNCTION();

// no need to compare _adajency and _quadInfo
return (_pointCount == other._pointCount);
return _pointCount == other._pointCount;
}

bool
HdSimpleTextTopology::operator!=(HdSimpleTextTopology const &other) const
{
return !(*this == other);
return _pointCount != other._pointCount;
}

HdTopology::ID
Expand All @@ -74,6 +73,7 @@ HdSimpleTextTopology::ComputeHash() const
HD_TRACE_FUNCTION();

HdTopology::ID hash = 0;
// We only need to hash the point count.
hash = ArchHash64((const char*)&_pointCount, sizeof(int), hash);

// Note: We don't hash topological visibility, because it is treated as a
Expand All @@ -84,7 +84,7 @@ HdSimpleTextTopology::ComputeHash() const
std::ostream&
operator << (std::ostream &out, HdSimpleTextTopology const &topo)
{
out << "";
out << "(" << topo.GetPointCount() << ")";
return out;
}

Expand Down
23 changes: 13 additions & 10 deletions pxr/imaging/hd/simpleTextTopology.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2016 Pixar
// Copyright 2021 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
Expand All @@ -25,18 +25,20 @@
#define PXR_IMAGING_HD_SIMPLE_TEXT_TOPOLOGY_H

#include "pxr/pxr.h"
#include "pxr/imaging/hd/api.h"
#include "pxr/imaging/hd/version.h"
#include "pxr/imaging/hd/topology.h"
#include "pxr/imaging/hd/tokens.h"

#include "pxr/base/vt/array.h"
#include "pxr/base/vt/value.h"

#include "pxr/base/tf/token.h"

PXR_NAMESPACE_OPEN_SCOPE

/// \class HdSimpleTextTopology
///
/// Topology data for simpleText.
///
/// HdSimpleTextTopology holds the raw input topology data for simpleText.
///
/// The geometries of the text render items are always triangles, and we provide separate position
/// for each point. So the indices are always from zero to the count of points. The topology only
/// diffs by pointCount.
///
class HdSimpleTextTopology : public HdTopology {
public:
HD_API
Expand All @@ -52,11 +54,12 @@ class HdSimpleTextTopology : public HdTopology {
HD_API
virtual ID ComputeHash() const;

/// Returns point count of the text geometry.
int GetPointCount() const {
return _pointCount;
}

/// Equality check between two basisCurves topologies.
/// Equality check between two simpleText topologies.
HD_API
bool operator==(HdSimpleTextTopology const &other) const;
HD_API
Expand Down
34 changes: 20 additions & 14 deletions pxr/imaging/hdSt/shaders/text.glslfx
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,16 @@ out VSOutputShaderData
{
// The vertex position.
vec4 Peye;
// The interpolated texture coordinates.
vec2 UV;

vec2 UV;
vec2 TriType;
vec3 TextColor;
}outData;

// Vertex shader - for shader based text.
void main(void)
{
// The position in object space.
vec4 position = vec4(HdGet_points(), 1.0);

// Transform the position from object space to clip space for output.
Expand All @@ -135,6 +137,12 @@ void main(void)
outData.UV = HdGet_textUV();
outData.TriType = HdGet_triType();

// Pass the text color.
outData.TextColor = vec3(1.0, 0.5, 0.5);
#ifdef HD_HAS_displayColor
outData.TextColor = HdGet_displayColor().rgb;
#endif

gl_Position = vec4(GetProjectionMatrix() * outData.Peye);
gl_Position.z -= gl_Position.w* 2.0 * GetDepthPriority();
ApplyClipPlanes(outData.Peye);
Expand All @@ -148,18 +156,17 @@ in VSOutputShaderData
{
// The vertex position.
vec4 Peye;
// The interpolated texture coordinates.
vec2 UV;
vec2 UV;
vec2 TriType;
vec3 TextColor;
}inData;

// Pixel shader - for shader based text.
void main(void)
{
float alpha = 1.0;
#ifdef HD_HAS_displayOpacity
alpha = HdGet_displayOpacity().r;
#endif

vec2 uv = inData.UV;
vec2 triType = inData.TriType;

Expand Down Expand Up @@ -207,13 +214,12 @@ void main(void)
alpha *= dist < -0.5 ? 1.0 : ( dist < 0.5 ? (0.5 - dist) : 0.0);
}

vec3 displayColor = vec3(1.0, 0.5, 0.5);
#ifdef HD_HAS_displayColor
displayColor = HdGet_displayColor().rgb;
#endif

vec4 finalColor = vec4(displayColor * alpha, alpha);
finalColor = ApplyColorOverrides(finalColor);
// The text primitive have alpha natively. So here we first get the override color, then
// multiply the alpha of the primitive with the override alpha, and finally set the alpha
// to the final color.
vec4 overrideColor = ApplyColorOverrides(vec4(inData.TextColor.rgb, 1.0));
alpha = alpha * overrideColor.a;
vec4 finalColor = vec4(overrideColor.rgb * alpha, alpha);

vec3 Peye = inData.Peye.xyz / inData.Peye.w;
RenderOutput(vec4(Peye, 1), vec3(0, 0, 1), finalColor, vec4(1));
Expand Down
36 changes: 18 additions & 18 deletions pxr/imaging/hdSt/simpleText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,9 @@

#include "pxr/base/arch/hash.h"

#include "pxr/base/gf/matrix4d.h"
#include "pxr/base/gf/matrix4f.h"
#include "pxr/base/gf/vec2d.h"
#include "pxr/base/gf/vec2i.h"

#include "pxr/imaging/hd/bufferSource.h"
#include "pxr/imaging/hd/computation.h"
#include "pxr/imaging/hd/repr.h"
#include "pxr/imaging/hd/vertexAdjacency.h"
#include "pxr/imaging/hd/vtBufferSource.h"
#include "pxr/base/vt/value.h"

Expand All @@ -56,6 +50,7 @@ HdStSimpleText::HdStSimpleText(SdfPath const& id)
: HdSimpleText(id)
, _topology()
, _topologyId(0)
, _customDirtyBitsInUse(0)
, _refineLevel(0)
, _displayOpacity(false)
{
Expand Down Expand Up @@ -197,19 +192,15 @@ HdStSimpleText::_UpdateDrawItem(HdSceneDelegate *sceneDelegate,
// XXX: _PopulateTopology should be split into two phase
// for scene dirtybits and for repr dirtybits.
if (*dirtyBits & (HdChangeTracker::DirtyTopology
| HdChangeTracker::DirtyDisplayStyle)) {
| HdChangeTracker::DirtyDisplayStyle
| DirtyIndices)) {
_PopulateTopology(
sceneDelegate, renderParam, drawItem, dirtyBits);
}

/* PRIMVAR */
if ((*dirtyBits & HdChangeTracker::NewRepr) ||
HdChangeTracker::IsAnyPrimvarDirty(*dirtyBits, id)) {
// XXX: curves don't use refined vertex primvars, however,
// the refined renderpass masks the dirtiness of non-refined vertex
// primvars, so we need to see refined dirty for updating coarse
// vertex primvars if there is only refined reprs being updated.
// we'll fix the change tracking in order to address this craziness.
_PopulateVertexPrimvars(
sceneDelegate, renderParam, drawItem, dirtyBits);
}
Expand All @@ -220,8 +211,7 @@ HdStSimpleText::_UpdateDrawItem(HdSceneDelegate *sceneDelegate,
// work with delegates that don't keep information around once extracted.
*dirtyBits &= ~HdChangeTracker::AllSceneDirtyBits;

// Topology and VertexPrimvar may be null, if the curve has zero line
// segments.
// Topology and VertexPrimvar may be null.
TF_VERIFY(drawItem->GetConstantPrimvarRange());
}

Expand Down Expand Up @@ -260,7 +250,7 @@ HdStSimpleText::_PopulateTopology(HdSceneDelegate *sceneDelegate,
_topologyId = ArchHash64((const char*)&refined, sizeof(refined),
_topologyId);

// ask the registry if there is a sharable basisCurves topology
// ask the registry if there is a sharable simpleText topology
HdInstance<HdSt_SimpleTextTopologySharedPtr> topologyInstance =
resourceRegistry->RegisterSimpleTextTopology(_topologyId);

Expand All @@ -282,7 +272,9 @@ HdStSimpleText::_PopulateTopology(HdSceneDelegate *sceneDelegate,
}
}

if ((*dirtyBits & DirtyIndices) == 0) return;
TfToken indexToken;
*dirtyBits &= ~DirtyIndices;
indexToken = HdTokens->indices;

HdInstance<HdBufferArrayRangeSharedPtr> rangeInstance =
Expand Down Expand Up @@ -373,7 +365,7 @@ HdStSimpleText::_PopulateVertexPrimvars(HdSceneDelegate *sceneDelegate,
// are points
if (!_topology) {
if (primvar.name == HdTokens->points) {
TF_CODING_ERROR("No topology set for BasisCurve %s",
TF_CODING_ERROR("No topology set for SimpleText %s",
id.GetName().c_str());
break;
}
Expand Down Expand Up @@ -464,12 +456,13 @@ HdStSimpleText::_UpdateRepr(HdSceneDelegate *sceneDelegate,
}

// Filter custom dirty bits to only those in use.
*dirtyBits &= (HdChangeTracker::AllSceneDirtyBits |
*dirtyBits &= (_customDirtyBitsInUse |
HdChangeTracker::AllSceneDirtyBits |
HdChangeTracker::NewRepr);

if (TfDebug::IsEnabled(HD_RPRIM_UPDATED)) {
TfDebug::Helper().Msg(
"HdStBasisCurves::_UpdateRepr for %s : Repr = %s\n",
"HdStSimpleText::_UpdateRepr for %s : Repr = %s\n",
GetId().GetText(), reprToken.GetText());
HdChangeTracker::DumpDirtyBits(*dirtyBits);
}
Expand Down Expand Up @@ -551,6 +544,11 @@ HdStSimpleText::Finalize(HdRenderParam *renderParam)
HdDirtyBits
HdStSimpleText::_PropagateDirtyBits(HdDirtyBits bits) const
{
// propagate scene-based dirtyBits into rprim-custom dirtyBits
if (bits & HdChangeTracker::DirtyTopology) {
bits |= _customDirtyBitsInUse & DirtyIndices;
}

return bits;
}

Expand All @@ -566,6 +564,8 @@ HdStSimpleText::_InitRepr(TfToken const &reprToken, HdDirtyBits *dirtyBits)
HdReprSharedPtr &repr = _reprs.back().second;

*dirtyBits |= HdChangeTracker::NewRepr;
_customDirtyBitsInUse |= DirtyIndices;
*dirtyBits |= DirtyIndices;

HdRepr::DrawItemUniquePtr drawItem =
std::make_unique<HdStDrawItem>(&_sharedData);
Expand Down
34 changes: 10 additions & 24 deletions pxr/imaging/hdSt/simpleText.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2016 Pixar
// Copyright 2021 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
Expand Down Expand Up @@ -28,7 +28,6 @@
#include "pxr/imaging/hdSt/api.h"
#include "pxr/imaging/hd/version.h"
#include "pxr/imaging/hd/simpleText.h"
#include "pxr/imaging/hd/drawingCoord.h"
#include "pxr/imaging/hd/enums.h"
#include "pxr/imaging/hd/perfLog.h"

Expand All @@ -43,31 +42,13 @@ class HdStDrawItem;
using HdSt_SimpleTextTopologySharedPtr =
std::shared_ptr<class HdSt_SimpleTextTopology>;

/// \class HdStBasisCurves
/// \class HdStSimpleText
///
/// A collection of curves using a particular basis.
/// A single line single style text.
///
/// Render mode is dependent on both the HdBasisCurvesGeomStyle, refinement
/// level, and the authored primvars.
/// Currently we only support render the text using shader based technique. Next we will support
/// texture based technique.
///
/// If style is set to HdBasisCurvesGeomStyleWire, the curves will always draw
/// as infinitely thin wires. Cubic curves will be refined if complexity is
/// above 0, otherwise they draw the unrefined control points. (This may
/// provide a misleading representation for Catmull-Rom and Bspline curves.)
///
/// If style is set to HdBasisCurvesGeomStylePatch, the curves will draw as
/// patches ONLY if refinement level is above 0. Otherwise, they draw
/// as the unrefined control points (see notes on HdBasisCurvesGeomStyleWire).
///
/// Curves rendered as patches may be rendered as ribbons or halftubes.
/// Curves with primvar authored normals will always render as ribbons.
/// Curves without primvar authored normals are assumed to be round and may be
/// rendered in one of three styles:
/// * if complexity is 1, a camera facing normal is used
/// * if complexity is 2, a fake "bumped" round normal is used
/// * if complexity is 3 or above, the patch is displaced into a half tube
/// We plan for future checkins will remove the need for the camera facing normal
/// mode, using the fake "bumped" round normal instead.
class HdStSimpleText final: public HdSimpleText
{
public:
Expand Down Expand Up @@ -120,13 +101,18 @@ class HdStSimpleText final: public HdSimpleText
bool updateGeometricShader);

private:
enum DirtyBits : HdDirtyBits {
DirtyIndices = HdChangeTracker::CustomBitsBegin,
};

void _UpdateDrawItem(HdSceneDelegate *sceneDelegate,
HdRenderParam *renderParam,
HdStDrawItem *drawItem,
HdDirtyBits *dirtyBits);

HdSt_SimpleTextTopologySharedPtr _topology;
HdTopology::ID _topologyId;
HdDirtyBits _customDirtyBitsInUse;
int _refineLevel; // XXX: could be moved into HdBasisCurveTopology.
bool _displayOpacity : 1;
};
Expand Down
8 changes: 7 additions & 1 deletion pxr/imaging/hdSt/simpleTextShaderKey.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2016 Pixar
// Copyright 2021 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
Expand Down Expand Up @@ -34,10 +34,16 @@ PXR_NAMESPACE_OPEN_SCOPE
TF_DEFINE_PRIVATE_TOKENS(
_tokens,
((baseGLSLFX, "text.glslfx"))

// texture based
((vsTextureText, "VSTextureText"))
((psTextureText, "PSTextureText"))

// shader based
((vsShaderText, "VSShaderText"))
((psShaderText, "PSShaderText"))

// point id mixins
((pointIdNoneVS, "PointId.Vertex.None"))
((pointIdFallbackFS, "PointId.Fragment.Fallback"))
);
Expand Down
Loading

0 comments on commit b841b81

Please sign in to comment.