fix(shader): emulate sRGB sampling when unsupported #5371
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5371"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by likecrank-dotcom on 2026-10-13T18:13:42Z.
What
Issue #3751 reports sampled R8_SRGB/R8G8_SRGB images failing on devices that cannot sample those Vulkan formats. This adds a shader fallback for direct, single-mip 2D images: fetch texels through the UNORM view, decode each texel from sRGB, then apply point or bilinear filtering. The shader receives the sampler state through runtime metadata in registration modules or specialization constants in specialized modules.
Unsupported shapes and sampler modes still fail with explicit diagnostics; gathers and bindless image tables remain unsupported. Updated the old sRGB load test so it no longer expects ordinary sampling to fail when the fallback is enabled. The separate fallback test checks R8 and RG8 point and bilinear results against a per-texel decode-and-filter oracle.
Tested
Windows 11 Pro, WinLibs MinGW GCC 15.2.0, CMake 4.4.4, NVIDIA GeForce RTX 5060 Ti (driver 617.14).
agc_shader_memory,agc_driver_image_sample_force_degamma,agc_driver_image_srgb_load,agc_driver_image_sample_srgb_fallback, andagc_driver_emulated_color_compare_srgb_decode: 5/5 passed, repeated three times (15 successful executions).tools/check_conventions.py --base origin/main --head HEADandgit diff --check origin/main...HEADpassed.Checklist
main; no other open PR implements the same functionsImported from boykopovar/AnyPS5. Originally posted by coltken95 on 2026-10-13T19:17:34Z.
Tested on Linux x86-64 (Release build, lavapipe / llvmpipe LLVM 20.1.2), with
273e2695emerged into main18c438b10:agc_driver_image_sample_srgb_fallback, plusagc_driver_image_srgb_loadandagc_driver_image_sample_force_degamma.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T18:48:22Z.
Review: CHANGES_REQUESTED
Measured on RDNA2 (gfx1035) with
tools/hw-oracle:image_sample_lzof 8_SRGB and 8_8_SRGB with point filtering returns the bf16SrgbToLineartable value bit for bit. That holds under wrap, clamp-to-edge and all three clamp-to-border colours, out-of-range coordinates included. Bilinear decodes before filtering and stays within 1.1e-3 of a decode-then-filter model (8-bit HW weights). So the fallback's semantics match the hardware. Build and the AgcDriver/Agc tests pass on the PR head. The PR needs to be reworked on currentmainbefore it can go in:mainthat aren't mechanical. #4475 replacedImageResource::indirectRoot/NoIndirectImagewith image tables (NoTable), and the runtime image metadata block this PR extends inDescriptorBindingBuilder.cpp:448-450no longer exists. The new table path has to decide what happens to an sRGB table entry (still zero-read, or refused).mainnow turns unsupported images intorejectImage(PoisonReason::SrgbUnsupported, ...)indecodeImageDescriptorinstead of throwing. The throws inemulatedSrgbSampleState(ResourceMaterializer.cpp:587-627) should follow that model, so an unsupported shape behaves the same way as onmain.SamplerDescriptorword00x92).EmitEmulatedSrgbSamplealso implements wrap, clamp-to-border (transparent black, opaque black, white) and texel offsets, and none of these run. Please add rows with coordinates outside [0, 1] for each address mode and one with an offset. The point rows can also compare againstSrgbToLinearbit for bit instead of within 0.005, because the hardware does.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-24T01:09:13Z.
Review: CHANGES_REQUESTED
Thanks for the work; the semantics are right (decode before filter matches RDNA2 point and bilinear sampling, measured earlier in this thread). The problem is that the base moved under it:
APS5_SRGB_SHADER_DECODE=1; those cases still throw onmain(docs/dev/debt/shader.md, "Samples and gathers"). Please say in the PR which real device hits that case. If none does, this fallback has no user and should be dropped; if one does, rebase ontomainand scope the PR to it.mainbeyond adjacency:ImageResource::indirectRoot/NoIndirectImagebecame image tables (NoTable), anddecodeImageDescriptor/inspectImageDescriptor(ResourceMaterializer.cpp) now returnrejectImage(PoisonReason::SrgbUnsupported, ...)instead of throwing. The throws inemulatedSrgbSampleStateshould follow that model, and the table path has to decide what an sRGB table entry does.SpirvImageEmitter.cppEmitEmulatedSrgbSample: wrap, clamp-to-border (transparent black, opaque black, white) and texel offsets are implemented but no test runs them. Add rows with coordinates outside [0, 1] for each address mode and one with an offset, and compare the point rows bit for bit againstSrgbToLinear(the hardware does).docs/dev/debt/shader.md: the new line must replace the current "Samples and gathers" line onmain(it now describes the copy path), not the old throw line.Happy to re-review once rebased.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T03:06:13Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: fix sRGB mode dispatch so specialized modules emit fallback sampling only for sRGB descriptors.
What I ran
273e2695e4df17f5036cef403b761ebb406547cfand the prepared patch; no commands or tests were run.description.md,commits.md,manifest.json,diff.patch,ResourceMaterializer.cpp,DescriptorBindingBuilder.cpp,SpirvImageEmitter.cpp,RuntimeAbi.hpp,Resources.hpp, andImageSampleForceDegamma.cpp.Results
files.jsonwas not available through the readerFindings
Blocking
SpirvImageEmitter.cpp:1506— The fallback is selected using the staticimage.srgbDecodemode before the runtime descriptor format is resolved.RuntimeImageModesadds both ordinary and sRGB modes for eligible images, andEmitImageemits both as separate specialized branches, but the sRGB branch’s sample operation is emitted as unconditional fallback code. When the descriptor is UNORM, that branch still decodes its texels as sRGB; when the descriptor is sRGB, the ordinary branch can also be selected and sample through the native path. The format-dependent mode therefore does not reliably select matching behavior. Make the fallback conditional on the selected mode or descriptor format, and verify both descriptor variants.Non-blocking
None.
Not verified
CMakeLists.txt,ShaderMemory.cpp,ImageSrgbLoad.cpp,PipelineSpecialization.hpp, anddocs/dev/debt/shader.mdwere not read directly from repository paths; their prepared diff sections were reviewed where present.files.jsonand the packaged full-file texts were unavailable through the reader, so complete file coverage and exclusions could not be confirmed.To get this to a good state
Fix mode-dependent sRGB dispatch, then run the focused CTest group on the new head and confirm both sRGB and UNORM descriptor behavior.
Pull request closed