| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  GLPool.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/GLPool.hpp"
 | 
					
						
							|  |  |  | #include "AllShader.hpp"
 | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | #include "backend/opengl/GLBackend.hpp"
 | 
					
						
							|  |  |  | #include "core/Macro.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | namespace MNN { | 
					
						
							| 
									
										
										
										
											2019-05-24 11:26:54 +08:00
										 |  |  | namespace OpenGL { | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | ErrorCode GLPool::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) { | 
					
						
							|  |  |  |     auto inputTensor = inputs[0]; | 
					
						
							|  |  |  |     auto pool        = mPool; | 
					
						
							|  |  |  |     if (!pool->isGlobal()) { | 
					
						
							|  |  |  |         int kx      = pool->kernelX(); | 
					
						
							|  |  |  |         int ky      = pool->kernelY(); | 
					
						
							|  |  |  |         int sx      = pool->strideX(); | 
					
						
							|  |  |  |         int sy      = pool->strideY(); | 
					
						
							|  |  |  |         int px      = pool->padX(); | 
					
						
							|  |  |  |         int py      = pool->padY(); | 
					
						
							|  |  |  |         mSetUniform = [=] { | 
					
						
							|  |  |  |             glUniform2i(2, kx, ky); | 
					
						
							|  |  |  |             glUniform2i(3, sx, sy); | 
					
						
							|  |  |  |             glUniform2i(4, px, py); | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         mSetUniform = [=] { | 
					
						
							|  |  |  |             glUniform2i(2, inputTensor->width(), inputTensor->height()); | 
					
						
							|  |  |  |             glUniform2i(3, 1, 1); | 
					
						
							|  |  |  |             glUniform2i(4, 0, 0); | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return NO_ERROR; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorCode GLPool::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) { | 
					
						
							|  |  |  |     auto imageInput   = inputs[0]->deviceId(); | 
					
						
							|  |  |  |     auto imageOutput  = outputs[0]->deviceId(); | 
					
						
							|  |  |  |     auto outputTensor = outputs[0]; | 
					
						
							|  |  |  |     auto inputTensor  = inputs[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     MNN_ASSERT(mPoolProgram.get() != NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 11:26:54 +08:00
										 |  |  |     mPoolProgram->useProgram(); | 
					
						
							| 
									
										
										
										
											2019-07-25 13:36:35 +08:00
										 |  |  |     glBindImageTexture(0, imageInput, 0, GL_TRUE, 0, GL_READ_ONLY, ((GLBackend *)backend())->getTextrueFormat()); | 
					
						
							|  |  |  |     glBindImageTexture(1, imageOutput, 0, GL_TRUE, 0, GL_WRITE_ONLY, ((GLBackend *)backend())->getTextrueFormat()); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |     mSetUniform(); | 
					
						
							|  |  |  |     glUniform3i(10, outputTensor->width(), outputTensor->height(), UP_DIV(outputTensor->channel(), 4)); | 
					
						
							|  |  |  |     glUniform3i(11, inputTensor->width(), inputTensor->height(), UP_DIV(inputTensor->channel(), 4)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     OPENGL_CHECK_ERROR; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto depthQuad = UP_DIV(outputTensor->channel(), 4); | 
					
						
							| 
									
										
										
										
											2019-07-02 18:01:08 +08:00
										 |  |  |     ((GLBackend *)backend())->compute(UP_DIV(outputTensor->width(), 2), UP_DIV(outputTensor->height(), 2), UP_DIV(depthQuad, 16)); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     OPENGL_CHECK_ERROR; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return NO_ERROR; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GLPool::~GLPool() { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 11:26:54 +08:00
										 |  |  | GLPool::GLPool(const std::vector<Tensor *> &inputs, const Op *op, Backend *bn) : Execution(bn) { | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |     auto extra = (GLBackend *)bn; | 
					
						
							| 
									
										
										
										
											2019-05-24 11:26:54 +08:00
										 |  |  |     auto pool = op->main_as_Pool(); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |     switch (pool->type()) { | 
					
						
							|  |  |  |         case PoolType_MAXPOOL: | 
					
						
							|  |  |  |             mPoolProgram = extra->getProgram("maxPool", glsl_maxpool_glsl); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case PoolType_AVEPOOL: | 
					
						
							|  |  |  |             mPoolProgram = extra->getProgram("meanPool", glsl_avgpool_glsl); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             MNN_ASSERT(false); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     mPool = pool; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-24 11:26:54 +08:00
										 |  |  | GLCreatorRegister<TypedCreator<GLPool>> __pool_op(OpType_Pooling); | 
					
						
							|  |  |  | } // namespace OpenGL
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | } // namespace MNN
 |