Skip to content

Commit

Permalink
Modify uniform buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
NALLEIN committed Jul 12, 2020
1 parent fa95ba0 commit 8377efd
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion modules/dnn/src/webgpu/include/op_softmax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class OpSoftmax: public OpBase
std::vector<Tensor>& outs) CV_OVERRIDE;
Tensor* max_tensor_ = nullptr;
Tensor* sum_tensor_ = nullptr;
Tensor* uniformTensor_ = nullptr;
wgpu::Buffer uniformBuffer_ = nullptr;
private:
bool init(const int axis, const bool log_softmax);
bool computeGroupCount();
Expand Down
1 change: 0 additions & 1 deletion modules/dnn/src/webgpu/include/tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class Buffer;
class Tensor{
public:
Tensor(Format fmt = wFormatFp32);
Tensor( const void* data, size_t size_in_byte); //uniform buffer
Tensor( const void* data, std::vector<int>& shape,
Format fmt = wFormatFp32);
const void* mapRead();
Expand Down
13 changes: 13 additions & 0 deletions modules/dnn/src/webgpu/src/internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ void bindTensor(Tensor& tensor, uint32_t binding,
bgEntries.push_back(bgEntry);
}

void bindUniform(wgpu::Buffer& buffer, uint32_t binding, size_t size,
std::vector<wgpu::BindGroupEntry>& bgEntries)
{
wgpu::BindGroupEntry bgEntry = {};
bgEntry.binding = binding;
bgEntry.buffer = buffer;
bgEntry.offset = 0;
bgEntry.size = size;
bgEntry.sampler = nullptr;
bgEntry.textureView = nullptr;
bgEntries.push_back(bgEntry);
}

void computeConvOutputShapeAndPadding(const PaddingMode& padding_mode,
int& padding_top, int& padding_left,
const int& in_h, const int& in_w,
Expand Down
2 changes: 2 additions & 0 deletions modules/dnn/src/webgpu/src/internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ std::vector<uint32_t> compile(const std::string& name,
const std::string& data);
void bindTensor(Tensor& tensor, uint32_t binding,
std::vector<wgpu::BindGroupEntry>& bgEntries);
void bindUniform(wgpu::Buffer& buffer, uint32_t binding, size_t size,
std::vector<wgpu::BindGroupEntry>& bgEntries);
void computeConvOutputShapeAndPadding(const PaddingMode& padding_mode,
int& padding_top, int& padding_left,
const int& in_h, const int& in_w,
Expand Down
11 changes: 7 additions & 4 deletions modules/dnn/src/webgpu/src/op_softmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,22 @@ bool OpSoftmax::forward(Tensor& in, Tensor& out)
max_tensor_ = new Tensor(NULL, shape);
sum_tensor_ = new Tensor(NULL, shape);
}
if(uniformTensor_ == NULL && needsUniform)
if(uniformBuffer_ == NULL && needsUniform)
{
SoftmaxParam param = {channel_size_, outer_size_, channels_,
log_softmax_ == true ? 1 : 0};
uniformTensor_ = new Tensor((const void*) &param,
sizeof(SoftmaxParam));
wgpu::BufferDescriptor descriptor = {};
descriptor.size = sizeof(SoftmaxParam);
descriptor.usage = wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst;
uniformBuffer_ = wDevice->CreateBuffer(& descriptor);
uniformBuffer_.SetSubData(0, sizeof(SoftmaxParam), & param);
}

bindTensor( in, 0, bgEntries);
bindTensor( *max_tensor_, 1, bgEntries);
bindTensor( *sum_tensor_, 2, bgEntries);
bindTensor( out, 3, bgEntries);
bindTensor( *uniformTensor_, 4, bgEntries);
bindUniform( uniformBuffer_, 4, sizeof(SoftmaxParam), bgEntries);

createBindGroup();
createCommandBuffer();
Expand Down
13 changes: 0 additions & 13 deletions modules/dnn/src/webgpu/src/tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,6 @@ Tensor::Tensor(Format fmt) : size_in_byte_(0), format_(fmt)
device_ = wDevice;
}

Tensor::Tensor(const void* data, size_t size_in_byte)
{
createContext();
device_ = wDevice;
size_in_byte_ = size_in_byte;
usage_ = wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst;
if (device_ == nullptr)
{
CV_Error(Error::StsError, "device is NULL");
}
fillData(data);
}

Tensor::Tensor(const void* data, std::vector<int>& shape, Format fmt)
{
createContext();
Expand Down

0 comments on commit 8377efd

Please sign in to comment.