From ad79bf7cc5cd6b4f6cde9168716260f297ce8467 Mon Sep 17 00:00:00 2001 From: baldurk Date: Tue, 7 Apr 2020 17:13:49 +0100 Subject: [PATCH] Support applying derivatives on builtins --- renderdoc/driver/shaders/spirv/spirv_debug.h | 3 +- .../shaders/spirv/spirv_debug_setup.cpp | 7 ++- renderdoc/driver/vulkan/vk_shaderdebug.cpp | 49 ++++++++++++++----- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/renderdoc/driver/shaders/spirv/spirv_debug.h b/renderdoc/driver/shaders/spirv/spirv_debug.h index 538a0e682..16377a370 100644 --- a/renderdoc/driver/shaders/spirv/spirv_debug.h +++ b/renderdoc/driver/shaders/spirv/spirv_debug.h @@ -54,7 +54,8 @@ public: Vec4f ddyfine; }; - virtual DerivativeDeltas GetDerivative(uint32_t location, uint32_t component) = 0; + virtual DerivativeDeltas GetDerivative(ShaderBuiltin builtin, uint32_t location, + uint32_t component) = 0; }; struct GlobalState diff --git a/renderdoc/driver/shaders/spirv/spirv_debug_setup.cpp b/renderdoc/driver/shaders/spirv/spirv_debug_setup.cpp index a4a47cc50..984cf9195 100644 --- a/renderdoc/driver/shaders/spirv/spirv_debug_setup.cpp +++ b/renderdoc/driver/shaders/spirv/spirv_debug_setup.cpp @@ -1136,6 +1136,10 @@ uint32_t Debugger::ApplyDerivatives(uint32_t quadIndex, const Decorations &curDe // only floats have derivatives if(outVar.type == VarType::Float) { + ShaderBuiltin builtin = ShaderBuiltin::Undefined; + if(curDecorations.flags & Decorations::HasBuiltIn) + builtin = MakeShaderBuiltin(stage, curDecorations.builtIn); + uint32_t component = 0; for(const DecorationAndParamData &dec : curDecorations.others) { @@ -1196,7 +1200,8 @@ uint32_t Debugger::ApplyDerivatives(uint32_t quadIndex, const Decorations &curDe if(curDecorations.flags & Decorations::HasLocation) location = curDecorations.location; - DebugAPIWrapper::DerivativeDeltas derivs = apiWrapper->GetDerivative(location, component); + DebugAPIWrapper::DerivativeDeltas derivs = + apiWrapper->GetDerivative(builtin, location, component); Vec4f &dst = *(Vec4f *)outVar.value.fv; diff --git a/renderdoc/driver/vulkan/vk_shaderdebug.cpp b/renderdoc/driver/vulkan/vk_shaderdebug.cpp index a32bc3c54..c8671e1b8 100644 --- a/renderdoc/driver/vulkan/vk_shaderdebug.cpp +++ b/renderdoc/driver/vulkan/vk_shaderdebug.cpp @@ -89,13 +89,24 @@ public: component); } - virtual DerivativeDeltas GetDerivative(uint32_t location, uint32_t component) override + virtual DerivativeDeltas GetDerivative(ShaderBuiltin builtin, uint32_t location, + uint32_t component) override { + if(builtin != ShaderBuiltin::Undefined) + { + auto it = builtin_derivatives.find(builtin); + if(it != builtin_derivatives.end()) + return it->second; + + RDCERR("Couldn't get input for %s", ToStr(builtin).c_str()); + return DerivativeDeltas(); + } + // TODO handle components RDCASSERT(component == 0); - if(location < derivatives.size()) - return derivatives[location]; + if(location < location_derivatives.size()) + return location_derivatives[location]; RDCERR("Couldn't get derivative for location=%u, component=%u", location, component); return DerivativeDeltas(); @@ -105,7 +116,8 @@ public: std::map builtin_inputs; rdcarray location_inputs; - rdcarray derivatives; + std::map builtin_derivatives; + rdcarray location_derivatives; private: WrappedVulkan *m_pDriver = NULL; @@ -1535,18 +1547,31 @@ ShaderDebugTrace *VulkanReplay::DebugPixel(uint32_t eventId, uint32_t x, uint32_ Vec4f *ddxfine = (Vec4f *)(PSInputs + 3 * structStride); Vec4f *ddyfine = (Vec4f *)(PSInputs + 4 * structStride); - rdcarray &locations = apiWrapper->location_inputs; for(size_t i = 0; i < shadRefl.refl.inputSignature.size(); i++) { const SigParameter ¶m = shadRefl.refl.inputSignature[i]; - locations.resize(RDCMAX((uint32_t)locations.size(), param.regIndex + 1)); - apiWrapper->derivatives.resize(RDCMAX((uint32_t)locations.size(), param.regIndex + 1)); - memcpy(&locations[param.regIndex].value.uv, &value[i], sizeof(Vec4f)); - memcpy(&apiWrapper->derivatives[param.regIndex].ddxcoarse, &ddxcoarse[i], sizeof(Vec4f)); - memcpy(&apiWrapper->derivatives[param.regIndex].ddycoarse, &ddycoarse[i], sizeof(Vec4f)); - memcpy(&apiWrapper->derivatives[param.regIndex].ddxfine, &ddxfine[i], sizeof(Vec4f)); - memcpy(&apiWrapper->derivatives[param.regIndex].ddyfine, &ddyfine[i], sizeof(Vec4f)); + bool builtin = true; + if(param.systemValue == ShaderBuiltin::Undefined) + { + builtin = false; + apiWrapper->location_inputs.resize( + RDCMAX((uint32_t)apiWrapper->location_inputs.size(), param.regIndex + 1)); + apiWrapper->location_derivatives.resize( + RDCMAX((uint32_t)apiWrapper->location_derivatives.size(), param.regIndex + 1)); + } + + ShaderVariable &var = builtin ? apiWrapper->builtin_inputs[param.systemValue] + : apiWrapper->location_inputs[param.regIndex]; + rdcspv::DebugAPIWrapper::DerivativeDeltas &deriv = + builtin ? apiWrapper->builtin_derivatives[param.systemValue] + : apiWrapper->location_derivatives[param.regIndex]; + + memcpy(&var.value.uv, &value[i], sizeof(Vec4f)); + memcpy(&deriv.ddxcoarse, &ddxcoarse[i], sizeof(Vec4f)); + memcpy(&deriv.ddycoarse, &ddycoarse[i], sizeof(Vec4f)); + memcpy(&deriv.ddxfine, &ddxfine[i], sizeof(Vec4f)); + memcpy(&deriv.ddyfine, &ddyfine[i], sizeof(Vec4f)); } ret = debugger->BeginDebug(apiWrapper, ShaderStage::Pixel, entryPoint, spec,