Skip to content

Commit

Permalink
[hdx] Add shader configurations and mixins to enable occluded selecti…
Browse files Browse the repository at this point in the history
…on to show through

- Add a render pass mixin that writes out whether a fragment is selected in addition to the shaded color. Storm task(s) that use this render pass configuration need to bind an additional AOV (of type float).
- Add a render pass mixin that adjusts the alpha component of the computed fragment color to allow occluded selection to show through. This requires a "selected" AOV to be bound and registered for readback in the task.
- Add selection mixin for use in the fragment shader to determine if a fragment is selected.

(Note: This change does not add the tasks that use these render pass configurations.)

(Internal change: 2136475)
  • Loading branch information
rajabala authored and pixar-oss committed Jan 21, 2021
1 parent ea21ae4 commit 100d826
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pxr/imaging/hdx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ pxr_library(hdx
shaders/outline.glslfx
shaders/renderPass.glslfx
shaders/renderPassColorShader.glslfx
shaders/renderPassColorAndSelectionShader.glslfx
shaders/renderPassColorWithOccludedSelectionShader.glslfx
shaders/renderPassIdShader.glslfx
shaders/renderPassPickingShader.glslfx
shaders/renderPassOitShader.glslfx
Expand Down
16 changes: 16 additions & 0 deletions pxr/imaging/hdx/package.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ HdxPackageRenderPassColorShader()
return shader;
}

TfToken
HdxPackageRenderPassColorAndSelectionShader()
{
static TfToken shader =
_GetShaderPath("renderPassColorAndSelectionShader.glslfx");
return shader;
}

TfToken
HdxPackageRenderPassColorWithOccludedSelectionShader()
{
static TfToken shader =
_GetShaderPath("renderPassColorWithOccludedSelectionShader.glslfx");
return shader;
}

TfToken
HdxPackageRenderPassIdShader()
{
Expand Down
2 changes: 2 additions & 0 deletions pxr/imaging/hdx/package.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ PXR_NAMESPACE_OPEN_SCOPE

TfToken HdxPackageFullscreenShader();
TfToken HdxPackageRenderPassColorShader();
TfToken HdxPackageRenderPassColorAndSelectionShader();
TfToken HdxPackageRenderPassColorWithOccludedSelectionShader();
TfToken HdxPackageRenderPassIdShader();
TfToken HdxPackageRenderPassPickingShader();
TfToken HdxPackageRenderPassShadowShader();
Expand Down
59 changes: 59 additions & 0 deletions pxr/imaging/hdx/shaders/renderPass.glslfx
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,62 @@ void RenderOutput(vec4 Peye, vec3 Neye, vec4 color, vec4 patchCoord)

neyeOut = IntToVec4(hd_vec4_2_10_10_10_set(vec4(Neye,0)));
}

-- glsl HdxRenderPass.RenderColorAndSelection
// Note: This mixin expects color and selected color attachments in the bound
// FBO. It writes out the computed fragment color and whether it is selected
// (as a float, so, 1.0 or 0.0).

bool IsSelected(); // defined in selection.glslfx

layout (location = 0) out vec4 colorOut;
layout (location = 1) out float selectedOut;

void RenderOutput(vec4 Peye, vec3 Neye, vec4 color, vec4 patchCoord)
{
colorOut = color;
selectedOut = float(IsSelected());
}

-- glsl HdxRenderPass.RenderColorWithOccludedSelection
// Note: This mixin expects color and selected color attachments in the bound
// FBO. The alpha component of the computed fragment color is adjusted to blend
// the existing (destination) fragment color if it is selected.

bool
HasOccludedSelection(vec2 fragcoord)
{
#ifdef HD_HAS_selectedReadback
const float isSelected = texelFetch(HdGetSampler_selectedReadback(),
ivec2(fragcoord),
0).x;
return bool(isSelected);
#endif
return false;
}

float
GetShowThroughOpacity()
{
#ifdef HD_HAS_occludedSelectionOpacity
// Note: occludedSelectionOpacity flows in as a parameter to the selection
// setup task (HdxSelectionTask)
const float dstOpacity = HdGet_occludedSelectionOpacity();
// adjust source alpha used to blend source and dst colors.
return 1.0 - dstOpacity;
#else
return 0.5;
#endif
}

layout (location = 0) out vec4 colorOut;

// Input AOV textures are provided by the task.
void RenderOutput(vec4 Peye, vec3 Neye, vec4 color, vec4 patchCoord)
{
if (HasOccludedSelection(gl_FragCoord.xy)) {
color.a = GetShowThroughOpacity();
}

colorOut = color;
}
67 changes: 67 additions & 0 deletions pxr/imaging/hdx/shaders/renderPassColorAndSelectionShader.glslfx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- glslfx version 0.1

//
// Copyright 2020 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//

