Caffe2 - C++ API
A deep learning, cross platform ML framework
GLTexture.cc
1 
2 #include "GLTexture.h"
3 #include "DataTransfer.h"
4 #include "GLPBO.h"
5 
6 #include "caffe2/core/logging.h"
7 #include "caffe2/core/timer.h"
8 
9 #if CAFFE2_ANDROID && defined(__ARM_NEON__)
10 
11 #include "../android/AndroidGLContext.h"
12 
13 // https://community.arm.com/thread/10002
14 void arm_memcpy(volatile unsigned char* dst, volatile unsigned char* src, int sz) {
15  if (sz & 63) {
16  sz = (sz & -64) + 64;
17  }
18 
19  asm volatile(
20  "NEONCopyPLD: \n"
21  " VLDM %[src]!,{d0-d7} \n"
22  " VSTM %[dst]!,{d0-d7} \n"
23  " SUBS %[sz],%[sz],#0x40 \n"
24  " BGT NEONCopyPLD \n"
25  : [dst] "+r"(dst), [src] "+r"(src), [sz] "+r"(sz)
26  :
27  : "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "cc", "memory");
28 }
29 #endif
30 
31 const GLTexture::Type GLTexture::FP16 = {GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT};
32 const GLTexture::Type GLTexture::UI8 = {GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE};
33 const GLTexture::Type GLTexture::FP16_COMPAT = {GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT};
34 
35 void GLTexture::map_read(std::function<void(const void* buffer,
36  size_t width,
37  size_t height,
38  size_t stride,
39  size_t channels,
40  const Type& type)> process) const {
41  GLPBO* pbo = GLPBO::getContext();
42  pbo->mapTextureData(_textureId, _width, _height, _stride, _channels, _type, process);
43 }
44 
45 void GLTexture::map_load(std::function<void(void* buffer,
46  size_t width,
47  size_t height,
48  size_t stride,
49  size_t channels,
50  const Type& type)> process) const {
51  const int alignment = 32; // 4 * _type.dataSize();
52  void* buffer = nullptr;
53  size_t buffer_size = _width * _height * _channels * _type.dataSize();
54 
55 #ifdef __ANDROID__
56  buffer = (void*)memalign(alignment, buffer_size);
57 #else
58  posix_memalign((void**)&buffer, alignment, buffer_size);
59 #endif
60  CAFFE_ENFORCE(buffer);
61 
62  process(buffer, _width, _height, _width, _channels, _type);
63  loadData(buffer);
64  free(buffer);
65 }
66 
67 void GLTexture::loadData(const void* pixels) const {
68  glBindTexture(GL_TEXTURE_2D, _textureId);
69  glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _width, _height, _type.format, _type.type, pixels);
70  glBindTexture(GL_TEXTURE_2D, 0);
71 }
Definition: GLPBO.h:7