Skip to content

Commit

Permalink
Merge branch 'sultimt/ub-fix' into 'main'
Browse files Browse the repository at this point in the history
[REMIX-3157] Fix dangling pointer to prevent incorrect shader bindings

See merge request lightspeedrtx/dxvk-remix-nv!874
  • Loading branch information
sultim-t-nv committed Jun 18, 2024
2 parents 2f33b3b + 294e3f6 commit 6e5135e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
16 changes: 10 additions & 6 deletions src/dxvk/dxvk_descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

namespace dxvk {

VkWriteDescriptorSet DxvkDescriptor::texture(const VkDescriptorSet& set, const VkDescriptorImageInfo& in, const VkDescriptorType t, const uint32_t bindingIdx)
// Note: 'imageInfo' must have the same or longer lifetime than returned VkWriteDescriptorSet
VkWriteDescriptorSet DxvkDescriptor::texture(const VkDescriptorSet& set, const VkDescriptorImageInfo* imageInfo, const VkDescriptorType t, const uint32_t bindingIdx)
{
VkWriteDescriptorSet desc;
desc.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Expand All @@ -36,22 +37,24 @@ namespace dxvk {
desc.dstArrayElement = 0;
desc.descriptorCount = 1;
desc.descriptorType = t;
desc.pImageInfo = ∈
desc.pImageInfo = imageInfo;
desc.pBufferInfo = nullptr;
desc.pTexelBufferView = nullptr;
return desc;
}

// Note: 'stagingInfo' must have the same or longer lifetime than returned VkWriteDescriptorSet
VkWriteDescriptorSet DxvkDescriptor::texture(const VkDescriptorSet& set, VkDescriptorImageInfo* stagingInfo, const DxvkImageView& imageView, const VkDescriptorType t, const uint32_t bindingIdx, const VkSampler sampler)
{
stagingInfo->sampler = sampler;
stagingInfo->imageView = imageView.handle();
stagingInfo->imageLayout = imageView.imageInfo().layout;

return texture(set, *stagingInfo, t, bindingIdx);
return texture(set, stagingInfo, t, bindingIdx);
};

VkWriteDescriptorSet DxvkDescriptor::buffer(const VkDescriptorSet& set, const VkDescriptorBufferInfo& in, const VkDescriptorType t, const uint32_t bindingIdx)
// Note: 'bufferInfo' must have the same or longer lifetime than returned VkWriteDescriptorSet
VkWriteDescriptorSet DxvkDescriptor::buffer(const VkDescriptorSet& set, const VkDescriptorBufferInfo* bufferInfo, const VkDescriptorType t, const uint32_t bindingIdx)
{
VkWriteDescriptorSet desc;
desc.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Expand All @@ -62,11 +65,12 @@ namespace dxvk {
desc.descriptorCount = 1;
desc.descriptorType = t;
desc.pImageInfo = nullptr;
desc.pBufferInfo = ∈
desc.pBufferInfo = bufferInfo;
desc.pTexelBufferView = nullptr;
return desc;
}

// Note: 'stagingInfo' must have the same or longer lifetime than returned VkWriteDescriptorSet
VkWriteDescriptorSet DxvkDescriptor::buffer(const VkDescriptorSet& set, VkDescriptorBufferInfo* stagingInfo, const DxvkBuffer& bufferView, const VkDescriptorType t, const uint32_t bindingIdx)
{
auto surfaceBufferSlice = bufferView.getSliceHandle();
Expand All @@ -75,7 +79,7 @@ namespace dxvk {
stagingInfo->offset = surfaceBufferSlice.offset;
stagingInfo->range = surfaceBufferSlice.length;

return buffer(set, *stagingInfo, t, bindingIdx);
return buffer(set, stagingInfo, t, bindingIdx);
};


Expand Down
8 changes: 6 additions & 2 deletions src/dxvk/dxvk_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,13 @@ namespace dxvk {

struct DxvkDescriptor
{
static VkWriteDescriptorSet buffer(const VkDescriptorSet& set, const VkDescriptorBufferInfo& in, const VkDescriptorType t, const uint32_t bindingIdx);
// Note: 'bufferInfo' must have the same or longer lifetime than returned VkWriteDescriptorSet
static VkWriteDescriptorSet buffer(const VkDescriptorSet& set, const VkDescriptorBufferInfo* bufferInfo, const VkDescriptorType t, const uint32_t bindingIdx);
// Note: 'stagingInfo' must have the same or longer lifetime than returned VkWriteDescriptorSet
static VkWriteDescriptorSet buffer(const VkDescriptorSet& set, VkDescriptorBufferInfo* stagingInfo, const DxvkBuffer& bufferView, const VkDescriptorType t, const uint32_t bindingIdx);
static VkWriteDescriptorSet texture(const VkDescriptorSet& set, const VkDescriptorImageInfo& in, const VkDescriptorType t, const uint32_t bindingIdx);
// Note: 'imageInfo' must have the same or longer lifetime than returned VkWriteDescriptorSet
static VkWriteDescriptorSet texture(const VkDescriptorSet& set, const VkDescriptorImageInfo* imageInfo, const VkDescriptorType t, const uint32_t bindingIdx);
// Note: 'stagingInfo' must have the same or longer lifetime than returned VkWriteDescriptorSet
static VkWriteDescriptorSet texture(const VkDescriptorSet& set, VkDescriptorImageInfo* stagingInfo, const DxvkImageView& imageView, const VkDescriptorType t, const uint32_t bindingIdx, VkSampler sampler = VK_NULL_HANDLE);
};

Expand Down
15 changes: 8 additions & 7 deletions src/dxvk/rtx_render/rtx_nrd_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,16 +680,19 @@ namespace dxvk {

std::vector<VkWriteDescriptorSet> descriptorWriteSets;

// Variables referenced inside descriptorWriteSets must have the same lifetime, so preallocate
std::vector<VkDescriptorImageInfo> samplerDescs{ denoiserDesc.samplersNum };
VkDescriptorBufferInfo cbDesc{};
std::vector<VkDescriptorImageInfo> imageDesc{ dispatchDesc.resourcesNum };

// Static sampler descriptors
std::vector<VkDescriptorImageInfo> samplerDescs;
samplerDescs.resize(denoiserDesc.samplersNum);
for (size_t i = 0; i < denoiserDesc.samplersNum; i++) {

const VkDescriptorSetLayoutBinding& binding = computePipeline.bindings[i];
samplerDescs[i].sampler = m_staticSamplers[i]->handle();
samplerDescs[i].imageView = VK_NULL_HANDLE;
samplerDescs[i].imageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
descriptorWriteSets.emplace_back(DxvkDescriptor::texture(descriptorSet, samplerDescs[i], binding.descriptorType, binding.binding));
descriptorWriteSets.emplace_back(DxvkDescriptor::texture(descriptorSet, &samplerDescs[i], binding.descriptorType, binding.binding));

ctx->getCommandList()->trackResource<DxvkAccess::None>(m_staticSamplers[i]);
}
Expand All @@ -710,8 +713,8 @@ namespace dxvk {
const VkDescriptorSetLayoutBinding& cb = computePipeline.bindings[computePipeline.constantBufferIndex];
assert(cb.descriptorCount == 1);

const VkDescriptorBufferInfo& cbDesc = cbSlice.getDescriptor().buffer;
descriptorWriteSets.emplace_back(DxvkDescriptor::buffer(descriptorSet, cbDesc, cb.descriptorType, cb.binding));
cbDesc = cbSlice.getDescriptor().buffer;
descriptorWriteSets.emplace_back(DxvkDescriptor::buffer(descriptorSet, &cbDesc, cb.descriptorType, cb.binding));

barriers.accessBuffer(cbSlice.getSliceHandle(),
VK_PIPELINE_STAGE_TRANSFER_BIT,
Expand All @@ -721,8 +724,6 @@ namespace dxvk {
}

// Gather needed resource infos for the pipeline
std::vector<VkDescriptorImageInfo> imageDesc;
imageDesc.resize(dispatchDesc.resourcesNum);
for (size_t i = 0; i < dispatchDesc.resourcesNum; i++) {

const nrd::ResourceRangeDesc& descRange = pipelineDesc.resourceRanges[i];
Expand Down

0 comments on commit 6e5135e

Please sign in to comment.