1 /*
2 * Copyright 2019 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/gpu/dawn/GrDawnProgramDataManager.h"
9
10 #include "src/gpu/dawn/GrDawnGpu.h"
11
GrDawnProgramDataManager(const UniformInfoArray & uniforms,uint32_t uniformBufferSize)12 GrDawnProgramDataManager::GrDawnProgramDataManager(const UniformInfoArray& uniforms,
13 uint32_t uniformBufferSize)
14 : GrUniformDataManager(uniforms.count(), uniformBufferSize) {
15 memset(fUniformData.get(), 0, uniformBufferSize);
16 // We must add uniforms in same order is the UniformInfoArray so that UniformHandles already
17 // owned by other objects will still match up here.
18 int i = 0;
19 for (const auto& uniformInfo : uniforms.items()) {
20 Uniform& uniform = fUniforms[i];
21 SkDEBUGCODE(
22 uniform.fArrayCount = uniformInfo.fVariable.getArrayCount();
23 uniform.fType = uniformInfo.fVariable.getType();
24 )
25 uniform.fOffset = uniformInfo.fUBOOffset;
26 ++i;
27 }
28 }
29
make_bind_group_entry(uint32_t binding,const wgpu::Buffer & buffer,uint32_t offset,uint32_t size)30 static wgpu::BindGroupEntry make_bind_group_entry(uint32_t binding, const wgpu::Buffer& buffer,
31 uint32_t offset, uint32_t size) {
32 wgpu::BindGroupEntry result;
33 result.binding = binding;
34 result.buffer = buffer;
35 result.offset = offset;
36 result.size = size;
37 result.sampler = nullptr;
38 result.textureView = nullptr;
39 return result;
40 }
41
uploadUniformBuffers(GrDawnGpu * gpu,wgpu::BindGroupLayout layout)42 wgpu::BindGroup GrDawnProgramDataManager::uploadUniformBuffers(GrDawnGpu* gpu,
43 wgpu::BindGroupLayout layout) {
44 if (fUniformsDirty && 0 != fUniformSize) {
45 std::vector<wgpu::BindGroupEntry> bindings;
46 GrDawnRingBuffer::Slice slice;
47 slice = gpu->allocateUniformRingBufferSlice(fUniformSize);
48 gpu->queue().WriteBuffer(slice.fBuffer, slice.fOffset, fUniformData.get(), fUniformSize);
49 bindings.push_back(make_bind_group_entry(GrSPIRVUniformHandler::kUniformBinding,
50 slice.fBuffer, slice.fOffset,
51 fUniformSize));
52 wgpu::BindGroupDescriptor descriptor;
53 descriptor.layout = layout;
54 descriptor.entryCount = bindings.size();
55 descriptor.entries = bindings.data();
56 fBindGroup = gpu->device().CreateBindGroup(&descriptor);
57 fUniformsDirty = false;
58 }
59 return fBindGroup;
60 }
61