--- This is what an import might look like.
--- #import $TOOLS/hdx/shaders/renderPassShader.glslfx
// Render pass shader configuration that writes color and selection information
// (whether the fragment is selected).

#import $TOOLS/hdSt/shaders/renderPass.glslfx
#import $TOOLS/hdx/shaders/renderPass.glslfx
#import $TOOLS/hdx/shaders/selection.glslfx

-- configuration
{
"techniques": {
"default": {
"vertexShader" : {
"source": [ "RenderPass.Camera",
"RenderPass.ApplyClipPlanes" ]
},
"tessControlShader" : {
"source": [ "RenderPass.Camera",
"RenderPass.ApplyClipPlanes" ]
},
"tessEvalShader" : {
"source": [ "RenderPass.Camera",
"RenderPass.ApplyClipPlanes" ]
},
"geometryShader" : {
"source": [ "RenderPass.Camera",
"RenderPass.ApplyClipPlanes" ]
},
"fragmentShader" : {
"source": [ "RenderPass.Camera",
"RenderPass.ShouldCullFace",
"Selection.DecodeUtils",
"Selection.ComputeColor",
"Selection.Fragment",
"RenderPass.ApplyColorOverrides",
"HdxRenderPass.RenderColorAndSelection" ]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
-- glslfx version 0.1

//
// Copyright 2020 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//

--- This is what an import might look like.
--- #import $TOOLS/hdx/shaders/renderPassShader.glslfx
// Render pass shader configuration that writes color and selection information
// (whether the fragment is selected).

#import $TOOLS/hdSt/shaders/renderPass.glslfx
#import $TOOLS/hdx/shaders/renderPass.glslfx
#import $TOOLS/hdx/shaders/selection.glslfx

-- configuration
{
"techniques": {
"default": {
"vertexShader" : {
"source": [ "RenderPass.Camera",
"RenderPass.ApplyClipPlanes" ]
},
"tessControlShader" : {
"source": [ "RenderPass.Camera",
"RenderPass.ApplyClipPlanes" ]
},
"tessEvalShader" : {
"source": [ "RenderPass.Camera",
"RenderPass.ApplyClipPlanes" ]
},
"geometryShader" : {
"source": [ "RenderPass.Camera",
"RenderPass.ApplyClipPlanes" ]
},
"fragmentShader" : {
"source": [ "RenderPass.Camera",
"RenderPass.ShouldCullFace",
"Selection.DecodeUtils",
"Selection.ComputeColor",
"RenderPass.ApplyColorOverrides",
"HdxRenderPass.RenderColorWithOccludedSelection" ]
}
}
}
}
26 changes: 24 additions & 2 deletions pxr/imaging/hdx/shaders/selection.glslfx
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ SelectionResult GetNetSelectionResult(int elementId, int edgeId, int pointId)

--- --------------------------------------------------------------------------
-- glsl Selection.Vertex.PointSel
// Mixin for use in the vertex shader stage.
// Decodes the selection buffer to find out if the current vertex (point) is
// selected. This is called from hdSt/shaders/pointId.glslfx
bool IsPointSelected(int pointId)
Expand All @@ -331,8 +332,9 @@ bool IsPointSelected(int pointId)

--- --------------------------------------------------------------------------
-- glsl Selection.Geometry.ElementSel
/// Helper functions to determine if the element (face) is selected for either
/// a given selection mode, or any of the modes.
// Mixin for use in the geometry shader stage.
// Helper functions to determine if the element (face) is selected for either
// a given selection mode, or any of the modes.

int GetElementID(); // code gen

Expand Down Expand Up @@ -364,6 +366,8 @@ bool IsSelected()

--- --------------------------------------------------------------------------
-- glsl Selection.Geometry.WireSelOffset
// Mixin for use in the geometry shader stage for wireframe rendering.
// See comment below.

int GetMaxNumSelectionHighlightModes();
bool IsElementSelected(int mode);
Expand Down Expand Up @@ -414,6 +418,24 @@ vec4 ComputeSelectionOffset()
return vec4(0);
}

--- --------------------------------------------------------------------------
-- glsl Selection.Fragment
// Mixin for use in the fragment shader stage to determine if the fragment is
// selected.

int GetElementID(); // generated via codeGen
int GetAuthoredEdgeId(int); // generated via codeGen
int GetPrimitiveEdgeId(); // defined in edgeId.glslfx, or generated via codeGen
int GetPointId(); // defined in pointId.glslfx

bool IsSelected()
{
SelectionResult res = GetNetSelectionResult(
GetElementID(), GetAuthoredEdgeId(GetPrimitiveEdgeId()), GetPointId());

return res.primOrInstanceSel || res.subprimSel.elementSelected;
}

--- --------------------------------------------------------------------------
-- glsl Selection.ComputeColor

Expand Down

0 comments on commit 100d826

Please sign in to comment.