Skip to content

Commit

Permalink
Hd: Link to Sdr target.
Browse files Browse the repository at this point in the history
  • Loading branch information
furby-tm committed Aug 6, 2024
1 parent 6f05388 commit 4c02b54
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,7 @@ let package = Package(
.target(name: "Vt"),
.target(name: "Work"),
.target(name: "Sdf"),
.target(name: "Sdr"),
.target(name: "CameraUtil"),
.target(name: "Hf"),
.target(name: "PxOsd"),
Expand Down
11 changes: 7 additions & 4 deletions Sources/HgiMetal/graphicsCmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ HgiMetalGraphicsCmds::HgiMetalGraphicsCmds(HgiMetal *hgi, HgiGraphicsCmdsDesc co
_primitiveIndexSize(0),
_drawBufferBindingIndex(0),
_debugLabel(nil),
_hasWork(false),
_viewportSet(false),
_scissorRectSet(false),
_enableParallelEncoder(false),
Expand Down Expand Up @@ -496,7 +495,7 @@ void HgiMetalGraphicsCmds::Draw(uint32_t vertexCount,
}
}

_hasWork = true;
_hgi->SetHasWork();
}

void HgiMetalGraphicsCmds::DrawIndirect(HgiBufferHandle const &drawParameterBuffer,
Expand Down Expand Up @@ -548,6 +547,8 @@ void HgiMetalGraphicsCmds::DrawIndirect(HgiBufferHandle const &drawParameterBuff
});
}
});

_hgi->SetHasWork();
}

void HgiMetalGraphicsCmds::DrawIndexed(HgiBufferHandle const &indexBuffer,
Expand Down Expand Up @@ -595,7 +596,7 @@ void HgiMetalGraphicsCmds::DrawIndexed(HgiBufferHandle const &indexBuffer,
baseInstance);
}

_hasWork = true;
_hgi->SetHasWork();
}

void HgiMetalGraphicsCmds::DrawIndexedIndirect(
Expand Down Expand Up @@ -663,6 +664,8 @@ void HgiMetalGraphicsCmds::DrawIndexedIndirect(
});
}
});

_hgi->SetHasWork();
}

void HgiMetalGraphicsCmds::PushDebugGroup(const char *label)
Expand Down Expand Up @@ -750,7 +753,7 @@ bool HgiMetalGraphicsCmds::_Submit(Hgi *hgi, HgiSubmitWaitType wait)
_encoders.clear();
_CachedEncState.ResetCachedEncoderState();

return _hasWork;
return true;
}

PXR_NAMESPACE_CLOSE_SCOPE
3 changes: 3 additions & 0 deletions Sources/HgiMetal/include/HgiMetal/graphicsPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

#include "HgiMetal/api.h"

#include <Foundation/Foundation.hpp>
#include <Metal/Metal.hpp>

PXR_NAMESPACE_OPEN_SCOPE

class HgiMetal;
Expand Down
72 changes: 53 additions & 19 deletions Sources/HgiMetal/shaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

#include <Metal/Metal.hpp>

#include "HgiMetal/shaderGenerator.h"
#include "Hgi/tokens.h"
#include "HgiMetal/resourceBindings.h"
#include "HgiMetal/shaderGenerator.h"

#include <sstream>
#include <unordered_map>
Expand Down Expand Up @@ -583,6 +583,8 @@ HgiMetalShaderSectionPtrVector ShaderStageData::AccumulateParams(
HgiShaderStage stage,
bool iterateAttrs)
{
// Currently we don't add qualifiers for function parameters.
const static std::string emptyQualifiers("");
HgiMetalShaderSectionPtrVector stageShaderSections;
// only some roles have an index
if (!iterateAttrs) {
Expand Down Expand Up @@ -615,20 +617,12 @@ HgiMetalShaderSectionPtrVector ShaderStageData::AccumulateParams(
attributes.push_back(HgiShaderSectionAttribute{role, indexAsStr});
}

switch (p.interpolation) {
case HgiInterpolationDefault:
break;
case HgiInterpolationFlat:
attributes.push_back(HgiShaderSectionAttribute{"flat", ""});
break;
case HgiInterpolationNoPerspective:
attributes.push_back(HgiShaderSectionAttribute{"center_no_perspective", ""});
break;
}
attributes.push_back(
HgiShaderSectionAttribute{_GetInterpolationString(p.interpolation, p.sampling), ""});

HgiMetalMemberShaderSection *const section =
generator->CreateShaderSection<HgiMetalMemberShaderSection>(
p.nameInShader, p.type, attributes, p.arraySize);
p.nameInShader, p.type, emptyQualifiers, attributes, p.arraySize);
stageShaderSections.push_back(section);
}
}
Expand All @@ -648,11 +642,10 @@ HgiMetalShaderSectionPtrVector ShaderStageData::AccumulateParams(

HgiMetalMemberShaderSection *const section =
generator->CreateShaderSection<HgiMetalMemberShaderSection>(
p.nameInShader, p.type, attributes, p.arraySize);
p.nameInShader, p.type, emptyQualifiers, attributes, p.arraySize);
stageShaderSections.push_back(section);
}
}

