Caffe2 - C++ API
A deep learning, cross platform ML framework
GLTexture.h
1 
2 #pragma once
3 #include "GL.h"
4 #include "GLLogging.h"
5 
6 class GLTexture {
7  public:
8  struct Type {
9  const GLenum internalFormat;
10  const GLenum format;
11  const GLenum type;
12 
13  int dataSize() const {
14  switch (type) {
15  case GL_UNSIGNED_INT:
16  return 4;
17  case GL_HALF_FLOAT:
18  return 2;
19  case GL_UNSIGNED_BYTE:
20  return 1;
21  default:
22  throw std::runtime_error("Unknown Texture Type");
23  }
24  }
25 
26  int channels() const {
27  switch (format) {
28  case GL_R8:
29  return 1;
30  case GL_RG8:
31  return 2;
32  // case GL_BGRA:
33  case GL_RG_INTEGER:
34  case GL_RGBA:
35  return 4;
36  default:
37  throw std::runtime_error("Unknown Texture Format");
38  }
39  }
40  };
41 
42  static const Type FP16;
43  static const Type FP16_COMPAT;
44  static const Type UI8;
45 
46  protected:
47  const Type& _type;
48 
49  const GLsizei _width;
50  const GLsizei _height;
51  const GLsizei _stride;
52  const GLsizei _channels;
53  const bool _use_padding;
54 
55  GLint _filter;
56  GLint _wrap;
57  GLuint _textureId;
58 
59  public:
60  GLTexture(const Type& type,
61  int width,
62  int height,
63  int stride,
64  bool use_padding,
65  GLint filter,
66  GLint wrap)
67  : _type(type),
68  _width(width),
69  _height(height),
70  _stride(stride),
71  _channels(type.channels()),
72  _use_padding(use_padding),
73  _filter(filter),
74  _wrap(wrap) {}
75 
76  GLTexture(const Type& type, int width, int height, bool use_padding, GLint filter, GLint wrap)
77  : GLTexture(type,
78  width,
79  height,
80  use_padding ? (width + 7) / 8 * 8 : width,
81  use_padding,
82  filter,
83  wrap) {}
84 
85  virtual ~GLTexture() {}
86  virtual GLuint name() const = 0;
87  virtual GLenum target() const = 0;
88  virtual bool flipped() const = 0;
89 
90  virtual void map_read(std::function<void(const void* buffer,
91  size_t width,
92  size_t height,
93  size_t stride,
94  size_t channels,
95  const Type& type)> process) const;
96 
97  virtual void map_load(std::function<void(void* buffer,
98  size_t width,
99  size_t height,
100  size_t stride,
101  size_t channels,
102  const Type& type)> process) const;
103 
104  void loadData(const void* pixels) const;
105 };