forked from alibaba/MNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLReshape.cpp
More file actions
129 lines (113 loc) · 4.71 KB
/
Copy pathGLReshape.cpp
File metadata and controls
129 lines (113 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// GLReshape.cpp
// MNN
//
// Created by MNN on 2019/01/31.
// Copyright © 2018, Alibaba Group Holding Limited
//
#include "GLReshape.hpp"
#include <sstream>
#include "AllShader.hpp"
#include "GLBackend.hpp"
#include "Macro.h"
#include "TensorUtils.hpp"
namespace MNN {
namespace OpenGL {
GLReshape::GLReshape(const std::vector<Tensor *> &inputs, const Op *op, Backend *bn) : Execution(bn) {
}
GLReshape::~GLReshape() {
}
ErrorCode GLReshape::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
std::vector<std::string> prefix;
setLocalSize(prefix, mLocalSize, 8, 8, 1);
mTempBuffer.reset(new GLSSBOBuffer(inputs[0]->size()));
if (TensorUtils::getDescribe(inputs[0])->dimensionFormat == MNN_DATA_FORMAT_NC4HW4) {
mSrcProgram = ((GLBackend *)backend())->getProgram("src", glsl_image_to_nchw_buffer_glsl, prefix);
mDstProgram = ((GLBackend *)backend())->getProgram("dst", glsl_nchw_buffer_to_image_glsl, prefix);
}else{
mSrcProgram = ((GLBackend *)backend())->getProgram("src", glsl_image_to_nhwc_buffer_glsl, prefix);
mDstProgram = ((GLBackend *)backend())->getProgram("dst", glsl_nhwc_buffer_to_image_glsl, prefix);
}
return NO_ERROR;
}
ErrorCode GLReshape::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
auto input = inputs[0];
auto output = outputs[0];
std::vector<int> inputShape = tensorShapeFormat(input);
std::vector<int> outputShape = tensorShapeFormat(output);
int ib = inputShape.at(0);
int ih = inputShape.at(1);
int iw = inputShape.at(2);
int ic = inputShape.at(3);
int ic_4 = UP_DIV(ic, 4);
int ob = outputShape.at(0);
int oh = outputShape.at(1);
int ow = outputShape.at(2);
int oc = outputShape.at(3);
int oc_4 = UP_DIV(oc, 4);
if (TensorUtils::getDescribe(input)->dimensionFormat == MNN_DATA_FORMAT_NC4HW4) {
//image -> buffer(nchw)
{
mSrcProgram->useProgram();
glBindImageTexture(0, input->deviceId(), 0, GL_TRUE, 0, GL_READ_ONLY, TEXTURE_FORMAT);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, mTempBuffer->getId());
glUniform1i(2, iw);
glUniform1i(3, ih);
OPENGL_CHECK_ERROR;
((GLBackend *)backend())->compute(UP_DIV(iw, mLocalSize[0]), UP_DIV(ih, mLocalSize[1]), UP_DIV(ic_4, mLocalSize[2]));
OPENGL_CHECK_ERROR;
}
//buffer(nchw) -> image
{
mDstProgram->useProgram();
glBindImageTexture(0, output->deviceId(), 0, GL_TRUE, 0, GL_WRITE_ONLY, TEXTURE_FORMAT);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, mTempBuffer->getId());
glUniform1i(2, ow);
glUniform1i(3, oh);
OPENGL_CHECK_ERROR;
((GLBackend *)backend())->compute(UP_DIV(ow, mLocalSize[0]), UP_DIV(oh, mLocalSize[1]), UP_DIV(oc_4, mLocalSize[2]));
OPENGL_CHECK_ERROR;
}
}else{
//image -> buffer(nhwc)
{
mSrcProgram->useProgram();
glBindImageTexture(0, input->deviceId(), 0, GL_TRUE, 0, GL_READ_ONLY, TEXTURE_FORMAT);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, mTempBuffer->getId());
glUniform1i(2, iw);
glUniform1i(3, ih);
glUniform1i(4, ic);
OPENGL_CHECK_ERROR;
((GLBackend *)backend())->compute(UP_DIV(iw, mLocalSize[0]), UP_DIV(ih, mLocalSize[1]), UP_DIV(ic_4, mLocalSize[2]));
OPENGL_CHECK_ERROR;
}
//buffer -> image
{
mDstProgram->useProgram();
glBindImageTexture(0, output->deviceId(), 0, GL_TRUE, 0, GL_WRITE_ONLY, TEXTURE_FORMAT);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, mTempBuffer->getId());
glUniform1i(2, ow);
glUniform1i(3, oh);
glUniform1i(4, oc);
OPENGL_CHECK_ERROR;
((GLBackend *)backend())->compute(UP_DIV(ow, mLocalSize[0]), UP_DIV(oh, mLocalSize[1]), UP_DIV(oc_4, mLocalSize[2]));
OPENGL_CHECK_ERROR;
}
}
return NO_ERROR;
}
class ReshapeCreator : public GLBackend::Creator {
public:
virtual ~ReshapeCreator() = default;
virtual Execution *onCreate(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs,
const MNN::Op *op, Backend *backend) const override {
if(inputs[0]->dimensions() == 3 || outputs[0]->dimensions() == 3){
MNN_PRINT("reshape not support dimensions == 3 \n");
return nullptr;
}
return new GLReshape(inputs, op, backend);
}
};
GLCreatorRegister<ReshapeCreator> __reshape_op(OpType_Reshape);
} // namespace OpenGL
} // namespace MNN