| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  GLConverter.cpp
 | 
					
						
							|  |  |  | //  MNN
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Created by MNN on 2019/01/31.
 | 
					
						
							|  |  |  | //  Copyright © 2018, Alibaba Group Holding Limited
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-15 13:33:47 +08:00
										 |  |  | #include "backend/opengl/GLConverter.hpp"
 | 
					
						
							|  |  |  | #include "AllShader.hpp"
 | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | #include "backend/opengl/GLBackend.hpp"
 | 
					
						
							|  |  |  | #include "core/Macro.h"
 | 
					
						
							|  |  |  | #include "core/TensorUtils.hpp"
 | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace MNN { | 
					
						
							|  |  |  | namespace OpenGL { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GLConverter::GLConverter(const std::vector<Tensor *> &inputs, const Op *op, Backend *bn) : Execution(bn) { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  | GLConverter::~GLConverter() { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  | ErrorCode GLConverter::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) { | 
					
						
							|  |  |  |     std::vector<std::string> prefix; | 
					
						
							|  |  |  |     setLocalSize(prefix, mLocalSize, 8, 8, 1); | 
					
						
							|  |  |  |     mProgram = ((GLBackend *)backend())->getProgram("convert", glsl_converter_glsl, prefix); | 
					
						
							|  |  |  |     return NO_ERROR; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorCode GLConverter::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) { | 
					
						
							|  |  |  |     auto input = inputs[0]; | 
					
						
							|  |  |  |     auto output = outputs[0]; | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |     std::vector<int> inputShape  = tensorShapeFormat(input); | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |     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); | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  |     mProgram->useProgram(); | 
					
						
							| 
									
										
										
										
											2019-07-25 13:36:35 +08:00
										 |  |  |     glBindImageTexture(0, output->deviceId(), 0, GL_TRUE, 0, GL_WRITE_ONLY, ((GLBackend *)backend())->getTextrueFormat()); | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  |     OPENGL_CHECK_ERROR; | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         int texId = 0; | 
					
						
							|  |  |  |         glActiveTexture(GL_TEXTURE0 + texId); | 
					
						
							|  |  |  |         glUniform1i(1, texId); | 
					
						
							|  |  |  |         glBindTexture(GL_TEXTURE_3D, input->deviceId()); | 
					
						
							|  |  |  |         OPENGL_CHECK_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |     glUniform1i(2, iw); | 
					
						
							|  |  |  |     glUniform1i(3, ih); | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  |     glUniform1i(4, ic_4); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |     ((GLBackend *)backend())->compute(UP_DIV(iw, mLocalSize[0]), UP_DIV(ih, mLocalSize[1]), | 
					
						
							| 
									
										
										
										
											2019-06-24 11:32:41 +08:00
										 |  |  |                       UP_DIV(ic_4, mLocalSize[2])); | 
					
						
							|  |  |  |     return NO_ERROR; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GLCreatorRegister<TypedCreator<GLConverter>> __converter_op(OpType_ConvertTensor); | 
					
						
							|  |  |  | } // namespace OpenGL
 | 
					
						
							|  |  |  | } // namespace MNN
 |