Debian Codegraph @

debian/patches/CVE-2026-14387

raw
1 From 0f4027ff431caf25d9e9289c2b40cbc150889ecf Mon Sep 17 00:00:00 2001
2 From: Nicolette Prevost <[email protected]>
3 Date: Wed, 13 May 2026 14:59:55 -0400
4 Subject: [PATCH] [graphite] Cache single-buffer BindGroups on DawnBuffer
5
6 * This follows the Vulkan backend in caching single-buffer bind groups on to Buffer implementations. This allows us to remove DawnResourceProvider's special caching of single-texture bindgroups and findOrCreateSingleTextureSamplerBindGroup(...).
7
8 * The DawnCommandBuffer instead defines the bind group entries for single-texture groups and uses the generic createBindGroup(...) method.
9
10 Bug: https://issues.chromium.org/issues/500305404
11 Change-Id: Iaf86d7751815806cba1353f74ee2ce3e49042b9d
12 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1232757
13 Commit-Queue: Nicolette Prevost <[email protected]>
14 Reviewed-by: Thomas Smith <[email protected]>
15 Reviewed-by: Michael Ludwig <[email protected]>
16 ---
17 src/gpu/graphite/dawn/DawnBuffer.cpp | 13 ++
18 src/gpu/graphite/dawn/DawnBuffer.h | 7 +
19 src/gpu/graphite/dawn/DawnCommandBuffer.cpp | 71 ++++++----
20 src/gpu/graphite/dawn/DawnCommandBuffer.h | 2 +-
21 src/gpu/graphite/dawn/DawnGraphicsPipeline.h | 6 +-
22 .../graphite/dawn/DawnResourceProvider.cpp | 130 +++++++-----------
23 src/gpu/graphite/dawn/DawnResourceProvider.h | 22 +--
24 src/gpu/graphite/dawn/DawnSharedContext.cpp | 2 +-
25 8 files changed, 127 insertions(+), 126 deletions(-)
26
27 --- a/src/gpu/graphite/dawn/DawnBuffer.cpp
28 +++ b/src/gpu/graphite/dawn/DawnBuffer.cpp
29 @@ -364,4 +364,17 @@
30 }
31 }
32
33 +const wgpu::BindGroup* DawnBuffer::getCachedSingleBufferBindGroup(size_t bindingSize) const {
34 + for (auto& cachedGroup : fCachedSingleBufferBindGroups) {
35 + if (cachedGroup.first == bindingSize) {
36 + return &cachedGroup.second;
37 + }
38 + }
39 + return nullptr;
40 +}
41 +void DawnBuffer::addCachedSingleBufferBindGroup(wgpu::BindGroup bindGroup,
42 + size_t bindingSize) const {
43 + fCachedSingleBufferBindGroups.push_back({bindingSize, bindGroup});
44 +}
45 +
46 } // namespace skgpu::graphite
47 --- a/src/gpu/graphite/dawn/DawnBuffer.h
48 +++ b/src/gpu/graphite/dawn/DawnBuffer.h
49 @@ -30,6 +30,9 @@
50
51 const wgpu::Buffer& dawnBuffer() const { return fBuffer; }
52
53 + const wgpu::BindGroup* getCachedSingleBufferBindGroup(size_t bindingSize) const;
54 + void addCachedSingleBufferBindGroup(wgpu::BindGroup, size_t bindingSize) const;
55 +
56 private:
57 DawnBuffer(const DawnSharedContext*, size_t size, wgpu::Buffer, void* mapAtCreationPtr);
58
59 @@ -54,6 +57,10 @@
60 wgpu::Buffer fBuffer;
61 SkMutex fAsyncMutex;
62 skia_private::STArray<1, AutoCallback> fAsyncMapCallbacks SK_GUARDED_BY(fAsyncMutex);
63 +
64 + // By the time the command buffer requests a bind group, the provided Buffer pointer is const
65 + // so this attribute must be mutable to avoid a const_cast.
66 + mutable skia_private::TArray<std::pair<size_t, wgpu::BindGroup>> fCachedSingleBufferBindGroups;
67 };
68
69 } // namespace skgpu::graphite
70 --- a/src/gpu/graphite/dawn/DawnCommandBuffer.cpp
71 +++ b/src/gpu/graphite/dawn/DawnCommandBuffer.cpp
72 @@ -932,43 +932,66 @@
73 }
74
75 void DawnCommandBuffer::syncUniformBuffers() {
76 - static constexpr int kNumBuffers = DawnGraphicsPipeline::kNumUniformBuffers;
77 -
78 - if (fBoundUniformBuffersDirty) {
79 - fBoundUniformBuffersDirty = false;
80 + if (!fBoundUniformBuffersDirty) {
81 + return;
82 + }
83 + fBoundUniformBuffersDirty = false;
84
85 - std::array<uint32_t, kNumBuffers> dynamicOffsets;
86 - std::array<std::pair<const DawnBuffer*, uint32_t>, kNumBuffers> boundBuffersAndSizes;
87 + bool usePushConstants = fSharedContext->dawnCaps()->
88 + resourceBindingRequirements().fUsePushConstantsForIntrinsicConstants;
89
90 - std::array<bool, kNumBuffers> enabled = {
91 - !fSharedContext->dawnCaps()
92 - ->resourceBindingRequirements()
93 - .fUsePushConstantsForIntrinsicConstants, // intrinsic uniforms
94 - fActiveGraphicsPipeline->hasCombinedUniforms(), // paint AND renderstep uniforms!
95 - fActiveGraphicsPipeline->hasGradientBuffer(), // gradient SSBO
96 + // We expect to have up to 3 uniforms in this bind group.
97 + static constexpr int kMaxUniformsInGroup = 3;
98 + // Until/unless uniform bind group structure gets reorganized, this should be equivalent to the
99 + // size of our bound uniform array.
100 + SkASSERT(kMaxUniformsInGroup == fBoundUniforms.size());
101 +
102 + wgpu::BindGroup bindGroup;
103 + std::array<uint32_t, kMaxUniformsInGroup> dynamicOffsets {0};
104 + // Check if we can use an optimized route for single-uniform buffer bind groups:
105 + if (usePushConstants &&
106 + !fActiveGraphicsPipeline->hasGradientBuffer() &&
107 + fActiveGraphicsPipeline->hasCombinedUniforms()) {
108 + const BindBufferInfo& bufferInfo =
109 + fBoundUniforms[DawnGraphicsPipeline::kCombinedUniformIndex];
110 + bindGroup = fResourceProvider->findOrCreateSingleUniformBindGroup(bufferInfo);
111 + dynamicOffsets[DawnGraphicsPipeline::kCombinedUniformIndex] = bufferInfo.fOffset;
112 + } else {
113 + std::array<bool, kMaxUniformsInGroup> enabled = {
114 + !usePushConstants, // intrinsic uniforms
115 + fActiveGraphicsPipeline->hasCombinedUniforms(), // paint AND renderstep uniforms!
116 + fActiveGraphicsPipeline->hasGradientBuffer(), // gradient SSBO
117 + };
118 + constexpr uint32_t kBindingIndices[] = {
119 + DawnGraphicsPipeline::kIntrinsicUniformBufferIndex,
120 + DawnGraphicsPipeline::kCombinedUniformIndex,
121 + DawnGraphicsPipeline::kGradientBufferIndex,
122 };
123
124 - for (int i = 0; i < kNumBuffers; ++i) {
125 + std::array<wgpu::BindGroupEntry, kMaxUniformsInGroup> bindGroupEntries {};
126 + for (int i = 0; i < kMaxUniformsInGroup; ++i) {
127 + bindGroupEntries[i].binding = kBindingIndices[i];
128 if (enabled[i] && fBoundUniforms[i]) {
129 - boundBuffersAndSizes[i].first =
130 - static_cast<const DawnBuffer*>(fBoundUniforms[i].fBuffer);
131 - boundBuffersAndSizes[i].second = fBoundUniforms[i].fSize;
132 + bindGroupEntries[i].size = fBoundUniforms[i].fSize;
133 + bindGroupEntries[i].buffer =
134 + static_cast<const DawnBuffer*>(fBoundUniforms[i].fBuffer)->dawnBuffer();
135 dynamicOffsets[i] = fBoundUniforms[i].fOffset;
136 } else {
137 // Unused or null binding
138 - boundBuffersAndSizes[i].first = nullptr;
139 - dynamicOffsets[i] = 0;
140 + bindGroupEntries[i].buffer = fResourceProvider->getOrCreateNullBuffer();
141 }
142 }
143
144 - auto bindGroup =
145 - fResourceProvider->findOrCreateUniformBuffersBindGroup(boundBuffersAndSizes);
146 -
147 - fActiveRenderPassEncoder.SetBindGroup(DawnGraphicsPipeline::kUniformBufferBindGroupIndex,
148 - bindGroup,
149 - dynamicOffsets.size(),
150 - dynamicOffsets.data());
151 + const auto& groupLayouts = fActiveGraphicsPipeline->dawnGroupLayouts();
152 + bindGroup = fResourceProvider->createBindGroup(
153 + bindGroupEntries,
154 + groupLayouts[DawnGraphicsPipeline::kUniformBufferBindGroupIndex]);
155 }
156 +
157 + fActiveRenderPassEncoder.SetBindGroup(DawnGraphicsPipeline::kUniformBufferBindGroupIndex,
158 + bindGroup,
159 + dynamicOffsets.size(),
160 + dynamicOffsets.data());
161 }
162
163 void DawnCommandBuffer::setScissor(const Scissor& scissor) {
164 --- a/src/gpu/graphite/dawn/DawnCommandBuffer.h
165 +++ b/src/gpu/graphite/dawn/DawnCommandBuffer.h
166 @@ -164,7 +164,7 @@
167
168 bool fBoundUniformBuffersDirty = false;
169
170 - std::array<BindBufferInfo, DawnGraphicsPipeline::kNumUniformBuffers> fBoundUniforms;
171 + std::array<BindBufferInfo, DawnGraphicsPipeline::kMaxNumUniformBuffers> fBoundUniforms;
172
173 wgpu::CommandEncoder fCommandEncoder;
174 wgpu::RenderPassEncoder fActiveRenderPassEncoder;
175 --- a/src/gpu/graphite/dawn/DawnGraphicsPipeline.h
176 +++ b/src/gpu/graphite/dawn/DawnGraphicsPipeline.h
177 @@ -45,10 +45,14 @@
178 inline static constexpr unsigned int kTextureBindGroupIndex = 1;
179 inline static constexpr unsigned int kBindGroupCount = 2;
180
181 + // TODO(b/512814646): WASM does not support push constant usage, meaning that we often have 2
182 + // uniform buffers within one BindGroup. This is unideal since we can store single-uniform
183 + // BindGroups on DawnBuffers. Consider reorganizing uniform buffers such that we can more often
184 + // only have one uniform buffer per BindGroup.
185 inline static constexpr unsigned int kIntrinsicUniformBufferIndex = 0;
186 inline static constexpr unsigned int kCombinedUniformIndex = 1;
187 inline static constexpr unsigned int kGradientBufferIndex = 2;
188 - inline static constexpr unsigned int kNumUniformBuffers = 3;
189 + inline static constexpr unsigned int kMaxNumUniformBuffers = 3;
190
191 inline static constexpr unsigned int kIntrinsicUniformSize = 32;
192
193 --- a/src/gpu/graphite/dawn/DawnResourceProvider.cpp
194 +++ b/src/gpu/graphite/dawn/DawnResourceProvider.cpp
195 @@ -31,7 +31,6 @@
196 namespace {
197
198 constexpr uint32_t kBufferBindingSizeAlignment = 16;
199 -constexpr int kMaxNumberOfCachedBufferBindGroups = 1024;
200 constexpr int kMaxNumberOfCachedTextureBindGroups = 4096;
201
202 wgpu::ShaderModule create_shader_module(const wgpu::Device& device, const char* source) {
203 @@ -113,36 +112,6 @@
204 using BindGroupKey = typename DawnResourceProvider::BindGroupKey<NumEntries>;
205 using UniformBindGroupKey = BindGroupKey<DawnResourceProvider::kNumUniformEntries>;
206
207 -UniformBindGroupKey make_ubo_bind_group_key(
208 - const std::array<std::pair<const DawnBuffer*, uint32_t>,
209 - DawnResourceProvider::kNumUniformEntries>& boundBuffersAndSizes) {
210 - UniformBindGroupKey uniqueKey;
211 - {
212 - // Each entry in the bind group needs 2 uint32_t in the key:
213 - // - buffer's unique ID: 32 bits.
214 - // - buffer's binding size: 32 bits.
215 - // We need total of 4 entries in the uniform buffer bind group.
216 - // Unused entries will be assigned zero values.
217 - UniformBindGroupKey::Builder builder(&uniqueKey);
218 -
219 - for (uint32_t i = 0; i < boundBuffersAndSizes.size(); ++i) {
220 - const DawnBuffer* boundBuffer = boundBuffersAndSizes[i].first;
221 - const uint32_t bindingSize = boundBuffersAndSizes[i].second;
222 - if (boundBuffer) {
223 - builder[2 * i] = boundBuffer->uniqueID().asUInt();
224 - builder[2 * i + 1] = bindingSize;
225 - } else {
226 - builder[2 * i] = 0;
227 - builder[2 * i + 1] = 0;
228 - }
229 - }
230 -
231 - builder.finish();
232 - }
233 -
234 - return uniqueKey;
235 -}
236 -
237 BindGroupKey<1> make_texture_bind_group_key(const DawnSampler* sampler,
238 const DawnTexture* texture) {
239 BindGroupKey<1> uniqueKey;
240 @@ -409,7 +378,6 @@
241 uint32_t recorderID,
242 size_t resourceBudget)
243 : ResourceProvider(sharedContext, singleOwner, recorderID, resourceBudget)
244 - , fUniformBufferBindGroupCache(kMaxNumberOfCachedBufferBindGroups)
245 , fSingleTextureSamplerBindGroups(kMaxNumberOfCachedTextureBindGroups)
246 , fSingleOwner(singleOwner) {
247 fIntrinsicConstantsManager = std::make_unique<IntrinsicConstantsManager>(this);
248 @@ -637,53 +605,52 @@
249 return fNullBuffer;
250 }
251
252 -const wgpu::BindGroup& DawnResourceProvider::findOrCreateUniformBuffersBindGroup(
253 - const std::array<std::pair<const DawnBuffer*, uint32_t>, kNumUniformEntries>&
254 - boundBuffersAndSizes) {
255 +wgpu::BindGroup DawnResourceProvider::findOrCreateSingleUniformBindGroup(
256 + const BindBufferInfo& bufferInfo) {
257 SKGPU_ASSERT_SINGLE_OWNER(fSingleOwner)
258 -
259 - auto key = make_ubo_bind_group_key(boundBuffersAndSizes);
260 - auto* existingBindGroup = fUniformBufferBindGroupCache.find(key);
261 - if (existingBindGroup) {
262 - // cache hit.
263 - return *existingBindGroup;
264 - }
265 -
266 - // Translate to wgpu::BindGroupDescriptor
267 - std::array<wgpu::BindGroupEntry, kNumUniformEntries> entries;
268 -
269 - constexpr uint32_t kBindingIndices[] = {
270 - DawnGraphicsPipeline::kIntrinsicUniformBufferIndex,
271 - DawnGraphicsPipeline::kCombinedUniformIndex,
272 - DawnGraphicsPipeline::kGradientBufferIndex,
273 + // We should only hit the single-uniform case if push constant usage is supported for intrinsic
274 + // constants.
275 + SkASSERT(this->dawnSharedContext()->dawnCaps()->
276 + resourceBindingRequirements().fUsePushConstantsForIntrinsicConstants);
277 +
278 + auto buffer = static_cast<const DawnBuffer*>(bufferInfo.fBuffer);
279 +
280 + if (auto cachedBindGroup = buffer->getCachedSingleBufferBindGroup(bufferInfo.fSize)) {
281 + return *cachedBindGroup;
282 + }
283 +
284 + // We should be able to assume that if we only have one uniform that it is the combined uniform
285 + // buffer. Construct a list of bind group entries to represent the single combined uniform
286 + // buffer case.
287 + wgpu::BindGroupEntry intrinsicConstantNullEntry;
288 + intrinsicConstantNullEntry.binding = DawnGraphicsPipeline::kIntrinsicUniformBufferIndex;
289 + intrinsicConstantNullEntry.buffer = this->getOrCreateNullBuffer();
290 +
291 + wgpu::BindGroupEntry combinedUniformEntry;
292 + combinedUniformEntry.binding = DawnGraphicsPipeline::kCombinedUniformIndex;
293 + combinedUniformEntry.offset = 0; // Use dynamic offsets; ignore bufferInfo.fOffset
294 + combinedUniformEntry.buffer = buffer->dawnBuffer();
295 + combinedUniformEntry.size = SkAlignTo(bufferInfo.fSize, kBufferBindingSizeAlignment);
296 +
297 + wgpu::BindGroupEntry gradientBufferNullEntry;
298 + gradientBufferNullEntry.binding = DawnGraphicsPipeline::kGradientBufferIndex;
299 + gradientBufferNullEntry.buffer = this->getOrCreateNullBuffer();
300 +
301 + std::array<wgpu::BindGroupEntry, kNumUniformEntries> entries = {
302 + intrinsicConstantNullEntry,
303 + combinedUniformEntry,
304 + gradientBufferNullEntry
305 };
306
307 - for (uint32_t i = 0; i < boundBuffersAndSizes.size(); ++i) {
308 - const DawnBuffer* boundBuffer = boundBuffersAndSizes[i].first;
309 - const uint32_t bindingSize = boundBuffersAndSizes[i].second;
310 -
311 - entries[i].binding = kBindingIndices[i];
312 - entries[i].offset = 0;
313 - if (boundBuffer) {
314 - entries[i].buffer = boundBuffer->dawnBuffer();
315 - entries[i].size = SkAlignTo(bindingSize, kBufferBindingSizeAlignment);
316 - } else {
317 - entries[i].buffer = this->getOrCreateNullBuffer();
318 - entries[i].size = wgpu::kWholeSize;
319 - }
320 - }
321 -
322 - wgpu::BindGroupDescriptor desc;
323 - desc.layout = this->dawnSharedContext()->getUniformBuffersBindGroupLayout();
324 - desc.entryCount = entries.size();
325 - desc.entries = entries.data();
326 + wgpu::BindGroup bindGroup = this->createBindGroup(
327 + entries, this->dawnSharedContext()->getUniformBuffersBindGroupLayout());
328
329 - const auto& device = this->dawnSharedContext()->device();
330 - auto bindGroup = device.CreateBindGroup(&desc);
331 + buffer->addCachedSingleBufferBindGroup(bindGroup, bufferInfo.fSize);
332
333 - return *fUniformBufferBindGroupCache.insert(key, bindGroup);
334 + return bindGroup;
335 }
336
337 +
338 const wgpu::BindGroup& DawnResourceProvider::findOrCreateSingleTextureSamplerBindGroup(
339 const DawnSampler* sampler, const DawnTexture* texture) {
340 SKGPU_ASSERT_SINGLE_OWNER(fSingleOwner)
341 @@ -721,7 +688,6 @@
342 // when the DawnTexture and DawnBuffer is destroyed, but removing the bind groups themselves
343 // helps reduce CPU memory periodically.
344 fSingleTextureSamplerBindGroups.reset();
345 - fUniformBufferBindGroupCache.reset();
346 }
347
348 void DawnResourceProvider::onPurgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime) {
349 --- a/src/gpu/graphite/dawn/DawnResourceProvider.h
350 +++ b/src/gpu/graphite/dawn/DawnResourceProvider.h
351 @@ -65,14 +65,8 @@
352 AccessPattern,
353 std::string_view label);
354
355 - // Find the cached bind group or create a new one based on the bound buffers and their
356 - // binding sizes (boundBuffersAndSizes) for these uniforms (in order):
357 - // - Intrinsic constants.
358 - // - Render step uniforms.
359 - // - Paint uniforms.
360 - const wgpu::BindGroup& findOrCreateUniformBuffersBindGroup(
361 - const std::array<std::pair<const DawnBuffer*, uint32_t>, kNumUniformEntries>&
362 - boundBuffersAndSizes);
363 + // Find or create a bind group containing the given buffer.
364 + wgpu::BindGroup findOrCreateSingleUniformBindGroup(const BindBufferInfo&);
365
366 // Find or create a bind group containing the given sampler & texture.
367 const wgpu::BindGroup& findOrCreateSingleTextureSamplerBindGroup(const DawnSampler* sampler,
368 @@ -82,6 +76,10 @@
369 BindBufferInfo findOrCreateIntrinsicBindBufferInfo(DawnCommandBuffer* cb,
370 UniformDataBlock intrinsicValues);
371
372 + // For BindGroupEntries, using this method to get a null buffer pointer rather than simply using
373 + // nullptr allows for assigning a label (when enabled in Caps) for more clear debugging.
374 + const wgpu::Buffer& getOrCreateNullBuffer();
375 +
376 private:
377 sk_sp<ComputePipeline> createComputePipeline(const ComputePipelineDesc&) override;
378
379 @@ -95,8 +93,6 @@
380 BackendTexture onCreateBackendTexture(SkISize dimensions, const TextureInfo&) override;
381 void onDeleteBackendTexture(const BackendTexture&) override;
382
383 - const wgpu::Buffer& getOrCreateNullBuffer();
384 -
385 DawnSharedContext* dawnSharedContext() const;
386
387 void onFreeGpuResources() override;
388 @@ -111,7 +107,6 @@
389 wgpu::BindGroup,
390 typename BindGroupKey<NumEntries>::Hash>;
391
392 - BindGroupCache<kNumUniformEntries> fUniformBufferBindGroupCache;
393 BindGroupCache<1> fSingleTextureSamplerBindGroups;
394
395 class IntrinsicBuffer;
396 --- a/src/gpu/graphite/dawn/DawnSharedContext.cpp
397 +++ b/src/gpu/graphite/dawn/DawnSharedContext.cpp
398 @@ -111,7 +111,7 @@
399 void DawnSharedContext::createUniformBuffersBindGroupLayout() {
400 const Caps* caps = this->caps();
401
402 - std::array<wgpu::BindGroupLayoutEntry, DawnGraphicsPipeline::kNumUniformBuffers> entries;
403 + std::array<wgpu::BindGroupLayoutEntry, DawnGraphicsPipeline::kMaxNumUniformBuffers> entries;
404 entries[0].binding = DawnGraphicsPipeline::kIntrinsicUniformBufferIndex;
405 entries[0].visibility = wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment;
406 entries[0].buffer.type = wgpu::BufferBindingType::Uniform;