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