return stageShaderSections;
}

Expand All @@ -674,7 +667,12 @@ HgiMetalInterstageBlockShaderSectionPtrVector ShaderStageData::AccumulateParamBl

HgiMetalMemberShaderSection *const memberSection =
generator->CreateShaderSection<HgiMetalMemberShaderSection>(
m.name, m.type, attributes, std::string(), p.instanceName);
m.name,
m.type,
_GetInterpolationString(m.interpolation, m.sampling),
attributes,
std::string(),
p.instanceName);
blockMembers.push_back(memberSection);
}

Expand Down Expand Up @@ -1152,23 +1150,36 @@ std::unique_ptr<HgiMetalShaderStageEntryPoint> HgiMetalShaderGenerator::
}
}

HgiMetalShaderGenerator::HgiMetalShaderGenerator(const HgiShaderFunctionDesc &descriptor,
MTL::Device *device)
HgiMetalShaderGenerator::HgiMetalShaderGenerator(HgiMetal const *hgi,
const HgiShaderFunctionDesc &descriptor)
: HgiShaderGenerator(descriptor),
_hgi(hgi),
_generatorShaderSections(_BuildShaderStageEntryPoints(descriptor))
{
// Currently we don't add qualifiers for global uniforms.
const static std::string emptyQualifiers("");
for (const auto &member : descriptor.stageGlobalMembers) {
HgiShaderSectionAttributeVector attrs;
CreateShaderSection<HgiMetalMemberShaderSection>(
member.nameInShader, member.type, attrs, member.arraySize);
member.nameInShader, member.type, emptyQualifiers, attrs, member.arraySize);
}

std::stringstream macroSection;
macroSection << _GetHeader(device, descriptor.shaderStage);
macroSection << _GetHeader(hgi->GetPrimaryDevice(), descriptor.shaderStage);

if (_IsTessFunction(descriptor)) {
macroSection << "#define VERTEX_CONTROL_POINTS_PER_PATCH "
<< descriptor.tessellationDescriptor.numVertsPerPatchIn << "\n";
}

if (_hgi->GetCapabilities()->requiresReturnAfterDiscard) {
macroSection << "#define discard discard_fragment(); "
"discarded_fragment = true;\n";
}
else {
macroSection << "#define discard discard_fragment();\n";
}

CreateShaderSection<HgiMetalMacroShaderSection>(macroSection.str(), "Headers");
}

Expand Down Expand Up @@ -1204,6 +1215,11 @@ void HgiMetalShaderGenerator::_Execute(std::ostream &ss)
section->VisitScopeStructs(ss);
}
ss << "\n// //////// Scope Member Declarations ////////\n";
if (_hgi->GetCapabilities()->requiresReturnAfterDiscard) {
if (this->_GetShaderStage() == HgiShaderStageFragment) {
ss << "bool discarded_fragment;\n";
}
}
for (const HgiMetalShaderSectionUniquePtr &section : *shaderSections) {
section->VisitScopeMemberDeclarations(ss);
}
Expand Down Expand Up @@ -1315,13 +1331,31 @@ void HgiMetalShaderGenerator::_Execute(std::ostream &ss)

// Execute all code that hooks into the entry point function
ss << "\n// //////// Entry Point Function Executions ////////\n";
if (_hgi->GetCapabilities()->requiresReturnAfterDiscard) {
if (this->_GetShaderStage() == HgiShaderStageFragment) {
ss << _generatorShaderSections->GetScopeInstanceName() << ".discarded_fragment = false;\n";
}
}
for (const HgiMetalShaderSectionUniquePtr &section : *shaderSections) {
if (section->VisitEntryPointFunctionExecutions(
ss, _generatorShaderSections->GetScopeInstanceName()))
{
ss << "\n";
}
}
if (_hgi->GetCapabilities()->requiresReturnAfterDiscard) {
if (this->_GetShaderStage() == HgiShaderStageFragment) {
ss << "if (" << _generatorShaderSections->GetScopeInstanceName() << ".discarded_fragment)\n";
ss << "{\n";
if (outputs) {
ss << " return {};\n";
}
else {
ss << " return;\n";
}
ss << "}\n";
}
}
// return the instance of the shader entrypoint output type
if (outputs && (_GetShaderStage() != HgiShaderStagePostTessellationControl)) {
const std::string outputInstanceName = _generatorShaderSections->GetOutputInstanceName();
Expand Down

0 comments on commit 4c02b54

Please sign in to comment.