diff --git a/renderdoc/driver/vulkan/vk_shader_feedback.cpp b/renderdoc/driver/vulkan/vk_shader_feedback.cpp index 72609414e..2f5cb3b3d 100644 --- a/renderdoc/driver/vulkan/vk_shader_feedback.cpp +++ b/renderdoc/driver/vulkan/vk_shader_feedback.cpp @@ -374,6 +374,9 @@ void AnnotateShader(const ShaderReflection &refl, const SPIRVPatchData &patchDat // store the maximum slot we can use, for clamping outputs to avoid writing out of bounds rdcspv::Id maxSlotID = targetIndexWidth == 64 ? editor.AddConstantImmediate(maxSlot) : editor.AddConstantImmediate(maxSlot); + rdcspv::Id maxSlotByteOffsetID = + targetIndexWidth == 64 ? editor.AddConstantImmediate(maxSlot * sizeof(uint32_t)) + : editor.AddConstantImmediate(maxSlot * sizeof(uint32_t)); rdcspv::Id maxPrintfWordOffset = editor.AddConstantImmediate(Vulkan_Debug_PrintfBufferSize() / sizeof(uint32_t)); @@ -1409,17 +1412,6 @@ void AnnotateShader(const ShaderReflection &refl, const SPIRVPatchData &patchDat } } - // clamp the index to the maximum slot. If the user is reading out of bounds, don't write - // out of bounds. - { - rdcspv::Id clampedtype = - editor.DeclareType(rdcspv::Scalar(rdcspv::Op::TypeInt, targetIndexWidth, false)); - index = editor.AddOperation( - it, rdcspv::OpGLSL450(clampedtype, editor.MakeId(), glsl450, - rdcspv::GLSLstd450::UMin, {index, maxSlotID})); - it++; - } - rdcspv::Id bufptr; if(IsBDA(storageMode)) @@ -1438,6 +1430,17 @@ void AnnotateShader(const ShaderReflection &refl, const SPIRVPatchData &patchDat it, rdcspv::OpIAdd(indexOffsetType, editor.MakeId(), varIt->second, shiftedindex)); it++; + // clamp the offset address to the maximum byte offset. If the user is reading out of + // bounds, don't write out of bounds. + { + rdcspv::Id clampedtype = + editor.DeclareType(rdcspv::Scalar(rdcspv::Op::TypeInt, targetIndexWidth, false)); + offsetaddr = editor.AddOperation( + it, rdcspv::OpGLSL450(clampedtype, editor.MakeId(), glsl450, + rdcspv::GLSLstd450::UMin, {offsetaddr, maxSlotByteOffsetID})); + it++; + } + // make a pointer out of it // uint32_t *bufptr = (uint32_t *)(ptr + offsetaddr) bufptr = MakeOffsettedPointer( @@ -1454,6 +1457,17 @@ void AnnotateShader(const ShaderReflection &refl, const SPIRVPatchData &patchDat it, rdcspv::OpIAdd(uint32Type, editor.MakeId(), index, varIt->second)); it++; + // clamp the ssboindex to the maximum slot. If the user is reading out of bounds, don't + // write out of bounds. + { + rdcspv::Id clampedtype = + editor.DeclareType(rdcspv::Scalar(rdcspv::Op::TypeInt, targetIndexWidth, false)); + ssboindex = editor.AddOperation( + it, rdcspv::OpGLSL450(clampedtype, editor.MakeId(), glsl450, + rdcspv::GLSLstd450::UMin, {ssboindex, maxSlotID})); + it++; + } + // accesschain to get the pointer we'll atomic into. // accesschain is 0 to access rtarray (first member) then ssboindex for array index // uint32_t *bufptr = (uint32_t *)&buf.rtarray[ssboindex];