Overte C++ Documentation
GLESBackend.h
1 //
2 // GLESBackend.h
3 // libraries/gpu-gl-android/src/gpu/gles
4 //
5 // Created by Gabriel Calero & Cristian Duarte on 9/27/2016.
6 // Copyright 2016 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 #ifndef hifi_gpu_gles_GLESBackend_h
12 #define hifi_gpu_gles_GLESBackend_h
13 
14 #include <gl/Config.h>
15 
16 #include <gpu/gl/GLBackend.h>
17 #include <gpu/gl/GLTexture.h>
18 
19 
20 namespace gpu { namespace gles {
21 
22 using namespace gpu::gl;
23 
24 class GLESBackend : public GLBackend {
25  using Parent = GLBackend;
26  // Context Backend static interface required
27  friend class Context;
28 
29 public:
30  static const GLint RESOURCE_TRANSFER_TEX_UNIT { 32 };
31  static const GLint RESOURCE_TRANSFER_EXTRA_TEX_UNIT { 33 };
32  static const GLint RESOURCE_BUFFER_TEXBUF_TEX_UNIT { 34 };
33  static const GLint RESOURCE_BUFFER_SLOT0_TEX_UNIT { 35 };
34 
35  explicit GLESBackend(bool syncCache) : Parent(syncCache) {}
36  GLESBackend() : Parent() {}
37  virtual ~GLESBackend() {
38  // call resetStages here rather than in ~GLBackend dtor because it will call releaseResourceBuffer
39  // which is pure virtual from GLBackend's dtor.
40  resetStages();
41  }
42 
43  bool supportedTextureFormat(const gpu::Element& format) override;
44 
45  static const std::string GLES_VERSION;
46  const std::string& getVersion() const override { return GLES_VERSION; }
47 
48  class GLESTexture : public GLTexture {
49  using Parent = GLTexture;
50  friend class GLESBackend;
51  friend class GLESFramebuffer;
52  GLuint allocate(const Texture& texture);
53  protected:
54  GLESTexture(const std::weak_ptr<GLBackend>& backend, const Texture& buffer);
55  void generateMips() const override;
56  Size copyMipFaceLinesFromTexture(uint16_t mip, uint8_t face, const uvec3& size, uint32_t yOffset, GLenum internalFormat, GLenum format, GLenum type, Size sourceSize, const void* sourcePointer) const override;
57  void syncSampler() const override;
58 
59  void withPreservedTexture(std::function<void()> f) const;
60  };
61 
62  //
63  // Textures that have fixed allocation sizes and cannot be managed at runtime
64  //
65 
66  class GLESFixedAllocationTexture : public GLESTexture {
67  using Parent = GLESTexture;
68  friend class GLESBackend;
69 
70  public:
71  GLESFixedAllocationTexture(const std::weak_ptr<GLBackend>& backend, const Texture& texture);
72  ~GLESFixedAllocationTexture();
73 
74  protected:
75  Size size() const override { return _size; }
76  void allocateStorage() const;
77  void syncSampler() const override;
78  const Size _size { 0 };
79  };
80 
81  class GLESAttachmentTexture : public GLESFixedAllocationTexture {
82  using Parent = GLESFixedAllocationTexture;
83  friend class GLESBackend;
84  protected:
85  GLESAttachmentTexture(const std::weak_ptr<GLBackend>& backend, const Texture& texture);
86  ~GLESAttachmentTexture();
87  };
88 
89  class GLESStrictResourceTexture : public GLESFixedAllocationTexture {
90  using Parent = GLESFixedAllocationTexture;
91  friend class GLESBackend;
92  protected:
93  GLESStrictResourceTexture(const std::weak_ptr<GLBackend>& backend, const Texture& texture);
94  ~GLESStrictResourceTexture();
95  };
96 
97  class GLESVariableAllocationTexture : public GLESTexture, public GLVariableAllocationSupport {
98  using Parent = GLESTexture;
99  friend class GLESBackend;
100  using PromoteLambda = std::function<void()>;
101 
102 
103  protected:
104  GLESVariableAllocationTexture(const std::weak_ptr<GLBackend>& backend, const Texture& texture);
105  ~GLESVariableAllocationTexture();
106 
107  void allocateStorage(uint16 allocatedMip);
108  void syncSampler() const override;
109  size_t promote() override;
110  size_t demote() override;
111  void populateTransferQueue(TransferJob::Queue& queue) override;
112 
113  Size copyMipFaceLinesFromTexture(uint16_t mip, uint8_t face, const uvec3& size, uint32_t yOffset, GLenum internalFormat, GLenum format, GLenum type, Size sourceSize, const void* sourcePointer) const override;
114  Size copyMipsFromTexture();
115 
116  void copyTextureMipsInGPUMem(GLuint srcId, GLuint destId, uint16_t srcMipOffset, uint16_t destMipOffset, uint16_t populatedMips) override;
117 
118  Size size() const override { return _size; }
119  };
120 
121  class GLESResourceTexture : public GLESVariableAllocationTexture {
122  using Parent = GLESVariableAllocationTexture;
123  friend class GLESBackend;
124  protected:
125  GLESResourceTexture(const std::weak_ptr<GLBackend>& backend, const Texture& texture);
126  ~GLESResourceTexture();
127  };
128 
129 protected:
130 
131  void draw(GLenum mode, uint32 numVertices, uint32 startVertex) override;
132 
133  GLuint getFramebufferID(const FramebufferPointer& framebuffer) override;
134  GLFramebuffer* syncGPUObject(const Framebuffer& framebuffer) override;
135 
136  GLuint getBufferID(const Buffer& buffer) override;
137  GLuint getBufferIDUnsynced(const Buffer& buffer) override;
138  GLuint getResourceBufferID(const Buffer& buffer);
139  GLBuffer* syncGPUObject(const Buffer& buffer) override;
140 
141  GLTexture* syncGPUObject(const TexturePointer& texture) override;
142 
143  GLuint getQueryID(const QueryPointer& query) override;
144  GLQuery* syncGPUObject(const Query& query) override;
145 
146  // Draw Stage
147  void do_draw(const Batch& batch, size_t paramOffset) override;
148  void do_drawIndexed(const Batch& batch, size_t paramOffset) override;
149  void do_drawInstanced(const Batch& batch, size_t paramOffset) override;
150  void do_drawIndexedInstanced(const Batch& batch, size_t paramOffset) override;
151  void do_multiDrawIndirect(const Batch& batch, size_t paramOffset) override;
152  void do_multiDrawIndexedIndirect(const Batch& batch, size_t paramOffset) override;
153 
154  // Input Stage
155  void resetInputStage() override;
156  void updateInput() override;
157 
158  // Synchronize the state cache of this Backend with the actual real state of the GL Context
159  void transferTransformState(const Batch& batch) const override;
160  void initTransform() override;
161  void updateTransform(const Batch& batch) override;
162 
163  // Resource Stage
164  bool bindResourceBuffer(uint32_t slot, const BufferPointer& buffer) override;
165  void releaseResourceBuffer(uint32_t slot) override;
166 
167  // Output stage
168  void do_blit(const Batch& batch, size_t paramOffset) override;
169 
170  shader::Dialect getShaderDialect() const override { return shader::Dialect::glsl310es; }
171 };
172 
173 } }
174 
175 Q_DECLARE_LOGGING_CATEGORY(gpugleslogging)
176 
177 
178 #endif