Overte C++ Documentation
GL41Backend.h
1 //
2 // GL41Backend.h
3 // libraries/gpu/src/gpu
4 //
5 // Created by Sam Gateau on 10/27/2014.
6 // Copyright 2014 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_41_GL41Backend_h
12 #define hifi_gpu_41_GL41Backend_h
13 
14 #include <gl/Config.h>
15 
16 #include <gpu/gl/GLBackend.h>
17 #include <gpu/gl/GLTexture.h>
18 
19 #define GPU_CORE_41 410
20 #define GPU_CORE_43 430
21 
22 #define GPU_INPUT_PROFILE GPU_CORE_41
23 
24 namespace gpu { namespace gl41 {
25 
26 using namespace gpu::gl;
27 
28 class GL41Backend : public GLBackend {
29  using Parent = GLBackend;
30  // Context Backend static interface required
31  friend class Context;
32 
33 public:
34  static const GLint RESOURCE_TRANSFER_TEX_UNIT { 32 };
35  static const GLint RESOURCE_TRANSFER_EXTRA_TEX_UNIT { 33 };
36  static const GLint RESOURCE_BUFFER_TEXBUF_TEX_UNIT { 34 };
37  static const GLint RESOURCE_BUFFER_SLOT0_TEX_UNIT { 35 };
38 
39  explicit GL41Backend(bool syncCache) : Parent(syncCache) {}
40  GL41Backend() : Parent() {}
41  virtual ~GL41Backend() {
42  // call resetStages here rather than in ~GLBackend dtor because it will call releaseResourceBuffer
43  // which is pure virtual from GLBackend's dtor.
44  resetStages();
45  }
46 
47  static const std::string GL41_VERSION;
48  const std::string& getVersion() const override { return GL41_VERSION; }
49 
50  bool supportedTextureFormat(const gpu::Element& format) override;
51 
52  class GL41Texture : public GLTexture {
53  using Parent = GLTexture;
54  friend class GL41Backend;
55  static GLuint allocate(const Texture& texture);
56  protected:
57  GL41Texture(const std::weak_ptr<GLBackend>& backend, const Texture& texture);
58  void generateMips() const override;
59  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;
60  void syncSampler() const override;
61 
62  void withPreservedTexture(std::function<void()> f) const;
63  };
64 
65  //
66  // Textures that have fixed allocation sizes and cannot be managed at runtime
67  //
68 
69  class GL41FixedAllocationTexture : public GL41Texture {
70  using Parent = GL41Texture;
71  friend class GL41Backend;
72 
73  public:
74  GL41FixedAllocationTexture(const std::weak_ptr<GLBackend>& backend, const Texture& texture);
75  ~GL41FixedAllocationTexture();
76 
77  protected:
78  Size size() const override { return _size; }
79  void allocateStorage() const;
80  void syncSampler() const override;
81  const Size _size { 0 };
82  };
83 
84  class GL41AttachmentTexture : public GL41FixedAllocationTexture {
85  using Parent = GL41FixedAllocationTexture;
86  friend class GL41Backend;
87  protected:
88  GL41AttachmentTexture(const std::weak_ptr<GLBackend>& backend, const Texture& texture);
89  ~GL41AttachmentTexture();
90  };
91 
92  class GL41StrictResourceTexture : public GL41FixedAllocationTexture {
93  using Parent = GL41FixedAllocationTexture;
94  friend class GL41Backend;
95  protected:
96  GL41StrictResourceTexture(const std::weak_ptr<GLBackend>& backend, const Texture& texture);
97  ~GL41StrictResourceTexture();
98  };
99 
100  class GL41VariableAllocationTexture : public GL41Texture, public GLVariableAllocationSupport {
101  using Parent = GL41Texture;
102  friend class GL41Backend;
103  using PromoteLambda = std::function<void()>;
104 
105 
106  protected:
107  GL41VariableAllocationTexture(const std::weak_ptr<GLBackend>& backend, const Texture& texture);
108  ~GL41VariableAllocationTexture();
109 
110  void allocateStorage(uint16 allocatedMip);
111  void syncSampler() const override;
112  size_t promote() override;
113  size_t demote() override;
114  void populateTransferQueue(TransferQueue& pendingTransfers) override;
115 
116  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;
117  Size copyMipsFromTexture();
118 
119  void copyTextureMipsInGPUMem(GLuint srcId, GLuint destId, uint16_t srcMipOffset, uint16_t destMipOffset, uint16_t populatedMips) override;
120 
121  Size size() const override { return _size; }
122  };
123 
124  class GL41ResourceTexture : public GL41VariableAllocationTexture {
125  using Parent = GL41VariableAllocationTexture;
126  friend class GL41Backend;
127  protected:
128  GL41ResourceTexture(const std::weak_ptr<GLBackend>& backend, const Texture& texture);
129  ~GL41ResourceTexture();
130  };
131 
132 protected:
133 
134  void draw(GLenum mode, uint32 numVertices, uint32 startVertex) override;
135 
136  GLuint getFramebufferID(const FramebufferPointer& framebuffer) override;
137  GLFramebuffer* syncGPUObject(const Framebuffer& framebuffer) override;
138 
139  GLuint getBufferID(const Buffer& buffer) override;
140  GLuint getBufferIDUnsynced(const Buffer& buffer) override;
141  GLuint getResourceBufferID(const Buffer& buffer);
142  GLBuffer* syncGPUObject(const Buffer& buffer) override;
143 
144  GLTexture* syncGPUObject(const TexturePointer& texture) override;
145 
146  GLuint getQueryID(const QueryPointer& query) override;
147  GLQuery* syncGPUObject(const Query& query) override;
148 
149  // Draw Stage
150  void do_draw(const Batch& batch, size_t paramOffset) override;
151  void do_drawIndexed(const Batch& batch, size_t paramOffset) override;
152  void do_drawInstanced(const Batch& batch, size_t paramOffset) override;
153  void do_drawIndexedInstanced(const Batch& batch, size_t paramOffset) override;
154  void do_multiDrawIndirect(const Batch& batch, size_t paramOffset) override;
155  void do_multiDrawIndexedIndirect(const Batch& batch, size_t paramOffset) override;
156 
157  // Input Stage
158  void resetInputStage() override;
159  void updateInput() override;
160 
161  // Synchronize the state cache of this Backend with the actual real state of the GL Context
162  void transferTransformState(const Batch& batch) const override;
163  void initTransform() override;
164  void updateTransform(const Batch& batch) override;
165 
166  // Resource Stage
167  bool bindResourceBuffer(uint32_t slot, const BufferPointer& buffer) override;
168  void releaseResourceBuffer(uint32_t slot) override;
169 
170  // Output stage
171  void do_blit(const Batch& batch, size_t paramOffset) override;
172 
173  shader::Dialect getShaderDialect() const override { return shader::Dialect::glsl410; }
174  void postLinkProgram(ShaderObject& programObject, const Shader& program) const override;
175 };
176 
177 } }
178 
179 Q_DECLARE_LOGGING_CATEGORY(gpugl41logging)
180 
181 
182 #endif