Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APPLE: Texture and buffer writability in Metal/HGI along with settings where appropriate #2042

Merged
merged 1 commit into from
Sep 28, 2022
Merged
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
2 changes: 2 additions & 0 deletions pxr/imaging/hdSt/domeLightComputations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ HdSt_DomeLightComputationGPU::Execute(
HgiTextureBindDesc texBind0;
texBind0.bindingIndex = 0;
texBind0.stageUsage = HgiShaderStageCompute;
texBind0.writable = false;
texBind0.textures.push_back(srcTextureName);
texBind0.samplers.push_back(srcSamplerName);
texBind0.resourceType = HgiBindResourceTypeCombinedSamplerImage;
Expand All @@ -246,6 +247,7 @@ HdSt_DomeLightComputationGPU::Execute(
HgiTextureBindDesc texBind1;
texBind1.bindingIndex = 1;
texBind1.stageUsage = HgiShaderStageCompute;
texBind1.writable = true;
texBind1.textures.push_back(dstTextureView->GetViewTexture());
texBind1.samplers.push_back(srcSamplerName);
texBind1.resourceType = HgiBindResourceTypeStorageImage;
Expand Down
1 change: 1 addition & 0 deletions pxr/imaging/hdSt/extCompGpuComputation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ _AppendResourceBindings(
bufBind.bindingIndex = location;
bufBind.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind.stageUsage = HgiShaderStageCompute;
bufBind.writable = true;
bufBind.offsets.push_back(0);
bufBind.buffers.push_back(buffer);
resourceDesc->buffers.push_back(std::move(bufBind));
Expand Down
4 changes: 4 additions & 0 deletions pxr/imaging/hdSt/flatNormals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ _CreateResourceBindings(
bufBind0.bindingIndex = BufferBinding_Points;
bufBind0.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind0.stageUsage = HgiShaderStageCompute;
bufBind0.writable = false;
bufBind0.offsets.push_back(0);
bufBind0.buffers.push_back(points);
resourceDesc.buffers.push_back(std::move(bufBind0));
Expand All @@ -85,6 +86,7 @@ _CreateResourceBindings(
bufBind1.bindingIndex = BufferBinding_Normals;
bufBind1.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind1.stageUsage = HgiShaderStageCompute;
bufBind1.writable = true;
bufBind1.offsets.push_back(0);
bufBind1.buffers.push_back(normals);
resourceDesc.buffers.push_back(std::move(bufBind1));
Expand All @@ -95,6 +97,7 @@ _CreateResourceBindings(
bufBind2.bindingIndex = BufferBinding_Indices;
bufBind2.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind2.stageUsage = HgiShaderStageCompute;
bufBind2.writable = false;
bufBind2.offsets.push_back(0);
bufBind2.buffers.push_back(indices);
resourceDesc.buffers.push_back(std::move(bufBind2));
Expand All @@ -105,6 +108,7 @@ _CreateResourceBindings(
bufBind3.bindingIndex = BufferBinding_PrimitiveParam;
bufBind3.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind3.stageUsage = HgiShaderStageCompute;
bufBind3.writable = false;
bufBind3.offsets.push_back(0);
bufBind3.buffers.push_back(primitiveParam);
resourceDesc.buffers.push_back(std::move(bufBind3));
Expand Down
2 changes: 2 additions & 0 deletions pxr/imaging/hdSt/quadrangulate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ _CreateResourceBindings(
bufBind0.bindingIndex = BufferBinding_Primvar;
bufBind0.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind0.stageUsage = HgiShaderStageCompute;
bufBind0.writable = true;
bufBind0.offsets.push_back(0);
bufBind0.buffers.push_back(primvar);
resourceDesc.buffers.push_back(std::move(bufBind0));
Expand All @@ -83,6 +84,7 @@ _CreateResourceBindings(
bufBind1.bindingIndex = BufferBinding_Quadinfo;
bufBind1.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind1.stageUsage = HgiShaderStageCompute;
bufBind1.writable = true;
bufBind1.offsets.push_back(0);
bufBind1.buffers.push_back(quadrangulateTable);
resourceDesc.buffers.push_back(std::move(bufBind1));
Expand Down
4 changes: 4 additions & 0 deletions pxr/imaging/hdSt/resourceBinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ HdSt_ResourceBinder::GetBufferBindingDesc(
HgiShaderStageVertex | HgiShaderStageFragment |
HgiShaderStagePostTessellationVertex;
HgiBufferBindDesc desc;
desc.writable = true;

switch (binding.GetType()) {
case HdBinding::SSBO:
Expand All @@ -1004,6 +1005,7 @@ HdSt_ResourceBinder::GetBufferBindingDesc(
desc.bindingIndex = static_cast<uint32_t>(binding.GetLocation());
desc.resourceType = HgiBindResourceTypeStorageBuffer;
desc.stageUsage = stageUsage;
desc.writable = false;
bindingsDesc->buffers.push_back(desc);
break;
case HdBinding::UBO:
Expand Down Expand Up @@ -1123,6 +1125,7 @@ HdSt_ResourceBinder::GetTextureBindingDesc(
texelDesc.samplers = { texelSampler };
texelDesc.resourceType = HgiBindResourceTypeSampledImage;
texelDesc.bindingIndex = binding.GetTextureUnit();
texelDesc.writable = false;
bindingsDesc->textures.push_back(std::move(texelDesc));
}

Expand All @@ -1146,6 +1149,7 @@ HdSt_ResourceBinder::GetTextureWithLayoutBindingDesc(
layoutDesc.samplers = { };
layoutDesc.resourceType = HgiBindResourceTypeSampledImage;
layoutDesc.bindingIndex = layoutBinding.GetTextureUnit();
layoutDesc.writable = false;
bindingsDesc->textures.push_back(std::move(layoutDesc));
}

Expand Down
3 changes: 3 additions & 0 deletions pxr/imaging/hdSt/smoothNormals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ _CreateResourceBindings(
bufBind0.bindingIndex = BufferBinding_Points;
bufBind0.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind0.stageUsage = HgiShaderStageCompute;
bufBind0.writable = false;
bufBind0.offsets.push_back(0);
bufBind0.buffers.push_back(points);
resourceDesc.buffers.push_back(std::move(bufBind0));
Expand All @@ -84,6 +85,7 @@ _CreateResourceBindings(
bufBind1.bindingIndex = BufferBinding_Normals;
bufBind1.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind1.stageUsage = HgiShaderStageCompute;
bufBind1.writable = true;
bufBind1.offsets.push_back(0);
bufBind1.buffers.push_back(normals);
resourceDesc.buffers.push_back(std::move(bufBind1));
Expand All @@ -94,6 +96,7 @@ _CreateResourceBindings(
bufBind2.bindingIndex = BufferBinding_Adjacency;
bufBind2.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind2.stageUsage = HgiShaderStageCompute;
bufBind2.writable = false;
bufBind2.offsets.push_back(0);
bufBind2.buffers.push_back(adjacency);
resourceDesc.buffers.push_back(std::move(bufBind2));
Expand Down
5 changes: 5 additions & 0 deletions pxr/imaging/hdSt/subdivision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,7 @@ _CreateResourceBindings(
bufBind0.bindingIndex = BufferBinding_Sizes;
bufBind0.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind0.stageUsage = HgiShaderStageCompute;
bufBind0.writable = false;
bufBind0.offsets.push_back(0);
bufBind0.buffers.push_back(sizes);
resourceDesc.buffers.push_back(std::move(bufBind0));
Expand All @@ -1739,6 +1740,7 @@ _CreateResourceBindings(
bufBind1.bindingIndex = BufferBinding_Offsets;
bufBind1.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind1.stageUsage = HgiShaderStageCompute;
bufBind1.writable = false;
bufBind1.offsets.push_back(0);
bufBind1.buffers.push_back(offsets);
resourceDesc.buffers.push_back(std::move(bufBind1));
Expand All @@ -1749,6 +1751,7 @@ _CreateResourceBindings(
bufBind2.bindingIndex = BufferBinding_Indices;
bufBind2.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind2.stageUsage = HgiShaderStageCompute;
bufBind2.writable = false;
bufBind2.offsets.push_back(0);
bufBind2.buffers.push_back(indices);
resourceDesc.buffers.push_back(std::move(bufBind2));
Expand All @@ -1759,6 +1762,7 @@ _CreateResourceBindings(
bufBind3.bindingIndex = BufferBinding_Weights;
bufBind3.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind3.stageUsage = HgiShaderStageCompute;
bufBind3.writable = false;
bufBind3.offsets.push_back(0);
bufBind3.buffers.push_back(weights);
resourceDesc.buffers.push_back(std::move(bufBind3));
Expand All @@ -1769,6 +1773,7 @@ _CreateResourceBindings(
bufBind4.bindingIndex = BufferBinding_Primvar;
bufBind4.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind4.stageUsage = HgiShaderStageCompute;
bufBind4.writable = true;
bufBind4.offsets.push_back(0);
bufBind4.buffers.push_back(primvar);
resourceDesc.buffers.push_back(std::move(bufBind4));
Expand Down
1 change: 1 addition & 0 deletions pxr/imaging/hdSt/unitTestHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ HdSt_TextureTestDriver::_CreateTextureBindings(
HgiTextureBindDesc texBindDesc;
texBindDesc.bindingIndex = 0;
texBindDesc.stageUsage = HgiShaderStageFragment;
texBindDesc.writable = true;
texBindDesc.textures.push_back(textureHandle);
if (samplerHandle) {
texBindDesc.samplers.push_back(samplerHandle);
Expand Down
1 change: 1 addition & 0 deletions pxr/imaging/hdx/boundingBoxTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ HdxBoundingBoxTask::_CreateResourceBindings()
bufBind1.offsets.push_back(0);
bufBind1.sizes.push_back(0);
bufBind1.buffers.push_back(_transformsBuffer);
bufBind1.writable = false;
resourceDesc.buffers.push_back(std::move(bufBind1));

_resourceBindings = _GetHgi()->CreateResourceBindings(resourceDesc);
Expand Down
2 changes: 2 additions & 0 deletions pxr/imaging/hdx/colorCorrectionTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ HdxColorCorrectionTask::_CreateResourceBindings(
HgiTextureBindDesc texBind0;
texBind0.bindingIndex = 0;
texBind0.stageUsage = HgiShaderStageFragment;
texBind0.writable = false;
texBind0.textures.push_back(aovTexture);
texBind0.samplers.push_back(_sampler);
resourceDesc.textures.push_back(std::move(texBind0));
Expand All @@ -356,6 +357,7 @@ HdxColorCorrectionTask::_CreateResourceBindings(
HgiTextureBindDesc texBind1;
texBind1.bindingIndex = 1;
texBind1.stageUsage = HgiShaderStageFragment;
texBind1.writable = false;
texBind1.textures.push_back(_texture3dLUT);
texBind1.samplers.push_back(_sampler);
resourceDesc.textures.push_back(std::move(texBind1));
Expand Down
2 changes: 2 additions & 0 deletions pxr/imaging/hdx/fullscreenShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ HdxFullscreenShader::_CreateResourceBindings(TextureMap const& textures)
HgiTextureBindDesc texBind;
texBind.bindingIndex = bindSlots++;
texBind.stageUsage = HgiShaderStageFragment;
texBind.writable = false;
texBind.textures.push_back(texHandle);
texBind.samplers.push_back(_sampler);
resourceDesc.textures.push_back(std::move(texBind));
Expand All @@ -372,6 +373,7 @@ HdxFullscreenShader::_CreateResourceBindings(TextureMap const& textures)
bufBind.bindingIndex = buffer.first;
bufBind.resourceType = HgiBindResourceTypeStorageBuffer;
bufBind.stageUsage = HgiShaderStageFragment;
bufBind.writable = false;
bufBind.offsets.push_back(0);
bufBind.buffers.push_back(bufferHandle);
resourceDesc.buffers.push_back(std::move(bufBind));
Expand Down
1 change: 1 addition & 0 deletions pxr/imaging/hdx/visualizeAovTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ HdxVisualizeAovTask::_CreateResourceBindings(
HgiTextureBindDesc texBind0;
texBind0.bindingIndex = 0;
texBind0.stageUsage = HgiShaderStageFragment;
texBind0.writable = false;
texBind0.textures.push_back(inputAovTexture);
texBind0.samplers.push_back(_sampler);
resourceDesc.textures.push_back(std::move(texBind0));
Expand Down
6 changes: 4 additions & 2 deletions pxr/imaging/hgi/resourceBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ bool operator==(
lhs.offsets == rhs.offsets &&
lhs.sizes == rhs.sizes &&
lhs.bindingIndex == rhs.bindingIndex &&
lhs.stageUsage == rhs.stageUsage;
lhs.stageUsage == rhs.stageUsage &&
lhs.writable == rhs.writable;
}

bool operator!=(
Expand All @@ -78,7 +79,8 @@ bool operator==(
lhs.resourceType == rhs.resourceType &&
lhs.bindingIndex == rhs.bindingIndex &&
lhs.stageUsage == rhs.stageUsage &&
lhs.samplers == rhs.samplers;
lhs.samplers == rhs.samplers &&
lhs.writable == rhs.writable;
}

bool operator!=(
Expand Down
2 changes: 2 additions & 0 deletions pxr/imaging/hgi/resourceBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ struct HgiBufferBindDesc
HgiBindResourceType resourceType;
uint32_t bindingIndex;
HgiShaderStage stageUsage;
bool writable;
};
using HgiBufferBindDescVector = std::vector<HgiBufferBindDesc>;

Expand Down Expand Up @@ -130,6 +131,7 @@ struct HgiTextureBindDesc
HgiBindResourceType resourceType;
uint32_t bindingIndex;
HgiShaderStage stageUsage;
bool writable;
};
using HgiTextureBindDescVector = std::vector<HgiTextureBindDesc>;

Expand Down
50 changes: 31 additions & 19 deletions pxr/imaging/hgiMetal/resourceBindings.mm
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,20 @@
atIndex:0];
}
if (metalTexture) {
if (metalSampler) {
[renderEncoder useResource:metalTexture
usage:MTLResourceUsageSample|
MTLResourceUsageRead|
MTLResourceUsageWrite];
}
else {
[renderEncoder useResource:metalTexture
usage:MTLResourceUsageRead|MTLResourceUsageWrite];
}
}
MTLResourceUsage usageFlags;
if (metalSampler) {
usageFlags = MTLResourceUsageSample|
MTLResourceUsageRead;
}
else {
usageFlags = MTLResourceUsageRead;
}
if (texDesc.writable) {
usageFlags = usageFlags | MTLResourceUsageWrite;
}
[renderEncoder useResource:metalTexture
usage:usageFlags];
}
}

[renderEncoder setVertexBuffer:argBuffer
Expand Down Expand Up @@ -151,7 +154,6 @@

id<MTLBuffer> bufferId = metalbuffer->GetBufferId();
NSUInteger offset = bufDesc.offsets.front();

if ((bufDesc.stageUsage & HgiShaderStageVertex) ||
(bufDesc.stageUsage & HgiShaderStagePostTessellationVertex)) {
NSUInteger argBufferOffset = HgiMetalArgumentOffsetBufferVS
Expand All @@ -168,9 +170,13 @@
offset:argBufferOffset];
[argEncoderBuffer setBuffer:bufferId offset:offset atIndex:0];
}
MTLResourceUsage usageFlags = MTLResourceUsageRead;
if (bufDesc.writable) {
usageFlags = usageFlags | MTLResourceUsageWrite;
}

[renderEncoder useResource:bufferId
usage:(MTLResourceUsageRead | MTLResourceUsageWrite)];
usage:usageFlags];
}


Expand Down Expand Up @@ -233,17 +239,19 @@
+ (texDesc.bindingIndex * sizeof(void*));
[argEncoderTexture setArgumentBuffer:argBuffer
offset:offsetTexture];
MTLResourceUsage usage = MTLResourceUsageRead;
if (texDesc.writable) {
usage = usage | MTLResourceUsageWrite;
}
[argEncoderTexture setTexture:metalTexture->GetTextureId() atIndex:0];
if (metalSmp) {
usage = usage | MTLResourceUsageSample;
[computeEncoder useResource:metalTexture->GetTextureId()
usage:MTLResourceUsageSample|
MTLResourceUsageRead|
MTLResourceUsageWrite];
usage:usage];
}
else {
[computeEncoder useResource:metalTexture->GetTextureId()
usage:MTLResourceUsageRead|
MTLResourceUsageWrite];
usage:usage];
}
}
}
Expand Down Expand Up @@ -277,8 +285,12 @@
[argEncoderBuffer setArgumentBuffer:argBuffer
offset:argBufferOffset];
[argEncoderBuffer setBuffer:bufferId offset:offset atIndex:0];
MTLResourceUsage usage = MTLResourceUsageRead;
if (bufDesc.writable) {
usage = usage | MTLResourceUsageWrite;
}
[computeEncoder useResource:bufferId
usage:(MTLResourceUsageRead | MTLResourceUsageWrite)];
usage:usage];
}

[computeEncoder setBuffer:argBuffer
Expand Down
12 changes: 9 additions & 3 deletions pxr/imaging/hgiMetal/shaderSection.mm
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,10 @@
{
if (!_writable) {
ss << "const ";
ss << "constant ";
} else {
ss << "device ";
}
ss << "device ";
WriteType(ss);

switch (_binding) {
Expand All @@ -702,8 +704,10 @@

if (!_writable) {
ss << "const ";
ss << "constant ";
} else {
ss << "device ";
}
ss << "device ";
WriteType(ss);

switch (_binding) {
Expand Down Expand Up @@ -731,8 +735,10 @@

if (!_writable) {
ss << "const ";
ss << "constant ";
} else {
ss << "device ";
}
ss << "device ";
WriteType(ss);
ss << "* _";
WriteIdentifier(ss);
Expand Down