12 #ifndef hifi_gpu_45_GL45Backend_h
13 #define hifi_gpu_45_GL45Backend_h
15 #include <gpu/gl/GLBackend.h>
16 #include <gpu/gl/GLTexture.h>
19 #include <gpu/TextureTable.h>
21 #define INCREMENTAL_TRANSFER 0
22 #define GPU_SSBO_TRANSFORM_OBJECT 1
23 #define GPU_BINDLESS_TEXTURES 0
25 namespace gpu {
namespace gl45 {
27 using namespace gpu::gl;
28 using TextureWeakPointer = std::weak_ptr<Texture>;
30 class GL45Backend :
public GLBackend {
31 using Parent = GLBackend;
36 static const GLint RESOURCE_TRANSFER_TEX_UNIT { 32 };
37 static GLint MAX_COMBINED_SHADER_STORAGE_BLOCKS;
38 static GLint MAX_UNIFORM_LOCATIONS;
39 #if GPU_BINDLESS_TEXTURES
40 virtual bool supportsBindless()
const override {
return true; }
43 explicit GL45Backend(
bool syncCache);
45 virtual ~GL45Backend() {
51 static const std::string GL45_VERSION;
52 const std::string& getVersion()
const override {
return GL45_VERSION; }
54 bool supportedTextureFormat(
const gpu::Element& format)
override;
56 class GL45Texture :
public GLTexture {
57 using Parent = GLTexture;
58 friend class GL45Backend;
59 static GLuint allocate(
const Texture& texture);
62 GL45Texture(
const std::weak_ptr<GLBackend>& backend,
const Texture& texture);
63 void generateMips()
const override;
64 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;
65 void syncSampler()
const override;
67 #if GPU_BINDLESS_TEXTURES
68 bool isBindless()
const {
69 return _bindless.operator bool();
75 uint32_t sampler{ 0 };
77 bool operator==(
const Bindless& other)
const {
78 return handle == other.handle && minMip == other.minMip && sampler == other.sampler;
81 bool operator!=(
const Bindless& other)
const {
82 return !(*
this == other);
85 operator bool()
const {
90 virtual const Bindless& getBindless()
const;
91 void releaseBindless()
const;
92 void recreateBindless()
const;
95 mutable Bindless _bindless;
98 static Sampler getInvalidSampler();
101 mutable Sampler _cachedSampler{ getInvalidSampler() };
104 #if GPU_BINDLESS_TEXTURES
105 class GL45TextureTable :
public GLObject<TextureTable> {
106 static GLuint allocate();
107 using Parent = GLObject<TextureTable>;
110 using BindlessArray = std::array<GL45Texture::Bindless, TextureTable::COUNT>;
112 GL45TextureTable(
const std::weak_ptr<GLBackend>& backend,
const TextureTable& texture);
115 void update(
const BindlessArray& newHandles);
119 BindlessArray _handles;
127 class GL45FixedAllocationTexture :
public GL45Texture {
128 using Parent = GL45Texture;
129 friend class GL45Backend;
132 GL45FixedAllocationTexture(
const std::weak_ptr<GLBackend>& backend,
const Texture& texture);
133 ~GL45FixedAllocationTexture();
136 Size size()
const override {
return _size; }
138 void allocateStorage()
const;
139 void syncSampler()
const override;
140 const Size _size{ 0 };
143 class GL45AttachmentTexture :
public GL45FixedAllocationTexture {
144 using Parent = GL45FixedAllocationTexture;
145 friend class GL45Backend;
148 GL45AttachmentTexture(
const std::weak_ptr<GLBackend>& backend,
const Texture& texture);
149 ~GL45AttachmentTexture();
152 class GL45StrictResourceTexture :
public GL45FixedAllocationTexture {
153 using Parent = GL45FixedAllocationTexture;
154 friend class GL45Backend;
157 GL45StrictResourceTexture(
const std::weak_ptr<GLBackend>& backend,
const Texture& texture);
158 ~GL45StrictResourceTexture();
165 class GL45VariableAllocationTexture :
public GL45Texture,
public GLVariableAllocationSupport {
166 using Parent = GL45Texture;
167 friend class GL45Backend;
168 using PromoteLambda = std::function<void()>;
171 GL45VariableAllocationTexture(
const std::weak_ptr<GLBackend>& backend,
const Texture& texture);
172 ~GL45VariableAllocationTexture();
174 Size size()
const override {
return _size; }
176 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;
177 void copyTextureMipsInGPUMem(GLuint srcId, GLuint destId, uint16_t srcMipOffset, uint16_t destMipOffset, uint16_t populatedMips)
override;
179 #if GPU_BINDLESS_TEXTURES
180 virtual const Bindless& getBindless()
const override;
184 class GL45ResourceTexture :
public GL45VariableAllocationTexture {
185 using Parent = GL45VariableAllocationTexture;
186 friend class GL45Backend;
189 GL45ResourceTexture(
const std::weak_ptr<GLBackend>& backend,
const Texture& texture);
191 void syncSampler()
const override;
192 size_t promote()
override;
193 size_t demote()
override;
194 void populateTransferQueue(TransferQueue& pendingTransfers)
override;
196 void allocateStorage(uint16 mip);
197 Size copyMipsFromTexture();
201 class GL45SparseResourceTexture :
public GL45VariableAllocationTexture {
202 using Parent = GL45VariableAllocationTexture;
203 friend class GL45Backend;
204 using TextureTypeFormat = std::pair<GLenum, GLenum>;
205 using PageDimensions = std::vector<uvec3>;
206 using PageDimensionsMap = std::map<TextureTypeFormat, PageDimensions>;
207 static PageDimensionsMap pageDimensionsByFormat;
208 static Mutex pageDimensionsMutex;
210 static bool isSparseEligible(
const Texture& texture);
211 static PageDimensions getPageDimensionsForFormat(
const TextureTypeFormat& typeFormat);
212 static PageDimensions getPageDimensionsForFormat(GLenum type, GLenum format);
213 static const uint32_t DEFAULT_PAGE_DIMENSION = 128;
214 static const uint32_t DEFAULT_MAX_SPARSE_LEVEL = 0xFFFF;
217 GL45SparseResourceTexture(
const std::weak_ptr<GLBackend>& backend,
const Texture& texture);
218 ~GL45SparseResourceTexture();
219 uint32 size()
const override {
return _allocatedPages * _pageBytes; }
220 void promote()
override;
221 void demote()
override;
224 uvec3 getPageCounts(
const uvec3& dimensions)
const;
225 uint32_t getPageCount(
const uvec3& dimensions)
const;
227 uint32_t _allocatedPages { 0 };
228 uint32_t _pageBytes { 0 };
229 uvec3 _pageDimensions { DEFAULT_PAGE_DIMENSION };
230 GLuint _maxSparseLevel { DEFAULT_MAX_SPARSE_LEVEL };
236 void draw(GLenum mode, uint32 numVertices, uint32 startVertex)
override;
237 void recycle()
const override;
239 GLuint getFramebufferID(
const FramebufferPointer& framebuffer)
override;
240 GLFramebuffer* syncGPUObject(
const Framebuffer& framebuffer)
override;
242 GLuint getBufferID(
const Buffer& buffer)
override;
243 GLuint getBufferIDUnsynced(
const Buffer& buffer)
override;
244 GLBuffer* syncGPUObject(
const Buffer& buffer)
override;
246 GLTexture* syncGPUObject(
const TexturePointer& texture)
override;
248 GLuint getQueryID(
const QueryPointer& query)
override;
249 GLQuery* syncGPUObject(
const Query& query)
override;
252 void do_draw(
const Batch& batch,
size_t paramOffset)
override;
253 void do_drawIndexed(
const Batch& batch,
size_t paramOffset)
override;
254 void do_drawInstanced(
const Batch& batch,
size_t paramOffset)
override;
255 void do_drawIndexedInstanced(
const Batch& batch,
size_t paramOffset)
override;
256 void do_multiDrawIndirect(
const Batch& batch,
size_t paramOffset)
override;
257 void do_multiDrawIndexedIndirect(
const Batch& batch,
size_t paramOffset)
override;
260 void resetInputStage()
override;
261 void updateInput()
override;
264 void transferTransformState(
const Batch& batch)
const override;
265 void initTransform()
override;
266 void updateTransform(
const Batch& batch)
override;
269 bool bindResourceBuffer(uint32_t slot,
const BufferPointer& buffer)
override;
270 void releaseResourceBuffer(uint32_t slot)
override;
273 void do_blit(
const Batch& batch,
size_t paramOffset)
override;
276 shader::Dialect getShaderDialect()
const override;
279 void initTextureManagementStage()
override;
281 #if GPU_BINDLESS_TEXTURES
282 GL45TextureTable* syncGPUObject(
const TextureTablePointer& textureTable);
284 void do_setResourceTextureTable(
const Batch& batch,
size_t paramOffset)
override;
290 Q_DECLARE_LOGGING_CATEGORY(gpugl45logging)